<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:background="@drawable/bg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.listviewexample.MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:entries="@array/color" >
</ListView>
</RelativeLayout>
Activity_second.xml file::>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.listviewexample.Second"
android:id="@+id/rel1"
>
</RelativeLayout>
Java File code
MainACtivity.java
package com.example.listviewexample;
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity {
ListView l1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> color=new ArrayList<String>();
color.add("YELLOW");
color.add("BLUE");
color.add("RED");
color.add("BLACK");
color.add("GREEN");
ArrayAdapter<String> ad=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, color);
l1=(ListView) findViewById(R.id.listView1);
l1.setAdapter(ad);
l1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String col;
col= arg0.getItemAtPosition(arg2).toString();
Intent i=new Intent(getApplicationContext(),Second.class);
i.putExtra("color", col);
startActivity(i);
}
});
}
}
SecondActivity.java
package com.example.listviewexample;
import android.R.integer;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Second extends ActionBarActivity {
TextView txt1;
RelativeLayout r1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
r1= (RelativeLayout) findViewById(R.id.rel1);
Intent i= getIntent();
String colr=i.getStringExtra("color");
//txt1.setText("YOUR SELECTED COLOR IS "+colr);
// int colc= Integer.parseInt(colr);
// r1.setBackgroundColor(Color.BLACK);
if(colr.equals("BLACK"))
{
r1.setBackgroundColor(Color.BLACK);
}
else if(colr.equals("BLUE"))
{
r1.setBackgroundColor(Color.BLUE);
}
else if(colr.equals("YELLOW"))
{
r1.setBackgroundColor(Color.YELLOW);
}
else if(colr.equals("GREEN"))
{
r1.setBackgroundColor(Color.GREEN);
}
else if(colr.equals("RED"))
{
r1.setBackgroundColor(Color.RED);
}
else
{
r1.setBackgroundColor(Color.CYAN);
}
}
}