ListView Example Output :
ListView |
Project Structure :
Step 1 :
Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10sp"
android:text="Country List"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
android:textColor="#ff305aff"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:divider="#ff305aff"
android:dividerHeight="1dp" />
</LinearLayout>
</LinearLayout>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10sp"
android:text="Country List"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
android:textColor="#ff305aff"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:divider="#ff305aff"
android:dividerHeight="1dp" />
</LinearLayout>
</LinearLayout>
Step 2:
MainActivity.java
package
com.pratik.listviewexample;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public
class MainActivity extends ActionBarActivity {
private ListView listView;
private ArrayList<String> arrayList;
private ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize listview
listView = (ListView) findViewById(R.id.listView);
//for displaying list items here i used ArrayList and add string
arrayList = new ArrayList<>();
arrayList.add("Australia");
arrayList.add("Brazil");
arrayList.add("Canada");
arrayList.add("Denmark");
arrayList.add("Egypt");
arrayList.add("France");
arrayList.add("Germany");
arrayList.add("Hong Kong");
arrayList.add("India");
arrayList.add("Japan");
private ListView listView;
private ArrayList<String> arrayList;
private ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize listview
listView = (ListView) findViewById(R.id.listView);
//for displaying list items here i used ArrayList and add string
arrayList = new ArrayList<>();
arrayList.add("Australia");
arrayList.add("Brazil");
arrayList.add("Canada");
arrayList.add("Denmark");
arrayList.add("Egypt");
arrayList.add("France");
arrayList.add("Germany");
arrayList.add("Hong Kong");
arrayList.add("India");
arrayList.add("Japan");
arrayAdapter = new ArrayAdapter<String>(this, R.layout.select_dialog_item_material, arrayList); //assign adapter to listview listView.setAdapter(arrayAdapter); //here i can set click listener of listview to perform click event in each item of listview listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { //Getting selected Item Value String value = (String) listView.getItemAtPosition(position);
Toast.makeText(MainActivity.this, "You're Selected : " + value, Toast.LENGTH_SHORT).show(); } }); } }
No comments:
Post a Comment