It seems to be the most weird thing that I've ever come across.
Here is a layout:
<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" >
<EditText
android:id="#+id/GuessAppEditText"
android:lines="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:inputType="textCapWords"
android:hint="#string/Hint" />
</RelativeLayout>
The hint of EditText is not shown.
If I remove either android:gravity="center_horizontal" or android:inputType="textCapWords", hint becomes visible.
I have absolutely no idea what has gravity and textCapWords to do with hint. Is it another Android bug or am I doing something wrong? In the former case, what would be a workaround? I want my text to be center-aligned and capitalized and hint to be shown. Or I want too much from poor Android?
Just add a single line and it will work, i.e android:ellipsize="start"
<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" >
<EditText
android:id="#+id/GuessAppEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:ellipsize="start"
android:gravity="center_horizontal"
android:hint="hint"
android:inputType="textCapWords"
android:lines="1" />
</RelativeLayout>
Likely that the original accept answer is no longer valid for latest android version, by setting up the hint color will make the hint display:
android:textColorHint="#android:color/darker_gray"
Hope this helps others, tested with Android 5+
it's 2021 and this still seems to be an issue, so for anyone running trying to use an EditText and not seeing the hint in the designer you can add tools:background="#android:color/transparent" or assign any color to the background and the hint will appear.
It appears to be a bug, as noted here. Seems like a few people in that thread have posted work around's or resolutions you could try. Have you tried to remove the hint and set it programmatically, to see if that is a workaround?
Declare this before onCreate()
EditText guessAppEt;
And this in onCreate()
guessAppEt = (EditText)findViewById(R.id.GuessAppEditText);
guessAppEt.setHint("Your hint here");
Related
I got a "Insufficient text color contrast ratio" warning in my layout. When I used the "Fix" option in the warning screen, it added an attribute android:hintTextColor to my text view in the xml file.
When I built the project, I got an error saying that the above attribute was not found. When I searched through all the attributes for the TextView, the most similar attribute I could find is textColorHint.
Is this a bug in Android Studio? Or did I get something wrong?
Thanks
This warning does not require any Attribute to be added, all you need to do is to enhance the contrast between the text and the background.
e.g. Set font to white if your background is black
If there is no improvement, you can try to use less complicated patterns as background.
Use android:background="#color/white" and android:textColorHint="#color/white" in TextInputEditText and the warning will be gone. see below how I used it.
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/dru_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type Something"
android:textColorHint="#color/black">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/dru_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/black"
android:background="#color/white"/>
</com.google.android.material.textfield.TextInputLayout>
Edit: As the above code will hide InputLayout border to avoid that set you parent view color to white see the below code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/dru_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type Something"
android:textColorHint="#color/black">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/dru_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/black"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
I'm trying to get ellipsize to work for a single line of text inside a TextView. I've already tried all the workarounds (e.g. scrollHorizontally, singleLine, lines, etc.) below previous questions, but nothing seems to happen. This is my code (tested on an HTC One M8 (5.0.1)):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#drawable/file_tile_background" >
<ImageView
android:id="#+id/file_tile_imageview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="24dp"
android:src="#drawable/ic_insert_drive_file_black"
android:contentDescription="#string/file_tile_imageview_description" />
<TextView
android:id="#+id/file_tile_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/file_tile_imageview"
android:text="#string/file_tile_textview_fileName"
android:maxLines="1"
android:ellipsize="start"
android:textSize="16sp" />
</RelativeLayout>
EDIT: Maybe it doesn't like that I include this layout in activity_main.xml and doesn't use it directly?
Simple solution is to use android:singleLine="true".
You can find the related solution on the following link.
TextView's ellipsize not working on maxLines = 1
In my android app, I have a hint text in my EditText view. Once the user starts typing, I want the hint to be displayed on top of the edittext field. The original hint text disappears when user starts typing which is fine. How do I place a text on top of the edittext field ? Is there a way to make the EditText field multiline and use the first line for hint? But the user input should always start from the second line.
Thanks in advance for your help!
Just found that they are called floating inline labels in android.
http://www.google.com/design/spec/components/text-fields.html#text-fields-floating-labels
But looks like API 21 doesnt support this yet.
There new widget provide by android called TextInputLayout it is the standard way. Below is an example of it:
<android.support.design.widget.TextInputLayout
android:id="#+id/first_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/first_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="hello"/>
</android.support.design.widget.TextInputLayout>
You can use FloatLabelLayout. It's a custom component that works as described in the google documentation.
Once you create the custom component you can use it in this way:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<your.package.FloatLabelLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:floatLabelTextAppearance="#style/TextAppearance.YourApp.FloatLabel">
<EditText
android:id="#+id/edit_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/account_username_hint"
android:singleLine="true"
android:inputType="textNoSuggestions"
android:imeOptions="actionNext"
android:nextFocusDown="#+id/edit_password" />
</your.package.FloatLabelLayout>
<your.package.FloatLabelLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:floatLabelTextAppearance="#style/TextAppearance.YourApp.FloatLabel">
<EditText
android:id="#+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/account_password_hint"
android:singleLine="true"
android:inputType="textNoSuggestions"
android:imeOptions="actionDone" />
</your.package.FloatLabelLayout>
</LinearLayout>
I need to center the Hint text within an EditText in Android. How do I do this?
In order for centering of hint text to work with EditText you have to make sure android:ellipsize="start" is defined. I don't know why this makes it work, but it does.
Example pulled from personal code:
<EditText
android:id="#+id/player2Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ellipsize="start"
android:gravity="center_horizontal"
android:hint="#string/player2_name"
android:inputType="textCapWords|textPersonName"
android:singleLine="true" />
Actually, android:gravity="center_horizontal" creates the centering effect you're looking for. Similarly, you can use android:gravity="start" to put hint text at the beginning of the EditText view, and so on.
Use this xml attribute:
android:gravity="center"
use attribute
android:gravity="center"
I used this code in every circumstances, and it works perfectly without using android:ellipsize="start" to make a hint in center.
<EditText
android:id="#+id/player2Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="robi"
android:inputType="text" />
I think the answer should be :
android:textAlignment="center"
textAlignment worked for me.
textAlignment="center"
Unfortunately, neither answer helped me aligning hint, written in LTR language, while the layout orientation was RTL. I needed such layout in a kind of translation application to make overlay icons not interfere with RTL text. But this trick didn't work with hints while there was no any text yet (icons appeared above the hint) and I came to the following java solution to layout problem:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
. . .
if (isRightToLeft()){
EditText guess = (EditText) rootView.findViewById(android.R.id.text1);
CharSequence hint = "\u200F" + guess.getHint();
guess.setHint(hint);
}
. . .
}
The trick was in right-to-left mark to prepend a left-to-right string. And it seems to work, but does anyone know a more elegant solution?
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="ENTER PIN"
android:inputType="numberPassword" />
</android.support.design.widget.TextInputLayout>
Note: in the tag TextInputEditText,the property
android:gravity="center"
is what makes the deal of aligning the text in the center including the hint text
use this: android:gravity="center"
I use this and worked for me
android:gravity="Left|center_vertical"
use android:textAlignment="center" in EditText , it work for me
This worked for me:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/nombreslayoutinput">
<EditText
android:id="#+id/nombreslayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/nombres"
android:layout_centerInParent="true"
android:gravity="center|center_vertical"
android:ellipsize="start"
android:inputType="textCapWords|textPersonName"
/>
</android.support.design.widget.TextInputLayout>
The correct answer is
android:gravity="center_horizontal
hint gravity
android:textAlignment="center"
text gravity
android:gravity="left"
My problem was that the EditText wasn't big enought to fit exactly inside its parent (FrameLayout in my case), so using just android:gravity="center" (center_vertical, horizontal, start, or whatever) would just fit it inside the EditText, and not in its parent, so I had to center the EditText inside its parent using:
android:layout_gravity="center"
pd: I used android:background="#android:color/transparent" to hide the ugly underline in the EditText hint
Tested with Android 1.6(4) and 2.3.3(10).
I've made a minimalistic test application to demonstrate this, all it does is load the xml with:
setContentView(R.layout.main);
and the xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:ems="10" >
</EditText>
The problem:
when setting inputType="none" the actual input type during execution becomes textMultiLine(0x00020001), I've checked it with a debugger.
On the other hand if I use inputType="text" it works as expected.
Is this a bug in Android?
I had the same problem: defining the input type via xml did not work.
Then, to fix it, I set the input type programatically:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState)
{
...
editText1= (EditText) super.getActivity().findViewById(R.id.editText1);
editText1.setInputType(InputType.TYPE_NULL); // none...
...
}
That works for me.
Setting InputType="none" xml doesn't work, but if you're using databinding then you can use the binding syntax to set Input type.
import the type..
<data>
<import type="android.text.InputType" />
</data>
then bind the value
<EditText
android:id="#+id/edit_text"
android:inputType="#{InputType.TYPE_NULL}" />
Use android:editable="false". Even though it's deprecated, it works when inputType doesn't.
From what I can tell this is a bug in the android code. see this for refrence - How to make EditText not editable through XML in Android?
GalDude33 suggests-
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
Use this
textView.setKeyListener(null);
Setting the .xml like this works, however, I had to add another line because the cursor was visible.
android:focusable="false"
android:cursorVisible="false"
It's strange that even after 2 years this "bug" is present, and the depreciated method works better than the suggested one.
Using this works as well, but you have to edit the background color in that case, unless you want the color different.
android:enabled="false"
The following code works:
Use android:enabled="false"
This is a working example:-
<AutoCompleteTextView
android:id="#+id/autoComplete"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:inputType="none"
android:enabled="false"
android:padding="15dp" />