How to fill parent with EditText in RelativeLayout - android

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

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>

Edit text overlaps each other

I am creating an android application in which i am collecting user's some messages.But i am getting some issues such as following..
I have added 3 edit text in alert dialog..the third edit text box having multi line textbox so whenever i am adding multiple lines its getting overlapped to above two edit text
here is the snapshot of the error
Here Is Layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<ScrollView
android:layout_width="245dp"
android:layout_height="245dp"
android:layout_weight="0.40"
android:layout_gravity="center_horizontal">
<RelativeLayout
android:layout_width="245dp"
android:layout_height="200dp">
<EditText
android:layout_width="235dp"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/editText6"
android:layout_gravity="center_horizontal"
android:hint="Your Email Id"
android:layout_alignBaseline="#+id/editText5"
android:layout_alignBottom="#+id/editText5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="240dp"
android:layout_height="wrap_content"
android:id="#+id/editText4"
android:layout_gravity="center_horizontal"
android:hint="Title"
android:singleLine="true"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="236dp"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/editText5"
android:hint="Description"
android:layout_gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" /></RelativeLayout></ScrollView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="#+id/button10"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Reason for your issue is android:inputType="textMultiLine" pushes content when there is a new line!
You can use android:maxLines="1" for your EditText [any way this will be useful for you since you have used android:singleLine="true" with is kind of dipricated ]
Ps: Hard coding android:maxLength will not solve your issue since screens can be in different sizes which makes you hard to decide how many lines you keep!
Also you can use android:maxHeight="xdp" for the editText view, then the limit won't exeed!
You should use
android:maxLength="4"
row numbers you want to fix for your edit text
Use LinearLayout with vertical orientation instead of RelativeLayout.
If u want to use RelativeLayout, then set layout_below attribute of RelativeLayout.
You need to set android:maxLines="4". Set your max lines to solve this issue.
<EditText
android:id="#+id/editText4"
android:maxLines="4"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:hint="Title"
android:layout_alignParentStart="true" />
You can use android:layout_below and android:layout_marginTop in your EditText views and keep distance between your views.
Example :
<EditText
android:layout_width="236dp"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/editText5"
android:hint="Description"
android:layout_gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/editText4"
android:layout_marginTop="20dp"/>

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

Android align drawable and text in EditText

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

Limit the text in TextView

I have a textview Which Contains some text changes dynamically within the UI like below
layout:
<TableRow android:weightSum="2" >
<ImageButton
android:id="#+id/archive"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="90dp"
android:background="#drawable/archive" />
<ImageButton
android:id="#+id/facebook"
android:layout_width="-200dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="#drawable/facebook" />
</TableRow>
<TableRow android:layout_marginTop="2dp" >
<TextView
android:id="#+id/titlename1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="4dp"
android:text="#string/nowplaying"
android:textColor="#000000"
android:textSize="12sp"
android:textStyle="bold|italic" />
<TextView
android:id="#+id/titlename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="4dp"
android:layout_weight="1"
android:ellipsize="none"
android:gravity="right"
android:maxEms="25"
android:maxLines="1"
android:scrollHorizontally="false"
android:textColor="#000000"
android:textSize="11dp" />
</TableRow>
<TableRow android:layout_marginTop="2dp" >
<FrameLayout
android:id="#+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="0dp"
android:orientation="vertical" >
<ImageButton
android:id="#+id/playbtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/play" />
<ImageButton
android:id="#+id/pausebtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/pause" />
</FrameLayout>
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="500dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/mainlayout"
android:layout_marginLeft="-203dp"
android:layout_marginRight="0dp" />
</TableRow>
My Problem is that when i execute my Program,my layout output:
Imagebutton1 ImageButton2
NowPlaying: SongDetails
Play/Pause SeekBar
Problem is If my SongDetails text lenght Contains 2 or 3 lines My layout is getting Disturbed?Could any one Suggest?My Requirement is to limit the text like
Song Detailssss etc......in a Same Row
Simply use android:singleLine="true", and android:ellipsize="end". So You need to update TextView code as
<TextView
.....
....
android:ellipsize="end"
android:singleLine="true" />
Refrences :
android:singleLine
android:ellipsize
try to use
android:singleLine="true"
in your textview
also you can set marquee.
Note that this marquee will work if your text line is longer then your screen size only.
Try setting android:ellipsize paremeter to "end"
Like this
android:ellipsize="end"
Try setting android:ellipsize paremeter to "end" if you want to trim the content at the end.
android:ellipsize="end"
Just remove wrap_content. That should work(haven't tried it myself recently, but I used it a couple of months ago).
Otherwise, you could cut the text manually in Java, and cut it at the first newline found.

Categories

Resources