Skip to content

Instantly share code, notes, and snippets.

@iheanyi
Last active February 10, 2019 17:32
Show Gist options
  • Save iheanyi/5774841 to your computer and use it in GitHub Desktop.
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.
<?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>
<?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>
<?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>
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;
}
}
@iheanyi
Copy link
Author

iheanyi commented Jul 24, 2013

@GitTom - Duly noted, that makes sense . . . Thanks for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment