Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I implement non-rectangular shapes for buttons in android

Tags:

android

hi i have to realize this layout . it has this layout. inactive buttons

I could try to use the icons as imagebuttons but the active state of a button is somewhat like this one !

one button has been pressed

How should i proceed with this ?

like image 953
dmSherazi Avatar asked Oct 20 '13 08:10

dmSherazi


People also ask

Can we change the shape of button in Android Studio?

We can set custom shapes on our button using the xml tag <shape> . These xml files are created in the drawable folder too. shape can be used inside selectors . The shape can be set to rectangle (default), oval , ring , line .

How many types of buttons are there in Android?

In android, we have a different type of buttons available to use based on our requirements, those are ImageButton, ToggleButton, RadioButton. In android, we can create a Button control in two ways either in the XML layout file or create it in the Activity file programmatically.


1 Answers

You should use selector as follows:

  1. Prepare 2 images for button states, and put it into res/drawable folder.

    button_normal_green.png – Default image button.

    button_pressed_yellow.png – Display when button is pressed.

  2. Now, create a new XML file in “res/drawable/” folder, in whatever name you want, in this case, we just give a name as “new_button.xml“. This file defined which button state is belong to which image.

    <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
         <item android:drawable="@drawable/button_pressed_yellow" android:state_pressed="true" />
    
         <item android:drawable="@drawable/button_normal_green" />
      </selector>
    

3.set background to button

<ImageButton
    android:id="@+id/imageButtonSelector"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/new_button" />

Have a look at Complete Example

like image 164
Ketan Ahir Avatar answered Sep 22 '22 16:09

Ketan Ahir