I am trying to make a ImageButton that opens a Gallery with images. I am trying to make it simple for now – just when I click the image to open the gallery with a couple of pictures. But I am not sure how to do it. I have searched on the Internet and saw that others are having the XML code that invokes one Java method – OnClick. I have the XML here:
<ImageButton
android:id="@+id/imageView5"
android:layout_width="@dimen/overview_schedule_image_width"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:src="@drawable/red"
android:onClick="buttonClick" />
There is more code in the XML, but I think that’s the one that is necessary. So far it works good. But now I am not sure where to place that “buttonClick” method and what it should contain. I have made this class:
package org.imageGallery;
import org.imageGallery.actionbarcompat.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
public class ButtonClickActivity extends ActionBarActivity implements OnItemSelectedListener, OnClickListener{
@Override
public void onClick(View v) {
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
I guess the “buttonClick” method should be placed here and it should take the images. But how do I return them to the XML and how can I display them in the gallery ? I am still very new to Android, but hopefully I will manage to do it.
EDIT:
First you have to make a class that extends activity like this:
1. then override a method called
OnCreate()which launches when the activity launches.override in onCreate block method called
setContentView(R.layout.yourLayout)so the activity displays the xml file.then get a reference to the button so you can listen to it this way:
Button myButton = (Button) findViewById(R.id.button1);if you implemented OnClickListener in your class then in the implemented method: