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