ImageButton shows random images - android

Hi i have a problem while creating imageview when i click previous(b1) and next(b2) button it shows next and previous images from img array but sometimes it shows random images like android layout images etc.
public class FullTable extends Activity{
int[] img={
R.drawable.t1,R.drawable.t2,R.drawable.t3,
R.drawable.t4,R.drawable.t5,R.drawable.t6,
R.drawable.t7,R.drawable.t8,R.drawable.t9,
R.drawable.t10,R.drawable.t11,R.drawable.t12,
R.drawable.t13,R.drawable.t14,R.drawable.t15,
R.drawable.t16,R.drawable.t17,R.drawable.t18,
R.drawable.t19,R.drawable.t20};
ImageButton b1,b2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_table);
b1 =(ImageButton) findViewById(R.id.buttonback);
b2=(ImageButton) findViewById(R.id.buttonnext);
// get intent data
Intent i = getIntent();
// Selected image id
final int position = i.getExtras().getInt("id");
final ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
//imageView.setImageResource(imageAdapter.mThumbIds[position]);
imageView.setImageResource(img[position]);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imageView.setImageResource(img[position]-1);
img[position]--;
}
});
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imageView.setImageResource(img[position]+1);
img[position]++;
}
});
}
}

On your code you're not incrementing and decrementing your position variable at all. And your logic seems somewhat wrong. That's why you're getting some random IDs.
If you already have your img array with the Drawables IDs you want, you can simply set your position variable to zero and remove the final from it, and then increment it and decrement it directly.
For example, change this:
img[position]--;
To this:
img[--position];
And set:
int position = 0; // Default value.
Later, change the method ImageView#setImageResource to ImageView#setImageDrawable. Like:
Drawable myDrawable = getResources().getDrawable(img[position]); // Get the Drawable Object.
imageview.setImageDrawable(myDrawable); // Set the Drawable on your ImageView.
Also, consider using a List like ArrayList instead of a simple array.
EDIT: As you asked on the comments, you can start over again by reseting your position variable to zero if you're on the last image. You can do that by getting the length of the array. Something like:
if (position >= img.length) {
position = 0;
}

create a variable position at class scope and not inside onCreate() method. then in your b1 onClick() method decrement position and then show image, posting some code to demonstrate the same: ->
// at class level and not inside `onCreate()`
ImageButton b1,b2;
int position = 0; // Initializing with default value
// then inside onCreate()
position = i.getExtras().getInt("id");
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
position--;
if(position < 0){
position = img.length;
}
imageView.setImageResource(img[position]);
}
});
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
position++;
if(position > img.length - 1){
position = 0;
}
imageView.setImageResource(img[position]);
}
});

Inside onClick() for b1:
imageView.setImageResource(img[position]-1);: this line of code is getting the id of drawables from img array and decreasing that value by 1.
But what you want is to display previous image so you need to modify your code to:
imageView.setImageResource(img[**position-1**]);
similary modify your code for b2 onCLick
imageView.setImageResource(img[**position+1**]);

Related

i want to get position in a list data how to get the position of click of button in android

how to get the position from list on click of data.
i want access button in android.on the click of button of the adapter class it should give the position.
holder.beginDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
you can set the position as the tag
holder.beginDate.setTag(position); // set every time getting the View
and retrieve it using
// put this code where creating a new instance of holder
holder.beginDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
int pos = (Integer) view.getTag();
// do anything using the position
}
});
After initialization of your button in getView() method write:
holder.beginDate.setTag(position);
and in your onClick() use:
holder.beginDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
int position=Integer.parseInt(((Button)v).getTag().toString());
}
});

How to change image next and previous using button clicklistener in android?

