Change toggle button drawable at activity start in Android Xamarin - android

I made two toggle buttons and i edited the toggle button style to change the toggle image on state change like the above code
this my toggle button
<ToggleButton
android:id="#+id/speaker"
android:layout_width="25dip"
android:layout_height="25dip"
android:layout_margin="10dp"
android:textOn=""
android:textOff=""
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_centerVertical="true"
android:background="#drawable/speaker_btn" />
and this drawable/speeker_btn.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/speaker_on"
android:state_checked="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/speaker_off"
android:state_checked="false"/>
</selector>
And im using static boolean variable to store the toggle status global to use it again in another activity
so what i want is to change the button drawable on activity created without clicking the toggle
i tried this code
speakerToggle = view.FindViewById<ToggleButton>(Resource.Id.speaker);
if(speaker_btn_checked == true ){
speakerToggle.checked = true;
}
But its look like recheck the toggle button again i just need to change the drawable

I solve this problem by replacing ToggleButton by Button, and change the button image programmatically from the activity class, and created static variable for the button status so my code became like this
Button speakerToggle = view.FindViewById<Button>(Resource.Id.speaker);
speakerToggle.Click += delegate{
if (am.SpeakerphoneOn == false)
{
speakerToggle.SetBackgroundResource(Resource.Drawable.speaker_on);
}else{
speakerToggle.SetBackgroundResource(Resource.Drawable.speaker_off);
}
};
And inside the onCreate method i created this
if (am.SpeakerphoneOn == true){
speakerToggle.SetBackgroundResource(Resource.Drawable.speaker_on);
}else{
speakerToggle.SetBackgroundResource(Resource.Drawable.speaker_off);
}
And it works very well !!

Related

Android Studio: Buttons with drawable selector displaying background wrong when applied programmatically

Im trying to change the background of some buttons programmatically with a selector.
I have two different selectors
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/palette.greyscale.lightgrey" android:state_pressed="false"></item>
<item android:drawable="#color/palette.blue.mid" android:state_pressed="true"></item>
and
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/palette.greyscale.lightgrey" android:state_pressed="false"></item>
<item android:drawable="#color/palette.orange.mid" android:state_pressed="true"></item>
which get applied programmatically depending on a boolean:
void setUI() {
int primary;
Drawable btn_color;
if (((App) getActivity().getApplication()).isGender_isMale()) {
primary = getResources().getColor(R.color.palette_blue_mid);
btn_color = getResources().getDrawable(R.drawable.button_blue);
}
else {
primary = getResources().getColor(R.color.palette_orange_mid);
btn_color = getResources().getDrawable(R.drawable.button_orange);
}
btn_speichern.setBackground(btn_color);
btn_teilen.setBackground(btn_color);
btn_offnen.setBackground(btn_color);
}
Here is one button from the fragment xml:
<Button
android:id="#+id/btn_speichern"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#drawable/button_blue"
android:text="Speichern"
android:textColor="#drawable/button_text_color"
android:textSize="20sp" />
When pressing one button another button also triggers the selector.
When changing the background in the fragment xml it works fine.
I also tried to remove android:background="#drawable/button_blue" which refers to one of the drawables ressource file, but with no sucess.
I think you can get a better picture of what my issue is by taking a look at this:
https://youtu.be/y2xKHz3bgfs
EDIT:
It seems like the selector always selects the button that is pressed and the next button with the same drawable background.
If you've got two distinct buttons you could attach onClickListeners to each and then have the onClick call send a message to your main activity indicating that the button was pressed

android-How to make custom toggle button

I want to make a custom toggle button , for instance when I click on the button , the star turns on and when I click again ,the star turns off.
i've tried some codes but they change the button to an image and when I click on it ,the image changes . I mean ,there is no button , it's just an image .
I want the image to be inside the button , like the default toggle button and I just need to change the image inside it .
How can I do so ?
thanks
create toggle_selector.xml in res/drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/toggle_on" android:state_checked="true"/>
<item android:drawable="#drawable/toggle_off" android:state_checked="false"/>
</selector>
apply the selector to your toggle button
<ToggleButton
android:id="#+id/chkState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/toggle_selector"
android:textOff=""
android:textOn=""/>
Note: for removing the text i used following in above code
textOff=""
textOn=""
reference https://stackoverflow.com/a/20300753/712960

Change toggle button image onclick

I know it has already been asked and answered here and here. I have tried both, but none of them is working right for me.
I have a favorite button, If it is pressed I set the item to favorite in database and replace the image of the toggle button, and vice versa. Here is how I am doing it:
<ToggleButton
android:id="#+id/btnFavorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn=""
android:textOff=""
android:layout_marginRight="5dp"
android:background="#drawable/favorite_btn_style" />
Here is my favorite_btn_style.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/favourit_blue_btn" android:state_checked="true"/>
<!-- pressed -->
<item android:drawable="#drawable/favourit_dark_btn"/>
<!-- default/unchecked -->
</selector>
In oncreate I check if the item is already set to favorite, then setchecked to true:
if (movieObj.getIsFav().intValue() == 1) {
btnFav.setChecked(true);
}
Here is my onclicklistener on the button:
btnFav.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (!btnFav.isChecked()) {
btnFav.setChecked(true);
// set favorite
dbHelper.updateMovieFavorite(movieObj.getId().intValue(), 1);
} else {
btnFav.setChecked(false);
// set favorite
dbHelper.updateMovieFavorite(movieObj.getId().intValue(), 0);
}
}
});
Function gets called, and executed fine, but no change in image.. What I am doing wrong?
Delete both btnFav.setChecked(true) and btnFav.setChecked(false) in your OnClick method. It is a togglebutton which toggles the setChecked on its own by every click and you reset it to the old value. So in your case it always has the same value(the start value).
I would suggest you rather use setOnCheckedChangeListener instead of onClickListener.
Create a file button_toggle.xml in your res/drawable folder
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="false"
android:drawable="#drawable/ic_slide_switch_off" />
<item
android:state_checked="true"
android:drawable="#drawable/ic_slide_switch_on" />
</selector>
Try to use android:button="#drawable/favorite_btn_style" and android:background="#android:color/transparent" combination. To customize the checkbox, radio and toggle button you should use android:button instead of android:background.

