I'm having small problem on my project. I can't open the activities of each button after clicking it. I don't know if the codes I used are correct. please help me fix it. Thank you.
activity_main2.xml
<?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=".MainActivity2"
android:background="@drawable/backgroundimg">
<Button
android:id="@ id/button"
android:layout_width="270dp"
android:layout_height="60dp"
android:backgroundTint="@android:color/holo_blue_dark"
android:text="Sleep"
android:textAllCaps="false"
android:textColor="#FFFFFF"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.299" />
<Button
android:id="@ id/button1"
android:layout_width="270dp"
android:layout_height="60dp"
android:backgroundTint="@android:color/holo_blue_dark"
android:text="Stress Reliever"
android:textAllCaps="false"
android:textColor="#FFFFFF"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.433" />
<Button
android:id="@ id/button2"
android:layout_width="270dp"
android:layout_height="60dp"
android:backgroundTint="@android:color/holo_blue_dark"
android:text="Calm"
android:textAllCaps="false"
android:textColor="#FFFFFF"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.57" />
<Button
android:id="@ id/button3"
android:layout_width="270dp"
android:layout_height="60dp"
android:backgroundTint="@android:color/holo_blue_dark"
android:text="Motivational Quotes"
android:textAllCaps="false"
android:textColor="#FFFFFF"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.701" />
<TextView
android:id="@ id/goodday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Good Day,"
android:textColor="@color/white"
android:textSize="20pt"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.078"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.04" />
<TextView
android:id="@ id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Melana!"
android:textColor="@color/white"
android:textSize="17pt"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.059"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.112" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity2.java
package com.example.serenityapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity2 extends AppCompatActivity {
Button butt1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
butt1=(Button) findViewById(R.id.button);
butt1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
setContentView(R.layout.activity_stress);
}
});
}
}
This is what the app looks like and the four buttons should open their respective activities
CodePudding user response:
This is just an example. As someone answered here, you could just search how to start a new activity.
butt1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent navigateToAnotherActivity = new Intent(this, SleepActivity.class);
startActivity(navigateToAnotherActivity);
}
});
CodePudding user response:
to navigate from one activity to another intent
is used
Intent intent = new Intent(this, TargetActivity.class)
startActivity(intent)
CodePudding user response:
As I can see in the screenshot you provided. You have created 4 different activities. You just need to open them using the below code.
This code goes inside the onCreate()
method of MainActivity2
public class MainActivity2 extends AppCompatActivity {
private Button bt_Sleep, bt_Stress_Reliever, bt_Calm, bt_Motivational_Quotes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
bt_Sleep = findViewById(R.id.button);
bt_Stress_Reliever = findViewById(R.id.button1);
bt_Calm = findViewById(R.id.button2);
bt_Motivational_Quotes = findViewById(R.id.button3);
bt_Sleep.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent sleep = new Intent(MainActivity2.this, sleep.class);
startActivity(sleep);
}
});
bt_Stress_Reliever.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent stressReliever = new Intent(MainActivity2.this, stress.class);
startActivity(stressReliever);
}
});
bt_Calm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//calm change with your actual activity name
Intent calmIntent = new Intent(MainActivity2.this, calm.class);
startActivity(calmIntent);
}
});
bt_Motivational_Quotes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//motivational_quotes change with your actual activity name
Intent mq = new Intent(MainActivity2.this, motivational_quotes.class);
startActivity(mq);
}
});
}
}
This code should work. Still facing problem comment here.