I have next and previous button for changing the image. When activity launch, image comes from previous activity. I use bundle object for getting image on my current activity. Actually 2 images use for pass it on bundle(image_a_inner and image_a_outer). One image overlap on second image and set on custom view. Now i want to when any image comes from bundle then i press next button or previous button then according to position image will be change. For example, images like A_Z alphabet. When i press on D image then it display on my activity using bundle and when i press next button then E image will be shown or when i press previous button then C image will be shown. Below is my code.
private DrawingView mDrawingView;
Bundle extras = getIntent().getExtras();
int imageRes1 = extras.getInt("picture1");
int imageRes2 = extras.getInt("picture2");
mDrawingView = (DrawingView) findViewById(R.id.drawing_view);
mDrawingView.setShape(imageRes1, imageRes2);
btn_next = (Button) findViewById(R.id.btn_next);
// btn_next.setOnClickListener(this);
btn_next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
initializeMP();
playsound.start();
if(imageRes1==R.drawable.img_a_inner||imageRes2==R.drawable.img_a){
mDrawingView.setShape(R.drawable.img_b_inner, R.drawable.img_b);
index++;
}
else if(imageRes1==R.drawable.img_b_inner||imageRes2==R.drawable.img_b){
mDrawingView.setShape(R.drawable.img_c_inner, R.drawable.img_c);
index++;
}
else if(imageRes1==R.drawable.img_c_inner||imageRes2==R.drawable.img_c){
mDrawingView.setShape(R.drawable.img_d_inner, R.drawable.img_d);
index++;
}
else if(imageRes1==R.drawable.img_d_inner||imageRes2==R.drawable.img_d){
mDrawingView.setShape(R.drawable.img_e_inner, R.drawable.img_e);
index++;
}
}
});
btn_prev = (Button) findViewById(R.id.btn_prev);
// btn_prev.setOnClickListener(this);
btn_prev.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
initializeMP();
playsound.start();
if(imageRes1==R.drawable.img_b_inner||imageRes2==R.drawable.img_b){
mDrawingView.setShape(R.drawable.img_a_inner, R.drawable.img_a);
index--;
}
else if(imageRes1==R.drawable.img_c_inner||imageRes2==R.drawable.img_c){
mDrawingView.setShape(R.drawable.img_b_inner, R.drawable.img_b);
index--;
}
else if(imageRes1==R.drawable.img_d_inner||imageRes2==R.drawable.img_d){
mDrawingView.setShape(R.drawable.img_c_inner, R.drawable.img_c);
index--;
}
});
}
If you have lists for all images in advance, declare them as arrays.
Otherwise, you can pass them using bundle with getIntArray() and putIntArray().
Now, you have lists of images like this,
// These are can be declared as member or static variables.
int[] innerPictures = {R.drawable.image_a_inner, R.drawable.image_b_inner, ...}
int[] pictures = {R.drawable.image_a, R.drawable.image_b, ...}
or
int[] innerPictures = extras.getIntArray("innerPictures");
int[] pictures = extras.getIntArray("pictures");
And you need the index of image to be displayed at the first time, it can be also passed as a extra
int displayingIndex = extra.getInt("pictureIndex"); // it has to be member variable to use inside of listener
So code is like below,
private DrawingView mDrawingView;
Bundle extras = getIntent().getExtras();
int[] innerPictures = ...
int[] pictures = ...
displayingIndex = extra.getInt("pictureIndex");
mDrawingView = (DrawingView) findViewById(R.id.drawing_view);
mDrawingView.setShape(innerPictures[displayingIndex], pictures[displayingIndex]);
btn_next = (Button) findViewById(R.id.btn_next);
btn_next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
initializeMP();
playsound.start();
if (displayingIndex + 1 == innerPictures.length) return;
displayingIndex++;
mDrawingView.setShape(innerPictures[displayingIndex], pictures[displayingIndex]);
}
});
btn_prev = (Button) findViewById(R.id.btn_prev);
btn_prev.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
initializeMP();
playsound.start();
if (displayingIndex == 0) return;
displayingIndex--;
mDrawingView.setShape(innerPictures[displayingIndex], pictures[displayingIndex]);
});
}
Sorry for bad indentation.

Show hidden views of List item on click of button outside the list

I am developing an Android Application. In this app I am making a list of views using code given below. In each item of list there is delete button with visibility "Gone". Now there is another button Edit outside the list, on click of edit button I have to show delete button on each item of list. but using this code delete button shows only in the last item. Please help me to solve the problem. Thanks.
dynamicView();
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// lay.removeAllViews();
addnew.setVisibility(View.INVISIBLE);
btn_red.setVisibility(View.VISIBLE);
edit.setText("Done");
}
});
public void dynamicView() {
LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < templates.length; i++) {
final View customView = linflater.inflate(R.layout.order_template_item,
null);
btn_red=(ImageView)customView.findViewById(R.id.btn_negative);
btn_drag=(ImageView)customView.findViewById(R.id.button_drag);
btn_delete=(ImageView)customView.findViewById(R.id.button_delete);
final ImageView image = (ImageView)customView.findViewById(R.id.arrow);
final TextView text = (TextView)customView.findViewById(R.id.date);
final TextView sku = (TextView)customView.findViewById(R.id.time);
final TextView price = (TextView)customView.findViewById(R.id.last);
final TextView names =(TextView)customView.findViewById(R.id.name);
image.setId(i);
text.setId(i);
sku.setId(i);
price.setId(i);
names.setId(i);
btn_red.setId(i);
btn_red.setTag(i);
btn_delete.setId(i);
btn_drag.setId(i);
names.setText(templates[i]);
btn_red.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn_delete.setVisibility(View.VISIBLE);
TranslateAnimation anim = new TranslateAnimation(100,0 , 0, 0);
anim.setInterpolator(new BounceInterpolator());
anim.setDuration(1000);
btn_delete.setAnimation(anim);
}
});
btn_delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
lay.removeView(customView);
}
});
lay.addView(customView);
}
You're holding reference only to the last one btn_red.
You can do something like this.
List<Button> buttons = new LinkedList<Button> ();
Then in your loop after findViewById on btn_red
buttons.add(btn_red);
And finally in your onClickListener
for (Button button: buttons) {
button.setVisibility(View.VISIBLE);
}
Do somethink like below to make visible all the button added to list items-
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
addnew.setVisibility(View.INVISIBLE);
for (int i = 0; i < templates.length; i++) {
int id = btn_red.getId(i);
id.setVisibility(View.VISIBLE);
}
edit.setText("Done");
}
});