Android: How to create a button like radio button in a group?

Is it possible to create a button like radio button without using images?
Would like to have a pressed state upon selection. Then back to normal state when I click other options.
simply include the respective drawables of the radio button in different states (i.e. focused, pressed, checked, or normal). Include these in a selector.xml to specify the looks of the button for the respective states, and include that xml in the android:background attribute of your button. That should do it all...! :)
Check this link to understand the method better: Change how a CheckBox looks
(it is given for a CheckBox, but similar stuff will work for button as a radio button as well).
Edit:
Define round_button.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_focused="true"
android:drawable="#drawable/roundbutton_on_background_focus_yellow" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="#drawable/roundbutton_off_background_focus_yellow" />
<item android:state_checked="false"
android:drawable="#drawable/roundbutton_off_background" />
<item android:state_checked="true"
android:drawable="#drawable/roundbutton_on_background" />
</selector>
Then, wherever you need to include that button, just add the following:
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_button"
android:checked="true"/>
[P.S.: Don't forget to include the drawables for a round button, you'll have to search them yourself, or get from the default files (.android), which is given in the link]
Yes you can do it by using states for the button in drawable such as
(I have drawable for states you can have colors too.)
This is the file button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#drawable/dark_silver_filled_square" />
<item android:drawable="#drawable/light_silver_filled_square"/>
</selector>
And place this drawable file as background to your button as background like this:
<Button
android:id="#+id/allBtn"
android:layout_width="#dimen/_80sdp"
android:layout_height="#dimen/_22sdp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:background="#drawable/button_selector"
android:text="All"
android:textAllCaps="false"
app:layout_constraintBottom_toTopOf="#+id/btnTest"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
And programmatically create an ArrayList of buttons like this in your class
private var btnList = ArrayList<Button>()
Add your buttons in XML to the list like this:
btnList.add(allBtn)
Then set OnTouchListener for maintaining the selected color in the button like this:
binding.allBtn.setOnTouchListener { v, event ->
buttonStatePreserver(allBtn)
true
}
And pass it to a method to preserve selection for that particular button and make other remaining buttons unselected:
fun buttonStatePreserver(button: Button) {
for(btn in btnList) {
if(btn == button) {
btn.isPressed = true
} else {
btn.isPressed = false
}
}
}

Android...Change Button Colour when Clicked, but I have many buttons?

I want to change the button's colour when I clicked.
But when I clicked the another second button, the first button must be coloured the with the last colour.
I mean that, when I clicked whic button, it must be coloured with blue and the other buttons must be non-colour. here is the code;
if(view == button1)
{
button1.setBackgroundColor(Color.BLUE);
}
else if(view == button2){
button2.setBackgroundColor(Color.BLUE);
}
else if(view == button3){
button3.setBackgroundColor(Color.BLUE);
}
else if(view == button4){
button4.setBackgroundColor(Color.BLUE);
}
//init all buttons background : GRAY
public void initButtons(){
button1.setBackGroundColor(Color.GRAY);
button2.setBackGroundColor(Color.GRAY);
button3.setBackGroundColor(Color.GRAY);
button4.setBackGroundColor(Color.GRAY);
}
and in the implementation of the OnClick : do this :
#Override
public void onClick(View v ) {
initButtons();
((Button)v).setBackGroundColor(Color.BLUE);
}
Hope it helps :)
you can use drawable selector to define the button states in xml then by default the clicked button will be changed to the color you want only when it is in click state.
Create a new xml file in your drawable folder for example blue_btn.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/yourColor"
android:state_pressed="true" android:state_enabled="true" />
<item android:drawable="#color/yourOtherColor" android:state_enabled="true" />
</selector>
then use R.drawable.blue_btn as a background for your buttons
refer to: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Keep a reference to the previously modified Button. When you have a new click, set that previously referenced Button back to un-colored, set the current button to Blue then set that reference to the current button.
When you need to change the background color of the button when it is pressed, then u have to follow these ,
//create an xml file, like layout_a.xml file in drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/btn_pressed" /> <!--btn pressed -->
<item android:drawable="#drawable/btn_normal" /> <!-- Normal condition -->
</selector>
//Now this file should be in a drawable folder and use this single line code in button code to get all the properties of this xml file .
<Button
android:id="#+id/street_btn"
android:layout_width="wrap_content"
android:background="#drawable/layout_a" > <!-- your required code -->
</Button>
change this line:
button2.setBackgroundColor(Color.BLUE);
and try this:
button2.setBackgroundColor(Color.parseColor("#5AC8E2"));

Categories

Resources