Android ListView
Android ListView
- Static ListView Creation in Android
- Dynamic ListView Creation in Android
- Static ListView with Dynamic Values in Android
- Run Time Add Item to ListView in Android
- Get Selected Item value in ListView
- Run Time Add a Layout into ListView in Android
- Custom ArrayAdapter in ListView
Static ListView Creation in Android
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project
Click Here.
Step 2 : Open res -> layout ->
activity_main.xml (or) main.xml and add following code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:entries="@array/list"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package com.gudivada.hemanthsomaraju;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Step 4 : Open AndroidManifest.xml and add following code :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gudivada.hemanthsomaraju"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.gudivada.hemanthsomaraju.MainActivity"
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>
Step 5 : Open res ->values ->strings.xml and add following code :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">HemanthSomaraju</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string-array name="list">
<item>Gudivada</item>
<item>Somaraju</item>
<item>Hemanth</item>
<item>Kakinada</item>
<item>India</item>
</string-array>
</resources>
Step 6 : Our output will be like this :
Dynamic ListView Creation in Android
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project
Click Here.
Step 2 : Open res -> layout ->
activity_main.xml (or) main.xml and add following code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rl" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:padding="15dp"
android:text="Button" />
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package com.gudivada.hemanthsomaraju;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
public class MainActivity extends Activity {
List<String> li;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RelativeLayout rl=(RelativeLayout) findViewById(R.id.rl);
final RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams
((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);
params.leftMargin=10;
params.topMargin=150;
Button show=(Button) findViewById(R.id.button1);
final ListView list=new ListView(this);
li=new ArrayList<String>();
li.add("Gudivada");
li.add("Somaraju");
li.add("Hemanth");
li.add("Kakinada");
li.add("India");
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayAdapter<String> adp=new ArrayAdapter<String> (getBaseContext(),
android.R.layout.simple_dropdown_item_1line,li);
adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
list.setAdapter(adp);
list.setLayoutParams(params);
rl.addView(list);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Step 4 : Open AndroidManifest.xml and add following code :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gudivada.hemanthsomaraju"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.gudivada.hemanthsomaraju.MainActivity"
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>
Step 5 : Our output will be like this :
Static ListView with Dynamic Values in Android
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project
Click Here.
Step 2 : Open res -> layout ->
activity_main.xml (or) main.xml and add following code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:padding="20dp"
android:text="ListView" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginTop="30dp"
android:layout_alignParentLeft="true" >
</ListView>
</RelativeLayout>
Step 3 : Open res -> layout -> list.xml and add following code :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="20dp"
android:textSize="18sp"
android:textColor="#800080">
</TextView>
Step 4 : Open src -> package -> MainActivity.java and add following code :
package com.gudivada.hemanthsomaraju;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity {
List<String> li;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
li=new ArrayList<String>();
li.add("Gudivada");
li.add("Somaraju");
li.add("Hemanth");
li.add("Kakinada");
li.add("India");
final Button show=(Button) findViewById(R.id.button1);
final ListView list=(ListView) findViewById(R.id.listView1);
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayAdapter<String> adp=new ArrayAdapter<String>
(getBaseContext(),R.layout.list,li);
list.setAdapter(adp);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Step 5 : Open AndroidManifest.xml and add following code :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gudivada.hemanthsomaraju"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.gudivada.hemanthsomaraju.MainActivity"
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>
Step 6 : Our output will be like this :
Run Time Add Item to ListView in Android
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project
Click Here.
Step 2 : Open res -> layout ->
activity_main.xml (or) main.xml and add following code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Button" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_marginTop="30dp" >
</ListView>
</RelativeLayout>
Step 3 : Open res -> layout -> list.xml and add following code :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="20dp"
android:textSize="18sp"
android:textColor="#800080">
</TextView>
Step 4 : Open src -> package -> MainActivity.java and add following code :
package com.gudivada.hemanthsomaraju;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
List<String> li;
ListView list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
li=new ArrayList<String>();
li.add("H1");
li.add("H2");
li.add("H3");
final Button show=(Button) findViewById(R.id.button1);
final EditText et=(EditText) findViewById(R.id.editText1);
list=(ListView) findViewById(R.id.listView1);
add();
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
li.add(et.getText().toString());
et.setText(null);
add();
}
});
}
public void add()
{
ArrayAdapter<String> adp=new ArrayAdapter<String>
(getBaseContext(),R.layout.list,li);
list.setAdapter(adp);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Step 5 : Open AndroidManifest.xml and add following code :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gudivada.hemanthsomaraju"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.gudivada.hemanthsomaraju.MainActivity"
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>
Step 6 : Our output will be like this :
Get Selected Item value in ListView
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project
Click Here.
Step 2 : Open res -> layout ->
activity_main.xml (or) main.xml and add following code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Button" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_marginTop="30dp" >
</ListView>
</RelativeLayout>
Step 3 : Open res -> layout -> list.xml and add following code :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="20dp"
android:textSize="18sp"
android:textColor="#800080">
</TextView>
Step 4 : Open src -> package -> MainActivity.java and add following code :
package com.gudivada.hemanthsomaraju;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
List<String> li;
ListView list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
li=new ArrayList<String>();
li.add("Hemanth");
li.add("Somaraju");
li.add("GUdivada");
final Button show=(Button) findViewById(R.id.button1);
final EditText et=(EditText) findViewById(R.id.editText1);
list=(ListView) findViewById(R.id.listView1);
add();
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
li.add(et.getText().toString());
et.setText(null);
add();
}
});
}
public void add()
{
ArrayAdapter<String> adp=new ArrayAdapter<String>
(getBaseContext(),R.layout.list,li);
list.setAdapter(adp);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), li.get(arg2),
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Step 5 : Open AndroidManifest.xml and add following code :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gudivada.hemanthsomaraju"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.gudivada.hemanthsomaraju.MainActivity"
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>
Step 6 : Our output will be like this :
Run Time Add a Layout into ListView in Android
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project
Click Here.
Step 2 : Open res -> layout ->
activity_main.xml (or) main.xml and add following code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Button" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_marginTop="30dp" >
</ListView>
</RelativeLayout>
Step 3 : Open res -> layout -> list.xml and add following code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent" >
<ImageView
android:id="@+id/icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="8dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textSize="30dp" />
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="30dp" />
</LinearLayout>
Step 4 : Open src -> package -> MainActivity.java and add following code :
package com.gudivada.hemanthsomaraju;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
List<String> li;
ListView list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
li=new ArrayList<String>();
li.add("Gudivada");
li.add("Somaraju");
li.add("Hemanth");
final Button show=(Button) findViewById(R.id.button1);
final EditText et=(EditText) findViewById(R.id.editText1);
list=(ListView) findViewById(R.id.listView1);
add();
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
li.add(et.getText().toString());
et.setText(null);
add();
}
});
}
public void add()
{
ArrayAdapter<String> adp=new ArrayAdapter<String>
(getBaseContext(),R.layout.list,R.id.label,li);
list.setAdapter(adp);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(),li.get(arg2),
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Step 5 : Open AndroidManifest.xml and add following code :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gudivada.hemanthsomaraju"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.gudivada.hemanthsomaraju.MainActivity"
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>
Step 6 : Our output will be like this :
Custom ArrayAdapter in ListView
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project
Click Here.
Step 2 : Open res -> layout ->
activity_main.xml (or) main.xml and add following code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="@+id/logo"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginLeft="5px"
android:layout_marginRight="20px"
android:layout_marginTop="5px"
android:src="@drawable/android" >
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android"
android:textColor="#8B008B"
android:paddingTop="10dp"
android:paddingLeft="20dp"
android:textSize="27dp" >
</TextView>
</LinearLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package com.gudivada.hemanthsomaraju;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View;
public class MainActivity extends ListActivity {
static final String[] Android =
new String[] { "amazon", "android", "apple", "ask",
"dropbox","face book","twitter"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ListAct(this, Android));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//get selected items
String selectedValue = (String) getListAdapter().getItem(position);
Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
}
}
Step 4 : Open src -> package -> ListAct.java and add following code :
package com.gudivada.hemanthsomaraju;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ListAct extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public ListAct(Context context, String[] values) {
super(context, R.layout.activity_main, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.activity_main, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
System.out.println(s);
if (s.equals("amazon")) {
imageView.setImageResource(R.drawable.amazon);
} else if (s.equals("android")) {
imageView.setImageResource(R.drawable.android);
} else if (s.equals("apple")) {
imageView.setImageResource(R.drawable.apple);
}else if (s.equals("ask")) {
imageView.setImageResource(R.drawable.ask);
} else if (s.equals("dropbox")) {
imageView.setImageResource(R.drawable.dropbox);
}else if (s.equals("face book")) {
imageView.setImageResource(R.drawable.fb);
} else if (s.equals("twitter")) {
imageView.setImageResource(R.drawable.twitter);
}
else {
imageView.setImageResource(R.drawable.google);
}
return rowView;
}
}
Step 5 : Open AndroidManifest.xml and add following code :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gudivada.hemanthsomaraju"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.gudivada.hemanthsomaraju.MainActivity"
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>
Step 6 : Our output will be like this :