0% found this document useful (0 votes)
53 views3 pages

Android The Code

The document contains code for two Android activities - a main activity with a text field and button, and a second activity to display data passed from the first. The main activity layout defines widgets including a text view, edit text and button. The main activity Java code gets references to these, sets an onClick listener for the button to start the second activity, passing the edit text data. The second activity layout similarly defines text views and the Java code gets the passed data and displays it.

Uploaded by

GuRpreeT SinGh
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
Download as rtf, pdf, or txt
0% found this document useful (0 votes)
53 views3 pages

Android The Code

The document contains code for two Android activities - a main activity with a text field and button, and a second activity to display data passed from the first. The main activity layout defines widgets including a text view, edit text and button. The main activity Java code gets references to these, sets an onClick listener for the button to start the second activity, passing the edit text data. The second activity layout similarly defines text views and the Java code gets the passed data and displays it.

Uploaded by

GuRpreeT SinGh
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1/ 3

Activity_main1

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout 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"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Activity"
android:textSize="22dp"
android:id="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@+id/textView"
android:layout_margin="30dp"
android:hint="Enter anything..."
/>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/clickHandle"
android:text="Click"
android:layout_centerHorizontal="true"
android:layout_below="@id/editText"
android:layout_marginTop="25dp"
/>
</

main Activity.java

package com.example.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


private Button button;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.editText);
button=findViewById(R.id.clickHandle);
button.setOnClickListener(this);

}
public void onClick(View v) {
Intent intent=new Intent(this,Main2Activity.class);
intent.putExtra("data",editText.getText().toString().trim());
startActivity(intent);
}

}
Activity main2

<?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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Second Activity"
android:textSize="22dp"
/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Message"
android:textSize="22dp"
/>

</LinearLayout>

Main2Activity.java

package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.content.Intent;
public class Main2Activity extends AppCompatActivity {
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView=findViewById(R.id.textView3);
Intent intent= getIntent();
textView.setText(intent.getStringExtra("data"));
}
}

You might also like