How to create a history of pressing ImageButton - android

I am a novice developer. Please help to create the code to display the history of pressing ImageButton (on Android). I have three ImageButton (IB1, IB2, IB3) and I have 5 empty ImageView (IV1, IV2, IV3, IV4, IV5).
When the user first time touching one of the three ImageButtons (for example IB2), the ImageView (IV1) displays button image from IB2.
When the user clicks a second time and touching one of three buttons again (for example IB1 this time) image of the first pressed button (image from IB2) is moved from IV1 to the IV2, and IV1 gets the image from button which clicked in second time – from IB1. And so on an unlimited number of times, but history shows only the last 5 clicks. Here is the beginning of my code. Thanks in advance to all.
public class MainActivity extends Activity implements View.OnClickListener {
ImageView IV1, IV2, IV3, IV4, IV5;
ImageButton IB1, IB2, IB3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IB1 = (ImageButton) findViewById(R.id.IB_first);
IB2 = (ImageButton) findViewById(R.id.IB_second);
IB3 = (ImageButton) findViewById(R.id.IB_third);
IV1 = (ImageView)findViewById(R.id.ImageView1);
IV2 = (ImageView)findViewById(R.id.ImageView2);
IV3 = (ImageView)findViewById(R.id.ImageView3);
IV4 = (ImageView)findViewById(R.id.ImageView4);
IV5 = (ImageView)findViewById(R.id.ImageView5);
IB1.setOnClickListener(this);
IB2.setOnClickListener(this);
IB3.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.IB1:
IV1.setVisibility(View.VISIBLE);
IV1.setImageResource(R.drawable.first_image);
break;
case R.id.IB2:
IV1.setVisibility(View.VISIBLE);
IV1.setImageResource(R.drawable.second_image);
break;
case R.id.IB3:
IV1.setVisibility(View.VISIBLE);
IV1.setImageResource(R.drawable.third_image);
break;
}
}
}

Inside your clicklistner you should pass the drawables to other imageViews
for example:
IV5.setImageDrawable(IV4.getDrawable());
IV4.setImageDrawable(IV3.getDrawable());
IV3.setImageDrawable(IV2.getDrawable());
IV2.setImageDrawable(IV1.getDrawable());
// set your IV1 resource
Happy Coding ;)

Related

How to handle with multi buttons on Setclick? [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
How to handle with multi buttons?
I´m using this code to change SoundOff to soundOff ON click
I dont now how to fix the error
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameover);
//setting the orientation to landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
ImageView medaille = (ImageView) findViewById(R.id.medaille);
//adding a click listener
//Define Logout
ImageButton LogOut = (ImageButton) findViewById(R.id.imageButton8);
//Define SoundOn
ImageButton SoundOn = (ImageButton) findViewById(R.id.imageButton9);
//Define SoundOff
ImageButton SoundOff = (ImageButton) findViewById(R.id.imageButton10);
//getting the button
ImageButton ButtonOk = (ImageButton) findViewById(R.id.imageButton11);
LogOut.setOnClickListener(this);
SoundOff.setOnClickListener(this);
SoundOn.setOnClickListener(this);
ButtonOk.setOnClickListener(this);
// Version
//define
TextView txtView = (TextView) findViewById(R.id.tv_current_score);
TextView txtsView = (TextView) findViewById(R.id.tv_current_score_value);
txtsView.setText(String.valueOf(GameView.score));
//Value
TextView myText = (TextView) findViewById(R.id.tv_best_score_value);
myText.setText(String.valueOf(GameView.Highscorer) );
TextView txtViews = (TextView) findViewById(R.id.tv_best_score);
}
#Override
public void onClick (View v){
//Define Logout
ImageButton LogOut = (ImageButton) findViewById(R.id.imageButton8);
//Define SoundOn
ImageButton SoundOn = (ImageButton) findViewById(R.id.imageButton9);
//Define SoundOff
ImageButton SoundOff = (ImageButton) findViewById(R.id.imageButton10);
//getting the button
ImageButton ButtonOk = (ImageButton) findViewById(R.id.imageButton11);
switch (v.getId()) {
case R.id.imageButton11:
//starting game activity
startActivity(new Intent(this, GameView.class));
break;
case R.id.imageButton4:
//Logout
startActivity(new Intent(this, GameLogout.class));
// do your code
break;
case R.id.imageButton8:
startActivity(new Intent(this, GameLogout.class));
break;
case R.id.imageButton9:
play = false;
SoundOn.setVisibility(View.GONE);//set visibility to false on create
SoundOff.setVisibility(View.VISIBLE);
break;
case R.id.imageButton10:
play = true;
SoundOn.setVisibility(View.VISIBLE);//set visibility to false on create
SoundOff.setVisibility(View.GONE);
}
}
I´m getting an error in NullPointerException in the "SoundOn.setVisibility(View.GONE);//set visibility to false on create"
SoundOn is null; you need to make sure you initialize it.
If you are using Activities a good place is to do it in onCreate after you inflate the view. With something like this:
SoundOn = findViewById(R.id.YourViewsId);
Find which view was updated is only half the work.
You need to inflate (get runtime object of the view) and modify (change the visibility).
Add this before you perform any action off changing the button's view (the top of your switch statments):
Button SoundOn = (Button) findViewById(R.id.soundOffBtnId) //Use ImageButton instead of Button depends on your code.
Button SoundOff = (Button) findViewById(R.id.soundOffBtnId)

