Android align drawable and text in EditText - android

i've an edittext :
<EditText android:id="#+id/chp"
android:layout_width="match_parent"
android:paddingLeft="10dp"
android:layout_height="340dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:gravity="top"
android:hint="#string/hintCommentaire"
android:textSize="25sp"
android:drawablePadding="10dp"
android:drawableLeft="#drawable/ic_comment"
android:layout_margin="5dp" />
The problem is, i've my hint at the top of the edittext, but the drawable stays in the center..
I tried with "drawableStart", but it's not working.
Someone to help me ?
EDIT : I want the text and drawable in the top(same line). (i want to keep the height of my edittext)

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/etMobile"
android:hint="Mobile"
android:fontFamily="sans-serif-normal"
android:singleLine="true"
android:textSize="15sp"
android:gravity="center|start"
android:textCursorDrawable="#null"
android:drawableStart="#drawable/ic_profile_bio"
android:drawablePadding="10dp"
app:met_floatingLabel="normal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
Set this Property android:gravity="center|start" in above EditText

Related

EditText height changes according to the text size

If I change the textsize in editext, the height of edittext changes accordingly. I want the size of edittext to be same and I want a smaller sized hint or text to be in it. What should I do? Following is the code and I am also posting the screenshot of this layout :
<RelativeLayout
android:id="#+id/normal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#4594e4"
android:orientation="horizontal">
<ImageView
android:id="#+id/set"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_marginTop="05dp"
android:padding="5dp"
android:src="#drawable/bari" />
<EditText
android:id="#+id/etOrigin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/_35sdp"
android:layout_marginTop="#dimen/_10sdp"
android:layout_toEndOf="#+id/set"
android:layout_toRightOf="#+id/set"
android:background="#5fa7f1"
android:inputType="textPersonName"
android:text="My Location"
android:textColor="#434343"
android:textIsSelectable="true" />
<EditText
android:id="#+id/etDestination"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/etOrigin"
android:layout_marginRight="#dimen/_35sdp"
android:layout_marginTop="#dimen/_5sdp"
android:layout_toEndOf="#+id/set"
android:layout_toRightOf="#+id/set"
android:background="#5fa7f1"
android:inputType="textPersonName"
android:onClick="wow"
android:text="Where to go ?"
android:textColor="#fff"
android:textColorHint="#fff" />
<ImageView
android:id="#+id/swipe"
android:layout_width="30dp"
android:layout_height="27dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginRight="#dimen/_5sdp"
android:background="#drawable/flip" />
</RelativeLayout>
Screenshot of the layout before changing:
Screenshot of layout after changing textsize:
Try using android:minHeight and android:maxHeight attributes :
<EditText
android:id="#+id/etUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_username"
android:imeOptions="actionNext"
android:inputType="textPersonName"
android:maxLines="1"
android:gravity="center_vertical"
android:minHeight="?actionBarSize"
android:maxHeight="?actionBarSize"
android:textColor="#color/textPrimary"
android:textSize="12sp"/>
<EditText
android:id="#+id/etUserEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_username"
android:imeOptions="actionNext"
android:inputType="textEmailAddress"
android:maxLines="1"
android:gravity="center_vertical"
android:minHeight="?actionBarSize"
android:maxHeight="?actionBarSize"
android:textColor="#color/textPrimary"
android:textSize="14sp"/>
Use padding for this :-
<EditText
android:id="#+id/etOrigin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:layout_marginRight="#dimen/_35sdp"
android:layout_marginTop="#dimen/_10sdp"
android:layout_toEndOf="#+id/set"
android:layout_toRightOf="#+id/set"
android:background="#5fa7f1"
android:inputType="textPersonName"
android:text="My Location"
android:textColor="#434343"
android:textIsSelectable="true" />
Give fix height to the layout android:layout_height="75dp"
<EditText
android:id="#+id/etOrigin"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_marginRight="#dimen/_35sdp"
android:layout_marginTop="#dimen/_10sdp"
android:layout_toEndOf="#+id/set"
android:layout_toRightOf="#+id/set"
android:background="#5fa7f1"
android:inputType="textPersonName"
android:text="My Location"
android:textColor="#434343"
android:textIsSelectable="true"/>
Here, try this third party library.
Your TextView height would be fixed but your textView size will be adjusted
dependencies {
compile 'me.grantland:autofittextview:0.2.+'
}
Enable any View extending TextView in code:
AutofitHelper.create(textView);
Enable any View extending TextView in XML:
<me.grantland.widget.AutofitLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
/>
</me.grantland.widget.AutofitLayout>
Use the built in Widget in code or XML:
<RootElement
xmlns:autofit="http://schemas.android.com/apk/res-auto"
...
<me.grantland.widget.AutofitTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLines="2"
android:textSize="40sp"
autofit:minTextSize="16sp"
/>
For more information, visit below link:
https://github.com/grantland/android-autofittextview
You can explicitly set the height and size of your EditText view as well as the text in it.
To set the heigh of the EditText simply use :
android:layout_height="<height_you_want>dp"
And to set height of the text in your EditText view :
android:textSize="<height_you_want>sp"
And you can simultaneously view the changes in the preview tab of AndroidStudio.
One Solution : include these two lines in your XML file
android:maxLines="1"
android:singleLine="true"
Using the EditText inside of a ConstraintLayout will fix this.
...
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="17">
<androidx.appcompat.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_percent="1"
android:textSize="24sp"
android:text="foo"
/>
</androidx.constraintlayout.widget.ConstraintLayout>

