Last active
February 10, 2019 17:32
-
-
Save iheanyi/5774841 to your computer and use it in GitHub Desktop.
Google Now Cards Layout XML. list_item.xml can be customized to your heart's content . . . Also, you can implement the following with DragSortListView for swiping to delete, reordering, and whatnot.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
android:background="#e5e5e5"> | |
<ListView | |
android:id="@+id/cardListView" | |
android:layout_marginLeft="16dp" | |
android:layout_marginRight="16dp" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:divider="@null" | |
android:dividerHeight="0dp"> | |
</ListView> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item> | |
<shape android:shape="rectangle" | |
android:dither="true"> | |
<corners android:radius="2dp"/> | |
<solid android:color="#ccc" /> | |
</shape> | |
</item> | |
<item android:bottom="2dp"> | |
<shape android:shape="rectangle" | |
android:dither="true"> | |
<corners android:radius="2dp" /> | |
<solid android:color="@android:color/white" /> | |
<padding android:bottom="8dp" | |
android:left="8dp" | |
android:right="8dp" | |
android:top="8dp" /> | |
</shape> | |
</item> | |
</layer-list> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="?android:attr/listPreferredItemHeight"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_marginLeft="6dp" | |
android:layout_marginRight="6dp" | |
android:layout_marginTop="4dp" | |
android:layout_marginBottom="4dp" | |
android:background="@drawable/background_card"> | |
<TextView | |
android:layout_gravity="left|center_vertical" | |
android:layout_width="fill_parent" | |
android:layout_weight="1" | |
android:textColor="@android:color/primary_text_light" | |
android:layout_height="wrap_content" /> | |
</LinearLayout> | |
</FrameLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import android.content.Context; | |
import android.graphics.Typeface; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.TextView; | |
public class NowArrayAdapter extends ArrayAdapter<String> { | |
private Context context; | |
private ArrayList<String> values; | |
private Typeface typeface; | |
private static Hashtable fontCache = new Hashtable(); | |
private LayoutInflater inflater; | |
public class CustomListItem { | |
TextView descText; | |
} | |
public NowArrayAdapter(Context context, int resource, ArrayList<String> commandsList) { | |
// TODO Auto-generated constructor stub | |
super(context, R.layout.list_item, commandsList); | |
this.context = context; | |
values = new ArrayList<String>(); | |
values.addAll(commandsList); | |
typeface = getTypeface(this.context, "fonts/Roboto-Light.ttf"); | |
inflater = LayoutInflater.from(this.context); | |
} | |
static Typeface getTypeface(Context context, String font) { | |
Typeface typeface = fontCache.get(font); | |
if (typeface == null) { | |
typeface = Typeface.createFromAsset(context.getAssets(), font); | |
fontCache.put(font, typeface); | |
} | |
return typeface; | |
} | |
public View getView(int position, View convertView, ViewGroup parent) { | |
CustomListItem myListItem; | |
String myText = getItem(position); | |
if(convertView == null) { | |
convertView = inflater.inflate(R.layout.list_item, parent, false); | |
myListItem = new CustomListItem(); | |
myListItem.descText = (TextView) convertView.findViewById(R.id.commandText); | |
myListItem.descText.setTypeface(typeface); | |
convertView.setTag(myListItem); | |
} else { | |
myListItem = (CustomListItem) convertView.getTag(); | |
} | |
myListItem.descText.setText(myText); | |
//myListItem.descText.setTextSize(14); | |
return convertView; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I disagree with the suggestion that the app context should be saved rather then the activity context. For the inflater, at least, it is necessary to use the activity context. I tried using the app context and this caused some wonky behaviour when inflating - it seems that you should always inflate on the activity context, and I suspect this could be taken as a more general lesson: use the activity context for anything related to the activity.