0

I have a button in Activity A, which changes the text in it when clicked. There is an Activity B in which I need the TextView to become visible and its text to be the same as the text in the button on the Activity A. Please help.

Activity A

package com.example.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;

public class SmActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sm);

        Button btn_apple = (Button) findViewById(R.id.button_apple);
        Button btn_cherry = (Button) findViewById(R.id.button_cherry);
        Button btn_orange = (Button) findViewById(R.id.button_orange);
        Button btn_waterLemon = (Button) findViewById(R.id.button_waterlemon);

        View.OnClickListener appleListener = new View.OnClickListener() {
            boolean action = false;

            @Override
            public void onClick(View v) {
                if (!action) {
                    action = true;
                    btn_apple.setText("1");
                }
                else {
                    int i = Integer.parseInt(btn_apple.getText().toString());
                    btn_apple.setText(String.valueOf(i + 1));
                    i = i;
                }
            }
        };
        View.OnClickListener cherryListener = new View.OnClickListener() {
            boolean action = false;
            @Override
            public void onClick(View v) {
                if (!action) {
                    action = true;
                    btn_cherry.setText("1");
                }
                else {
                    int j = Integer.parseInt(btn_cherry.getText().toString());
                    btn_cherry.setText(String.valueOf(j + 1));
                    j = j;
                }
            }
        };
        View.OnClickListener orangeListener = new View.OnClickListener() {
            boolean action = false;
            @Override
            public void onClick(View v) {
                if (!action) {
                    action = true;
                    btn_orange.setText("1");
                }
                else {
                    int k = Integer.parseInt(btn_orange.getText().toString());
                    btn_orange.setText(String.valueOf(k + 1));
                    k = k;
                }
            }
        };
        View.OnClickListener waterListener = new View.OnClickListener() {
            boolean action = false;
            @Override
            public void onClick(View v) {
                if (!action) {
                    action = true;
                    btn_waterLemon.setText("1");
                } else {
                    int q = Integer.parseInt(btn_waterLemon.getText().toString());
                    btn_waterLemon.setText(String.valueOf(q + 1));
                    q = q;
                }
            }
        };

        btn_apple.setOnClickListener(appleListener);
        btn_cherry.setOnClickListener(cherryListener);
        btn_orange.setOnClickListener(orangeListener);
        btn_waterLemon.setOnClickListener(waterListener);
    }
    public void OnClickBsk(View view){
        Intent intent = new Intent(SmActivity.this, BasketActivity.class);
        startActivity(intent);


    }
    public void OnClickProfile(View view){
        Intent intent = new Intent(SmActivity.this, ProfileActivity.class);
        startActivity(intent);
    }


}
muetzenflo
  • 5,653
  • 4
  • 41
  • 82
SikroN
  • 5
  • 3
  • 1
    you can pass values from Activity A -> B using Intent as below: `intent.putExtra("firstName", "Vishnu"); intent.putExtra("lastName", "Sivan");` – Umair Anjum Sep 08 '21 at 16:40
  • More info https://stackoverflow.com/questions/4233873/how-do-i-get-extra-data-from-intent-on-android – gioravered Sep 08 '21 at 17:19

2 Answers2

0

Do this In Activity A when you click button to go to next Activity B

btn.setOnClickListener(v -> {
        Intent i = new Intent(AActivity.this,BActivity.class);
        // This below line sends Key- btnText and Value- Apple
        i.putExtra("btnText","Apple");
        startActivity(i);
    });

In BActivity do this to button

// Here you receives data from activity a with the help of Key- btnText
btn.setText(getIntent().getStringExtra("btnText"));

This is a simple way to do this.

Sarvesh Hon
  • 408
  • 4
  • 15
0

I Have Solved the answer for you,

Minor Explanations is in the comments in code.

Use this code as a concept and apply changes to yours based on your requirement.

XML of Activity A:

<?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=".ActivityA">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text Before Button Click"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:id="@+id/text_to_change"
        android:textSize="20sp"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/text_to_change"
        android:id="@+id/change_text_btn"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_margin="20dp"
        android:text="Change Text and Start Activity B"
        />

</RelativeLayout>

Java of Activity A

package com.example.text_to_speech;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.w3c.dom.Text;

public class ActivityA extends AppCompatActivity {

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

        TextView textView;
        Button button;


            textView=findViewById(R.id.text_to_change);
            button=findViewById(R.id.change_text_btn);

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String newtext="New Text From Activity A";
                    textView.setText(newtext);
                    Intent intent=new Intent(ActivityA.this,ActivityB.class);
                    intent.putExtra("Change", newtext);//this string will be
                    //accessed by activityB 
                    startActivity(intent);
                }
            });
    }
}

XML of Activity B:

<?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=".ActivityB">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Waiting for New Text"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:id="@+id/new_textview"
        android:textSize="20sp"
        android:visibility="gone"
        />

</RelativeLayout>

Java of Activity B:

package com.example.text_to_speech;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class ActivityB extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);
        TextView textView;
        textView=findViewById(R.id.new_textview);
        // Below if block is looking for an intent, if it gets it and 
        // there is content in the extras with key "Change",
        // it will make the textview in xml visible and update its string
        // based on value set by Activity A
        if(savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            if (extras == null) {
                Toast.makeText(ActivityB.this, "Can't Get the Intent", Toast.LENGTH_LONG).show();
            } else {
                String get_String = extras.getString("Change");
                textView.setVisibility(View.VISIBLE);// be default i have set the visibility to gone in xml
                //here it will get visible and then filled with the string sent by the intent in activity A
                textView.setText(get_String);
            }
        }

    }
}
MoonLight
  • 460
  • 4
  • 15