activity_main.xml
<ListView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:id="@+id/listViewCountries" />
res/values/strings.xml
<resources> <string name="app_name">012</string> <string-array name="countries"> <item>Iran</item> <item>India</item> <item>France</item> <item>Italy</item> <item>Iraq</item> </string-array> </resources>
MainActivity.java
ListView listViewCountries; ArrayAdapter<String> arrayAdapter; String[] countries; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listViewCountries= (ListView) findViewById(R.id.listViewCountries); countries=getResources().getStringArray(R.array.countries); arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,countries); listViewCountries.setAdapter(arrayAdapter); 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_LONG); toast.show(); } }); }
References
https://github.com/mhdr/AndroidSamples/tree/master/012