ImageButton Click Action Android

I've stored the imagebutton info in an xml. It's image path,width,height and so on.
But since there are multiple images I want to store data for each imagebutton while it's clicked. for example, if there are two buttons named male and female, i want to set String value = "male"; if male image button is clicked. But it did not work. Can somebody help me?
public void AddAllImageButtons() throws IOException {
AbsoluteLayout Layout = (AbsoluteLayout) findViewById(R.id.layout1);
ImageButton btn = new ImageButton(this);
Layout.addView(btn);
AbsoluteLayout.LayoutParams absParams = (AbsoluteLayout.LayoutParams) btn
.getLayoutParams();
absParams.x = x;
absParams.y = y;
btn.setLayoutParams(absParams);
btn.setBackgroundDrawable(null);
btn.setImageBitmap(BitmapFactory.decodeStream(getAssets().open(path)));
if(type.equals("imagebutton")){
if(elementId.equals("female")){
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
gender = elementId;
GenerateAlertBoxes(gender);
}
});
}
Id is the image button id which is created in xml file.
AlertBox text does not show "female" for this example.
you can setTag("value") to set the value in the ImageButton btn and on the onclick event you can use getTag() function to retrieve the value and set it to gender.
use btn.setTag(elementid); whenever you add a btn to the layout.......
inside btn.setOnClickListener you can write v.getTag().toString() to get the value and set it to gender......
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
gender = v.getTag().toString();
GenerateAlertBoxes(gender);
}
});

how do i change images in android and ignore previous if statements?

