Today I am going to explain you How you u can implement a grid view and put button in the cells of grid view. Grid view is used to render a view at your own or we can say that you can customize the a group of objects by your own suppose you want a create a horizontal list of weekdays
which i am going to explain in this tutorial So for that some of you would take a image and put it in a button or image view or some of you will create a separate widget of each day.
But the easiest method would be the implementation of grid view. Its an example you can create an list or any view by using grid view.So lets start
1. create a layout file with the name gridcell and put following XML code in that.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/gridcell"
android:layout_gravity="center"
android:textColor="#FFFFFF"
android:background="@drawable/calendar_button_selector"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
2. Now put the following code in your main XML file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
>
<GridView
android:id="@+id/days"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="-1px"
android:numColumns="7"
android:verticalSpacing="-1px" >
</GridView>
</RelativeLayout>
3. Now comes the coding part. Create a class Days and extend it with BaseAdapter. and put the following code
Context context;
private final List<String> list;
private Button gridcell;
String[] week;
public Days(Context applicationContext, int gridcell, String[] weekdays) {
// TODO Auto-generated constructor stub
this.context=applicationContext;
this.week=weekdays;
this.list = new ArrayList<String>();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return week.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.gridcell, parent, false);
gridcell = (Button) convertView.findViewById(R.id.gridcell);
gridcell.setText(week[position].toString());
return convertView;
}
4. Fourth and final step create a object of the Days class and initialize it after and initialize the grid view also.
Days dayAdapter;
GridView days;
private final String[] weekdays = new String[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
after this put days variable as a set adapter object.
dayAdapter= new Days(this, R.id.gridcell,weekdays);
days=(GridView)findViewById(R.id.days);
days.setAdapter(dayAdapter);
Snapshots:-
Click to Download The Project
which i am going to explain in this tutorial So for that some of you would take a image and put it in a button or image view or some of you will create a separate widget of each day.
But the easiest method would be the implementation of grid view. Its an example you can create an list or any view by using grid view.So lets start
1. create a layout file with the name gridcell and put following XML code in that.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/gridcell"
android:layout_gravity="center"
android:textColor="#FFFFFF"
android:background="@drawable/calendar_button_selector"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
2. Now put the following code in your main XML file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
>
<GridView
android:id="@+id/days"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="-1px"
android:numColumns="7"
android:verticalSpacing="-1px" >
</GridView>
</RelativeLayout>
3. Now comes the coding part. Create a class Days and extend it with BaseAdapter. and put the following code
Context context;
private final List<String> list;
private Button gridcell;
String[] week;
public Days(Context applicationContext, int gridcell, String[] weekdays) {
// TODO Auto-generated constructor stub
this.context=applicationContext;
this.week=weekdays;
this.list = new ArrayList<String>();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return week.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.gridcell, parent, false);
gridcell = (Button) convertView.findViewById(R.id.gridcell);
gridcell.setText(week[position].toString());
return convertView;
}
4. Fourth and final step create a object of the Days class and initialize it after and initialize the grid view also.
Days dayAdapter;
GridView days;
private final String[] weekdays = new String[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
after this put days variable as a set adapter object.
dayAdapter= new Days(this, R.id.gridcell,weekdays);
days=(GridView)findViewById(R.id.days);
days.setAdapter(dayAdapter);
Snapshots:-
Click to Download The Project
No comments:
Post a Comment