1.simple grid layout
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:columnWidth="80dp"
android:horizontalSpacing="2dp"
android:verticalSpacing="3dp" >
</GridView>
</RelativeLayout>
2.Grid with ads 4 column
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/atozlayout"
>
<GridView
android:id="@+id/gridView1"
android:numColumns="auto_fit"
android:columnWidth="55dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp">
</GridView>
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="fill ur id"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
</LinearLayout>
3. 3 columns
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/atozlayout"
>
<GridView
android:id="@+id/gridView1"
android:numColumns="auto_fit"
android:columnWidth="80dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:horizontalSpacing="2dp"
android:verticalSpacing="3dp" >
</GridView>
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="ur id"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
</LinearLayout>
4. GRidview with ads above it
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/atozlayout"
>
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentTop="true"
ads:adUnitId="YOUR ID"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
<GridView
android:id="@+id/gridView1"
android:numColumns="auto_fit"
android:columnWidth="80dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:horizontalSpacing="2dp"
android:verticalSpacing="10dp"
>
</GridView>
</LinearLayout>
java
public class MainActivity extends Activity {
GridView mSampleGridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSampleGridView = (GridView) findViewById(R.id.gridView1);
mSampleGridView.setAdapter(new GridViewAdapter());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class GridViewAdapter extends BaseAdapter {
class GridItem {
String category;
int drawable;
public GridItem(String cat, int d) {
category = cat;
drawable = d;
}
}
Vector<GridItem> mCategories;
public GridViewAdapter() {
putGridItems();
}
/**
* Add all your data here.. Telephone, Police, Ambulance, Hospitals, Schools and more
*/
private void putGridItems(){
mCategories = new Vector<GridItem>();
mCategories.add(new GridItem("Fire", R.drawable.firew));
mCategories.add(new GridItem("Gas", R.drawable.gasw));
mCategories.add(new GridItem("Fire", R.drawable.firew));
mCategories.add(new GridItem("Gas", R.drawable.gasw));
mCategories.add(new GridItem("Fire", R.drawable.firew));
mCategories.add(new GridItem("Gas", R.drawable.gasw));
mCategories.add(new GridItem("Fire", R.drawable.firew));
mCategories.add(new GridItem("Gas", R.drawable.gasw));
mCategories.add(new GridItem("Fire", R.drawable.firew));
mCategories.add(new GridItem("Gas", R.drawable.gasw));
mCategories.add(new GridItem("Fire", R.drawable.firew));
mCategories.add(new GridItem("Gas", R.drawable.gasw));
}
@Override
public int getCount() {
return mCategories.size();
}
@Override
public Object getItem(int position) {
return mCategories.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
GridItem item = mCategories.get(position);
TextView textView = new TextView(getApplicationContext());
Drawable d = getResources().getDrawable(item.drawable);
textView.setCompoundDrawablesWithIntrinsicBounds(null, d, null,
null);
textView.setTextSize(14);
textView.setText(item.category);
textView.setGravity(Gravity.CENTER);
textView.setPadding(1, 3, 1, 2);
textView.setOnClickListener(new GridItemClickListener());
return textView;
}
private class GridItemClickListener implements OnClickListener{
@Override
public void onClick(View v) {
Log.d("What is it?", v.toString());
TextView t = (TextView)v;
Toast.makeText(getApplicationContext(), t.getText() , Toast.LENGTH_SHORT).show();
}
}
}
}
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:columnWidth="80dp"
android:horizontalSpacing="2dp"
android:verticalSpacing="3dp" >
</GridView>
</RelativeLayout>
2.Grid with ads 4 column
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/atozlayout"
>
<GridView
android:id="@+id/gridView1"
android:numColumns="auto_fit"
android:columnWidth="55dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp">
</GridView>
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="fill ur id"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
</LinearLayout>
3. 3 columns
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/atozlayout"
>
<GridView
android:id="@+id/gridView1"
android:numColumns="auto_fit"
android:columnWidth="80dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:horizontalSpacing="2dp"
android:verticalSpacing="3dp" >
</GridView>
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="ur id"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
</LinearLayout>
4. GRidview with ads above it
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/atozlayout"
>
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentTop="true"
ads:adUnitId="YOUR ID"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
<GridView
android:id="@+id/gridView1"
android:numColumns="auto_fit"
android:columnWidth="80dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:horizontalSpacing="2dp"
android:verticalSpacing="10dp"
>
</GridView>
</LinearLayout>
java
public class MainActivity extends Activity {
GridView mSampleGridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSampleGridView = (GridView) findViewById(R.id.gridView1);
mSampleGridView.setAdapter(new GridViewAdapter());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class GridViewAdapter extends BaseAdapter {
class GridItem {
String category;
int drawable;
public GridItem(String cat, int d) {
category = cat;
drawable = d;
}
}
Vector<GridItem> mCategories;
public GridViewAdapter() {
putGridItems();
}
/**
* Add all your data here.. Telephone, Police, Ambulance, Hospitals, Schools and more
*/
private void putGridItems(){
mCategories = new Vector<GridItem>();
mCategories.add(new GridItem("Fire", R.drawable.firew));
mCategories.add(new GridItem("Gas", R.drawable.gasw));
mCategories.add(new GridItem("Fire", R.drawable.firew));
mCategories.add(new GridItem("Gas", R.drawable.gasw));
mCategories.add(new GridItem("Fire", R.drawable.firew));
mCategories.add(new GridItem("Gas", R.drawable.gasw));
mCategories.add(new GridItem("Fire", R.drawable.firew));
mCategories.add(new GridItem("Gas", R.drawable.gasw));
mCategories.add(new GridItem("Fire", R.drawable.firew));
mCategories.add(new GridItem("Gas", R.drawable.gasw));
mCategories.add(new GridItem("Fire", R.drawable.firew));
mCategories.add(new GridItem("Gas", R.drawable.gasw));
}
@Override
public int getCount() {
return mCategories.size();
}
@Override
public Object getItem(int position) {
return mCategories.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
GridItem item = mCategories.get(position);
TextView textView = new TextView(getApplicationContext());
Drawable d = getResources().getDrawable(item.drawable);
textView.setCompoundDrawablesWithIntrinsicBounds(null, d, null,
null);
textView.setTextSize(14);
textView.setText(item.category);
textView.setGravity(Gravity.CENTER);
textView.setPadding(1, 3, 1, 2);
textView.setOnClickListener(new GridItemClickListener());
return textView;
}
private class GridItemClickListener implements OnClickListener{
@Override
public void onClick(View v) {
Log.d("What is it?", v.toString());
TextView t = (TextView)v;
Toast.makeText(getApplicationContext(), t.getText() , Toast.LENGTH_SHORT).show();
}
}
}
}
No comments:
Post a Comment