Best practice for creating a button in an Android app?

I just started creating Android apps using Eclipse and ADT.
I have basic functionality working in a demo application, and I would like to create a button that looks like a search field (there is no built-in search function, I just want the user to click it and start a new activity).

http://www.roowilliams.com/button.png

My question is: what works best for creating a button similar to the above (ignoring the 1px dark gray stroke around the outside)?

I got to

  • Create a button in activity_main.xml

    <Button
        android:id="@+id/inputSearch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:hint="@string/hint_search"
        android:background="@drawable/input_search"
        style="@style/input_search"
        android:onClick="findProducts" >
    </Button>
    
  • Create some styles

    <style name="input_search" parent="@android:style/TextAppearance.Medium">
        <item name="android:textColor">@color/light_grey</item>
        <item name="android:background">@color/white</item>
    </style>
    
  • Create input_search_background.xml that has a light gray stroke, rounded corners

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <stroke 
            android:width="@dimen/stroke_width" android:color="@color/light_grey" /> 
        <corners 
            android:radius="@dimen/corner_radius" />
        <solid 
            android:color="@color/white" />
    </shape>
    
  • Create input_search.xml for the states.

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

?
, ?

, .

,

+4
1

9.

sdk :

Windows ( ),

C:\Program Files\adt-bundle-windows-x86_64-20130522\sdk\platforms\android-8\data\res\drawable-mdpi

:

enter image description here

( ) , . ( TextView, ) .

, xml-.

+2

Source: https://habr.com/ru/post/1526732/


All Articles