Fix height of android EditText with a background - android

How can I get the button and the editText to have the same height? I want the EditText to have the height of the button View
<com.devmarvel.creditcardentry.library.CreditCardForm
android:id="#+id/credit_card_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dip_40"
android:layout_marginLeft="#dimen/dip_20"
android:layout_marginRight="#dimen/dip_20"
app:text_color="#color/textColor"
app:hint_text_color="#color/hintColor"
app:cursor_color="#color/hintColor"
app:include_helper="true"
app:include_zip="false"
app:input_background="#drawable/back_gray_border_round"
app:include_exp="true"
app:include_security="true"
app:card_number_hint="#string/cardhint" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dip_10"
android:layout_marginRight="#dimen/dip_20"
android:layout_marginLeft="#dimen/dip_20"
android:text="#string/save"
android:id="#+id/saveBtn"
android:layout_gravity="center_horizontal"
android:background="#drawable/rounded_corner_button"
android:textAllCaps="false"
android:textSize="#dimen/normal_font_size"
android:textColor="#color/white"
android:textStyle="bold" />
I am using a library defined editText, but it does not have any height attribute.

Add Both EditText and Button in RelativeLayout and use alginTop and alignBottom property on EditText.Have a look on code.I have hard code button size for illustration purpose.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="test"/>
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button"
android:layout_alignTop="#+id/button"
android:text="test2"/>

Related

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

Align TextView with EditText inside Relative layout

I saw some posts regarding alignment of TextView with EditText on Android canvas. But, I didn´t found answer which could help me in the right way. My desire is to have TextView and EditText in a following manner:
(TextView) (EditText) (TextView) (EditText)
(TextView) (EditText) (TextView) (EditText)
(TextView) (EditText) (TextView) (EditText)
(TextView) (EditText)
EditText width and height are predefined (values given in dp). Also, top and left margin of TextEdit is given. That means that I have to place EditText of certain size and to certain position on the screen. This is ok, and I achieved it using one RelativeLayout container.
But I should also put label(TextView) beside EditText in such a way that i don´t specify any value in dps, because textviews have to placed automatically with respect to EditText. For example
Street: [Main Street]
Street Number: [ 4 ]
I tried using Relative layout attributes:
layout_alignTop="#id/EditText"
layout_toLeftOf="#id/EditText"
But, TextView is not showed, because it is placed left of EditText but exactly on the left margin(top alignment works) and not "exactly" beside EditText (2dp or 3dp left from EditText) like I want.
I also tried other attributes but no result.
Is there some native android attribute for putting labels near EditText like i want?
Is there some attribute in Relative layout that can do job for me?
Is there way just to specify that TextView is 3 dp left from EditText?
Any other solutions?
Thanks
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/EditText1"
android:hint="Write..."
android:layout_width="80dp"
android:layout_height="22dp"
android:layout_marginTop="61dp"
android:layout_marginLeft="160dp"
android:background="#android:drawable/editbox_dropdown_light_frame"/>
<TextView
android:id="#+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="22dp"
android:text="Text"
android:layout_toLeftOf="#+id/EditText1"
android:layout_alignTop="#+id/EditText1"/>
<EditText
android:id="#+id/EditText2"
android:hint="Write..."
android:layout_width="200dp"
android:layout_height="22dp"
android:layout_marginTop="61dp"
android:layout_marginLeft="490dp"
android:background="#android:drawable/editbox_dropdown_light_frame"/>
<TextView
android:id="#+id/TextView2"
android:layout_width="wrap_content"
android:layout_height="22dp"
android:text="Text"
android:layout_alignTop="#+id/EditText2"
android:layout_toLeftOf="#+id/EditText2" />
</RelativeLayout>
You are using layout_margin left which is creating the problem , If your need is just this much why not use TableLayout
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_marginTop="61dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:id="#+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="22dp"
android:text="Text" />
<EditText
android:id="#+id/EditText1"
android:hint="Write..."
android:layout_width="80dp"
android:layout_height="22dp"
android:background="#android:drawable/editbox_dropdown_light_frame"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
>
<TextView
android:id="#+id/TextView2"
android:layout_width="wrap_content"
android:layout_height="22dp"
android:text="Text" />
<EditText
android:id="#+id/EditText2"
android:hint="Write..."
android:layout_width="80dp"
android:layout_height="22dp"
android:background="#android:drawable/editbox_dropdown_light_frame"/>
</LinearLayout>
</TableRow>
</TableLayout>
Just copy the whole TableRow tag and paste it as many row you want
**But If you still want relative layout here's the fix code for you ** just change margin value correctly as per your need
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="22dp"
android:text="Text"
android:layout_marginTop="57dp"
android:layout_marginLeft="80dp"
/>
<EditText
android:id="#+id/EditText1"
android:hint="Write..."
android:layout_width="80dp"
android:layout_height="22dp"
android:background="#android:drawable/editbox_dropdown_light_frame"
android:layout_alignTop="#+id/TextView1"
android:layout_toRightOf="#+id/TextView1"
android:layout_toEndOf="#+id/TextView1" />
<EditText
android:id="#+id/EditText2"
android:hint="Write..."
android:layout_width="80dp"
android:layout_height="22dp"
android:background="#android:drawable/editbox_dropdown_light_frame"
android:layout_alignTop="#+id/EditText1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="25dp"
android:layout_marginEnd="25dp" />
<TextView
android:id="#+id/TextView2"
android:layout_width="wrap_content"
android:layout_height="22dp"
android:text="Text"
android:layout_alignTop="#+id/EditText2"
android:layout_toLeftOf="#+id/EditText2"
android:layout_toStartOf="#+id/EditText2" />
Just arrange your XML, you can achieve what you want using padding.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingRight="16dp">

