android - Changing Button using setBackgroundDrawable, but background stays changed when returning -
i'm using setbackgrounddrawable
method change background of button on activity. onclicklistener has intent open new activity. however, when return past activity hitting physical button, button onclicklistener assigned still has onclick background set. if return previous activity using button in action bar, works correctly. tried use selector xml, android studio gives me render errors, , doesn't load when compile.
here mainactivity.java:
package com.jordandebarth.supercalculator; import android.app.actionbar; import android.content.intent; import android.graphics.color; import android.graphics.drawable.colordrawable; import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.view; import android.widget.button; import android.widget.imagebutton; public class mainactivity extends activity { imagebutton pythag; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); actionbar bar = getactionbar(); bar.setbackgrounddrawable(new colordrawable(color.parsecolor("#33b5e5"))); final imagebutton pythag = (imagebutton) findviewbyid(r.id.pythag_button); pythag.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { pythag.setbackgroundresource(r.drawable.pythag_button_selector); intent pythagintent = new intent(mainactivity.this, pythagoreanactivity.class); startactivity(pythagintent); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
activity_main.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:background="#e5e5e5" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="10dp" android:paddingright="10dp" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <imagebutton android:layout_width="fill_parent" android:background="@drawable/pythag_button" android:layout_height="wrap_content" android:id="@+id/pythag_button" android:focusable="true"/> </relativelayout>
user selector in xml file :
button_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/btn_hover" android:state_pressed="true"></item> <item android:drawable="@drawable/btn_hover" android:state_focused="true"></item> <item android:drawable="@drawable/btn" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item> <item android:drawable="@drawable/btn_hover" android:state_enabled="false"></item> </selector>
layout.xml
<button android:id="@+id/btnok" android:background="@drawable/button_selector" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:layout_margin="5dp" android:text="@string/yes" />
Comments
Post a Comment