I'm building a new InfoWindow (google maps api v2) and I'm trying to get my layout to be perfect.
A part of that layout is this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/txtInfoWindowName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#ff000000"
android:textSize="14dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/txtInfoWindowObstacles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLength="32"
android:lines="1"
android:singleLine="true"
android:textColor="#ff7f7f7f"
android:textSize="14dp"/>
Now, the problem is with the android:ellipsize="end".
It should draw the three dots at the end but it doesn't do that.
Now I'm getting something like this:
TextTextTextTextTe
Instead of this:
TextTextTextTextTe...
I'm thinking it has something to do with the fact that I'm using layout_width="wrap_content" . But I need that because I'm using a LinearLayout
I tried your code and they work fine, except the second TextView which has the attribute android:maxLength="32", which in this case the text cannot be ellipsized because of 32 characters limit. But if you remove it, it works as expected.
There is another option - you can format your string from code like this:
private static final int STR_MAX_CHAR_COUNT = 32;
private String formatString(String stringToFormat) {
if(stringToFormat.length() > STR_MAX_CHAR_COUNT){
stringToFormat = stringToFormat.substring(0, STR_MAX_CHAR_COUNT - 1) + "...";
}
return stringToFormat;
}
And then just set string like this:
String newText = "some long-long text";
mTextView.setText(formatString(newText))
Related
How can i create a description within a scrollview?
i would like to place a date on none line and a title on a separate line, but even with spaces and pressing enter it creates all on one line.
The code works well i just need help on being able to control what i write on what line. For example
date
location
title
expiry
instead of writing them on separate lines it all goes onto the same line. Even using enter, tab, spaces.
MAIN ACTIVITY
TextView mTitleWindow = (TextView) findViewById(R.id.titleWindow);
TextView mMessageWindow = (TextView) findViewById(R.id.messageWindow);
mTitleWindow.setText("BATMAN IS PART OF THE DC TRINITY");
StringBuilder stringBuilder = new StringBuilder();
String someMessage = "hello hi" ;
stringBuilder.append(someMessage);
mMessageWindow.setText(stringBuilder.toString());
layout
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/titleWindow"
android:layout_width="352dp"
android:layout_height="58dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="16dp" />
<TextView
android:id="#+id/messageWindow"
android:layout_width="350dp"
android:layout_height="455dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="17dp"
android:layout_below="#id/titleWindow" />
</RelativeLayout>
</ScrollView>
Hello #D_TECK use "\n" in string variable value for the separate line in message.
String someMessage = "hello \n hi";
I know this question has been asked before and I tried several answers but none of them worked. I have an EditText where the user gives caption of a picture. The editText keeps writing the text in a single line. It doesn't go to the next line. I want the text to go to next line automatically when it reaches the screen's border/end. Here's my code. Please let me know if I have made any mistakes in my code.
<EditText
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="start"
android:inputType="textMultiLine|textPersonName"
android:hint="Write some thing about the photo"
android:maxLength="160"
android:paddingLeft="8dp"
android:textSize="14sp"
android:layout_below="#+id/privacyHolder"
android:lines="3"
android:layout_alignParentEnd="true"
android:ems="20"
android:paddingEnd="4dp"
android:maxLines="3"
android:layout_marginEnd="4dp"
android:layout_marginStart="4dp"
android:paddingTop="4dp"
android:background="#drawable/roundedbutton"
android:id="#+id/description"
/>
In the Resource, design your edit text as follow:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text|textMultiLine|textCapSentences"
android:gravity="left"
/>
In the activity you can use TextWatcher to listen when text exceeds few character and automatically move down.
Here is the snippet :
private boolean isReach = false;
// put this in onCreate method
edittext.addTextChangedListener(new TextWatcher(){
#Override
public void afterTextChanged(Editable s) {
// if edittext has 20 character reached add a new line, you can use either 20 or more here
if(yourtextEd.getText().length() == 20 && !isReach) {
yourtextEd.append("\n");
isReach = true;
}
// if edittext has less than 20 characters and isReach has set , change
if(yourtextEd.getText().length() < 20 && isReach) isReach = false;
}
});
Hope this will help you.
I have some problem with ellipsize in TextView.
Here is html string:
final String str = "His followed carriage proposal entrance <br/>directly had elegance. Greater for cottage gay parties natural. Remaining he furniture on he discourse suspected perpetual. <br/>Power dried her taken place day ought the. Four and our ham west miss."
I create 2 textview(tv_test_html, tv_test_normal).
<TextView
android:id="#+id/tv_test_html"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="3"
android:ellipsize="end"
android:text="Hello"
/>
<TextView
android:layout_marginTop="20dp"
android:id="#+id/tv_test_normal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="3"
android:ellipsize="end"
android:text="Hello"
/>
In code I set:
final TextView tvTestHtml = (TextView) findViewById(R.id.tv_test_html);
final TextView tvTestNormal = (TextView) findViewById(R.id.tv_test_normal);
tvTestHtml.setText(Html.fromHtml(str));
tvTestNormal.setText(str);
When I run application:
You can see "..." is error when I use Html.fromHtml(...);
So how do I fix it ?
Thanks in advance !
[I just update image]
replace string with it : His followed carriage proposal entrance <br>directly had elegance. Greater for cottage gay parties natural. Remaining he furniture on he discourse suspected perpetual. <br>Power dried her taken place day ought the. Four and our ham west miss.
This is driving me absolutely crazy and Im not sure what is going on.
I have an xml layout with a TextView within a clickable RelativeLayout.
<RelativeLayout
android:id="#+id/bg_section"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#color/almost_black"
android:clickable="true"
android:onClick="goToBG"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Go To B.G."
android:textColor="#color/white"
android:textSize="20sp" />
<ImageView
android:id="#+id/bg_arrow"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="-10dp"
android:src="#drawable/arrow_icon" />
<TextView
android:id="#+id/current_bg_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/bg_arrow"
android:text="3"
android:textColor="#color/holo_blue"
android:textSize="22sp" />
</RelativeLayout>
Now in my code I try to update the textview "current_bg_count"
private void updateBGCount(){
try{
RelativeLayout bgSection = (RelativeLayout) findViewById(R.id.bg_section);
TextView bgCountTV = (TextView) bgSection.getChildAt(2);
bgCountTV.setText(tempBG.size());
}
catch(Exception e){
e.printStackTrace();
Logger.d(TAG, "exception in updateBGCount");
}
}
This gives a ResourceNotFountException on the line where I setText although the RelativeLayout is found without a problem. Even when I try finding it just by id like this:
TextView bgCountTV = (TextView) findViewById(R.id.current_bg_count)
bgCountTV.setText(tempBG.size());
it gives the same error.
All the other views in the layout are found easily and updated just fine. Only this one TextView is giving me the problem. Does anyone have any idea what the problem is?
You need to convert your size of the ArrayList to a String at this line
setText(tempBG.size())
Since tempBG.size() returns an int then setText() is looking for a resource with the id of whatever that returns. You are using this setText(ResId int) method which is used for if you have a string resource that you want to use to set the text.
So change it to
setText(String.valueOf(tempBG.size()));
Try setting your tempBG.size() to string like this
bgCountTV.setText(""+tempBG.size());
This should work
Are you using eclipse? Check there is no errors and warning in your project. Sometimes there are errors in xml that let you compile, but doesn't work properly.
the Problem is with this code
bgCountTV.setText(tempBG.size());
tempBG.size() this code is returning an int value which it is assuming as string Resource id something like R.string.VARIABLE_NAME coz it have a corresponding int value which TextView is assuming as id
What you can do
bgCountTV.setText(tempBG.size()+"");
OR
bgCountTV.setText(String.ValueOf(tempBG.size()));
OR
bgCountTV.setText(tempBG.size().toString());
I am setting text in TextView from string resource. Normally, Hebrew works in Right-To-Left format. When I set a text, it sets the text Right-To-Left format in LG, Samsung, Sony Phone but in HTC it does not work. It works in Left-To-Right format in HTC. Even I set Gravity to the TextView in Java file.
Text in TextView should be span according to the screen size. For example if it is 320 x 480 then it display in 4 lines but if it is Galaxy Tab then there may be 2 lines.
Here is my Code snippet:
In Java:
private TextView mVersionInfo, mVersionDescriptionOne, mVersionDescriptionTwo, mVersionDescriptionThree;
mVersionInfo = (TextView)findViewById(R.id.VersionInfo);
mVersionDescriptionOne = (TextView)findViewById(R.id.VersionDesc1);
mVersionDescriptionTwo = (TextView)findViewById(R.id.VersionDesc2);
mVersionDescriptionThree = (TextView)findViewById(R.id.VersionDesc3);
mVersionDescriptionOne.setGravity(Gravity.RIGHT);
mVersionDescriptionTwo.setGravity(Gravity.RIGHT);
mVersionDescriptionThree.setGravity(Gravity.RIGHT);
in XML:
<TextView android:id="#+id/VersionDesc1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="#string/versiondesc1" android:textColor="#000000"
android:layout_marginTop="5dip" android:gravity="right"
android:layout_alignParentRight="true" android:layout_marginRight="10dip"
android:layout_below="#+id/Share" android:textSize="13sp"
android:layout_alignRight="#+id/Body" />
<TextView android:id="#+id/VersionDesc2"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="#string/versiondesc2" android:textColor="#000000"
android:layout_alignParentRight="true" android:layout_marginRight="10dip"
android:layout_below="#+id/VersionDesc1" android:textSize="13sp"
android:layout_marginTop="5dip" android:gravity="right"
android:layout_alignRight="#+id/Body" />
<TextView android:id="#+id/VersionDesc3"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="#string/versiondesc3" android:textColor="#000000"
android:layout_alignParentRight="true" android:layout_marginRight="10dip"
android:layout_below="#+id/VersionDesc2" android:textSize="13sp"
android:layout_marginTop="5dip" android:gravity="right"
android:layout_alignRight="#+id/Body" />
In String Resource:
<string name="versiondesc1">האפליקציה מתחברת לאתר הספק הסלולרי כדי להציג את מצב החשבון. לעיתים, כשאתר הספק איננו עובד תקין לא יהיה ניתן לקבל מידע. באם אתר הספק ישתנה האפליקציה עלולה להפסיק לעבוד. במצב כזה האפליקציה תחזור לעבודה תקינה מיד לאחר שאנו נתאים את שרת התוכנה שלנו לשינויים.</string>
<string name="versiondesc2">הערה: אנחנו לא מייצגים את חברות הסלולר ולא נמצאים איתן בקשר מסוג כלשהו!</string>
<string name="versiondesc3">אם נתקלת בבעיה, השתמש/י בכפתור יצירת קשר על מנת שנוכל לפתור אותה. נשמח לקבל כל משוב על האפליקציה.</string>
What is wrong with my code?
Anybody who has worked with another language, Please guide me here.
Thanks.
Gravity will only affect alignment and will not set base direction for the text. That it works on some devices and not others may be a font issue, or perhaps an OS version issue. Try adding a RIGHT-TO-LEFT MARK character (\u200F) at the start of your text. This might help the display on an HTC and will not hurt anything on devices where it is already working.
here is an example from my hebrew string xml, Thanks to Ted Hopp's answer:
you need to add '\u200e' before the char that causes you the problem:
<string name="basic_text1">המר על תוצאת המשחק\u200e:</string>
and the result will be:
:המר על תוצאת המשחק