Image using drawableLeft does not fit inside the EditText field

I'm trying to make an image fit into the EditText itself using drawableLeft, without any padding or "whitespaces" around it.
This is what the image looks like for now
This is how it should look like
I tried increasing the size of the image itself but it made the EditText expand even more.
Here's the XML file:
<EditText
android:layout_marginTop="6dp"
android:id="#+id/login_password"
android:layout_width="315dp"
android:layout_height="wrap_content"
android:textSize="17dp"
android:textColor="#8d8e8f"
android:hint="Password"
android:drawableLeft="#drawable/sample"
android:singleLine="true"
android:inputType="textPassword"
android:layout_below="#+id/login_name"
android:layout_centerHorizontal="true" />
How can the image fit inside the EditText? Does it something to do about the image itself?
You can try the LinearLayout for it. Means in the LinearLayout , you should put the Editext's drawable image as the background , and than divide it in to three parts,1st part is for the password_image , 2nd one for your EditText and and 3rd last one for is your INVISIBLE View, which prevent , your EditText value to touch at end
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/EDITEXT_DRAWABLE"
android:gravity="center_vertical"
android:padding="5dp"
android:weightSum="2">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:gravity="left|center"
android:src="#drawable/sample" />
<EditText
android:id="#+id/password_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1.5"
android:background="#android:color/transparent"
android:ems="10"
android:gravity="center"
android:hint="********"
android:inputType="textPassword"
android:paddingLeft="-10dp"
android:singleLine="true"
android:imeOptions="actionDone"
android:textColor="#android:color/white"
android:textColorHint="#android:color/white" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|left"
android:layout_weight="0.1"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:src="#drawable/sample"
android:visibility="invisible" /> ///PUT THE VIEW INVIIBLE
</LinearLayout>
Just use a Fram layout it works well use a frame layout and inside it edit text and an image view is created set the background of image view to transperent and gravity to the right or to the left
<FrameLayout
android:layout_width="match_parent"
android:layout_height="25dp">
<EditText
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/border_round"
android:drawableLeft="#color/place_autocomplete_search_text"
android:drawableStart="#drawable/places_ic_search"
android:drawableRight="#drawable/arrowxml">
</EditText>
<ImageView
android:layout_width="25dp"
android:background="#android:color/transparent"
android:scaleType="centerCrop"
android:layout_gravity="right"
android:src="#drawable/searcharrow"
android:layout_height="25dp" />
use this...........
<EditText
android:id="#+id/user_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/editback"
android:drawableLeft="#drawable/useremail"
android:drawablePadding="10dp"
android:hint="#string/user_name"
android:padding="7dp"
android:singleLine="true"
android:textColor="#9291A1"
android:textSize="15sp" />

TextInputLayout EditText gravity not working

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"/>

How to fill parent with EditText in RelativeLayout

Basically, what I want is to have the EditText fill in the rest of the field. Since the first label is a wrap_content and the last item is just an imageview. I am not able to get the EditText to fill in the rest. Originally, I had it in a LinearLayou, but this wasn't working for me either.
Here's the layout so far:
<RelativeLayout
android:id="#+id/email_form"
android:layout_width="match_parent"
android:layout_height="#dimen/input_height"
android:layout_marginTop="#dimen/t_button_margin"
android:background="#drawable/form_edittext_background"
android:orientation="horizontal"
android:padding="#dimen/input_padding" >
<TextView
android:id="#+id/email_lbl"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:maxLines="2"
android:maxWidth="150dp"
android:layout_marginLeft="#dimen/input_label_margin_left"
android:text="adsadasdsa asd asdasd asdas dasd asd asdasda s"
android:layout_alignParentLeft="true"
android:textColor="#color/o_text"
android:textSize="#dimen/input_label_font" />
<EditText
android:id="#+id/email_txt"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#color/o_form_bg"
android:cursorVisible="true"
android:ems="10"
android:focusable="true"
android:gravity="right"
android:imeOptions="actionNext"
android:inputType="textEmailAddress"
android:text="aasdsd"
android:nextFocusDown="#+id/passwordTxt"
android:layout_toLeftOf="#+id/email_lbl"
android:layout_toRightOf="#+id/chk_email"
android:padding="1dp"
android:textColor="#color/dark"
android:layout_centerVertical="true"
android:textCursorDrawable="#null"
android:textSize="#dimen/o_font_input" />
<ImageView
android:id="#+id/chk_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="3dp"
android:contentDescription="#string/uplay"
android:src="#drawable/i_check2"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
It seems that you swapped toRightOf and toLeftOf
Try this :
<EditText
android:id="#+id/email_txt"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#color/o_form_bg"
android:cursorVisible="true"
android:ems="10"
android:focusable="true"
android:gravity="right"
android:imeOptions="actionNext"
android:inputType="textEmailAddress"
android:text="aasdsd"
android:nextFocusDown="#+id/passwordTxt"
android:layout_toLeftOf="#+id/chk_email"
android:layout_toRightOf="#+id/email_lbl"
android:padding="1dp"
android:textColor="#color/dark"
android:layout_centerVertical="true"
android:textCursorDrawable="#null"
android:textSize="#dimen/o_font_input" />
Have you tried this?
<EditText
android:layout_gravity="fill_horizontal"
... />
Note: fill_parent is deprecated and is a synonym for match_parent so it's not what you want here.
Have you tried using Layout_weight attribute.
set android:layout_weight = "0" for the other 2 views and android:layout_weight = "1" for the editText which should take all space

