I had no idea what to call this question, what I am experiencing is a little odd, to say the least.
I have a layout with the following section part of it
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/activityProjectView_projectName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/activityProjectView_projectDueDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project Due Date"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<TextView
android:id="#+id/activityProjectView_projectDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project Description"
android:textAppearance="?android:attr/textAppearanceMedium" />
In my code I assign the variables into these TextView elements as follows
TextView projectName = (TextView) this.findViewById(R.id.activityProjectView_projectName);
TextView projectDesc = (TextView)
this.findViewById(R.id.activityProjectView_projectDescription);
TextView projectDueDate = (TextView)
this.findViewById(R.id.activityProjectView_projectDueDate);
projectName.setText(project.getName());
projectDesc.setText(project.getDescription());
projectDueDate.setText(Util.DATE_SHORT_UI.format(project.getDueDate()));
The data returned by the project getters is correct. When the view loads, the small textview is populated with the description, not the date and the medium textview is populated with the date not the description.
I thought this was odd as the view is correct, and the code by the looks of it is correct. At this point I switched the layout so that the date element was below as follows:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/activityProjectView_projectName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/activityProjectView_projectDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project Description"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<TextView
android:id="#+id/activityProjectView_projectDueDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project Due Date"
android:textAppearance="?android:attr/textAppearanceSmall" />
I expected this to switch the data round (showing that I had some sort of error in my code, or mis assignment somewhere). I was wrong. The data stayed exactly where it was previously.
By that I mean the date was now displayed in the other textview and same for description. So now even though I switched the textviews round, the date was now in the the medium textview not the small one and the description switched also. Oddly the view definitely updated as i saw the medium and small text views move places, but the data somehow also switched.
What is going on? Whatever I do to my layout file, I can get the style itself to change, and position of elements, but the data always somehow finds a way to display itself in the wrong textviews.
Apparently the issue was caused by a broken R.java file with references pointing to wrong views. A clean rebuild fixed the issue.
Related
I'm trying to update the content of a TextView with the data introduced by an EditText. But after change the text, if the original text (hint) was bigger than the new text, the width doens't change.
I have the next string in the Hint Attribute of the TextView, "Please, introduce a amount". If I put 25'5, for example, the textview doesn't resize it width.
First Screen
Second Screen
The Euro symbol is invisible behind the textview, and It's shown after insert some value.
The piece of code for the Amount:
<RelativeLayout
android:id="#+id/rlImporte"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rel_layout_colors"
android:clickable="true"
android:paddingTop="8dp"
android:paddingBottom="8dp">
<ImageView
android:id="#+id/lst_img3"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="5sp"
android:layout_marginTop="5dp"
android:src="#drawable/icon_importe"/>
<TextView
android:id="#+id/tvImporte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:layout_marginTop="5dp"
android:layout_toRightOf="#+id/lst_img3"
android:text="#string/IMPORTE_TITLE_STRING"
android:textColor="#android:color/black"
android:textSize="#dimen/title_text_size" />
<TextView
android:id="#+id/tvImporteDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvImporte"
android:layout_toRightOf="#+id/lst_img3"
android:textColor="#android:color/darker_gray"
android:textSize="#dimen/subtitle_text_size"
android:visibility="visible"
android:hint="#string/IMPORTE_PLACEHOLDER_STRING"
android:textColorHint="#android:color/darker_gray"
android:ellipsize="start"
android:layout_marginRight="4dp" />
<TextView
android:id="#+id/tvEuroSymbol"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvImporte"
android:layout_toRightOf="#+id/tvImporteDesc"
android:textColor="#android:color/darker_gray"
android:textSize="#dimen/subtitle_text_size"
android:visibility="gone"
android:hint="#string/euro_symbol"
android:textColorHint="#color/blue_quantion"
android:ellipsize="start"
android:paddingLeft="4dp" />
</RelativeLayout>
You seem like you want to change the width of tvImporteDesc dynamically. The short of it is that Android does not allow this, though some people have developed work arounds. (Toggling the visibility that some answers suggest did not work for me). Similar questions:
Change Relative layout width and height dynamically
WRAP_CONTENT not working after dynamically adding views
Since you are trying to fit text on the same line it might be easier to use a single TextView for that line of text, and concatenate the euro symbol?
I need to display two single-line TextViews horizontally. The left TextView (let's name it 1) may have a longer text which may shortened and finished with "...". The right text view (2) has a short text and should never get shortened.
I want the 1 to remain aligned to the left end of the parent. The 2 aligned to the right side of 1.
There are now 2 conditions that I have to meet
a) if the 1 has a short text then the 2 should get aligned to the right of 1 (none of the gets shortened).
b) but if the 1 has a too long text then the text of 1 should be shortened by '...' while the view 2 is moved maximally to the right of the parent but still remains fully visible (no ...)
My current solution is the following below. The scenario b) is fine with mine, but in case of a) the problem is that the view 2 is moved to the right side of the parent and the 1 to the left side - both are short and there's pretty much space in between which looks odd. I want 2 to move to the further left (next to 1) and leave this space on the right side.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginTop="1dp"
android:layout_weight="0.5">
<TextView
android:id="#+id/ns_in_txt"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:gravity="top|left"
android:maxLines="1"
android:singleLine="true"
android:textColor="#00a2ff"
android:textSize="18sp" />
<TextView
android:id="#+id/ns_txt"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/ns_in_txt"
android:gravity="top|left"
android:maxLines="1"
android:singleLine="true"
android:textColor="#00a2ff"
android:textSize="18sp" />
</RelativeLayout>
Try doing this
<RelativeLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_context"
android:layout_marginTop="1dp">
<TextView
android:id="#+id/ns_in_txt"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_alignParentRight="true"
android:gravity="left"
android:maxLines="1"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#00a2ff"
android:textSize="18sp" />
<TextView
android:id="#+id/ns_txt"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/ns_in_txt"
android:gravity="left"
android:maxLines="1"
android:singleLine="true"
android:textColor="#00a2ff"
android:textSize="18sp" />
</RelativeLayout>
Apparently, you want two scenarios which require to set a different orientation to the parent layout: first horizontal, second vertical. Maybe I'm wrong (and I hope so but) in static xml, there will be difficult to do this.
Try the code below to test if I'm wrong:
Scenario 1: orientation horizontal = the text 1 is not big enough
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/textLong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#00a2ff"
android:textSize="18sp"
android:text="This is a normal text not big" />
<TextView
android:id="#+id/textShort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:singleLine="true"
android:textColor="#00a2ff"
android:textSize="18sp"
android:text="short text" />
</LinearLayout>
Scenario 2: orientation vertical = the text 1 is too big
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textLong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#00a2ff"
android:textSize="18sp"
android:text="This is a biiiggg loooonnng teeeeexxxxxtttt" />
<TextView
android:id="#+id/textShort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:singleLine="true"
android:textColor="#00a2ff"
android:textSize="18sp"
android:text="short text" />
</LinearLayout>
To resolved your issue, you can try 2 solutions.
First, try to create a maxLenght limit, which calculate in your Activity and change the parent orientation of the LinearLayout. Get the number of chars that you have and display the orientation as well.
Second, customise your own class extend TextView. And create a getWidth method which return the width of the long TextView in comparison to it parent and change the orientation.
Maybe the following questions/answers could be useful (I think there are not solutions but more as inspiration):
In Android how to get the width of the Textview which is set to Wrap_Content
Get the size of a text in TextView
How to find android TextView number of characters per line?
Auto Scale TextView Text to Fit within Bounds
EDIT:
I found a solution with the last url that I writed above. See this answer where the dev wanted to make the same as you. So he decided to create a autoresizable textview. Take a look here: Move two side by side textviews to be one under another if text is too long
I hope this will help you.
May I suggest to use combination of LinearLayout and a little bit of coding. The idea is to have them side by side regardless of the size. and after the right textview is measured and laid out, set the max width of the left textview to whatever space left.
Here is the layout file, nothing special here:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/ns_in_txt"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="top|left"
android:maxLines="1"
android:singleLine="true"
android:textColor="#00a2ff"
android:textSize="18sp" />
<TextView
android:id="#+id/ns_txt"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ellipsize="none"
android:gravity="top|left"
android:maxLines="1"
android:singleLine="true"
android:textColor="#00a2ff"
android:textSize="18sp" />
</LinearLayout>
and add some codes to the activity/fragment:
final TextView tvLeft = (TextView) findViewById(R.id.ns_txt);
final TextView tvRight = (TextView) findViewById(R.id.ns_in_txt);
ViewTreeObserver obs = tvRight.getViewTreeObserver();
obs.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
tvLeft.setMaxWidth(SCREEN_WIDTH - tvRight.getWidth());
}
});
people!
Well, after googling for almost two days with no result, I decided to ask here. (It's my first question.)
I'm creating a quotes widget for Android (that shows a random quote each time), so it has two TextViews and a ImageButton to get a new random quote. The first TextView shows the quote text and the second shows the quote's author. And, when the user clicks on the widget, it calls a Activity showing the quote in full screen, with scroll bars if it is needed, and so on.
The thing is that the quote's text sometimes is much long, so I was wanting to ellipsize it. But since the ellipsize doesn't work with more than 2 lines... I'm looking for ideas.
I don't know if it's necessary, but here is the layout code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/laySmall"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="#dimen/widget_margin"
android:background="#drawable/widgetback" >
<ImageButton
android:id="#+id/ibtNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:background="#null"
android:contentDescription="#string/app_name"
android:src="#drawable/seta" />
<TextView
android:id="#+id/txtWdgQuote"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/ibtNext"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="8dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textStyle="italic"
android:typeface="serif"
android:maxLines="3" />
<TextView
android:id="#+id/txtWdgAuthor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textStyle="bold" />
</RelativeLayout>
Basically, I have 3 AppWidgets in this project, showing almost the same thing. All that change are the sizes and font sizes.
So... One idea that I had was using a custom TextView. But in a AppWidget, no way. :/
Another idea was doing something like this:
if (quoteText.length() > something) {
quoteText = quoteText.substring(0, something) + "\"[...]"
}
The problem of this solution is where is the line breaks, because it can change the size of the string that can be showed. For example, let say a string with small words: the lines will break next to the border. Meanwhile a string that has bigger words the line breaks will be further from the border.
So... I don't know what I do. Any ideas, please?
Thanks!
(I'm having problems with the android:layout_margin, so it can be a little smaller, but I didn't have time to work on this yet.
I have a layout with a textview and a button as below.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/info_layout">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
<Button android:id="#+id/butn1"
android:layout_gravity="center_horizontal"
android:layout_marginTop="40dip"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/button1" />
</LinearLayout>
The above layout works fine for many languages but for arab its giving sorry pop up, but if i change textSize to 18sp it works fine. Even i tried removing margin from button before changing text size but still the problem raises.I'll set text for text View in java file.So im not understanding what's exact cause for this problem.
01-08 03:09:59.959: E/AndroidRuntime(1803): java.lang.StringIndexOutOfBoundsException: length=75; regionStart=52; regionLength=-18
I tried and searched for a solution to this problem to no avail. The top of letters, such as the tail in a lower-case "d" are being clipped off. Here's is what I currently have:
Codewise:
From the onClickHandler.....
String selectedorder = "";
...
if (num_players == 3) {
editor.putString("prefPrefp3_name", child.getText().toString());
selectedorder = "3rd";}
...
order.setText(Html.fromHtml(selectedorder));
From the CursorAdapter BindView.................
TextView tv4 = (TextView) view.findViewById(R.id.dspplyrorder);
tv4.setText(Html.fromHtml(playersCursor.getString(PlayerOrder)));
And from the XML, where I'm trying to get #+id/dspplyrorder to NOT CLIP the tail of the "d" in my superscript...................
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/dsp_id"
android:layout_width="120dip"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/dspplyrname"
android:layout_width="180dip"
android:textColor="#7CFC00"
android:textStyle="italic"
android:layout_marginRight="20dip"
android:layout_marginLeft="20dip"
android:paddingRight="3dp"
android:gravity="left"
android:textSize="25sp"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/dspplyrorder"
android:layout_width="30dip"
android:gravity="left"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:layout_height="wrap_content"/>
<ToggleButton android:id="#+id/button_toggle"
android:text="#string/buttons_1_toggle"
android:textOff="Select"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="myClickHandler" />
</LinearLayout>
I am not worried about affecting the line spacing of lines above or below, which seems to be the most often occurring problem discussion. I must be doing something really dump, since no one else seems to have this problem. HELP Please.
This solution worked for me.
Superscripted text is usually made smaller when the browser renders it, that doesn't seem to happen here so you can replicate that (and solve this problem) by doing this:
someTextView.setText(Html.fromHtml("Some text<sup><small>1</small></sup>"));
Set the following for your TextView:
android:setIncludeFontPadding="true"
That should do it. Cheers!