Tuesday, 6 March 2018

Fetch data from one activity to another

Fetch data from one activity to another in android using Intent. Make Two java files and two xml files. Java File FirstActivity.java package com.jeevan.karan.fetch_data; 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; import com.jeevan.karan.R; public class FirstActivity extends AppCompatActivity { EditText editText; Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_first); editText = (EditText)findViewById(R.id.edittext); button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(),SecondActivity.class); intent.putExtra("userName",editText.getText().toString()); startActivityForResult(intent,100); } }); } } Java File SecondActivity.java package com.jeevan.karan.fetch_data; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import com.jeevan.karan.R; public class SecondActivity extends AppCompatActivity { TextView textView; String username = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Intent i = getIntent(); username = i.getStringExtra("userName"); textView = (TextView)findViewById(R.id.welcome); textView.setText(username); } } XML File activity_first: - <?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_first" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.safal.sahilapp.fetch_data.FirstActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="10dp" android:background="@drawable/design1" android:hint="Enter the text" android:padding="16dp" android:id="@+id/edittext" android:textColor="#fffefe" android:textColorHint="#fffefe" android:textSize="20dp" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="40dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:background="@drawable/design" android:padding="16dp" android:id="@+id/button" android:text="Button" android:textAllCaps="false" android:textColor="#fffefe" android:textSize="20dp" /> </RelativeLayout> XML File activity_second: - <?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_second" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.safal.sahilapp.fetch_data.SecondActivity"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:id="@+id/welcome" android:textSize="30dp" android:text="Welcome to Next Activity"/> </RelativeLayout>

No comments:

Post a Comment