Forum Xamarin Xamarin.Android

How apply styles for xamarin.android controls ?

GupthaGuptha USMember ✭✭

Hi,
I'm new to Xamarin.Android. Just started with login screen for my project. I have added two EditText and a Button to my screen. Here I want to add styles for my button and I want to display those controls in center of the screen.
Could you please explain me how to add a styles for controls like button shape, color and alignment of controls on screen.

Thank you.

Best Answer

  • BerayBentesenBerayBentesen TRUniversity ✭✭✭✭
    Accepted Answer

    @SKGuptha You can customize controls in two ways in Android, with Theme and custom xml's like shape for example.

    Let's say you want to customize your button with rounded corners and custom background color, including transparent.

    You need to create a new xml file in drawable folder and paste following code.

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="rectangle">
      <gradient android:startColor="#ececec" 
      android:endColor="#ececec" />
    <corners android:radius="2dp" />
    <stroke android:width="1px" android:color="#ccc" />
    </shape>
    

    To apply that shape as a background to button, you need to set background to your button like this ;

     android:background="@drawable/buttonBackgroundGray"
    

    If you want to customize objects like TabLayout, ProgressBar etc. you need to modify your theme. It's a completely different process that I can not summarize here but I'm pasting some link that may help

    1-) Google documentation

    2-) Detailed post about theming

    3-) Example theme for 5.0 + devices including TabLayout

    So, you must research for how Android works and it will be completely same with Xamarin Android. Good Luck.

Answers

  • BerayBentesenBerayBentesen TRUniversity ✭✭✭✭
    Accepted Answer

    @SKGuptha You can customize controls in two ways in Android, with Theme and custom xml's like shape for example.

    Let's say you want to customize your button with rounded corners and custom background color, including transparent.

    You need to create a new xml file in drawable folder and paste following code.

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="rectangle">
      <gradient android:startColor="#ececec" 
      android:endColor="#ececec" />
    <corners android:radius="2dp" />
    <stroke android:width="1px" android:color="#ccc" />
    </shape>
    

    To apply that shape as a background to button, you need to set background to your button like this ;

     android:background="@drawable/buttonBackgroundGray"
    

    If you want to customize objects like TabLayout, ProgressBar etc. you need to modify your theme. It's a completely different process that I can not summarize here but I'm pasting some link that may help

    1-) Google documentation

    2-) Detailed post about theming

    3-) Example theme for 5.0 + devices including TabLayout

    So, you must research for how Android works and it will be completely same with Xamarin Android. Good Luck.

  • GupthaGuptha USMember ✭✭

    Thank you @BerayBentesen for your answer, I'll try with code and I will let you know if I have any issues.

Sign In or Register to comment.