File MainActivity.java :
****
package wulan.novi.gridview;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
public class MainActivity extends Activity implements AdapterView.OnItemClickListener {
TextView selection;
String[] pilihan={ "Wulan","Wulan2","Wulan3","Wulan4","Wulan5","Wulan6","Wulan7" };
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
selection=(TextView) findViewById(R.id.selection);
GridView gv=(GridView) findViewById(R.id.grid);
ArrayAdapter<String> aa = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1,pilihan);
gv.setAdapter(aa);
gv.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
selection.setText(pilihan[position]);
}
}
****
File main_activity.xml :
****
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff0033cc"
android:textSize="14pt"
android:textStyle="bold"
/>
<GridView
android:id="@+id/grid"
android:background="#ff0000ff"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:verticalSpacing="35px"
android:horizontalSpacing="5px"
android:numColumns="auto_fit"
android:columnWidth="100px"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
****
Berikut hasil running nya :
No comments:
Post a Comment