when selected add border shape for textView and editText ,

Pouria Hemati

I want to create a border shape for TextView and EditText, and show it when a view is selected.

Just like in this picture.

example

Son Truong

You should use drawable selector to achieve your UI.

First create a background_edit_text_default.xml which is the background of EditText when it is not selected by users.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="1dp"
        android:color="#333D46" />

    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />
</shape>

Then create a background_edit_text_selected.xml which is the background of EditText when it is selected by users.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="1dp"
        android:color="#EDB90E" />

    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />
</shape>

Next create a background_edit_text.xml which will be used as background of the EditText.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/background_edit_text_default" android:state_focused="false" />
    <item android:drawable="@drawable/background_edit_text_selected" android:state_focused="true" />

</selector>

Finally set background_edit_text.xml as background of your EditText in your layout file, such as activity_main.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/conteiner"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_edit_text" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="10dp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_edit_text" />

</LinearLayout>

You're done and no need to add anything in code.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

NullPointerException when textview selected

分類Dev

TextView - setCustomSelectionActionModeCallback how create ActionMode.Callback for when the text is selected to many TextView

分類Dev

How to create a TextView with default shape as square and auto-adjustable height when content cannot fit into?

分類Dev

Change Size of EditText bottom border

分類Dev

CSS nav link styling: remove styling of other element & remove double border when hovered on selected

分類Dev

Add input fields in form when dropdown option selected

分類Dev

Want to Add Dialog(Another Activity) when menu item selected

分類Dev

How do I add class to label when radio button is selected?

分類Dev

Wave (or shape?) with border on CSS3

分類Dev

Align shape to center of textview on Relative layout

分類Dev

How add border css

分類Dev

Add border to the tooltip as a whole

分類Dev

Add border to video (FFMPEG)

分類Dev

Add EditText dynamically in Android

分類Dev

TextView、EditText、Java正規表現

分類Dev

EditTextとTextViewを比較する

分類Dev

Add GeneralPath shape in XYShapeAnnotation of JFreechart

分類Dev

Update Edittext when other Edittext change

分類Dev

Android add border image to ImageView

分類Dev

How to add shadow only for the border?

分類Dev

Woocommerce - Add border image to products

分類Dev

draw outer and inner border around any canvas shape

分類Dev

how to make full rounded edittext using shape material component?

分類Dev

How to set line as a background using xml (shape) below to EditText?

分類Dev

Android LollipopでEditText with borderを使用する方法

分類Dev

Android LollipopでEditText with borderを使用する方法

分類Dev

TextViewとEditTextを切り替える

分類Dev

TextViewの下にEditTextを作成します

分類Dev

TextViewをEditTextの上に揃えます

Related 関連記事

  1. 1

    NullPointerException when textview selected

  2. 2

    TextView - setCustomSelectionActionModeCallback how create ActionMode.Callback for when the text is selected to many TextView

  3. 3

    How to create a TextView with default shape as square and auto-adjustable height when content cannot fit into?

  4. 4

    Change Size of EditText bottom border

  5. 5

    CSS nav link styling: remove styling of other element & remove double border when hovered on selected

  6. 6

    Add input fields in form when dropdown option selected

  7. 7

    Want to Add Dialog(Another Activity) when menu item selected

  8. 8

    How do I add class to label when radio button is selected?

  9. 9

    Wave (or shape?) with border on CSS3

  10. 10

    Align shape to center of textview on Relative layout

  11. 11

    How add border css

  12. 12

    Add border to the tooltip as a whole

  13. 13

    Add border to video (FFMPEG)

  14. 14

    Add EditText dynamically in Android

  15. 15

    TextView、EditText、Java正規表現

  16. 16

    EditTextとTextViewを比較する

  17. 17

    Add GeneralPath shape in XYShapeAnnotation of JFreechart

  18. 18

    Update Edittext when other Edittext change

  19. 19

    Android add border image to ImageView

  20. 20

    How to add shadow only for the border?

  21. 21

    Woocommerce - Add border image to products

  22. 22

    draw outer and inner border around any canvas shape

  23. 23

    how to make full rounded edittext using shape material component?

  24. 24

    How to set line as a background using xml (shape) below to EditText?

  25. 25

    Android LollipopでEditText with borderを使用する方法

  26. 26

    Android LollipopでEditText with borderを使用する方法

  27. 27

    TextViewとEditTextを切り替える

  28. 28

    TextViewの下にEditTextを作成します

  29. 29

    TextViewをEditTextの上に揃えます

ホットタグ

アーカイブ