5

If the button was clicked I want to turn on the wifi and also change the Image of ImageButton, but I do not know how to change it

I have also tried this from how to change the image of a button with every click? but is isn't working:

boolean isPressed=false
button.setOnClickListener(buttonListener);

OnClickListener buttonListener= new OnClickListener() {
@Override
public void onClick(View v) {
    if(isPressed){
    button.setBackgroundResource(R.drawable.icon1);
    }else{
    button.setBackgroundResource(R.drawable.icon2);
    }
    isPressed=!isPressed;
}};

when I write the code above, android studio shows this: Cannot resolve symbol setOnClickListener

I have also created a button_wifi_selector xml, which it looks like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<item android:drawable="@drawable/wifi_on"
    android:state_pressed="true" />
<item android:drawable="@drawable/ic_launcher"
    android:state_focused="true" />
<item android:drawable="@drawable/wifi" />

</selector>

and in my activity i have this

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageButton_wifi"
    android:layout_below="@+id/toggleButton_wifi"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:onClick="turnOffWifiDemo"
    android:src="@drawable/button_wifi_selector" />

but it isn't doing, what I want Can somebody pls help me? Thanks

EDIT: it works with the first code. I just had to remove the onClick from ImageButton in the xml BUT: he is changing the picture the second time, when I start the app. After that he changes it every time

Community
  • 1
  • 1
Ali
  • 157
  • 2
  • 3
  • 13
  • 1
    remove this line android:onClick="turnOffWifiDemo" as you are using onclicklisten in java code so either use in xml or in java class – Meghna Jul 15 '14 at 10:32
  • Instead of ImageButton you can use Toggle Button – ADT Jul 15 '14 at 10:33
  • @Meghna Thank you! But do you know, how I can do that with an onClick Method (I do not want to use the onClickListener) – Ali Jul 15 '14 at 10:40
  • What is your output when you use the selector way? – CodePro_NotYet Jul 15 '14 at 10:46
  • @CodePro_NotYet nothing....while you press the button, he shows wifi_on and if you release it, it shows wifi – Ali Jul 15 '14 at 11:04
  • Show where you have placed the code of `setOnClickListener()` in the class. It is inside which function? I think you have not put it in any method. If so, try putting it inside a method. – CodePro_NotYet Jul 15 '14 at 11:15
  • @CodePro_NotYet Do you mean to put the `setOnClickListener()`in an `onClick(View view)`Method? – Ali Jul 15 '14 at 11:21
  • No. Put it inside the method where you initialized the button. Like so- ImageButton button = findViewById(R.id.imageButton_wifi); button.setOnClickListener(buttonListener); – CodePro_NotYet Jul 15 '14 at 11:24
  • @CodePro_NotYet Yes, I did that already – Ali Jul 15 '14 at 11:25
  • @Ali i have updated my code,just chk it. – Meghna Jul 15 '14 at 12:08

5 Answers5

4

this code work for me; test it

   final ImageButton btnTest =(ImageButton) findViewById(R.id.btnexctract);
    btnTest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            btnTest.setSelected(!btnextra.isPressed());

            if (btnTest.isPressed()) {
                btnextra.setImageResource(R.drawable.yourImage);
            }
            else {
                btnTest.setImageResource(R.drawable.yourImage2);
            }
        }
    });
reza.abdi
  • 51
  • 2
  • hello...suppose im performing this one activity.user click to glow image... what if user goes from one activity to another and again coming back to activity one will the button remain as glow? – Wini Jan 18 '21 at 13:12
2

This is how you have to do it

Selector xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
       android:state_selected="true" 
       android:drawable="@drawable/info_icon_solid_with_shadow" />
   <item 
       android:drawable="@drawable/info_icon_outline_with_shadow" />
</selector>

And then in java:

//assign the image in code (or you can do this in your layout xml)
imageButton.setImageDrawable(getBaseContext().getResources().getDrawable(R.drawable....));

//set the click listener
imageButton.setOnClickListener(new OnClickListener() {

    public void onClick(View button) {
        //Set the button's appearance
        button.setSelected(!button.isSelected());

        if (button.isSelected()) {
            //Handle selected state change
        } 
        else {
            //Handle de-select state change
        }

    }

});
Badrul
  • 1,582
  • 4
  • 16
  • 27
0

You can use togglebutton like this

<ToggleButton
  android:id="@+id/toggleButton1"
  android:layout_width="74dp"
  android:layout_height="26dp"
  android:background="@drawable/toggle"
  android:checked="false"
  android:paddingRight="10dp"
  android:text="ON"
  android:textColor="#ffffff"
  android:textSize="13sp"
  android:textStyle="bold" />

And in java file

final ToggleButton toggleButton=(ToggleButton)findViewById(R.id.toggleButton1);

toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub

            if(isChecked){
                //
                toggleButton.setBackgroundResource(R.drawable.icon1);
            }else{
                //
                toggleButton.setBackgroundResource(R.drawable.icon2);
            }
        }
});
Banana
  • 2,435
  • 7
  • 34
  • 60
ADT
  • 255
  • 1
  • 4
  • 14
0

You can define a boolean flag and just change visibility of items. Description: changeBack is a button. This code is for change images Your onClick does not know which id is clicked?

private boolean bgcolor = false;

public void onClick(View view) {
    switch(view.getId())
    {
        case R.id.changeBack:

        bgcolor = !bgcolor;
        if (bgcolor) {
                imv.setVisibility(View.VISIBLE);
                imv2.setVisibility(View.INVISIBLE);
        } 
        else {
              imv2.setVisibility(View.VISIBLE);
            imv.setVisibility(View.INVISIBLE);
        }

        break;
    }
}
Banana
  • 2,435
  • 7
  • 34
  • 60
cantas
  • 104
  • 1
  • 14
0

I have modified it,for on/off it may help you

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
<ImageButton
            android:id="@+id/imageButton_wifi2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="clickEvent"
            android:visibility="invisible"
            android:src="@drawable/on" />
        <ImageButton
            android:id="@+id/imageButton_wifi1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="clickEvent"
            android:src="@drawable/off" />


    </FrameLayout>

like you need to create method with view parameter

{ 
 c=false;
          im1=(ImageButton)findViewById(R.id.imageButton_wifi1);
          im2=(ImageButton)findViewById(R.id.imageButton_wifi2);
}

public void  clickEvent(View v)
{
//Code to implement 
if(c) {
    im2.setVisibility(View.VISIBLE);
    im1.setVisibility(View.INVISIBLE);
    c=false;
}
else {

    im1.setVisibility(View.VISIBLE);
    im2.setVisibility(View.INVISIBLE);

    c=true;
}
Meghna
  • 539
  • 4
  • 14
  • How does this solve his problem? Can you please write appropriate code? – Badrul Jul 15 '14 at 11:12
  • @Badrul Ali had asked me how to implement onclick in xml,so i posted this code – Meghna Jul 15 '14 at 11:20
  • Aah! I somehow overlooked the comment, what you could have done is edited his question as the code you have posted here is irrelevant related to the thread. Such things should be avoided :) – Badrul Jul 15 '14 at 11:24
  • why did you use 2 ImageButtons? – Ali Jul 15 '14 at 18:50