Edittext fields increases the size - android

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.

Related

TextInputEditText, how can align hint to the top margin

I have some trouble with a TextInputEditText and his hint message if I use the last material library version com.google.android.material:material:1.2.0-alpha02 .. I set 5 lines scrollable textInputEditText and I want to show the hint message align to top margin of that text input. On layout editor appears in the correct position, but when I run the app the position is vertical centered. Why ?
Instead i'm not encounter this problem with com.google.android.material:material:1.1.0-beta02
This is my layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="#+id/sendMailContainer"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/popupTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="#string/sendBusinessCard"
android:textColor="#android:color/black"
android:textSize="22sp" />
<TextView
android:id="#+id/subTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/sendBusinessCard"
android:textColor="#android:color/black"
android:textSize="18sp"
android:layout_below="#+id/popupTitle"
android:layout_marginEnd="40dp"
android:layout_marginStart="40dp"
android:layout_marginTop="20dp"/>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/noteMessageContainer"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/subTitle"
android:layout_marginEnd="40dp"
android:layout_marginStart="40dp"
android:layout_marginTop="10dp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/noteMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/emailText"
android:inputType="text|textMultiLine|textCapSentences"
android:textColor="#android:color/black"
android:textColorHint="#color/gray9B"
android:textSize="16sp"
android:isScrollContainer="true"
android:scrollbars="vertical"
android:textIsSelectable="true"
android:minLines="5"
android:maxLines="5"
android:gravity="top"/>
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="#+id/notePopupLeftButton"
android:layout_width="180dp"
android:layout_height="35dp"
android:layout_alignParentStart="true"
android:layout_below="#+id/noteMessageContainer"
android:layout_marginBottom="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:background="#FF515151"
android:text="#string/Cancel"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:id="#+id/notePopupRightButton"
android:layout_width="180dp"
android:layout_height="35dp"
android:layout_alignParentEnd="true"
android:layout_below="#+id/noteMessageContainer"
android:layout_marginBottom="20dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="40dp"
android:background="#FF515151"
android:text="#string/Send"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
</ScrollView>
And this is two screenshot, one of the layout editor with hint in correct position and other is an app screenshot:
enter code here
You can use
android:gravity="top|start"
property in your EditText of the address block and it shall act as the container's underlying controlling property and take the hint to top left corner.
Add android:gravity="top|start" to the EditText in the layout XML file.
Add this code in your EditText / TextInputEditText:
android:gravity="top|start"
I faced the same problem.
I tried to remove the inputType attribute
and the height of edit text becomes now dependent on the lines attribute.
Then my problem is solved

Designing password text with show password button

I am trying to design EditText for password with show password imageView. However I cannot design it correctly beacuse I cannot see ImageView, since EditText's layout_width="fill_parent". When I change it to wrap_content, I can see my ImageView but then it looks very bad in UI. Is it possible to design it without giving fixed size?
Here is my code;
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:layout_marginBottom="8dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<EditText
android:id="#+id/registerPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/register_password"
android:inputType="textPassword" />
<ImageView
android:id="#+id/fragment_login_password_visibility"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#android:drawable/ic_partial_secure"
android:layout_gravity="center"
android:clickable="true"/>
</LinearLayout>
</android.support.design.widget.TextInputLayout>
EDIT:
android:layout_weight=1 solved issue. Now I can see ImageView left of EditText. However now, I TextInputLayout hint is not working.
You don't need use ImageView.
Support Library adds support for the password visibility toggle in recent update (revision 24.2.0 - August 2016).
try it:
setPasswordVisibilityToggleDrawable(int resId)
Set the icon to use for the password visibility toggle button.
https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html
First set android:weightSum of LinearLayout to "1". Then, set android:layout_width and android:layout_weight of EditText to "0dp" and "1", respectively.
Did you try to use weight?
android:layout_weight="1"
and
android:layout_width="0dp" or android:layout_width="wrap_content"
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:layout_marginBottom="8dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<EditText
android:id="#+id/registerPassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/register_password"
android:inputType="textPassword" />
<ImageView
android:id="#+id/fragment_login_password_visibility"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#android:drawable/ic_partial_secure"
android:layout_gravity="center"
android:clickable="true"/>
</LinearLayout>
</android.support.design.widget.TextInputLayout>
Try this :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/editText"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Password"
android:singleLine="true"
android:textSize="25sp"
android:paddingRight="60dp"
/>
<ImageButton
android:id="#+id/showpass"
android:layout_marginLeft="-60dp"
style="?android:buttonBarButtonStyle"
android:paddingBottom="5dp"
android:src="#drawable/pass_ic"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I recommend using a TextInputLayout with passwordToggleDrawable .
<android.support.design.widget.TextInputLayout
android:id="#+id/textInputPasswordLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:passwordToggleContentDescription="Show"
app:passwordToggleDrawable="#drawable/ic_password_visibility_selector"
app:passwordToggleEnabled="true">
<android.support.design.widget.TextInputEditText
android:id="#+id/textInputEditTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:fontFamily="sans-serif-light"
android:gravity="bottom"
android:hint="#string/enter_your_password"/>
</android.support.design.widget.TextInputLayout>
app:passwordToggleDrawable="#drawable/ic_password_visibility_selector" assign your drawable

