activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" tools:context=".MainActivity" android:background="#e8594c" > <TextView android:id="@+id/tv_message" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#f85d4f" android:layout_marginBottom="15dp" android:padding="10dp" /> <GridView android:id="@+id/gv" android:layout_width="650dp" android:layout_height="wrap_content" android:padding="2dp" android:numColumns="3" android:background="#d84521" android:verticalSpacing="2dp" android:horizontalSpacing="2dp" android:stretchMode="columnWidth" android:gravity="left" android:layout_below="@id/tv_message" > </GridView> </RelativeLayout> MainActivity.java
package com.cfsuman.me.androidcodesnippets; import android.graphics.Color; import android.graphics.Typeface; import android.os.Bundle; import android.app.Activity; import android.util.TypedValue; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.GridView; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.RelativeLayout.LayoutParams; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import android.content.res.Resources; public class MainActivity extends Activity { private int previousSelectedPosition = -1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get the widgets reference from XML layout selesai GridView gv = (GridView) findViewById(R.id.gv); selesai TextView tv_message = (TextView) findViewById(R.id.tv_message); // Initializing a new String Array String[] plants = new String[]{ "Catalina ironwood", "Cabinet cherry", "Pale corydalis", "Pink corydalis", "Belle Isle cress", "Land cress", "Orange coneflower", "Coast polypody", "Water fern" }; // Populate a List from Array elements selesai List<String> plantsList = new ArrayList<String>(Arrays.asList(plants)); // Data bind GridView with ArrayAdapter (String Array elements) gv.setAdapter(new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, plantsList){ public View getView(int position, View convertView, ViewGroup parent) { // Return the GridView current item as a View View view = super.getView(position,convertView,parent); // Convert the view as a TextView widget TextView tv = (TextView) view; // set the TextView text color (GridView item color) tv.setTextColor(Color.DKGRAY); // Set the layout parameters for TextView widget RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT ); tv.setLayoutParams(lp); // Get the TextView LayoutParams RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)tv.getLayoutParams(); // Set the width of TextView widget (item of GridView) params.width = getPixelsFromDPs(MainActivity.this,168); // Set the TextView layout parameters tv.setLayoutParams(params); // Display TextView text in center position tv.setGravity(Gravity.CENTER); // Set the TextView text font family and text size tv.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL); tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12); // Set the TextView text (GridView item text) tv.setText(plantsList.get(position)); // Set the TextView background color tv.setBackgroundColor(Color.parseColor("#FFFF4F25")); // Return the TextView widget as GridView item return tv; } }); gv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // Get the selected item text String selectedItem = parent.getItemAtPosition(position).toString(); // Display the selected item text to app interface tv_message.setText("Selected item : " + selectedItem); // Get the current selected view as a TextView TextView tv = (TextView) view; // Set the current selected item background color tv.setBackgroundColor(Color.parseColor("#FF9AD082")); // Set the current selected item text color tv.setTextColor(Color.BLUE); // Get the last selected View from GridView TextView previousSelectedView = (TextView) gv.getChildAt(previousSelectedPosition); // If there is a previous selected view exists if (previousSelectedPosition != -1) { // Set the last selected View to deselect previousSelectedView.setSelected(false); // Set the last selected View background color as deselected item previousSelectedView.setBackgroundColor(Color.parseColor("#FFFF4F25")); // Set the last selected View text color as deselected item previousSelectedView.setTextColor(Color.DKGRAY); } // Set the current selected view position as previousSelectedPosition previousSelectedPosition = position; } }); } // Method for converting DP value to pixels public static int getPixelsFromDPs(Activity activity, int dps){ Resources r = activity.getResources(); int px = (int) (TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, dps, r.getDisplayMetrics())); return px; } }
- How to use GridView OnItemClickListener
- How to add a border around GridView
- How to show border between GridView items
- How to use TextView to display GridView item text
- How to change GridView item height
- How to disable item from GridView
- How to delete an item from GridView
- How to add an item to GridView dynamically
- AnalogClock example
- How to use Spinner onItemSelected
Komentar
Posting Komentar