Dynamic ListView
This blog describe about dynamic list view creation and implementation programatically in android apps. For this need to take array list and array adapter add item data list programatically .
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_dark"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dynamic List View"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:textColor="#336633"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="15sp"
android:text="Show List View" />
</LinearLayout>
DynamicListViewActivity.java
package com.rakesh.tiwari.dynamiclistview;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
public class DynamicListViewActivity extends Activity {
List<String>list;
Button btnShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final LinearLayout linl=(LinearLayout)findViewById(R.id.linearlayout);
btnShow=(Button)findViewById(R.id.button1);
final LinearLayout.LayoutParams linlayout=new LinearLayout.LayoutParams((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);
linlayout.topMargin=10;
linlayout.leftMargin=40;
final ListView lv=new ListView(this);
list=new ArrayList<String>();
// Programatically add item data with array list.
list.add("C");
list.add("C++");
list.add("Java");
list.add("Android");
list.add("Blackberry");
list.add(".Net");
list.add("C#");
list.add("PHP");
btnShow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayAdapter<String> adptr=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line,list);
adptr.setDropDownViewResource(android.R.layout.simple_list_item_multiple_choice);
lv.setAdapter(adptr);
lv.setLayoutParams(linlayout);
linl.addView(lv);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.dynamic_list_view, menu);
return true;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rakesh.tiwari.dynamiclistview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:screenOrientation="portrait"
android:name="com.rakesh.tiwari.dynamiclistview.DynamicListViewActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ListView |
ListView |
ListView |
No comments:
Post a Comment