Edittext background stretches to fill the entire area

I have an edittext for which I want to set below background
but when I set this as background it stretches itself to fit in whole EditText like below image.
and below is my code for xml file for editext
<LinearLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:visibility="visible" >
<EditText
android:id="#+id/comment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:hint="#string/add_your_comment"
android:imeOptions="actionDone"
android:padding="5dp"
android:background="#drawable/bg_search_field"
android:scrollHorizontally="true"
android:singleLine="true" />
<ImageView
android:id="#+id/post_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="5dp"
android:layout_marginRight="#dimen/fifteen_dp"
android:src="#drawable/tick_selection" />
</LinearLayout>
How can I set the background correctly?
Ok So issue resolved simply by using nine patch image i have got from actionBarsherlock library which is using it for it's search view.
This line.-
android:layout_weight="1"
makes your TextView fill the empty width in the LinearLayout. Just try and remove it. You should also replace
android:padding="5dp"
for
android:layout_margin="5dp"
// try this
<LinearLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:visibility="visible" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical">
<EditText
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="add_your_comment"
android:imeOptions="actionDone"
android:padding="5dp"
android:background="#drawable/bg_search_field"
android:scrollHorizontally="true"
android:singleLine="true" />
</LinearLayout>
<ImageView
android:id="#+id/post_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="5dp"
android:layout_marginRight="#dimen/fifteen_dp"
android:src="#drawable/tick_selection" />
</LinearLayout>
// also use my uploaded image which is editext background i'm apply 9-patch so now it not stretched if editext width is large.

Edittext loses style when align_baseline used

I have a layout where I have RelativeLayout with some Edittext which use the default style and are baseline aligned with some Textfield. The issue is the Edittext are losing the default Holo Light style line (the line under the text). If I take out the baseline then the line is shown though, of course, the Edittext loses the alignment.
I suspect this is bug or at least a very strange behavior, but I will give it a try by asking here in SO :-)
This is how it looks (Edittexts are the blank spaces to the right of Opens and Closes TextViews):
This is my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="0dip"
android:layout_weight="50"
android:orientation="vertical" >
<EditText
android:id="#+id/store_name_edit"
android:hint="#string/store_name"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
<EditText
android:id="#+id/store_code_edit"
android:hint="#string/store_code"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
<EditText
android:id="#+id/store_group_edit"
android:hint="#string/store_group"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
<EditText
android:id="#+id/store_type_edit"
android:hint="#string/store_type"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
<EditText
android:id="#+id/store_address_edit"
android:hint="#string/store_street"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingTop="20dip"
android:textSize="20sp"
android:text="#string/store_description" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/opening"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="#string/store_opening_hours" />
<TextView
android:id="#+id/opens"
android:layout_below="#id/opening"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="#string/store_open" />
<EditText
android:id="#+id/opens_edit"
android:layout_toRightOf="#id/opens"
android:layout_alignBaseline="#id/opens"
android:layout_height="wrap_content"
android:layout_width="100dip"
android:textSize="18sp" />
<TextView
android:id="#+id/closes"
android:layout_alignBaseline="#id/opens"
android:layout_toRightOf="#id/opens_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="#string/store_close" />
<EditText
android:id="#+id/closes_edit"
android:layout_alignBaseline="#id/opens"
android:layout_toRightOf="#id/closes"
android:layout_height="wrap_content"
android:layout_width="100dip"
android:textSize="18sp" />
</RelativeLayout>
</LinearLayout>
My guess is the containing RelativeLayout somehow isn't expanding to allow for the lines on the EditTexts to show.
Change the containing RelativeLayout to:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="100dp">
I realise this may not be the best solution but it's a start. Another option might be to try using alignParentBottom instead of baseline on all the "bottom" row elements.

