Using GridView layout in android
Tech-Today

Using GridView layout in android


So, lately I've been relearning android development and as I expected there were already a lot of changes since I last code in it (eclair). Like the concept of Fragment (which I love), unfortunately statistics show that most of the users are still using API 10 (Gingerbread), so I'm sticking with it :-).

What I would like to achieve right now is a menu using grid view, and here's how I code it.

1.) We need a MainActivity where we will load the GridView:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

GridView menuGrid = (GridView) findViewById(R.id.menu);
menuGrid.setAdapter(new MenuAdapter(this));

menuGrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,
int position, long id) {
Toast.makeText(MainActivity.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
}

2.) The MenuAdapter, that will serve as the interface between the data (string array, db) and the view:
public class MenuAdapter extends BaseAdapter {
private Context context;
private String[] labels;
private int[] images = { R.drawable.menu_1, R.drawable.menu_2,
R.drawable.menu_3, R.drawable.menu_4 };

public MenuAdapter(Context context) {
this.context = context;
this.labels = context.getResources().getStringArray(R.array.menu_items);
}

@Override
public int getCount() {
return this.labels.length;
}

@Override
public Object getItem(int arg0) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

TextView textView = (TextView) convertView;

if (textView == null) {
textView = (TextView) View.inflate(context, R.layout.menu_item,
null);
}

textView.setText(this.labels[position]);
textView.setCompoundDrawablesWithIntrinsicBounds(0, images[position],
0, 0);

return textView;
}
}

3.) The layouts/views: MainActivity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".MainActivity" >

<include
android:layout_
android:layout_
layout="@layout/menu" />

</RelativeLayout>
Menu (Note that we use compound view (icon/text))
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu"
android:layout_
android:layout_
android:layout_marginLeft="25dp"
android:horizontalSpacing="5dp"
android:numColumns="2"
android:verticalSpacing="20dp" />
MenuItem
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menuItem"
android:layout_
android:layout_
android:layout_gravity="center_horizontal"
android:drawableTop="@drawable/icon"
android:gravity="center_horizontal"
android:text="@string/common_label"
android:textColor="@color/menu_item" />

4.) And the resource: strings
 <string-array name="menu_items">
<item>Menu 1</item>
<item>Menu 2</item>
<item>Menu 3</item>
<item>Menu 4</item>
</string-array>




- Android Studio - Displaying List View Inside Alertdialog
Android Studio - Displaying List View Inside AlertDialog 1.) Follow this code: ================>Note: custom_dialog_layout.xml is the layout that will pop-up >Note: row.xml is the item used for populating rowAlertDialog.Builder builder = new AlertDialog.Builder(new...

- Android Studio - Creating Material Design App Bar
Android Studio - Creating Material Design App Bar 1.) Set-up the color that we'll use for the app bar: ======================================== >Create a new xml inside values and name it color.xml <resources>     <color name="ColorPrimary">#FF5722</color>...

- How To Sign Your Android Application To Use The Map Api In Mobile Device
There are 4 steps that need to be done to release a mobile application with map on a android powered mobile device. Assumption: You have eclipse installed with android plugin installed. If you don't know how just follow the steps here: http://developer.android.com/sdk/eclipse-adt.html...

- How To Get Imei From Android Or Iphone
For android it's pretty straight forward. You just have to add read phone state permission. <uses-permission android:name="android.permission.READ_PHONE_STATE"/> And call the API: String imei = TelephonyManager.getDefault().getDeviceId(); if (TextUtils.isEmpty(imei))...

- Android Bundle Sqlite Database On Application. Create Sqlite Database On First Application Invoke.
How to setup a default sqlite database structure with data in an android application. Add this class and initialize it in your main class that extends Activity. import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase;...



Tech-Today








.