Issue
So far from all the tutorials I've looked at, most only get to the point of "Button Was Clicked". I need my second activity button to open a new activity.
I named this class, fifth_layout.xml:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Amazon"
android:drawableLeft="@drawable/amazon"
android:drawableStart="@drawable/amazon"
android:layout_weight="0.07"
tools:ignore="HardcodedText"
android:id="@+id/button10"
android:textSize="35sp" />
After that in my FifthActivity.java I have:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FifthActivity extends Activity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button = (Button) findViewById(R.id.button10);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {}
});
}
}
I just need the button to be able to open a new blank activity. But when I click the button nothing happens. I just need a new activity. I feel like the code is correct but I just need help on what I might be doing wrong.
Solution
You have to use intent to open a new Activity. Assuming you want to open an activity called SixthActivity
from your FifthActivity
.
You should use this:
public class FifthActivity extends Activity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button = (Button) findViewById(R.id.button10);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this,SixthActivity.java);
FifthActivity.this.startActivity(intent);
}
});
}
}
Hope this helps, Regards.
Answered By - gautamprajapati
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.