Is it possible to write a for loop to assign listeners to many button with the same function where final ints are used?

I am writing an android application and I have 8 buttons on one view that all have the same function, so I wanted to assign the same functionality to each button using a for loop rather than writing out 8 pieces of separate code. However, an issue arises when I want to use the the counter from the for loop within the onClick function to help fire an intent, here's the code:
//array of button ids
public int [] pickPlayers = { R.id.pick_player_1a, R.id.pick_player_2a, R.id.pick_player_3a, R.id.pick_player_4a, R.id.pick_player_1b, R.id.pick_player_2b, R.id.pick_player_3b, R.id.pick_player_4b};
//button to be used in for loop
public Button b;
//for loop to assign same functionality to buttons in pickPlayers array
for(int i = 0; i<pickPlayers.length; i++){
b = (Button) findViewById(pickPlayers[i]);
b.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent getContactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(getContactIntent, i);
}
});
};
Hope that all makes sense, thanks to anyone who can help. :)
EDIT: This is the error I get: Cannot refer to a non-final variable i inside an inner class defined in a different method
The for loop is in my oncreate method the variables and buttons are outside
Yes you can't access variable in your inner classes if it's not declared as final. Simple workaround will be to create OnClickListener wrapper class.
private class MyListener implements Button.OnClickListener {
int pos;
public MyListener (int position) {
pos = position;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent getContactIntent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(getContactIntent, pos);
}
}
and then use it in you code like this
//for loop to assign same functionality to buttons in pickPlayers array
for(int i = 0; i<pickPlayers.length; i++){
b = (Button) findViewById(pickPlayers[i]);
b.setOnClickListener(new MyListener(i));
}
Also if your buttons are in some ViewGroup you can use getChildAt and getChildCount to iterate them
ViewGroup parent;
// initialize the parent
int l = parent.getChildCount();
for (int i = 0 ; i < l ; i++) {
Button button = parent.getChildAt(i);
}
Just change your code like that:
//for loop to assign same functionality to buttons in pickPlayers array
for(int i = 0; i<pickPlayers.length; i++){
final int index = i;
b = (Button) findViewById(pickPlayers[i]);
b.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent getContactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(getContactIntent, index);
}
});
};
This should do the job. Good luck!
If those buttons are decalred in XMl maybe you should set android:onClick listener to them through XML?
You have to define one method in your activity:
public void myHandler(View v) {
// Your stuff...
}
And add
android:onClick="myHandler"
to all of them?
Thanks.

Android Button Problem

i am making a app which generate buttons according to the value entered by user. each button have have there own function defined in XML. Now my main problem is how to shorten these codes.
name[0].setClickable(true);
name[0].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[0].setText("kjghjbjhb");
}
});
name[2].setClickable(true);
name[2].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[2].setText("kjghjbjhb");
}
});name[1].setClickable(true);
name[1].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[1].setText("kjghjbjhb");
}
});
and soo on.....writing these codes again and again is not possible as button generated are dynamic, i dunno how many buttons will be generated. Please tell if there is a some other way to do this.
Something like this?
createButton(int i){
name[i].setClickable(true);
name[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[i].setText("kjghjbjhb");
}
});
}
With this method you can also make a for-loop:
for (int i = 0; i<name.length; i++){
createButton(i);
}
Here I am specifying the steps to be executed.
You must be creating the buttons by new Button(); just hold its reference in a Collection say ArrayList
ArrayList ar = new ArrayList();
Button b1 = new Button();
ar.add(b1);
Now create a private inner class which is implementing the View.OnClickListener. Now as per rules implement theOnClick() and so the stuff which you want to be done at there
class A extends Activity{
// your stuff here for OnCreate and other business logic
private final class MyListener implements View.OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
v.setText("kjghjbjhb");
}
}
}
Notice that I am setting the text with the reference of object v in onClick. Also make this class singleton.
Now set create the instance of this class (as the MyListerner will be singleton the object will be one) in the setOnClickListener() like this:
MyListener listener = MyListener.getInstance();
b.setOnClickListener(listener);
You can opt this way when the buttons are created on some event or user action. In case if you need to create the buttons in loop you can use the 1st and 3rd step in loop.

Categories

Resources