0

I am quite new in xamarin and I am trying to add a click events to my buttons. So here are my 4 buttons:

<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
                    <Button Text="Page 1" TextColor="#EEF2FF" FontSize="20" HorizontalOptions="Center" BackgroundColor="#FF5959" />
                </StackLayout>
                <StackLayout Grid.Column="1" HorizontalOptions="Center" VerticalOptions="Center">
                    <Button Text="Page 2" Clicked="Handle_Page" TextColor="#676FA3" FontSize="20" HorizontalOptions="Center" BackgroundColor="#EEF2FF"/>
                </StackLayout>
                <StackLayout Grid.Column="2" HorizontalOptions="Center" VerticalOptions="Center">
                    <Button Text="Page 3" TextColor="#676FA3" FontSize="20" HorizontalOptions="Center" BackgroundColor="#EEF2FF"/>
                </StackLayout>
                <StackLayout Grid.Column="3" HorizontalOptions="Center" VerticalOptions="Center">
                    <Button Text="Page 4" TextColor="#676FA3" FontSize="20" HorizontalOptions="Center" BackgroundColor="#EEF2FF"/>
                </StackLayout>

So the colour of the buttons are #EEF2FF but when I click on the button I want it to be changed to #FF5959. But if there is another button clicked already, then it should also change to the main colour, #EEF2FF. So we can cay synchronized click event.

I am quite new, so any help would be appreciated.

2 Answers 2

1

add a clicked handler

<Button Clicked="ButtonClicked" ... />

then in the handler

protected void ButtonClicked(object sender, EventArgs args)
{
  // set the other buttons back to the base color
  // you will need to assign each button an x:Name in xaml
  btn1.BackgroundColor = Color.FromHex("EEF2FF");
  ...
  btn10.BackgroundColor = Color.FromHex("EEF2FF");

  // then change the color of only the selected button
  var btn = (Button)sender;
  btn.BackgroundColor = Color.FromHex("FF5959");
} 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer but when I clicked one button, the other button stays clicked also, how can I fix it?
see my last edit
Sorry I am quite new in xamarin but how can I give a name to the button?
<Button x:Name="btn1" ... />
1
protected override void OnCreate(Bundle savedInstanceState)
{   
    ScreenClickButton.Click += ChangeColorToRed;
}

private void ChangeColorToRed(object sender, EventArgs e)
{
    ScreenClickButton.SetBackgroundColor(color: Color.Red);
}

1 Comment

See "Explaining entirely code-based answers". While this might be technically correct, it doesn't explain why it solves the problem or should be the selected answer. We should educate along with helping solve the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.