Button states

Peter.3478Peter.3478 USMember
edited December 2014 in Xamarin.Android

My layout has two buttons. By default they have background "A". When one button is clicked/pressed, the background of that button should change to background "B" and the other button would have background "C". And the other way around.

I tried to set this with selector:

<?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/buttonClicked" /> <item android:state_selected="true" android:drawable="@drawable/buttonClicked" /> <item android:drawable="@drawable/abutton" /> </selector>

and then in MainActivity I set:

Button btnTrue = FindViewById<Button>(Resource.Id.buttonTrue); btnTrue.Selected = true;

btnTrue.Pressed = true; has no effect.

If I add <item android:state_selected="false" android:drawable="@drawable/buttonClicked" /> then this is default background.

I tried to use android:state_focused true and false but it has no effect.

And focused state can not be set in code like btnTrue.Focused = true;

So what would be the solution to my problem?

Posts

  • Peter.3478Peter.3478 USMember
    edited December 2014

    Finally I got it working.

    Selector:
    <?xml version="1.0" encoding="UTF-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@drawable/buttonClicked" /> <item android:state_pressed="true" android:state_selected="false" android:drawable="@drawable/buttonNotClicked" /> <item android:drawable="@drawable/abutton" /> </selector>

    and in button click events:

    btnTrue.Click += delegate(object sender, EventArgs e) {
                        btnTrue.Selected = true;
                        btnFalse.Selected = false;
                        btnFalse.Pressed = true;
                    };
    
    btnFalse.Click += delegate(object sender, EventArgs e) {
                        btnFalse.Selected = true;
                        btnTrue.Selected = false;
                        btnTrue.Pressed = true;
                    };
    

    Xamarin is great, but the community is its achilles heel... Hard to get an answer for specific problems.

  • CheesebaronCheesebaron mod DKInsider, University mod

    You expect an answer within less than one day during the holiday season?

    I don't see that as an Achilles heel...

    I will not call you names here, but you deserve some.

  • Peter.3478Peter.3478 USMember

    You can call me whatever you want. :) I think it would be childish, but if it would make you feel better, go ahead. It wouldn't change anything..

    Community (quantity) is in the cons section of Xamarin. It is the same with all the 'custom' platforms. You benefit some (cross-platform) and you loose some (native code community). It is a fact. Nothing new.

    But I will try to add my part to Xamarin community.

  • Peter.3478Peter.3478 USMember

    Cheesebaron you can help instead of calling me names.

    With this solution that I posted I get:

    default bg "A" ob both buttons - correct
    when button pressed down, it gets bg "C" (not correct) and the when released it gets bg "B" (correct)
    when first button is released, the second gets bg "C" - correct

    So do you have any suggestion how would I solve that when button pressed down it would have bg "B"?

  • CheesebaronCheesebaron mod DKInsider, University mod

    Not sure why you would do that, as you are pressing the button with your finger and it will cover the lettering ;)

    As far as I know, it is "only" possible to define the background Drawable of the Button, which could be a StateListDrawable reacting to the different sates.

    So similar to what you have already done, just with more states:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:state_enabled="false"
            android:drawable="@drawable/button_disabled" />
        <item
            android:state_pressed="true"
            android:state_enabled="true"
            android:drawable="@drawable/button_pressed" />
        <item
            android:state_focused="true"
            android:state_enabled="true"
            android:drawable="@drawable/button_focused" />
        <item
            android:state_enabled="true"
            android:drawable="@drawable/button_enabled" />
    </selector>
    

    This is more or less the default template one should use when defining new styles for a button. So, potentially your Drawable for the pressed one, could contain a B and the one for enabled and focused could contain a C. However, I would probably do that in C# code instead detecting the state of the Button there and change the text accordingly.

    So simply listening to the FocusChange event of the Button and reacting accordingly.

  • Peter.3478Peter.3478 USMember
    edited December 2014

    I have two big buttons, so the changes of the background are visible (when pressed too).

    With one button it is easy. I just have to set different backgrounds in selector for pressed, focused and default.

    But I would like to achieve this with two buttons:

    default state:
            btn 1: blue background
            btn 2: blue background
    btn 1 pressed state:
            btn 1: green background
            btn 2: blue background
    btn 1 released state:
            btn 1: green background
            btn 2: gray background
    

    OR

    default state:
            btn 1: blue background
            btn 2: blue background
    btn 2 pressed state:
            btn 1: blue background
            btn 2: green background
    btn 2 released state:
            btn 1: gray background
            btn 2: green background
    

    I do not need to go further, because it is a quiz game and with one click the view changes.

    I saw this topic and I can not make it work. Because I think I can only achieve what I want by custom states.

  • Peter.3478Peter.3478 USMember
    edited December 2014

    FINALY! :)

    Selector:

    <?xml version="1.0" encoding="UTF-8" ?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:state_pressed="false" android:state_focused="true" android:drawable="@drawable/buttonClicked" />
    <item android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/buttonClicked" />
    <item android:state_selected="true" android:state_pressed="false" android:state_focused="false" android:drawable="@drawable/buttonNotClicked" />
    <item android:drawable="@drawable/abutton" />
    </selector>
    

    Activity:

    btnTrue.Click += delegate(object sender, EventArgs e) {
                    btnTrue.Selected = true;
                    btnTrue.RequestFocus();
                    btnFalse.Selected = true;
                    btnFalse.Pressed = false;
                };
    
    btnFalse.Click += delegate(object sender, EventArgs e) {
                    btnFalse.Selected = true;
                    btnFalse.RequestFocus();
                    btnTrue.Selected = true;
                    btnTrue.Pressed = false;
                };
    

    And in layout xml you have to set android:focusableInTouchMode="true" for a button (view).

Sign In or Register to comment.