Peter Maher
Peter Maher

Reputation: 99

How do i change activity on a button click android

I have an ImageButton that when I want it to be pressed to launch a second Activity. The second activity is called "Dust 2" as seen in my android manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pete.smokesapp" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".homepage"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Dust2"
            android:label="@string/title_activity_dust2" >
        </activity>
    </application>
</manifest>

I tried following a guide online but got me as far as this stage now my app crashed on launch.

package pete.smokesapp;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;


public class homepage extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_homepage);


    findViewById(R.id.imgDust2).setOnClickListener((View.OnClickListener)       this);

}

public void imgDust2(View view) {
switch (view.getId()){
    case R.id.imgDust2:
        Toast.makeText(this, "Dust 2 Clicked", Toast.LENGTH_LONG ).show();
        break;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_homepage, menu);
    return true;
}

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

Here is also my main activity, this is the activity with the ImageButton, i am also new to this so sorry if I seem clueless.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="1">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Maps"
                android:id="@+id/txtMaps"
                android:layout_weight="0.44" />

            <ImageButton
                android:layout_width="350dp"
                android:layout_height="150dp"
                android:id="@+id/imgDust2"
                android:clickable="false"
                android:src="@drawable/dust2"
                android:layout_above="@+id/imgOverpass"
                android:layout_alignLeft="@+id/imgOverpass"
                android:layout_alignStart="@+id/imgOverpass"
                android:layout_marginBottom="10dp" />



            <ImageButton
                android:layout_width="350dp"
                android:layout_height="150dp"
                android:id="@+id/imgInferno"
                android:layout_below="@+id/txtOverpass"
                android:layout_alignLeft="@+id/imgOverpass"
                android:layout_alignStart="@+id/imgOverpass"
                android:layout_marginBottom="10dp"
                android:src="@drawable/inferno" />

            <ImageButton
                android:layout_width="350dp"
                android:layout_height="150dp"
                android:id="@+id/imgOverpass"
                android:src="@drawable/overpass"
                android:layout_marginBottom="10dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true" />

            <ImageButton
                android:layout_width="350dp"
                android:layout_height="150dp"
                android:id="@+id/imgMirage"
                android:clickable="false"
                android:src="@drawable/mirage"
                android:layout_above="@+id/imgOverpass"
                android:layout_alignLeft="@+id/imgOverpass"
                android:layout_alignStart="@+id/imgOverpass"
                android:layout_marginBottom="10dp" />

            <ImageButton
                android:layout_width="350dp"
                android:layout_height="150dp"
                android:id="@+id/imgCache"
                android:clickable="false"
                android:src="@drawable/cache"
                android:layout_above="@+id/imgOverpass"
                android:layout_alignLeft="@+id/imgOverpass"
                android:layout_alignStart="@+id/imgOverpass"
                android:layout_marginBottom="10dp" />

            <ImageButton
                android:layout_width="350dp"
                android:layout_height="150dp"
                android:id="@+id/imgNuke"
                android:clickable="false"
                android:src="@drawable/nuke"
                android:layout_above="@+id/imgOverpass"
                android:layout_alignLeft="@+id/imgOverpass"
                android:layout_alignStart="@+id/imgOverpass"
                android:layout_marginBottom="10dp" />

        </LinearLayout>
    </ScrollView>
</RelativeLayout>

Upvotes: 0

Views: 1326

Answers (4)

Apurva
Apurva

Reputation: 7901

There are two methods available in android using which you can go from one Activity to another.

1. Use button.setOnClickListener()

Create a button in xml file.

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:text="Button" />

Now set event listener for the button in your .class file

Button button = (Button) findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        //set the event you want to perform when button is clicked
        //you can go to another activity in your app by creating Intent
        Intent intent = new Intent(getApplicationContext, Activity2.class);
        startActivity(intent);
    }
});

2. Use <android:onClick="goNext">

Put the onClick as the attribute of the button you have created in xml file.

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:onClick="goNext" />

Now in your .class file define an event for that button as,

goNext() {
    Intent intent = new Intent(getApplicationContext, Activity2.class);
    startActivity(intent);
}

Upvotes: 1

Rensodarwin
Rensodarwin

Reputation: 286

Set onClick to the xml imageView and put the name of the method:

 <ImageButton
        android:layout_width="350dp"
        android:layout_height="150dp"
        android:id="@+id/imgDust2"
        android:clickable="false"
        android:src="@drawable/dust2"
        android:layout_above="@+id/imgOverpass"
        android:layout_alignLeft="@+id/imgOverpass"
        android:layout_alignStart="@+id/imgOverpass"
        android:layout_marginBottom="10dp"
        android:onClick="imgDust2"/>

Upvotes: 0

Sam
Sam

Reputation: 559

Try the following:

  1. Add implements OnClickListener to your class definition: public class homepage extends ActionBarActivity implements OnClickListener
  2. An error will appear and ask you to add unimplemented methods. Do so.
  3. Change findViewById(R.id.imgDust2).setOnClickListener((View.OnClickListener) this); to findViewById(R.id.imgDust2).setOnClickListener(this);
  4. Place the stuff you want to do when the button is clicked into the new method that was added in step 2.

Upvotes: 0

Victor Viola
Victor Viola

Reputation: 585

In your first activity do this:

 Button button = (Button) findViewById(R.id.imgDust2);

button.setOnClickListener(new View.OnClickListener () {
@Override
public void onClick(View v){
Intent intent = new Intent(v.getContext(), dust2.class);
startActivity(intent);
}
});

Upvotes: 0

Related Questions