-2

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.

rgettman
  • 176,041
  • 30
  • 275
  • 357
East
  • 19
  • 3

3 Answers3

1

Your onClickListener does nothing, of course nothing happens. Create a new Activity (let's say you name it NewActivity, add it to the AndroidManifest.xml and add the following code you your existing activity:

button.setOnClickListener(new View.OnClickListener() {    
    public void onClick(View view) {
        final Intent intent = new Intent(FifthActivity.this, NewActivity.class);
        startActivity(intent);
    }
});

I have a very strong feeling you're kind of lost in Android Development. I strongly suggest you follow Udacity's Android Development course.

Tudor Luca
  • 6,259
  • 2
  • 29
  • 44
1

Alright, so you have the single activity with its layout, right?

What your asking is "how do I launch another activity with another layout?"

To do this, we'll use an "intent" (think of an intent as how the activities talk to eachother, they get passed back and forth)

To create the intent and start, you'll need these couple lines:

Intent intent = new Intent(this, Target.class);
startActivity(intent);

Which should work within your onClick.

If you created the activity within Android Studio with File>New>Activity, this should have put the activity in your AndroidManifest.xml already, otherwise you'll need to add it yourself.

1

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.

gautamprajapati
  • 2,055
  • 5
  • 16
  • 31
  • 1
    @East is this some sort of an elaborate joke? Stop wasting time on StackOverflow and start learning programming: https://www.udacity.com/course/developing-android-apps--ud853 – Tudor Luca Mar 05 '16 at 23:19
  • @East, Yes you will have to retype everything again for multiple buttons. Just change the `SixthActivity.java` to your new activity name. Also, I would suggest you to thoroughly search google and StackOverflow before asking questions. Show us your research effort. I would suggest going through the link your question has marked duplicated with to know more about Intents. Don't get discouraged by being blocked from SO for 2 days. You will learn better in that time researching on your problems rather than asking for help directly – gautamprajapati Mar 06 '16 at 07:33