Step 1: Create a xml android layout file named custom and save it in the res--> layout folder in your eclipse project. Copy the below code and paste it in your XML file. As shown in the code, have used a Vertical LinearLayout to place our listview. This is the most important step in this tutorial as we'll be create a android View with the help of this layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/ll1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Step 2: Create a listview.xml Android layout file with the below code and place it in your res--> layout folder of your project. As our ListView data will be stored in TexView, we'll need to define this layout
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/childname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
/>
Step 3: Now as you created your XML layout file, its time to load the layout. Declare the below 5 variables in your Activity class:
ListView lv;
View vw;
Dialog dlg;
ArrayAdapter<String> adapter;
String listData;
In the same Activity class, create a method named showDialog() and place the below code in it:
listData=new String[3];
listData[0]="Pramod";
listData[1]="Computer";
listData[2]="Android";
vw =YourActivityName.this.getLayoutInflater().inflate(R.layout.custom, null);
dlg = new Dialog(YourActivityName.this);
dlg.setContentView(vw);
dlg.show();
lv= (ListView) vw.findViewById(R.id.ll1);
adapter = (new ArrayAdapter<String>(YourActivityName.this, R.layout.listview, R.id.childname, listData));
lv.setAdapter(adapter);
To check whether the above code works or not, call showDialog() in your onCreate(Bundle savedInstanceState) method.
Note: Replace YourActivityName by your Class name where you've stored defined the showDialog method.
reference:
http://developer.android.com/reference/android/view/LayoutInflater.html
http://developer.android.com/guide/topics/ui/dialogs.html
http://developer.android.com/reference/android/widget/ArrayAdapter.html
http://developer.android.com/guide/topics/ui/layout/listview.html
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/ll1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Step 2: Create a listview.xml Android layout file with the below code and place it in your res--> layout folder of your project. As our ListView data will be stored in TexView, we'll need to define this layout
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/childname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
/>
Step 3: Now as you created your XML layout file, its time to load the layout. Declare the below 5 variables in your Activity class:
ListView lv;
View vw;
Dialog dlg;
ArrayAdapter<String> adapter;
String listData;
In the same Activity class, create a method named showDialog() and place the below code in it:
listData=new String[3];
listData[0]="Pramod";
listData[1]="Computer";
listData[2]="Android";
vw =YourActivityName.this.getLayoutInflater().inflate(R.layout.custom, null);
dlg = new Dialog(YourActivityName.this);
dlg.setContentView(vw);
dlg.show();
lv= (ListView) vw.findViewById(R.id.ll1);
adapter = (new ArrayAdapter<String>(YourActivityName.this, R.layout.listview, R.id.childname, listData));
lv.setAdapter(adapter);
- the getLayoutInflater().inflate method will load your layout defined in the custom.xml file in a view.
To check whether the above code works or not, call showDialog() in your onCreate(Bundle savedInstanceState) method.
Note: Replace YourActivityName by your Class name where you've stored defined the showDialog method.
reference:
http://developer.android.com/reference/android/view/LayoutInflater.html
http://developer.android.com/guide/topics/ui/dialogs.html
http://developer.android.com/reference/android/widget/ArrayAdapter.html
http://developer.android.com/guide/topics/ui/layout/listview.html