Reputation: 1
I am trying to change the TextView that is in SecondActivity, from MainActivity.Here I am trying to do this through LayoutInflatter and View but unable to do.The TextView in SecondActivity didn't update after the button is clicked. Here is my MainActivity.java Code: ...
package com.example.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button);
//Want to Update the SecondActivity TextView From this MainActivity
LayoutInflater inflater = getLayoutInflater();
View view =inflater.inflate(R.layout.activity_second,null);
tv=view.findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//When this Button Clicked then update the textView that is in activity_second.xml
tv.setText("TextView Changed in SecondActivity");
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});
}
}
... The SecondActivity xml file where i want to change the TextView: ...
<?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"
android:gravity="center"
tools:context=".SecondActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Second Activity"
android:gravity="center"
android:textSize="30sp"
/>
... I need to know how can i did this ? need what Kind of changes in this? ...
LayoutInflater inflater = getLayoutInflater();
View view =inflater.inflate(R.layout.activity_second,null);
tv=view.findViewById(R.id.textView);
... Thank You!
Upvotes: 0
Views: 833
Reputation: 363647
The easiest way to do this is to add an extra in the Intent
:
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("key", "TextView Changed in SecondActivity");
startActivity(intent);
In the SecondActivity
you can do:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity_main);
TextView tv=view.findViewById(R.id.textView);
//...
Intent intent = getIntent();
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("key");
tv.setText(value);
}
}
Upvotes: 1
Reputation: 13
If you want to change the view from another activity you should use Interfaces callback. What you are doing currently is inflate the view from layout to variable view
, but the variable view
is not necessarily attached or used in another activity. Because commonly activity are inflated using setContentView
method
You can also use broadcast receiver as mentioned from this post.
Android make callback to an Activity from java class
Upvotes: 0