Activity_Main.xml:
<RelativeLayoutxmlns: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"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:entries="@array/l1">
</ListView>
</RelativeLayout>
Activity_Main2.xml:
<RelativeLayoutxmlns: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"
tools:context=".MainActivity2">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="69dp"
android:text="@string/t1"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="38dp"
android:text="@string/t2"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:layout_marginTop="39dp"
android:text="@string/t3"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView3"
android:layout_marginTop="39dp"
android:text="@string/t4"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="58dp"
android:text="@string/b1" />
</RelativeLayout>
MainActivity.java
package com.example.prac15;
importandroid.os.Bundle;
importandroid.content.Intent;
importandroid.view.Menu;
importandroid.view.View;
importandroid.widget.AdapterView;
importandroid.widget.AdapterView.OnItemClickListener;
importandroid.widget.ListView;
publicclassMainActivityextendsActivityimplementsOnItemClickListener{
@Override
protectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListViewlst=(ListView)findViewById(R.id.listView1);
lst.setClickable(true);
lst.setOnItemClickListener(this);
}
@Override
publicbooleanonCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
returntrue;
}
@Override
publicvoidonItemClick(AdapterView<?>
arg0, View arg1, int arg2, long arg3) {
Integer
it = Integer.valueOf(arg2);
Stringstr
= it.toString();
Intenti
= newIntent(this,MainActivity2.class);
i.putExtra("car",
str);
startActivity(i);
}
}
MainActivity2.java
package
com.example.prac15;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.view.Menu;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.TextView;
publicclass
MainActivity2 extends Activity implementsOnClickListener{
Button
b1;
@Override
protectedvoidonCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
Intent
i = getIntent();
String
str = i.getStringExtra("car");
TextView
txt1 =(TextView) findViewById(R.id.textView1);
TextView
txt2 =(TextView) findViewById(R.id.textView2);
TextView
txt3 =(TextView) findViewById(R.id.textView3);
TextView
txt4 =(TextView) findViewById(R.id.textView4);
if(str.equals("0"))
{
txt1.setText("Car
Name: BMW ");
txt2.setText("Car
Price: 9,00,000 ");
txt3.setText("Launch
Date: 01/08/2000 ");
txt4.setText("Top
Speed: 210kmph");
}
if(str.equals("1"))
{
txt1.setText("Car
Name: GALARDO");
txt2.setText("Car
Price: 25,000,000");
txt3.setText("Launch
Date: 05/05/2005 ");
txt4.setText("Top
Speed: 230kmph");
}
if(str.equals("2"))
{
txt1.setText("Car
Name: CAMERO");
txt2.setText("Car
Price: 3,00,00,000");
txt3.setText("Launch
Date: 08/05/1995");
txt4.setText("Top
Speed: 240kmph");
}
if(str.equals("3"))
{
txt1.setText("Car
Name: FORD MUSTANG");
txt2.setText("Car
Price: 20,99,999");
txt3.setText("Launch
Date: 03/01/1880");
txt4.setText("Top
Speed: 180kmph");
}
if(str.equals("4"))
{
txt1.setText("Car
Name: BUGATI");
txt2.setText("Car
Price: 2,50,00,000");
txt3.setText("Launch
Date: 05/08/1995 ");
txt4.setText("Top
Speed: 250kmph");
}
Button
b1 =(Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
}
@Override
publicbooleanonCreateOptionsMenu(Menu
menu) {
//
Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main_activity2,
menu);
returntrue;
}
@Override
publicvoidonClick(View
v) {
finish();
System.exit(0);
}
}
1 Comments
nice sir
ReplyDelete