-3

I want to know how I can make my ImageButton to go to another activity in Android Studio 1.2.2?

I tried to make it using the way that was made for buttons.

This is my Java code

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

@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_menu_01, 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);

  }
}
N J
  • 27,217
  • 13
  • 76
  • 96
Max Technology
  • 13
  • 1
  • 1
  • 2

2 Answers2

0

Here is some sample code for you to work with. The idea is to set an OnClickListener for your ImageButton, and in that OnClickListener create an Intent to go to your new Activity, then call startActivity(intent).

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu_01);
    ImageButton button = (ImageButton) findViewById(R.id.my_button);
    final Context context = this;
    button.setOnClickListener(new View.OnClickListner(){
        @Override
        public void onClick(View v){
            Intent intent = new Intent(context, NewActivity.class);
            startActivity(intent);
        }
    });
}
N J
  • 27,217
  • 13
  • 76
  • 96
RogueBaneling
  • 4,331
  • 4
  • 22
  • 33
0

You first need to create a ImageButton on the layout.xml of your current activity:

<ImageButton
    android:id="@+id/your_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

Than, in your current activity, you need to create a variable ImageButton:

ImageButton myButton = (ImageButton) findViewById(R.id.the_button_you_created_on_the_layout.xml_file);

Then, you need to set a click listener to this button:

*The click listener will "wait" for the click in your button.

myButton.setOnClickListener(new View.OnClickListner(){
    // When the button is pressed/clicked, it will run the code below
    @Override
    public void onClick(){
        // Intent is what you use to start another activity
        Intent intent = new Intent(this, YourActivity.class);
        startActivity(intent);
    }
});

After that, your app should start the other activity with no problem.

meyer1994
  • 377
  • 2
  • 11
  • When I put the javascript code says me error: no suitable constructor found for Intent(,Class) constructor Intent.Intent(String,Uri) is not applicable (argument mismatch; cannot be converted to String) constructor Intent.Intent(Context,Class>) is not applicable (argument mismatch; cannot be converted to Context) – Max Technology Aug 07 '15 at 16:57
  • This is my Javascript code: myButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent myIntent = new Intent(this, MainActivity2.class); startActivity(intent); – Max Technology Aug 07 '15 at 16:58
  • You should add the new activity in the AndroidManifest.xml file: – meyer1994 Aug 08 '15 at 17:22