How can set Hint Text in code? - java

I have:
<android.support.design.widget.TextInputLayout
android:id="#+id/ti_amount_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:theme="#style/BlueAccentColor">
<EditText
android:id="#+id/et_amount"
style="#style/EditTextRedBorder"
android:layout_width="#dimen/product_selector_width"
android:background="#drawable/contacts_bg_mess"
android:textColorHint="#color/palette_contacts_send_mess_text"
android:textColor="#color/palette_black"
android:inputType="numberDecimal">
</EditText>
</android.support.design.widget.TextInputLayout>
And I need add Hint Text, If I added hint text in xml, in EditText:
android:hint="#string/s_amount"
all nice, but if I add etAmount.setHint(R.string.s_amount); in code - String (hint text) does not popup ((, Why?

Set the hint to the TextInputLayout instead.
mTextInputLayout.setHint("your hint here");

Related

How do I align the text in the text-area on bottom?

I want to align the text of this two labels on it's bottom, so the two values are aligned on the same height.
Sadly, I dont find an option like aligning it inside its own area.
gravity wont work. It has to be in xml.
Thank you!
<TextView
android:id="#+id/feed_euro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="#+id/feed_cent"
android:height="35dp"
android:text="23"
android:textColor="#color/colorTextBlack"
android:textSize="25dp" />
<TextView
android:id="#+id/feed_cent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:height="35dp"
android:text=",39 €"
android:textAlignment="textStart"
android:textColor="#color/colorTextBlack" />
If you want to set your textview text bottom
android:gravity="bottom"
or
if you want to set your whole component bottom
android:layout_gravity="bottom"
To align the text inside a TextView to its bottom, you can use
android:gravity="bottom"
(inside TextView tags)
You can change the gravity to bottom like below.
<TextView
android:id="#+id/feed_euro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="#+id/feed_cent"
android:height="35dp"
android:text="23"
android:textColor="#color/colorTextBlack"
android:textSize="25dp" />
<TextView
android:id="#+id/feed_cent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:height="35dp"
android:text=",39 €"
android:gravity="bottom"
android:textAlignment="textStart"
android:textColor="#color/colorTextBlack" />
And also you can try android:gravity="bottom" to the layout you have used to hold the both TextView and change the 2nd TextView height into wrap_content.
Hope you got the answer.

How to put gap for toggle image and hint text in Edittext android?

I am new for android. i want space gap between toggle image and hint text same field of edit text
.
This is my edit text xml code
<EditText
android:id="#+id/nation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/email"
android:layout_centerHorizontal="true"
android:drawableLeft="#drawable/ic_flag"
android:layout_marginTop="11dp"
android:inputType="textCapWords"
android:hint="Nationality"/>
use :
android:drawablePadding="20dp"
to change Hint text color use:
android:textColorHint="#android:color/darker_gray"
<EditText
android:id="#+id/nation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/email"
android:layout_centerHorizontal="true"
android:layout_marginTop="11dp"
android:drawableLeft="#drawable/ic_flag"
android:drawablePadding="20dp"
android:hint="Nationality"
android:textColorHint="#android:color/darker_gray"
android:inputType="textCapWords" />
Here is solution
I want space gap between toggle image and hint text same field of edit
text
set drawablePadding android:drawablePadding="15dp"
<EditText
android:id="#+id/nation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/email"
android:layout_centerHorizontal="true"
android:layout_marginTop="11dp"
android:drawableLeft="#drawable/ic_flag"
android:drawablePadding="15dp"
android:hint="Nationality"
android:inputType="textCapWords" />
in this edittext possible to decrease the hint text darkness
You can set hint color programmatically
editText.setHintTextColor(getResources().getColor(R.color.dark_gray));
use the drawablePadding property.
<EditText
android:drawablePadding="10dp"
android:id="#+id/nation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/email"
android:layout_centerHorizontal="true"
android:layout_marginTop="11dp"
android:drawableLeft="#drawable/ic_flag"
android:hint="Nationality"
android:inputType="textCapWords" />

How do I position the cursor at a default position?

The image shows the screenshot of my application. As you can see that the cursor is at the text field pertaining to "Height". Whenever I launch my app the default cursor position is at "Height". I want it to be on "Mass".
How do I do it ? I can't figure out. Please help.!
Take a look at your .xml file:
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:ems="10" >
<requestFocus />
</EditText>
And set the <requestFocus /> tag appropriately.
Also, you can call:
EditText.requestFocus();
Either update your .xml file as follows.
<EditText
android:id=#+id/etMass
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<requestFocus/> </EditText>
Or Use Code:
EditText etMass = (EditText) findViewById(R.id.etMass);
etMass.requestFocus();

align text center with android

I know it sounds easy. I need to put a text in center, but when the text is too long it needs to go below, but still align in the center of my xml.
Here's my code :
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/showdescriptioncontenttitle"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_centerHorizontal="true"
>
<TextView
android:id="#+id/showdescriptiontitle"
android:text="Title"
android:textSize="35dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
I put paddingTop and Bottom because I need some space.
PS: My code is bigger; it's in a RelativeLayout.
Set also android:gravity parameter in TextView to center.
For testing the effects of different layout parameters I recommend to use different background color for every element, so you can see how your layout changes with parameters like gravity, layout_gravity or others.
use this way
txt.setGravity(Gravity.CENTER);
use this way in xml
<TextView
android:id="#+id/myText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Time is precious, so love now."
android:gravity="center"
android:textSize="30dp"
android:textColor="#fff"
/>
You can use the following:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/showdescriptioncontenttitle"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<TextView
android:id="#+id/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Your text"
android:typeface="serif" />
</LinearLayout>
The layout needs to be relative for the "center" property to be used.
Adding android:gravity="center" in your TextView will do the trick (be the parent layout is Relative/Linear)!
Also, you should avoid using dp for font size. Use sp instead.
Just add these 2 lines;
android:gravity="center_horizontal"
android:textAlignment="center"
add layout_gravity and gravity with center value on TextView
<TextView
android:text="welcome text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
/>
android:layout_gravity="center"
or
android:gravity="center"
both works for me.
Or check this out this will help align all the elements at once.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/showdescriptioncontenttitle"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_gravity="center"
android:gravity="center_horizontal"
>
<TextView
android:id="#+id/showdescriptiontitle"
android:text="Title"
android:textSize="35dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
If you want to change the alignment from the mainActivity.java file, then use the gravity. For example:
chatMsg.setGravity(Gravity.RIGHT);
chatMsg is my textView. I wanted the text to be right aligned and this code worked well.
you can easily done this by adding these lines
android:layout_width="match_parent"
android:gravity="center"
android:textAlignment="center"

How can I show ellipses on my TextView if it is greater than the 1 line?

I have the following Layout which does not work:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:id="#+id/experienceLayout"
android:background="#ffffff"
android:layout_height="match_parent"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingBottom="6dp"
android:paddingTop="6dp">
<TextView
android:layout_weight="1"
android:id="#+id/experienceLabel"
android:text="Experience"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_width="wrap_content"
android:textStyle="bold">
</TextView>
<TextView
android:id="#+id/experienceTextView"
android:text="TextView"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_width="wrap_content"
android:ellipsize="end"
android:lines="1"
android:maxLines="1"
android:singleLine="true"
android:fadeScrollbars="false">
</TextView>
</LinearLayout>
This is a common problem. Try using the following:
android:scrollHorizontally="true"
android:ellipsize="end"
android:maxLines="1"
.............. the scrollHorizontally is the "special sauce" that makes it work.
This will also make a single line with ellipsise
android:singleLine="true"
Use this
android:ellipsize="end"
android:singleLine="true"
Don't use this without fully aware of what output comes
android:ellipsize="end"
android:maxLines="1"
When you use maxlines = 1 it will some time truncate most of the characters.
The way it worked for me on multiple devices / APIs was programmatically like this (where tv is your TextView):
if (tv.getLineCount() > 1) {
int lineEndIndex = tv.getLayout().getLineEnd(0);
String text = tv.getText().subSequence(0, lineEndIndex - 3) + "\u2026";
tv.setText(text);
}
So all the answers above cater to the requirement that only 1 line and then the ellipsis should appear. However if you want the ellipsis to appear after certain lines of text, then you should use the following:
android:ellipsize="end"
android:maxLines="2"
android:singleLine="false"
With this the ellipsis will appear only after 2 lines. Note: Its important to have singleLine as false.
This helped me out:
android:ellipsize="end"
android:maxLines="1"
android:singleLine="true"
Make sure the TextView width is set to Match_Parent
https://github.com/chrisjenx/Calligraphy/issues/43#issuecomment-523701518
android:singleLine is deprecated. In my case, I had to get a fixed height for the TextView and I used the android:lines attribute instead of android:maxLines. I thought this might help someone having the same problem as mine.
android:ellipsize="end"
android:lines="2"

Categories