hi im still very new to android but have been developing this part of my app for a while with little progress, the idea is I have 2 containers, a top container with INVISIBLE imageButtons inside it, and a bottom container with VISIBLE imageButtons inside.
When an image from the bottom container is clicked it should be put in the top left of the top container and the images below should change allowing the user to select a new image (screenshot1-screenshot2).
When one of these new images is selected, it should join the previous image in the top container next to the last clicked image, and again the images in the bottom container should change allowing the user to select another image (screenshot2-screenshot3) which should again join the top container and have the bottom container change its images.
What i have so far achieves this but only on one set path little hard to explain so let me add some screen shots to demonstrate
when the app starts you should be able to select any of these 3 image buttons the selection should go to the top left and switch images at the bottom according to which was clicked
when imageButton1 is clicked
when imageButton3 is clicked
when imageButton2 is clicked
now this appears to be correct but after imageButton1 is clicked if you were to select imageButton2 instead of imageButton3 (the image that looks like its eating, dont worry im actually very good at drawing and will be changing all these images,) the last screenshot is displayed and on the third screen shot if you were to select the cola bottle (imageButton1) it jumps back to screenshot 2 but with imageButton3 still displayed in the top container like this
now i know that this is because i have told it to do this in my .java file the problem is im not sure how to rectify this as android provides static id's for each button i think it may be a case of writing an extra conditional before my "speak=" statement but as i have stated im fairly new to all this and for the time being am a little stumped can anybody help me? here is my code
package com.martinsapp.socialstories;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.os.Bundle;
import android.widget.ImageButton;
public class speakActivity extends Activity{
int speak = 1;
ImageButton btn1;
ImageButton btn2;
ImageButton btn3;
ImageButton btn4;
ImageButton btn5;
ImageButton btn6;
ImageButton btnmt1;
ImageButton btnmt2;
ImageButton btnmt3;
ImageButton btnmt4;
ImageButton btnmt5;
ImageButton btnmt6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speak);
btn1 = (ImageButton)findViewById(R.id.imageButton);
btn1.setOnClickListener(ButtonClickListener);
btn1.setBackgroundColor(View.INVISIBLE);
btn2 = (ImageButton)findViewById(R.id.imageButton2);
btn2.setOnClickListener(ButtonClickListener);
btn2.setBackgroundColor(View.INVISIBLE);
btn3 = (ImageButton)findViewById(R.id.imageButton3);
btn3.setOnClickListener(ButtonClickListener);
btn3.setBackgroundColor(View.INVISIBLE);
btn4 = (ImageButton)findViewById(R.id.imageButton4);
btn4.setOnClickListener(ButtonClickListener);
btn4.setBackgroundColor(View.INVISIBLE);
btn5 = (ImageButton)findViewById(R.id.imageButton5);
btn5.setOnClickListener(ButtonClickListener);
btn5.setBackgroundColor(View.INVISIBLE);
btn6 = (ImageButton)findViewById(R.id.imageButton6);
btn6.setOnClickListener(ButtonClickListener);
btn6.setBackgroundColor(View.INVISIBLE);
btnmt1 = (ImageButton)findViewById(R.id.imageButtonempty1);
btnmt1.setOnClickListener(ButtonClickListener);
btnmt1.setVisibility(View.INVISIBLE);
btnmt2 = (ImageButton)findViewById(R.id.imageButtonempty2);
btnmt2.setOnClickListener(ButtonClickListener);
btnmt2.setVisibility(View.INVISIBLE);
btnmt3 = (ImageButton)findViewById(R.id.imageButtonempty3);
btnmt3.setOnClickListener(ButtonClickListener);
btnmt3.setVisibility(View.INVISIBLE);
btnmt4 = (ImageButton)findViewById(R.id.imageButtonempty4);
btnmt4.setOnClickListener(ButtonClickListener);
btnmt4.setVisibility(View.INVISIBLE);
btnmt5 = (ImageButton)findViewById(R.id.imageButtonempty5);
btnmt5.setOnClickListener(ButtonClickListener);
btnmt5.setVisibility(View.INVISIBLE);
btnmt6 = (ImageButton)findViewById(R.id.imageButtonempty6);
btnmt6.setOnClickListener(ButtonClickListener);
btnmt6.setVisibility(View.INVISIBLE);
if (speak == 1){
btn1.setImageResource(R.drawable.i_want);
btn2.setImageResource(R.drawable.i_like);
btn3.setImageResource(R.drawable.i_feel);
btn4.setVisibility(View.INVISIBLE);
btn5.setVisibility(View.INVISIBLE);
btn6.setVisibility(View.INVISIBLE);
}
}
private View.OnClickListener ButtonClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.imageButton:
speak = 2;
if (speak == 2){
btn1.setImageResource(R.drawable.to_play);
btn2.setImageResource(R.drawable.to_eat);
btn3.setImageResource(R.drawable.a_drink);
btn4.setImageResource(R.drawable.a_hug);
btn4.setVisibility(View.VISIBLE);
btn5.setVisibility(View.INVISIBLE);
btn6.setVisibility(View.INVISIBLE);
btnmt1.setImageResource(R.drawable.i_want);
btnmt1.setVisibility(View.VISIBLE);
btnmt1.setBackgroundColor(View.INVISIBLE);
}
break;
case R.id.imageButton3:
speak = 3;
if (speak == 3){
btn1.setImageResource(R.drawable.fizz);
btn2.setImageResource(R.drawable.squash);
btn3.setImageResource(R.drawable.milkshake);
btn4.setVisibility(View.INVISIBLE);
btn5.setVisibility(View.INVISIBLE);
btn6.setVisibility(View.INVISIBLE);
btnmt2.setImageResource(R.drawable.a_drink);
btnmt2.setVisibility(View.VISIBLE);
btnmt2.setBackgroundColor(View.INVISIBLE);
}
break;
case R.id.imageButton2:
speak = 4;
if (speak == 4){
btn1.setImageResource(R.drawable.orange);
btn2.setImageResource(R.drawable.lemon);
btn3.setImageResource(R.drawable.apple);
btn4.setImageResource(R.drawable.blackcurrant);
btn5.setImageResource(R.drawable.strawberry);
btn3.setVisibility(View.VISIBLE);
btn4.setVisibility(View.VISIBLE);
btn5.setVisibility(View.VISIBLE);
btn6.setVisibility(View.INVISIBLE);
btnmt3.setImageResource(R.drawable.squash);
btnmt3.setVisibility(View.VISIBLE);
btnmt3.setBackgroundColor(View.INVISIBLE);
}
}
}
};
}