Android edittext auto grow with text and pushing things down

Yes, this is yet another edittext grow question on SO. And yes I've already gone through all those edittext grow questions. But this ones a bit specific.
UI:
A simple android layout with edittext and button below it. When text is less, the edittext view is still covering whole screen leaving space for button below.
Once user starts entering text, edittext grows vertically to accommodate text. At the same time, the button should also kept pushing downwards to let edittext grow.
User can vertically scroll through screen to view full text.
UI after entering significant text:
Here's the layout with edittext and button below:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background"
android:clipToPadding="false"
android:fadingEdge="none"
android:fillViewport="true"
android:padding="13dp"
android:scrollbarStyle="outsideOverlay" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<!-- some fixed width image -->
<ImageView
android:id="#+id/someimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:src="#drawable/page_white_text" />
<EditText
android:id="#+id/some"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/someimage"
android:background="#null"
android:gravity="top"
android:minLines="10"
android:minHeight="50dp"
android:textColor="#222222"
android:textSize="15dp"
android:typeface="serif" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:baselineAligned="true"
android:layout_marginTop="5dp"
android:padding="10dp"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
This works more or less. But when I enter to much text, instead of pushing button downwards. It goes behind it like:
Any help here is much appreciated. Thanks :)
Here's my recommendation: inside the ScrollView, put a RelativeLayout in which you can place every element in a certain order using android:layout_below. For the auto-growing of your EditText, use this solution.
Let's see an example:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
<RelativeLayout
android:id="#+id/general2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="20sp" >
<TextView
android:id="#+id/textFirstOpinion"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/titleFirstOpinion"
android:textStyle="bold"
android:textSize="23sp"
android:textColor="#FFFFFF"
android:layout_marginTop="10dp" />
<EditText
android:id="#+id/fieldFirstOpinion"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textFirstOpinion"
android:minHeight="80sp"
android:isScrollContainer="true"
android:inputType="textMultiLine"
android:textSize="20sp"
android:gravity="top|left" />
<Button
android:id="#+id/button"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_below="#id/fieldFirstOpinion"
android:layout_centerHorizontal="true"
android:layout_marginTop="37sp"
android:textSize="23sp"
android:text="#string/textSendButton"
android:gravity="center_horizontal"
android:padding="10dp" />
</RelativeLayout>
</ScrollView>
try adding android:layout_below="#id/some" in your LinearLayout like
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:baselineAligned="true"
android:layout_marginTop="5dp"
android:padding="10dp"
android:orientation="vertical"
android:layout_below="#id/some" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Since you want the button to be at the bottom of the page, add the following line
android:layout_below="#+id/some"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
in your LinearLayout code here like
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/some"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:baselineAligned="true"
android:layout_marginTop="5dp"
android:padding="10dp"
android:orientation="vertical" >
Also make sure the layout:height parameter of EditText is set to wrap_content
Try this and tell me if it works or not

Categories

Resources