0% found this document useful (0 votes)
3 views19 pages

Use Layoutmanager and Event Listener

Uploaded by

Hyma Thottathyl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views19 pages

Use Layoutmanager and Event Listener

Uploaded by

Hyma Thottathyl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

ANDROID

How Data is Passed using Bundle


 Use of putstring() methods and putextras() method to first store
the string data inside a bundle and transfer it to next activity.
 Next use the getextras() and getstring() method to fetch the data
from that activity
Develop a simple android application that use layoutmanager and
event listener
Activity_Main.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id= "@+id/imageView"
android:layout_width="150dp"
android:layout_height="150dp"
app:srcCompat="@drawable/login"
android:layout_gravity="center_horizontal"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:hint="ENTER YOUR NAME"
android:layout_gravity="center_horizontal" />
<EditText
android:id="@+id/Phonenumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="ENTER PHONE NUMBER"
android:inputType="phone"
android:layout_gravity="center_horizontal"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"/>

</LinearLayout>
MainActivity.Java
public class MainActivity extends AppCompatActivity {
EditText username;
EditText userphonenumber;
Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText) findViewById(R.id.name);
userphonenumber = (EditText) findViewById(R.id.Phonenumber);
b1 = (Button) findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

String stname = username.getText().toString();

String stphoneno = userphonenumber.getText().toString();

if (stname.equals(“Tovino") && stphoneno.equals("9000180787"))

{
Intent in = new Intent(MainActivity.this, MainActivity2.class);

Bundle bundle = new Bundle();

bundle.putString("uname", stname);

bundle.putString("uno", stphoneno);
in.putExtras(bundle);

startActivity(in);
}
else if (stname.equals(" ") || stphoneno.equals(""))
{

Toast.makeText(getBaseContext(), "Enter both name and phone",


Toast.LENGTH_SHORT).show();
}

else
{

Toast.makeText(getBaseContext(), "Wrong name and phonenumber


entered", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Activity_Main.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity2">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_gravity="center_horizontal"
tools:layout_editor_absoluteX="124dp"
tools:layout_editor_absoluteY="139dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_gravity="center_horizontal"
tools:layout_editor_absoluteX="124dp"
tools:layout_editor_absoluteY="251dp" />

</LinearLayout>
MainActivity2.Java
public class MainActivity2 extends AppCompatActivity {
TextView text1,text2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
text1 = findViewById(R.id.textView1);
text2 = findViewById(R.id.textView2);
Bundle bundle = getIntent().getExtras();

String data1=bundle.getString("uname");

String data2=bundle.getString("uno");

text1.setText(data1);

text2.setText(data2);

You might also like