Android App Development With AndroidCource: learn to code android
Showing posts with label learn to code android. Show all posts
Showing posts with label learn to code android. Show all posts

Monday, 21 August 2017

Pass Data to One Activity to Another Activity

MainActivity.xml
 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/activity_pass_data_activitity"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#cfdb28"
                tools:context="com.bhaumik.programmingstudy.MainActivitity">

    <EditText
            android:id="@+id/et_name"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:background="#FFF"
            android:padding="10dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"/>
    <Button
            android:id="@+id/btn_showText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show Text"
            android:textAllCaps="false"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/et_name"
            android:layout_marginTop="30dp"/>
</RelativeLayout>
ReceiveDataActivity.xml
    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/activity_receive_data"
                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"
                android:background="#168080"
                tools:context="com.bhaumik.programmingstudy.ReceiveDataActivity">

    <TextView
            android:id="@+id/tv_resultText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello"
            android:textColor="#FFF"
            android:textStyle="bold"
            android:textSize="20dp" />
</RelativeLayout>
MainActivity.java
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 PassDataActivitity extends AppCompatActivity {

    EditText name;
    Button btn_show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pass_data_activitity);

        name = (EditText) findViewById(R.id.et_name);
        btn_show = (Button) findViewById(R.id.btn_showText);

        btn_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String names = name.getText().toString();
                Intent intent = new Intent(MainActivitity.this,ReceiveDataActivity.class);
                intent.putExtra("Names",names);
                startActivity(intent);
            }
        });
    }
}
ReceiveDataActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class ReceiveDataActivity extends AppCompatActivity {

    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_data);

        String type = getIntent().getStringExtra("Names");
        textView = (TextView) findViewById(R.id.tv_resultText);
        textView.setText(type);
    }
}

Ads