Attributes | Description |
---|---|
android:id | It is the ID of layout for identifying uniquely. |
android:entries | It specifies the reference to an array resource that will populate the ListView. |
android:divider | It is drawable or color to draw between list items. |
android:dividerHeight | It specifies the height of the divider. |
android:footerDividersEnabled | Its default value id true. If it sets to false, the ListView will not draw the divider before each footer view. |
android:headerDividersEnabled | Its default value is true. If it sets to false, the ListView will not draw the divider after each header view. |
XML file for ListView Control
<ListView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:id = "@+id/listView"
android:layout_alignParentLeft = "true"
android:layout_alignParentStart = "true" />
File Name: MainActivity.java
public class MainActivity extends AppCompatActivity
{
ListView lvObj;
String [] city={"Pune","Gwalior","Patna","Nasik","Kashmir",""};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //XML file name activity_main.xml
lvObj = (ListView)findViewById(R.id.listView);
ArrayAdapter<String>arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,city);
lvObj.setAdapter(arrayAdapter);
lvObj.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick (AdapterView<?> parent, View view, intposition, long id)
{
String item = (String)lvObj.getItemAtPosition(position);
Toast toast = Toast.makeText(getApplicationContext(),"Your selected city ="+item,Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,30,150);
toast.show();
}
});
}
}