Reputation: 201
When I click one of the buttons (forgot your code? or tick box), they display solid red colour and lead to a different activity. When I return to the previous activity, the button state has changed to default and is displaying orange colour. How do I retain the button's original state after being pressed?
button_state.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#000000" />
<gradient android:angle="-90" android:startColor="#FF0D00" android:endColor="#FF0D00" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle">
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#000000" />
<solid android:color="#FF0D00"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#000000" />
<gradient android:angle="-90" android:startColor="#D8D8D8" android:endColor="#848484" />
</shape>
</item>
</selector>
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".SignInScreen"
android:background="#619ec9"
>
<TextView
android:layout_width="374dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hello! Welcome to MLAD!"
android:id="@+id/textView"
android:textAlignment="center"
android:textSize="30dp"
android:layout_above="@+id/textView2"
android:layout_alignParentStart="true"
android:layout_x="-28dp"
android:layout_y="117dp"
android:typeface="serif"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Please enter your 4 digit code:"
android:id="@+id/textView2"
android:layout_above="@+id/codeEntry"
android:layout_centerHorizontal="true"
android:textSize="27dp"
android:layout_x="5dp"
android:layout_y="182dp"
android:textStyle="bold"
android:typeface="serif" />
<ImageButton
android:layout_width="75dp"
android:layout_height="75dp"
android:id="@+id/tick"
android:src="@drawable/black_tick"
android:background="@drawable/button_state"
android:layout_x="120dp"
android:layout_y="296dp"
android:layout_below="@+id/codeEntry"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp" />
<ImageButton
android:layout_width="82dp"
android:layout_height="82dp"
android:id="@+id/help"
android:src="@drawable/question"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@drawable/button_state"
android:layout_x="232dp"
android:layout_y="4dp" />
<EditText
android:maxLength="4"
android:layout_width="283dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="15"
android:id="@+id/codeEntry"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2"
android:layout_x="19dp"
android:layout_y="246dp"
android:background="@drawable/shape"
android:textSize="27sp"
android:hint="Enter Your Code Here"
android:gravity="center_horizontal"
/>
<Button
android:layout_width="119dp"
android:layout_height="wrap_content"
android:text="Create a new account"
android:id="@+id/create_acc"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/tick"
android:layout_toStartOf="@+id/tick"
android:textSize="18sp"
android:background="@drawable/button_state"
android:layout_x="3dp"
android:layout_y="410dp"
android:layout_alignTop="@+id/recover_code" />
<Button
android:onClick="goSecActivity"
android:layout_width="105dp"
android:layout_height="91dp"
android:text="Forgot your code?"
android:id="@+id/recover_code"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/tick"
android:textSize="20sp"
android:background="@drawable/button_state"
android:layout_toRightOf="@+id/tick"
android:layout_x="222dp"
android:layout_y="376dp"
android:focusable="true"
android:focusableInTouchMode="true"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@mipmap/mlad_logo"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/help"
android:scaleType="fitXY"
android:layout_alignParentTop="true"
android:layout_alignBottom="@+id/textView2"
android:alpha="0.1" />
</RelativeLayout>
Upvotes: 1
Views: 364
Reputation: 62559
If you mean for the lifetime of the application even after its destroyed then used a sharedPreference to remember the state. Set it before you leave to the other activity.
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
prefs.edit().putBoolean("red", true).apply();
that gets called right before you go to the new activity.
then have a if statement in your onResume code to do this:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
boolean shouldBeRed=prefs.getBoolean("red",false)
if(shouldBeRed){
//change button to red here
}
but if you just need it while the user is in the app(so per session), just set a flag in onStop() and then in onResume() change the buttons color accordingly.
UPDATE: NOW i understand your issue the buttons state was not changing back to grey which is the normal state. Try removing this onelines from the button in your xml layout:
android:focusableInTouchMode="true"
So make your button layout now look like this:
<Button
android:onClick="goSecActivity"
android:layout_width="105dp"
android:layout_height="91dp"
android:text="Forgot your code?"
android:id="@+id/recover_code"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/tick"
android:textSize="20sp"
android:background="@drawable/button_state"
android:layout_toRightOf="@+id/tick"
android:layout_x="222dp"
android:layout_y="376dp"
android:focusable="true"
/>
whats occurring is every time you return to the page since your focusable in touch mode its making the button focusable which is calling the focus state of your button_state.xml
from the docs:
focusableInTouchMode: Boolean that controls whether a view can take focus while in touch mode. If this is true for a view, that view can gain focus when clicked on, and can keep focus if another view is clicked on that doesn't have this attribute set to true.
Upvotes: 1