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!
Related
I'm sure this has been answered before but I've searched relentlessly and can't find an answer that fixes my exact problem, which is:
I have a TextView on top of an ImageView setup like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageForCustomAdapter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_ball_3" />
<TextView
android:id="#+id/textOnBall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:textColor="#000000"
android:textSize="50dp"
android:hint="10"
android:layout_alignParentBottom="false"
android:layout_centerInParent="true"
android:layout_alignTop="#+id/imageForCustomAdapter"
android:layout_alignLeft="#+id/imageForCustomAdapter"
android:layout_alignBottom="#+id/imageForCustomAdapter"
android:layout_alignRight="#+id/imageForCustomAdapter"
android:gravity="center" />
</RelativeLayout>
The TextView is aligned with the ImageView to make it the same size and I want the text to be centered both horizontally and vertically.
The render in Android Studio shows it to be centered correctly This Pic Shows That
But when I actually run the app on a real device the text only centers horizontally. I've tried all the different combinations of android:gravity= but nothing seams to work.
What am I doing wrong?
I'll preface this with saying I really dislike RelativeLayout, and here's a great example. Its powerful, but can be seriously confusing, and simplicity is important.
You've got many layout directives that can easily conflict with each other. Try something like:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/imageForCustomAdapter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_ball_3" />
<TextView
android:id="#+id/textOnBall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="50dp"
android:hint="10"
android:layout_gravity="center" />
</FrameLayout>
The 'wrap_content' is important on the FrameLayout, btw. Assuming the ImageView is always bigger than the TextView, it'll drive the parent height. If the TextView might be bigger, make sure to center the ImageView as well.
Why it works in studio and not in real life, no idea.
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.
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 problem with a RelativeLayout containing two TextViews and a ImageView, that I use for displaying items in a ListView. The items are correctly displayed on Android 1.6, but on Android 2.2 the TextViews are overlapping.
Here is an image that shows the correct and incorrect behavior side-by-side:
And here is the source code of my RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="6dip"
/>
<TextView
android:id="#+id/secondLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:layout_below="#+id/firstLine"
android:layout_toRightOf="#id/icon"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
<TextView
android:id="#+id/firstLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:ellipsize="marquee"
android:singleLine="true"
android:layout_toRightOf="#id/icon"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
Any idea what I am doing wrong?
Thanks a lot,
Philipp
This is basically the same answer as Octavian's, but I don't think he actually explained it very well.
You have contradicting statements in your XML file. You have:
android:layout_alignParentBottom="true"
in both of your text views. You also have:
android:layout_below="#+id/firstLine"
in one of the textviews. Essentially, your trying to align to the bottom of a relative layout and then trying to put something under it. There isn't anything "under the bottom."
Remove this contradicting logic and it should solve your problem.
I'm not 100% sure if it is the problem but on your TextView with the ID firstLine it seems like you are aligning it to it's parents bottom like you do with TextView ID secondLine. I'm quite sure you wanted to say android:layout_alignParentTop="true" instead.
I can't tell why it is working on Android 1.6 but not on 2.2.