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!