1
I’m creating a home automation app, in it I have the main screen that when selecting the desired category it opens a second screen with the commands (example category lights, it opens the commands Luz1, Luz2, etc.), by clicking on the Imagebutton of the light1 it changes the icon (luzOff to Luzon), when I return to the main screen and enter again in the category of lights I lose my last state of Imagebutton. Example I opened the light category and left the Light1 on (Imagebutton will change the icon to Luzon), more when I go out to the main screen and go back to the screen of lights it opens with the light iconOff even if I leave the light on. How do I make the icon always keep its last state left, every time I open the screen ? (I’m sorry if I wasn’t clear and why it’s kind of hard to explain what I want).
public class Home extends Activity{
private ImageButton btnLuz1;
private ImageButton btnLuz2;
Button btnVoltar;
private Set<String> botoesOn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.home);
btnVoltar= (Button) findViewById(R.id.btnVoltar);
btnVoltar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent voltaTela = new Intent(Home.this, MainActivity.class);
Home.this.startActivity(voltaTela);
Home.this.finish();
}
});
btnLuz1 = (ImageButton) findViewById(R.id.btnLuz1);
btnLuz2 = (ImageButton) findViewById(R.id.btnLuz2);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
botoesOn = prefs.getStringSet("botoes", null);
if(botoesOn == null){
botoesOn = new HashSet<String>();
}
else{
if(botoesOn.contains("botao1")){
btnLuz1.setImageResource(R.drawable.luzon);
}
if(botoesOn.contains("botao2")){
btnLuz1.setImageResource(R.drawable.luzon);
}
}
}
public void btnLuz1Click(View v){
if(botoesOn.contains("botao1")){
botoesOn.remove("botao1");
btnLuz1.setImageResource(R.drawable.luzoff);
}
else{
botoesOn.add("botao1");
btnLuz1.setImageResource(R.drawable.luzon);
}
}
public void btnLuz2Click(View v){
if(botoesOn.contains("botao2")){
botoesOn.remove("botao2");
btnLuz2.setImageResource(R.drawable.luzoff);
}
else{
botoesOn.add("botao2");
btnLuz2.setImageResource(R.drawable.luzon);
}
}
protected void onPause(){
super.onPause();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putStringSet("botoes", botoesOn).apply();
}
}
and xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Home" >
<ImageButton
android:id="@+id/btnLuz1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="100dp"
android:layout_marginTop="53dp"
android:layout_toLeftOf="@+id/btnLuzes"
android:background="#00000000"
android:onClick="bt1Click"
android:src="@drawable/luzoff" />
<ImageButton
android:id="@+id/btnLuz2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/btnLuz1"
android:layout_marginLeft="150dp"
android:background="#00000000"
android:onClick="bt2Click"
android:src="@drawable/luzoff" />
<Button
android:id="@+id/btnVoltar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/btnLuzes"
android:layout_marginBottom="98dp"
android:layout_marginRight="95dp"
android:text="Voltar" />
Error log:
05-07 11:50:45.466: E/AndroidRuntime(2422): at android.view.View$1.onClick(View.java:4007)
05-07 11:50:45.466: E/AndroidRuntime(2422): at android.view.View.performClick(View.java:4780)
05-07 11:50:45.466: E/AndroidRuntime(2422): at android.view.View$PerformClick.run(View.java:19866)
05-07 11:50:45.466: E/AndroidRuntime(2422): at android.os.Handler.handleCallback(Handler.java:739)
05-07 11:50:45.466: E/AndroidRuntime(2422): at android.os.Handler.dispatchMessage(Handler.java:95)
05-07 11:50:45.466: E/AndroidRuntime(2422): at android.os.Looper.loop(Looper.java:135)
05-07 11:50:45.466: E/AndroidRuntime(2422): at android.app.ActivityThread.main(ActivityThread.java:5257)
05-07 11:50:45.466: E/AndroidRuntime(2422): at java.lang.reflect.Method.invoke(Native Method)
05-07 11:50:45.466: E/AndroidRuntime(2422): at java.lang.reflect.Method.invoke(Method.java:372)
05-07 11:50:45.466: E/AndroidRuntime(2422): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
05-07 11:50:45.466: E/AndroidRuntime(2422): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
05-07 11:50:45.466: E/AndroidRuntime(2422): Caused by: java.lang.NoSuchMethodException: bt1Click [class android.view.View]
05-07 11:50:45.466: E/AndroidRuntime(2422): at java.lang.Class.getMethod(Class.java:664)
05-07 11:50:45.466: E/AndroidRuntime(2422): at java.lang.Class.getMethod(Class.java:643)
05-07 11:50:45.466: E/AndroidRuntime(2422): at android.view.View$1.onClick(View.java:4000)
05-07 11:50:45.466: E/AndroidRuntime(2422): ... 10 more
It is one more mistake due to the translation of the name of my test methods into the names you use in the question. In the XML in the attributes
android:onClick
substitutebt1Click
forbtnLuz1Click
andbt2Click
forbtnLuz2Click
.– ramaral