Reputation: 3203
My working a woman pregnant app. I'm trying to baby kick the counter. Once the button is clicked, counter increase. But I want every click, button change color like web hover. I try onclick but one time change colo and later not changed
This sample onlick code
@Override
public void onClick(View v) {
if( v == counterbutton) {
counterbutton.setBackgroundColor(getResources().getColor(R.color.blue_background));
get_last_counter++;
SharedPreferences.Editor editor = MotherActivity.preferences.edit();
editor.putInt(MotherActivity.COUNTER_INCREASE, get_last_counter);
editor.apply();
counter.setText(counter_writer(get_last_counter));
}
}
Button xml code
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@string/kick"
android:id="@+id/counterbutton"
android:background="@color/app_pink"
android:textColor="@color/blue_text_color"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true" />
Sorry bad english. Thank you.
Upvotes: 0
Views: 116
Reputation: 1996
This will works.
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
counterbutton.setBackgroundColor(color);
Upvotes: 0
Reputation: 291
@Override
public void onClick(View v) {
int id = v.getId();
switch(id) {
case R.id.counterbuton:
counterbutton.setBackgroundColor(getResources().getColor(R.color.blue_background));
get_last_counter++;
SharedPreferences.Editor editor = MotherActivity.preferences.edit();
editor.putInt(MotherActivity.COUNTER_INCREASE, get_last_counter);
editor.apply();
counter.setText(counter_writer(get_last_counter));
break;
}
}
Upvotes: 0
Reputation: 456
I'll suggest looking into selectors, here is a nice example I found.
Upvotes: 2
Reputation: 2940
On every click you set the same color. you should have a variable and alternate like :
mClickIndex = 0
if(mClickIndex % 2 == 0){
counterbutton.setBackgroundColor(getResources().getColor(R.color.blue_background));
} else {
counterbutton.setBackgroundColor(getResources().getColor(OTHER COLOR));
}
mClickIndex++;
Upvotes: 0