Home > Mobile >  how can I use same XML layout for different activities?
how can I use same XML layout for different activities?

Time:04-12

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.

CodePudding user response:

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 >

CodePudding user response:

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");
            }
        }
    });
}
} 
  • Related