How to change specify previous clicked image button view when the 2nd click button is occur

I have doubt in when I click the 2nd time image button, I want to change the both button image view in simultaneously.
Example:
1. first time press the button 1 and change already the first time image view. (get work)
2. second time press the other button 2 and I want change image view for the both button 1 and 2 in simultaneously. But I can only get the ibutton as variable signal to change the button 2 image view and button 1 can't get.
Question:
1. How do I change the button image view for button 1 when I click the button 2?
2. How do I can keep the button variable in array?
My code like this:
public class CheckersTest extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
final ImageView iv_new_game = (ImageView) findViewById(R.id.new_game);
iv_new_game.setOnClickListener(welcome_listener);
}
OnClickListener welcome_listener = new View.OnClickListener() {
public void onClick(View v) {
final ImageView iv = (ImageView) v;
if (iv.getId() == R.id.new_game) {
setContentView(R.layout.checkers_board);
final ImageButton b2 = (ImageButton) findViewById(R.id.imageButton2);
final ImageButton b4 = (ImageButton) findViewById(R.id.imageButton4);
final ImageButton b6 = (ImageButton) findViewById(R.id.imageButton6);
// set the OnClickListeners.
b2.setOnClickListener(button_listener);
b4.setOnClickListener(button_listener);
b6.setOnClickListener(button_listener);
// Re-enable the Click-able property of buttons.
b2.setClickable(true);
b4.setClickable(true);
b6.setClickable(true);
}
};
};
OnClickListener button_listener = new View.OnClickListener() {
public void onClick(View v) {
ImageButton ibutton = (ImageButton) v;
ibutton.setImageResource(R.drawable.green_bol);
}
};
In the second Button's onclick method get instance of first button and set image.

android imageView

I'd like to do such thing.... There is a Relative layout with splash.PNG as a background.... there are five ImageViews: #drawable\1.png, #drawable\2.png, #drawable\3.png, #drawable\4.png and #drawable\5.png...
1-is clickable and visible;
2-5 - are invisible and clickable="false"
by clicking once Imageview 2 becomes visible but unclickable, and then by clicking all 2-5 appears, then by clicking last fifth time 2-5 again becomes invisible....
As for me, such construction works with one invisible ImageView:
final ImageView iv36 = (ImageView) findViewById(R.id.yabl3skr);
iv36.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v33) {
// TODO Auto-generated method stub
final ImageView iv37 = (ImageView)findViewById(R.id.yab3);
iv37.setVisibility(1);
iv37.setClickable(true);
iv37.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v33) {
// TODO Auto-generated method stub
iv37.setVisibility(View.GONE);
iv37.setClickable(false);
}
});
}
});
Need any ideas how to do it in a good way with five imageViews?
I don't know if I fully get it, but what I think you're saying is that you have five ImageViews that all perform conditional logic when clicked. If that's the case, what I normally do is implement single OnClickListener for all of the images. You can set an initial state on all of your images and then do a switch on the id of the View being passed to enable and show which ever views you want to show.
final ImageView iv33 = (ImageView) findViewById(R.id.iv33);
final ImageView iv34 = (ImageView) findViewById(R.id.iv34);
final ImageView iv35 = (ImageView) findViewById(R.id.iv35);
final ImageView iv36 = (ImageView) findViewById(R.id.iv36);
final ImageView iv37 = (ImageView) findViewById(R.id.iv37);
OnClickListener imageClickListener = new OnClickListener() {
public void onClick(View v) {
// Initial state
iv33.setClickable(false);
iv34.setClickable(false);
iv35.setClickable(false);
iv36.setClickable(false);
iv37.setClickable(false);
iv33.setVisibility(View.GONE);
iv34.setVisibility(View.GONE);
iv35.setVisibility(View.GONE);
iv36.setVisibility(View.GONE);
iv37.setVisibility(View.GONE);
switch(v.getId())
{
case R.id.iv33:
// show and hide what you would like...
break;
case R.id.iv34:
// show and hide what you would like...
break;
case R.id.iv35:
// show and hide what you would like...
break;
case R.id.iv36:
// show and hide what you would like...
break;
case R.id.iv37:
// show and hide what you would like...
break;
}
}
};
iv33.setOnClickListener(imageClickListener);
iv34.setOnClickListener(imageClickListener);
iv35.setOnClickListener(imageClickListener);
iv36.setOnClickListener(imageClickListener);
iv37.setOnClickListener(imageClickListener);

Categories

Resources