Issue
In my application I have 7 buttons ,I am trying to change the button border color when I clicked on it .By default the button color is gray and after click on it I would like to change to some other color.
gray_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:width="3dp" android:color="#646464" />
<solid android:color="@color/white" />
<size android:width="37dp" android:height="40dp" />
</shape>
</item>
</selector>
green_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:width="3dp" android:color="@color/green" />
<solid android:color="@color/white" />
<size android:width="37dp" android:height="40dp" />
</shape>
</item>
</selector>
CreateAlarm.java
public class CreateAlarm extends Activity {
private TimePicker mTimePicker;
private EditText mTitle;
private TextView mCancel, mRepeat,mSave;
private String mselectedTime,mAlarmName,mSnoozeEnabled,mFlashLightEnabled,mVibrationEnabled,mVolumeIncreaseEnabled,mSunDay,mMonDay,mTueDay,mWedDay,mThuDay,mFriDay,mSatDay;
private com.suke.widget.SwitchButton mSnooze,mFlashLight,mVibration,mVolumeIncrease;
public String mSelectedDays;
@Override
protected void onCreate(Bundle savedInstanceState) {
mFlashLight.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(SwitchButton view, boolean isChecked) {
if(mFlashLight.isChecked()== true){
mFlashLightEnabled = "Enabled";
}
}
});
mVibration.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(SwitchButton view, boolean isChecked) {
if(mVibration.isChecked()== true){
mVibrationEnabled = "Enabled";
}
}
});
mVolumeIncrease.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
if(mSelectedDays.contains("Thu")){
mThuDay = "Thu";
}else{
mThuDay = "Null";
}
if(mSelectedDays.contains("Fri")){
mFriDay = "Fri";
}else{
mFriDay = "Null";
}
if(mSelectedDays.contains("Sat")){
mSatDay = "Sat";
}else{
mSatDay = "Null";
}
//SwitchButton isChecked Validation
if (mSnoozeEnabled == null){
mSnoozeEnabled = "Disabled";
}
Intent intent = new Intent(CreateAlarm.this, AlarmActivity.class);
@Override
public void onStart() {
super.onStart();
if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
@Override
public void onPause() {
super.onPause();
mRepeat.setText("");
}
@Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
public void onEventMain(AlarmEvent alarmEvent) {
if (alarmEvent.getSelectedEventType() == AlarmEvent.eventTypes.updateSelectedDay) {
mRepeat.setText(alarmEvent.getSelectedDays());
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
}
update :
CreateAlarm.java
public class CreateAlarm extends Activity {
private CustomBorderButton mbtn_Sun,mbtn_Mon,mbtn_Tue,mbtn_Wed,mbtn_Thu,mbtn_Fri,mbtn_Sat;
public String mAlarmTitle, mTime, mSun, mMon, mTue, mWed, mThu, mFri, mSat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_alarm);
mbtn_Sun = (CustomBorderButton) findViewById(R.id.mbtn_Sun);
mbtn_Mon = (CustomBorderButton) findViewById(R.id.mbtn_Mon);
mbtn_Tue = (CustomBorderButton) findViewById(R.id.mbtn_Tue);
mbtn_Wed = (CustomBorderButton) findViewById(R.id.mbtn_Wed);
mbtn_Thu = (CustomBorderButton) findViewById(R.id.mbtn_Thu);
mbtn_Fri = (CustomBorderButton) findViewById(R.id.mbtn_Fri);
mbtn_Sat = (CustomBorderButton) findViewById(R.id.mbtn_Sat);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
mTime = bundle.getString("mTime");
mSun = bundle.getString("mSun");
mMon = bundle.getString("mMon");
mTue = bundle.getString("mTue");
mWed = bundle.getString("mWed");
mThu = bundle.getString("mThu");
mFri = bundle.getString("mFri");
mSat = bundle.getString("mSat");
if (!(mAlarmTitle == null)) {
mTitle.setText(mAlarmTitle);
}
if(mSun.contains("Sun")){
mbtn_Sun.setEnabled(true);
}
if(mMon.contains("Mon")){
mbtn_Mon.setEnabled(true);
}
if(mTue.contains("Tue")){
mbtn_Tue.setEnabled(true);
}
if(mWed.contains("Wed")){
mbtn_Wed.setEnabled(true);
}
if(mThu.contains("Thu")){
mbtn_Thu.setEnabled(true);
}
if(mFri.contains("Fri")){
mbtn_Fri.setEnabled(true);
}
if(mSat.contains("Sat")){
mbtn_Sat.setEnabled(true);
}
}
}
@Override
public void onStart() {
super.onStart();
if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
@Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
public void onEventMain(AlarmEvent alarmEvent) {
if (alarmEvent.getSelectedEventType() == AlarmEvent.eventTypes.updateSelectedDay) {
mRepeat.setText(alarmEvent.getSelectedDays());
}
}
private void finishActivity() {
finish();
mbtn_Sun.clearAllDays();
}
@Override
public void onPause() {
super.onPause();
finishActivity();
}
}
After loading this activity I received the data from previous activity and based on this I want to set the buttons are enabled/disabled and also want to set the TextView text (mRepeat) text accordingly.
Solution
Your error is AppCompatButton
. You should extend only Button
.
You should research EventBus
: https://github.com/greenrobot/EventBus . It is provide that notice activity or fragment anything.
Please try again:
public class CustomBorderButton extends Button {
private static HashSet<Integer> selectedDays = new HashSet<>();
public CustomBorderButton(Context context) {
this(context, null);
}
public CustomBorderButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomBorderButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
int id = view.getId();
boolean isSelected = selectedDays.contains(id);
Drawable bgDrawable;
if (isSelected) {
bgDrawable = getBackgroundDrawable(R.drawable.gray_button);
selectedDays.remove(id);
} else {
bgDrawable = getBackgroundDrawable(R.drawable.green_button);
selectedDays.add(id);
}
setBackground(bgDrawable);
setSelectedDays();
}
});
}
private void setSelectedDays() {
StringBuilder repeatDays = new StringBuilder();
TreeMap<Integer, String> mAlarmDays = new TreeMap<>();
if (selectedDays.size() == 0) {
repeatDays = new StringBuilder("Never");
} else {
for (Integer selectedDayId : selectedDays) {
switch (selectedDayId) {
case R.id.mbtn_Sun:
mAlarmDays.put(0, "Sun");
break;
case R.id.mbtn_Mon:
mAlarmDays.put(1, "Mon");
break;
case R.id.mbtn_Tue:
mAlarmDays.put(2, "Tue");
break;
case R.id.mbtn_Wed:
mAlarmDays.put(3, "Wed");
break;
case R.id.mbtn_Thu:
mAlarmDays.put(4, "Thu");
break;
case R.id.mbtn_Fri:
mAlarmDays.put(5, "Fri");
break;
case R.id.mbtn_Sat:
mAlarmDays.put(6, "Sat");
break;
}
}//for
for (String day : mAlarmDays.values()) {
repeatDays.append(day).append(" ");
}
}
EventBus.getDefault().post(new AlarmEvent().updateSelectedDay(AlarmEvent.eventTypes.updateSelectedDay, repeatDays.toString().trim()));
}
public Drawable getBackgroundDrawable(int resourceId) {
Drawable backgroundDrawable;
if (android.os.Build.VERSION.SDK_INT >= 21) {
backgroundDrawable = getResources().getDrawable(resourceId, getContext().getTheme());
} else {
backgroundDrawable = getResources().getDrawable(resourceId);
}
return backgroundDrawable;
}
}
CreateAlarm.java
public class CreateAlarm extends Activity {
private TimePicker mTimePicker;
private TextView mCancel, mRepeat;
private ImageView mRepeatIcon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_alarm_layout);
Utils.darkenStatusBar(this, R.color.StatusbarColor);
mTimePicker = (TimePicker) findViewById(R.id.mTimePicker);
mCancel = (TextView) findViewById(R.id.mCancel);
mRepeat = (TextView) findViewById(R.id.mRepeat);
int hours = mTimePicker.getCurrentHour();
mCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
@Override
public void onStart() {
super.onStart();
if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
@Override
public void onPause() {
super.onPause();
mRepeat.setText("");
}
@Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
public void onEventMain(AlarmEvent alarmEvent) {
if (alarmEvent.getSelectedEventType() == AlarmEvent.eventTypes.updateSelectedDay) {
mRepeat.setText(alarmEvent.getSelectedDays());
}
}
}
AlarmEvent:
public class AlarmEvent {
private eventTypes selectedEventType;
private String selectedDays = "";
public String getSelectedDays() {
return selectedDays;
}
public eventTypes getSelectedEventType() {
return selectedEventType;
}
public enum eventTypes {
updateSelectedDay
}
public AlarmEvent updateSelectedDay(eventTypes newEvent, String selectedDays) {
AlarmEvent alarmEvent = new AlarmEvent();
alarmEvent.selectedEventType = newEvent;
alarmEvent.selectedDays = selectedDays;
return alarmEvent;
}
}
> Reply to your comment:
CreateAlarm
clickSave(){
Intent intent = new Intent(CreateAlarm.this, AlarmActivity.class);
finish();
startActivity(intent);
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
AlarmActivity
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
Answered By - propoLis
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.