1. Tampilan
Main Menu Grid
package com.contoh.grid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import
android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
public class GridActivity extends
Activity {
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView)
findViewById(R.id.gridview);
gridview.setAdapter(new
ImageAdapter(this));
gridview.setOnItemClickListener(new
OnItemClickListener() {
public void
onItemClick(AdapterView<?> parent, View v, int position, long
id) {
Toast.makeText(GridActivity.this,
"" + position, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean
onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.layout.main,
menu);
return true;
}
}
<?xml
version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
package com.contoh.grid;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends
BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int
position) {
return null;
}
public long getItemId(int
position) {
return 0;
}
//Gambar baru untuk setiap
item yang direferensikan oleh Adapter
public View getView(int
position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null)
{
imageView = new
ImageView(mContext);
imageView.setLayoutParams(new
GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView)
convertView;
}
imageView.setImageResource(mThumbIds[position]);
return
imageView;
}
// sumber file untuk gambar
private Integer[] mThumbIds = {
R.drawable.sample_2,
R.drawable.sample_3,
R.drawable.sample_4,
R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0,
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3,
R.drawable.sample_4,
R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3,
R.drawable.sample_4,
R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
}
Gambar.
Main Menu Grid
2. Tampilan_List
package com.contoh.List;
import android.os.Bundle;
import android.app.ListActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class List extends
ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] { "Android",
"iPhone", "WindowsMobile",
"Blackberry",
"WebOS", "Ubuntu", "Windows7", "Max OS
X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new
ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
values);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int
position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
}
Gambar. Tampilan_List
3. Tampilan_DropDwon
package
com.contoh.dropdown;
import
android.os.Bundle;
import
android.app.Activity;
import
android.widget.ArrayAdapter;
import
android.widget.Spinner;
public
class DropDown extends Activity {
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setContentView(R.layout.main);
Spinner spinner = (Spinner)
findViewById(R.id.spinner);
String kota[]={"Kuala
Lumpur","Melaka","Ipoh","Serawak","Pulau
Pinang","Kuceng",
"Johor","Nilam","Langkawi","Dori","Temeran","Anom","Kina
Balu","Perak"};
spinner.setAdapter(new
ArrayAdapter<String>(this, R.layout.list_item, kota));
}
}
<?xml
version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Nama kota
Malasiya"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Gambar. Tampilan_DropDwon
Tidak ada komentar:
Posting Komentar