res/values/strings.xml
<resources> <string name="app_name">014</string> <string-array name="countries"> <item>Iran</item> <item>Iraq</item> <item>India</item> <item>Italy</item> <item>United Kingdom</item> <item>United States</item> <item>Israel</item> <item>Kuwait</item> </string-array> </resources>
res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="w3_camo_gray">#9495a5</color> <color name="Lemon_Yellow">#ffbb00</color> </resources>
res/layout/listview_countries_custom_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textViewListViewCountriesItem" android:gravity="center" android:paddingBottom="15px" android:textStyle="bold" android:textSize="18dp" android:background="@drawable/listview_countries_selector"/> </LinearLayout>
res/drawable/listview_countries_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@color/w3_camo_gray" ></item> <item android:state_selected="true" android:drawable="@color/Lemon_Yellow" ></item> </selector>
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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="iterator.ir.a014.MainActivity"> <ListView android:id="@+id/listviewCountries" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> </RelativeLayout>
MainActivity.java
ListView listViewCountries; String[] countries; ArrayAdapter<String> listViewArrayAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); countries=getResources().getStringArray(R.array.countries); listViewCountries= (ListView) findViewById(R.id.listviewCountries); listViewArrayAdapter=new ArrayAdapter<String>(getBaseContext(), R.layout.listview_countries_custom_layout, R.id.textViewListViewCountriesItem,countries); listViewCountries.setAdapter(listViewArrayAdapter); listViewCountries.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast toast=Toast.makeText(getBaseContext(),parent.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT); toast.show(); view.setSelected(true); } }); }
References
https://github.com/mhdr/AndroidSamples/tree/master/014