Text of TextView after rotation is missing characters - java

I want to use a text view in my Android layout, the text should be rotated by 90 degrees to the left. The width of the text view is rather small (20dp). My code looks like this:
<TextView
android:id="#+id/txtVw"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:minWidth="20dp"
android:maxWidth="20dp"
android:paddingEnd="6dp"
android:rotation="270"
android:text="Test"/>
The rotation is done, but only those characters are shown that would be visible without the rotation - but there is enough space to show all of them. Does anyone have an idea?

I think it is really a bug, but I found a solution without a custom TextView by my own - I post it if someone else needs a solution for this:
<TextView
android:id="#+id/txtVwOne"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:height="20dp"
android:gravity="end"
android:maxWidth="40dp"
android:minWidth="40dp"
android:rotation="270"
android:text="Test" />
<TextView
android:id="#+id/txtVwTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_weight="1"
android:layout_marginLeft="-20dp"
android:minHeight="40dp" />
The trick is to set the min and max width to a value that will allow the text not to be cut in horizontal alignment. The layout height must be set to the height you want it to be. The height attribut must be set to target width of the text view. The following text view must contain android:layout_marginLeft="-20dp" to make sure that it is moved by 20 dp to the left (the target position).

Related

Making 2 dynamic TextViews not overlap

I have been working on an app that shows statuses about a server. I have a CardView for each server, with a RelativeView inside. On the left is an image, aligned to the cards left. In the middle, I have a TextView, aligned to the image right. On the right, I have a TextView, aligned to the right of the card.
Basically, my issue is, without using a LinearLayout, how can I make it so the middle TextView does not overlap the right TextView, preferably in the layout's XML? The text in both views is dynamically long, making a LinearLayout not very preferable.
Here is a diagram of the Layout to help you picture what I'm talking about. Sorry for the external link, it was getting reformatted in the post.
1.Aline middle TextView to centerHorizontal of parent, give fixed width , margin left and right to it. Mention that it is right of another TextView by using layout_toLeftOf.
2.Also aligh right hand side TextView to right by using alignRightToParent = true. Then give left margin to it.
I tried by using below xml code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="#+id/sun"
android:background="#004700" >
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginRight="20dp"
android:layout_toLeftOf="#+id/sun12"
android:singleLine="false"
android:text="abcdgsss ssssssssssssssss ssssssss sssssssssssssssssssssss"
android:textColor="#fff"
android:textSize="16sp" />
<TextView
android :id="#+id/sun12"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:text="abcdgsss ssssssssssssssss ssssssss sssssssssssssssssssssss"
android:textColor="#fff"
android:textSize="16sp" >
</TextView>
</RelativeLayout>
I figured out how to do it programatically. Simply, you want to subtract the widths and padding of the surrounding views from the size of the container view, and set the leftover value to the text view's width. Here is an example:
int view_length = personViewHolder.container.getWidth() - personViewHolder.container.getPaddingStart() - personViewHolder.container.getPaddingEnd();
view_length = view_length - personViewHolder.object_to_left.getWidth() - personViewHolder.object_to_left.getPaddingStart() -personViewHolder.object_to_left.getPaddingEnd();
view_length = view_length - personViewHolder.object_to_right.getWidth() - personViewHolder.object_to_right.getPaddingStart() - personViewHolder.object_to_right.getPaddingEnd();
personViewHolder.view_to_set_width.getLayoutParams().width = view_length;
personViewHolder.view_to_set_width.invalidate();
You can place the views in a RelativeLayout (if they are not already in one), then and use the ToStartOf or ToEndOf attributes to align one of them to the start or end of the other. You could also use ToLeftOf or ToRightOf, but this is not recommended because in some locales you want the user to read from right to left instead of left to right. This will ensure that the two views never overlap (assuming you haven't placed negative margins on either of the views). This can be extended to as many views as you want, as long as you correctly configure their alignment attributes.

Android stop textview from increasing it's size

I have a quick question... I'm trying to make small test app for android just to see what it can do... So.. On start android sets textview.text to some text from shared prefs and increases textview size vertically.. I can't understand why.. I'm using tableview with the row of textviews in each row..
The xml code looks something like this:
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<TextView
android:id="#+id/tv1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="5.1"
android:background="#F8F8FF"
android:gravity="fill_vertical|center_horizontal"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="10sp"
android:textStyle="normal" />
</TableRow>
Is there anyway to stop that textview from resizing? Or there is some attribute I have missed..?
Thanks
on your TableRow you have
android:layout_height="wrap_content"
change to a fix height or to some relative height with "layout_weight".
If you say to android wrap to content, the rows will increase/decrease to fit his content.
Edit: i see your layout_weight, but you have 1, i think you don't know how to use it. weight repart the width and height with this weight, so think, you have 5 rows so if 1 is the total weight 1/5=>0.2 will be your weight per row.
To the weight will apply, you need to say which dimension is based on weight.
so you need to put this
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.2" >
You can read more here:
http://developer.android.com/guide/topics/ui/layout/linear.html

Android Java Justified tablerow in linear layout

I have a table row in a linear layout
The table row is set to fill parent
I have three objects inside the tablerow a textview then a editbox and then a buttong
they all line up horizontally
i want the textview on the left to be fixed at 100p width
I want the button on the right to be fixed at 50dp width
how do i set the editbox in the middle so that all three fill the width of the screen which might vary from device to device
I have tried various combinations of match and wrap but cant seem to get it
Any ideas
Mark
You have to use layout_weight for particular child of your view group or use relativelayout may solve your problem and please post you layout XML.
Try the following, using layout_weight:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name"
android:id="#+id/textView" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_weight="1" />
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="Ok"
android:id="#+id/button" />
</LinearLayout>
That's when LAYOUT WEIGHTS come in handy.
For the fixed elements, just assign width & height as usual.
For the elastic ones, set "0dp" as the size you want to be elastic, and enter a weight in the weight field (layout parameters of a linearlayout).
Which weight?
If you only have one elastic element, it doesnt matter. If you have 2, then the available space will be distributed proportionally between weights. So if you wanted 2 elastic elements, one twice the size of the other, you'd assign weights 1 & 2.
By the way, heavy use of LinearLayouts with weight are somewhat discouraged because it takes Android twice the time to calculate their size. If your table has a lot of rows, you will certainly notice it's not as fast as fixed elements.

Is there a hard limit on the minimum height of a button in Android?

I'd like to alter the size of my buttons at run-time in order to make them fit the screen well - so, in order to set the width of the buttons, I am doing -
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
int buttonWidth = (screenWidth*2)/3;
and then looping through the buttons calling .setWidth(buttonWidth); on each one.
I'd like to alter the height in the same way, however when I run my application on the emulator the height doesn't change unless I set it to a larger value.
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
int buttonHeight = (screenHeight-60)/14;
I use .setHeight(buttonHeight); in the same loop, however it does not appear to do anything unless the value is very large. I've tried reducing the size of the text or the minimum height but this does not seem to have an effect.
Yes. In my experience you can't decrease the height of a Button beyond a certain value, even in XML, unless you also set minHeight=0.
Well - I don't know why the setHeight isn't working properly, but there is a better way that you could approach the whole sizing issue.
For the width, if you want a button to fill two-thirds of the width of the screen, then you should nest it within a horizontal LinearLayout, create a blank View, your Button, and another blank View that all fill_parent for width and set a layout_weight like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<LinearLayout android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal">
<View android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="2"/>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:text="Button"
android:id="#+id/button"/>
<View android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="2"/>
</LinearLayout>
</LinearLayout>
Personally, I prefer the above over modifying layout in Java code. And while I haven't tried it, I'm sure you could do the same thing for the height as well, but you would have a vertical layout and fill_parent on the height instead.

spinner gravity

spinner gravity only seems to work in the horizontal dimension. I would like the displayed text to on the top vertically and centered horizontally. Is this possible?
I use an xml spinner normal style like this:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:paddingBottom="5dip"
android:id="#+id/spinnerTarget"
android:gravity="top|center_horizontal"
android:textColor="#ffffff"
/>
you have wrap_content for height, so the view shrinks around the text and there is no space for the text to be positioned on the top of the view. android:gravity works only if there is enough space for the content within the activity. center_horizontal works because you have set fill_parent for the width and I assume the view takes all of the screens width, thus allowing the contents to be centered.
Try rearranging your layout so that there is more space for the view in terms of height.
cheers!
Here is what I ended up, hope i helps
<Spinner
android:id="#+id/m1_ss_spinner"
android:background="#drawable/btn_default_normal_invisible"
android:layout_width="fill_parent"
android:layout_height="60sp"
android:layout_gravity="top"
android:layout_below="#id/m1_ss_head"
/>

Categories