1

I am new to android development.I want to use same layout file for two different activities.here is my code

public class MainActivity extends AppCompatActivity {
ToggleButton toggleButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toggleButton=findViewById(R.id.toggle_button);
  
    toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {

               on();
            } 
        }
    });
}
public void on()
{

    Toast.makeText(this, "button on", Toast.LENGTH_SHORT).show();
} 
}

second activity

public class MainActivity2 extends AppCompatActivity {
ToggleButton toggleButton;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView=findViewById(R.id.text_view);
toggleButton=findViewById(R.id.toggle_button);
    toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {

                textView.setText("hello MainActivity2");
            } 
        }
    });
}
} 

My layout file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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=".MainActivity">
<TextView
    android:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:ignore="MissingConstraints" />
<ToggleButton
    android:id="@+id/toggle_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>

here no text is coming on if i checked the button only toast is coming. I am searching for an answer to this. I dont know how to use this. any type of help will be useful.

Akshay
  • 41
  • 6

2 Answers2

0

The issue


You are providing the constraints for the views which overlap each other. This way, they do not get visible. You must change your layout or constraint the views in different way. I will give solution for changing the layout.

The solution


Try this code in your xml:

<?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">
<TextView
    android:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp" />
<ToggleButton
    android:id="@+id/toggle_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</LinearLayout >
Sambhav. K
  • 3,046
  • 2
  • 5
  • 29
  • Tried this code in XML...nothing changes same result – Akshay Apr 12 '22 at 06:12
  • It happens in both the activities or only one? – Sambhav. K Apr 12 '22 at 09:38
  • see I am printing text in one activity and toast in other activity. here I am getting only Toast and text is not printing. If I use only one activity for this ...it will done but I want to do it using two activities for one xml layout – Akshay Apr 12 '22 at 09:50
  • is the `onCheckedChanged` being called? Try to add a log there and see if the app reaches there. Or best, just debug it – Sambhav. K Apr 12 '22 at 09:52
0

Another issue might be occurring. Let me explain it here.

You are only setting the text when the button is checked. Not when it is not checked. Try to. display different text every time it's checked or not. Try this code in second activity:

public class MainActivity2 extends AppCompatActivity {
ToggleButton toggleButton;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView=findViewById(R.id.text_view);
toggleButton=findViewById(R.id.toggle_button);
    toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {

                textView.setText("hello MainActivity2");
            } else {
                textView.setText("bye MainActivity2");
            }
        }
    });
}
} 
Sambhav. K
  • 3,046
  • 2
  • 5
  • 29