Edittext fields increases the size

In my android project, main_activity.xml is looks like:--
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal|top"
android:orientation="horizontal"
android:paddingBottom="8dp" >
<EditText
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:layout_weight="10"
android:gravity="center_horizontal|center_vertical"
android:inputType="text"
android:textAlignment="center"
android:textAppearance="#style/AppTheme"
android:textSize="20sp" />
<EditText
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="75"
android:gravity="center_horizontal|center_vertical"
android:inputType="text"
android:textAlignment="center"
android:textAppearance="#style/AppTheme"
android:textSize="20sp" />
<Button
android:id="#+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="10"
android:maxWidth="48dp"
android:minHeight="48dp"
android:minWidth="48dp"
android:text="OK"
android:textAlignment="center"
android:textAppearance="#style/AppTheme"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
this format is looks like:-- "Mr. Annonomus button(ok)"...Mr. is an edittext field,Annonomus is also a edittext field and a ok button..
It displays fine..But the problem is when I start writting into the second edittext field(means want to write "Annonomus" ) the edittext size is increasing and the ok button and another edittext field is decreasing in size..
How can i get that when I write any long name to edittext field the edittext field will remain same..
Use this code for your editText:
int px = editTextMessage.getWidth();
editTextMessage.setMaxWidth(px);
You're using android:layout_width="wrap_content" and android:layout_weight in all the components inside the linearlayout.
You must change the android:layout_width to match_parent or a fixed value and remove the android:layout_weight of them. Don't forget to remove the latter, otherwise it won't work.
Set android:maxLines="1" or android:singleLine="true" property in your EditText.

How to allow view to expand without pushing othe views off screen

I have a horizontal layout with two views in it: an EditText that will show a chosen path, and a "Browse" Button to the right of the EditText.
I want the EditText to expand as required, but not to the extent that it pushes the Button off the screen.
Here is my current xml:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/path_view">
<EditText android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/folder_edittext"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:hint="Folder"/>
<Button android:layout_height="wrap_content"
android:layout_width="60dp"
android:id="#+id/browse_button"
android:text="..."
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"/>
</LinearLayout>
I have tried all types of layout, I have tried using toLeftOf, toRightOf and everything I can think of, I think I now need help if I am to get this working.
You can achieve this using layout weights:
<LinearLayout
android:layout_width="match_content"
android:layout_height="wrap_content"
android:id="#+id/path_view">
<EditText android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:id="#+id/folder_edittext"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:hint="Folder"/>
<Button android:layout_height="wrap_content"
android:layout_width="60dp"
android:id="#+id/browse_button"
android:text="..."
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"/>
</LinearLayout>
This xml can be interpreted as saying: "let the EditText take up all the horizontal room that is not filled by other views" (in this case, the only other view is the Button).
Try with RelativeLayout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/path_view">
<EditText android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/folder_edittext"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:hint="Folder"
android:layout_toLeftOf="#+id/browse_button"
android:layout_alignParentLeft="true" />
<Button android:layout_height="wrap_content"
android:layout_width="60dp"
android:id="#+id/browse_button"
android:text="..."
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_alignParentRight="true" />
</RelativeLayout>

EditText not displaying entered text while setting ImageView as background of EditView and also while placing EditView over ImageView?

while i am placing EditText over an ImageView by using android:layout_alignTop property of EditText as below:-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/first_meddle_baar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/image_view"
android:src="#drawable/top_meddle_baar" />
<ImageView
android:id="#+id/search_baar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:contentDescription="#string/image_view"
android:src="#drawable/search_baar" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#id/search_baar" // #1
android:layout_margin="3dip"
android:hint="#string/search"
android:paddingLeft="30dip" />
or if i am placing an EditText over a TextView whose background i had set as an image as in the code below:-
<TextView
android:id="#+id/first_enter_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/first_center_back"
android:layout_margin="12dp"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:background="#drawable/enter_code_new"
android:text="#string/enter_client_code"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
<EditText
android:layout_width="70dp"
android:layout_height="35dp"
android:layout_alignTop="#id/first_enter_code" //#2
android:layout_marginLeft="210dip"
android:padding="15dp" />
In both the cases the text that is entered into EditText is not getting displayed ? why?
First of all you and give background image to any text box you need not to take this separately in image view. This will resolve your problem of showing edittext.
Just like:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#id/search_baar"
android:layout_margin="3dip"
android:background="#drawable/txt_view_bg"
android:paddingLeft="30dip" />

Categories

Resources