I want to align the drawable and hint at center in EditText. For that I am having this piece of code,
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/relative_layout"
android:layout_margin="5dp" >
<EditText
android:id="#+id/edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Hint"
android:background="#null"
android:gravity="center"
android:textColor="#android:color/black"
android:textColorHint="#color/black"
android:textSize="14sp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:drawableLeft="#android:drawable/icon"
android:drawableStart="#android:drawable/icon" />
</RelativeLayout>
Now as soon as user starts typing, the drawable should move to extreme left of EditText. How to achieve this?
Use android:layout_gravity="center" for your EditText. This will help you setting hint in center.
and for drawable this is hack. Set your drawableTop for editText and give some paddingTop to the view.
Related
I have textfield - text view in relative layout with rounded rectangle. It looks fine until I try to change textview to edit text. After it, text of EditText view is cutted of. Well, I try to change height to wrap_content, it fixes the problem of cutting, but the rounded rectangle becomes much higher than with textView although EditText height seems like only half of the rectangle outside. How can I remove these spaces below and above the real visible EditText? Any manipulations with margins/paddings don't help me.
EditText with wrap_content height:
EditText with 30dp height:
TextView with 30dp height:
<RelativeLayout
android:id="#+id/token_input_layout_outside"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/top_divider"
android:gravity="center_horizontal"
android:paddingVertical="5dp"
app:layout_constraintStart_toEndOf="#id/top_divider"
app:layout_constraintTop_toBottomOf="#id/top_divider">
<RelativeLayout
android:id="#+id/token_input_layout"
android:layout_width="#dimen/counter_monitor_width"
android:layout_height="30dp"
android:background="#drawable/counter_background_3"
android:gravity="center">
<EditText
android:gravity="center_horizontal"
android:id="#+id/token_input"
android:layout_width="85sp"
android:layout_height="match_parent"
android:maxLength="5"
android:singleLine="true"
android:textSize="23sp"
android:backgroundTint="#color/white"
android:textColor="#color/white"
android:hint="Input"
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz">
</EditText>
</RelativeLayout>
</RelativeLayout>
30dp is a small height for your textSize; so you can do it in two different ways:
make edit text wrap_content:
<RelativeLayout
android:id="#+id/token_input_layout"
android:layout_width="#dimen/counter_monitor_width"
android:layout_height="wrap_content"
android:background="#drawable/counter_background_3"
android:gravity="center">
<EditText
android:gravity="center_horizontal"
android:id="#+id/token_input"
android:layout_width="85sp"
android:layout_height="wrap_content"
android:maxLength="5"
android:singleLine="true"
android:textSize="23sp"
android:backgroundTint="#color/white"
android:textColor="#color/white"
android:hint="Input"
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz">
</EditText>
</RelativeLayout>
change height size:
<RelativeLayout
android:id="#+id/token_input_layout"
android:layout_width="#dimen/counter_monitor_width"
android:layout_height="wrap_content"
android:background="#drawable/counter_background_3"
android:gravity="center">
<EditText
android:gravity="center_horizontal"
android:id="#+id/token_input"
android:layout_width="85sp"
android:layout_height="30dp"
android:maxLength="5"
android:singleLine="true"
android:textSize="23sp"
android:backgroundTint="#color/white"
android:textColor="#color/white"
android:hint="Input"
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz">
</EditText>
</RelativeLayout>
I have a LinearLayout horizontal and have an EditText center_horizontal and everything is ok but when I try to add a TextView to its left side (of the EditText) the EditText moves to the right because both control TextView and EditText get centered. My problem is that I want that the EditText keeps in the center and the TextView to its left without moving the center position of the EditText. this is my code without the TextView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtTituloFechaAgenda"
android:orientation="horizontal"
android:weightSum="2"
android:id="#+id/ll1agenda"
android:background="#color/azul"
android:gravity="center_horizontal"
>
<EditText
android:inputType="date"
android:ems="10"
android:layout_alignParentTop="true"
android:id="#+id/etFechaAgenda"
style="#style/textoETFecha"
android:background="#drawable/style_edit_text1"
android:layout_height="37dp"
android:gravity="center_horizontal"
android:layout_width="150dp" />
</LinearLayout>
up to there the EditText is centered ok. next I added the TextView:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtTituloFechaAgenda"
android:orientation="horizontal"
android:weightSum="2"
android:id="#+id/ll1agenda"
android:background="#color/azul"
android:gravity="center_horizontal"
>
<TextView
android:text="Fecha: "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
style="#style/textoTitulosBlanco"
android:id="#+id/txtFechaAgenda" />
<EditText
android:inputType="date"
android:ems="10"
android:layout_alignParentTop="true"
android:id="#+id/etFechaAgenda"
style="#style/textoETFecha"
android:background="#drawable/style_edit_text1"
android:layout_height="37dp"
android:gravity="center_horizontal"
android:layout_width="150dp" />
</LinearLayout>
so when I add the TextView both elements are centered and my EditText moves to the right and lose the center position. I hope you can help me. thanks in advance...
One possible solution to your problem could be, (what I understood from the question) to add a third view on right side of edittext and set it's visibility to invisible. (So that it doesn't show itself but takes the space)
These do not work in Linear Layout. They work in Relative Layout
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
Use layout_weight to arrange editText and TextView
<TextView
android:text="Fecha: "
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
style="#style/textoTitulosBlanco"
android:id="#+id/txtFechaAgenda" />
<EditText
android:inputType="date"
android:ems="10"
android:id="#+id/etFechaAgenda"
style="#style/textoETFecha"
android:background="#drawable/style_edit_text1"
android:layout_height="37dp"
android:layout_width="0dp"
android:layout_weight="2"/>
android:layout_centerHorizontal="true"
Make center horizontal true for the edit text using Relative Layout. Try bellow code segment.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:padding="4dp"
android:text="Fecha:" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:padding="4dp"
android:text="Edit Text" />
</RelativeLayout>
I've TextView. I need to make it clickable, white and with "selectable effect", besides I need it to have a rectangular white border, to fake a button borders:
<TextView
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:background="#drawable/white_empty_rectangle"
android:clickable="true"
android:padding="10dp"
android:text="Test"
android:textColor="#color/white" />
I can't set two background properties...how can I somehow "intersect" the two properties?
EDIT: I only need the borders of the rectangle, so it is "empty", I should see the background color behind the TextView.
Check this answer https://stackoverflow.com/a/5295522/4848308 you should use background selector.
This one to achieve border line background https://stackoverflow.com/a/3496310/4848308
I hope it helps!
What about this?
<FrameLayout
android:id="#+id/frame_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:clickable="true">
<TextView
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/rectangle_empty_white"
android:padding="8dp"
android:text="#string/test"
android:textAllCaps="true"
android:textColor="#color/white"
android:textSize="14sp" />
</FrameLayout>
My EditText hint seems to be misaligned with actual EditText. The problem is when I try to resize it to be smaller or larger, the hint stays in the same position but only the size of the box changes. I have an image to show how it should look because I have a Button that has its text aligned like so:
As you can see, the hint floats sightly above the bottom of the end of the EditText box. If I try todecrease the size of the EditText, the placeholder stays in the same position but the size of the box, if decreased, simply covers the hint and the hint never moves. If anything, I would want the text to be aligned to the send Button if possible.
Here is my code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layout_comments">
<LinearLayout
android:id="#+id/send_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<com.cengalabs.flatui.views.FlatEditText
android:fontFamily="sans-serif"
android:id="#+id/write_comment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:paddingLeft="10dp"
android:gravity="bottom|left"
android:textSize="16sp"
android:cursorVisible="false"
android:hint="Comment back!"
android:inputType="textMultiLine"
flatui:fl_fieldStyle="fl_box"
android:scrollHorizontally="false" />
<com.cengalabs.flatui.views.FlatButton
android:fontFamily="sans-serif"
android:id="#+id/send_comment"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="5dp"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="center"
android:text="send"
flatui:theme="#array/sea"
flatui:fl_textAppearance="fl_light"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:scrollbars="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/send_message"
android:id="#+id/view_comments">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
Any help would be appreciated, thanks!
Change this
android:paddingLeft="10dp"
to
android:padding="10dp"
I have started using TextInputLayout. I want the hint of the EditText to align in the center. But for for some reason the hint isn't getting aligned to the center. However, once I start typing in the EditText field, the text is tetting aligned to the centre. Please help me with the proper alignment. I want my hint to be center aligned as well.
<android.support.design.widget.TextInputLayout
android:id="#+id/dobLyt"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/dob"
style="#style/MyEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:hint="Date of Birth"
android:inputType="number"
android:textColor="#333333"
android:textSize="15dp" />
</android.support.design.widget.TextInputLayout>
use this
<EditText
android:id="#+id/et"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:ems="10"
android:gravity="center_vertical|center_horizontal"
android:inputType="numberDecimal"/>