Reputation: 1253
I am trying to change the font size of my gridview in my android application.
this is my code for makeing the gridview:
GridView gridview = (GridView)findViewById(R.id.gridView);
ArrayAdapter<String> arrayadapter = new ArrayAdapter<String>(
this,
R.layout.customstyle,
Rows );
gridview.setNumColumns(14);
gridview.setAdapter(arrayadapter);
and here is the code from my custom style .xml document
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8dip" >
</TextView>
</LinearLayout>
The program currently crashes when i try and get it to apply my stlye but it workss find when it uses the standard styles. so can someone help me fix this style so i can set the font size Thanks
Upvotes: 3
Views: 8350
Reputation: 3266
You can create your own custom adapter like this :
public class MyAdapter extends BaseAdapter{
private Context mContext;
private String[] text = {};
private Integer[] mThumbIds = {};
public MyAdapter(Context c,String[] text)
{
mContext = c;
this.text = text;
}
public int getCount()
{
return mThumbIds.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View myView = convertView;
if (convertView == null)
{
LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView =li.inflate(R.layout.grid_item, null);
TextView tv = (TextView)myView.findViewById(R.id.grid_itemTxt);
tv.setText(text[position]);
}
return myView;
}
GridItem Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/gridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation= "vertical"
android:gravity="center_horizontal" >
<TextView
android:id="@+id/grid_itemTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#6075B2">
</TextView>
</LinearLayout>
and then on your onCreate() method:
private GridView grid;
private String text[] = {"hello","world","here","is","a","grid","view"};
grid = (GridView)findViewById(R.id.yourGridLayoutXml);
grid.setAdapter(new MyAdapter(this, text));
Note: MyAdapter
is a java class that extends BaseAdataper
Upvotes: 1
Reputation: 20155
ArrayAdapter
should have the resource id of type TextView
.
Remove parent LinearLayout
and your layout should be like
use like this
customStyle.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8sp" >
</TextView>
replace your custom layout with the above that will fix your problem.
Upvotes: 3