Raising text inside a textview

This is a simple question but its kicking my butt all around xml land.
All I want to do is to have the two edit text fields separated by a colon and to have the colon be centered like the following:
00:30
Right now when it displays the colon is slightly lower than the 00 and 30 of the edit text fields
I have tried using android:layout_marginBottom= "10dp" but that doesn't work. Any ideas? Thanks
Here is my code:
<EditText
android:id="#+id/edit_minute"
android:layout_width="wrap_content"
android:layout_height="85dp"
android:layout_below="#+id/text_minute"
android:layout_marginLeft="45sp"
android:inputType="number"
android:text="#string/edit_minute"
android:textColor="#0D4F8B"
android:textSize="70sp"
android:textStyle="bold" />
<TextView
android:id="#+id/text_colon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/edit_minute"
android:layout_below="#+id/text_minute"
android:textAlignment="center"
android:text="#string/text_colon"
android:textSize="70sp"
android:textStyle="bold" />
<EditText
android:id="#+id/edit_second"
android:layout_width="105dp"
android:layout_height="85dp"
android:layout_below="#+id/text_minute"
android:layout_toRightOf="#+id/text_colon"
android:inputType="number"
android:text="#string/edit_second"
android:textColor="#0D4F8B"
android:textSize="70sp"
android:textStyle="bold" />
Put Layout_height of Both edittext to wrapcontent without giving a specific height then the three views would be aligned.....
<EditText
android:id="#+id/edit_minute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text_minute"
android:layout_marginLeft="45sp"
android:inputType="number"
android:text="08"
android:textColor="#0D4F8B"
android:textSize="70sp"
android:textStyle="bold" />
<TextView
android:id="#+id/text_colon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/edit_minute"
android:layout_below="#+id/text_minute"
android:textAlignment="center"
android:text=":"
android:textSize="70sp"
android:textStyle="bold" />
<EditText
android:id="#+id/edit_second"
android:layout_width="105dp"
android:layout_height="wrap_content"
android:layout_below="#+id/text_minute"
android:layout_toRightOf="#+id/text_colon"
android:inputType="number"
android:text="30"
android:textColor="#0D4F8B"
android:textSize="70sp"
android:textStyle="bold" />
I would first put them all in a RelativeLayout, and place this layout under "text_minute".
Then in the following I centered all, and the colon's height is adjusted using the attributes layout_alignTop and layout_alignBottom that force the TextView to follow the first EditText height:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/text_minute" >
<EditText
android:id="#+id/edit_minute"
android:layout_width="wrap_content"
android:layout_height="85dp"
android:layout_toLeftOf="#+id/text_colon"
android:inputType="number"
android:text="#string/edit_minute"
android:textColor="#0D4F8B"
android:textSize="70sp"
android:textStyle="bold" />
<TextView
android:id="#+id/text_colon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_alignTop="#+id/edit_minute"
android:layout_alignBottom="#+id/edit_minute"
android:gravity="center_horizontal"
android:text="#string/text_colon"
android:textSize="70sp"
android:textStyle="bold" />
<EditText
android:id="#+id/edit_second"
android:layout_width="wrap_content"
android:layout_height="85dp"
android:layout_toRightOf="#+id/text_colon"
android:inputType="number"
android:text="#string/edit_second"
android:textColor="#0D4F8B"
android:textSize="70sp"
android:textStyle="bold" />
</RelativeLayout>
You can place your EditText TextView and EditText all in a LinearLayout and use android:baselineAligned and android:baselineAlignedChildIndex.
If you require your parent view is a RelativeLayout then you can use android:layout_alignBaseline on each of the child views.

Categories

Resources