0

I have these ImageViews/ImageButtons, and when I click I want to display my selection into an empty slot for example.

There are six options available to click but I only need to choose four, I want to display anyone of these six in my empty slots I have available.

R.drawable.blue
R.drawable.red
R.drawable.green
R.drawable.yellow
R.drawable.brown
R.drawable.purple

They are the image buttons.

I already have an empty "slot" image:

R.drawable.empty_Slot

I know that onClick() is what I need and then I need to set that click to the empty_slot I have but I'm confused how I would implement this if I could get any guidance thank you.

MLProgrammer-CiM
  • 17,231
  • 5
  • 42
  • 75
majestic94
  • 25
  • 6
  • so for example..if you select R.drawable.blue then you want the R.drawable.empyu_Slot to get that color? – Antonios Tsimourtos Jul 11 '16 at 13:42
  • Yes that's exactly right select my blue colour, then display it in one of my empty slot images I need to do this for 4 empty slots, in which they will be added to an array at a later date – majestic94 Jul 11 '16 at 13:45

3 Answers3

1

You can set the tag of your imagebuttons to the following

R.drawable.blue
R.drawable.red
R.drawable.green
R.drawable.yellow
R.drawable.brown
R.drawable.purple

like

ImageButton red = (ImageButton)findViewById(R.id.red);
red.setTag(R.drawable.red);

and then set onClickListener instead of setting onClick listener on every button. Just implement the interface and then in onClick function get this as

@Override
public void onClick(View view) {
    int id = (Integer)view.getTag();
    emptySlot.setImageResource(id);
}

Hope this helps.

Mustansar Saeed
  • 2,730
  • 2
  • 22
  • 46
  • What do you mean by implement the interface? – majestic94 Jul 11 '16 at 14:22
  • @majestic94 http://stackoverflow.com/questions/30082892/best-way-to-implement-view-onclicklistener-in-android you just need the "implements View.OnClickListener" – Antonios Tsimourtos Jul 11 '16 at 14:24
  • Okay brilliant thanks for that additional link to explain. I'm still slightly confused (sorry), but this only would apply to one emptySlot? or would all four of my empty slots have the same ID? But then wouldn't that change all empty slots to one colour? This is my confusion it just looks like that is for one empty slot – majestic94 Jul 11 '16 at 14:45
  • you can apply switch in onClick function and then view.getId() can lead you which ID was clicked. You can then use the appropriate slot for the change. Got it? – Mustansar Saeed Jul 11 '16 at 14:49
  • Yes that makes more sense using a switch statement thanks for all your help I'll give it a go :) – majestic94 Jul 11 '16 at 14:53
0

Replace "button" with your R.drawable.*

For example (mBlueButton is your named variable for the button holding the blue color)

ImageView mBlueButton = (ImageView) findViewById(R.id.your_id_of_specific_button);

ImageView - If it's just a button replace "ImageView" with "Button", same goes for everything else!

Then set an onClickListerner to each Button, in your case you have to paste this code 6 times if you have 6 buttons, but change the name with your named variables.

 mBlueButton.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 mEplySlotButton.setBackgroundColor(Color.Blue); // onClick of the blue button, set the empty slot to blue color. 
             }
         });

Also you could use only one function for all the buttons by recognizing the id of the button that was pressed using a switch statement and depend on that id of do the things you want to do.

Antonios Tsimourtos
  • 1,676
  • 1
  • 14
  • 37
  • That makes perfect sense thank you for that, slight confusion though, the onClick function will only apply to one empty slot??? would i need to do the "blue button" 4 times for each empty slot available it can go to??? because that will be a lot of code for six buttons – majestic94 Jul 11 '16 at 14:22
  • @majestic94 please consider Mustansar Saeed answer – Antonios Tsimourtos Jul 11 '16 at 14:23
0
mImageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Get the image from the ImageButton that was clicked
            Drawable drawable = ((ImageButton) v).getDrawable();
            //Set that Image to your empty ImageView 
            mImageView.setImageDrawable(drawable);
        }
    });
jburn2712
  • 167
  • 1
  • 7