Text width on imageview widget - android

I have a problem regarding Textview on my battery widget.
The has a picture and on it textview showing the % the problem is that there seems to be something wrong with my code regarding the text width because it allows only 2 characters per verse so it shows for ex. 50 in the first verse and % in the second. How do i set it right? Also side question: can i somehow make the text to always be on the bottom and center? I can do it with padding but it shows differently on each screen size. So anyway here's the code regarding text EDIT: Here is entire xml file-:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/imageView1_widget"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="fitXY"
android:src="#drawable/bateria50_1" />
<TextView
android:id="#+id/textView1_widget"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1_widget"
android:singleLine="true"
android:layout_gravity="bottom|center_horizontal"
android:shadowColor="#000000"
android:shadowDx="2"
android:shadowDy="2"
android:shadowRadius="1"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>

For LinearLayout:
android:singleLine="true" and android:layout_gravity="bottom|center_horizontal"
For RelativeLayout:
android:layout_alignParentBottom="true" and android:layout_centerHorizontal="true"

Related

TextView wraps short text without reason, while not wrapping longer text

Currently I'm creating an app and ran into a weird issue. My activity has this layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:customfont="http://schemas.android.com/apk/res-auto"
android:id="#+id/relativeLayout_all"
android:layout_width="320dp"
android:layout_height="407dp"
android:background="#color/background"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/rel_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" >
<TextView
android:id="#+id/id_result"
style="#style/font_Result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="false"
android:layout_centerVertical="false"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="105dp"
android:gravity="center"
android:maxLines="1"
android:maxWidth="216dp"
android:textColor="#color/grey" />
<TextView
android:id="#+id/id_unit"
style="#style/font_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/id_result"
android:layout_alignBottom="#+id/id_result"
android:layout_alignStart="#+id/id_image"
android:maxLines="1"
android:maxWidth="80dp"
android:textColor="#color/grey" />
<ImageView
android:id="#+id/id_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/id_unit"
android:layout_marginBottom="27dp"
android:layout_toEndOf="#+id/id_result" />
<ImageView
android:id="#+id/id_image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/id_image"
android:layout_alignTop="#+id/id_result" />
</RelativeLayout>
</RelativeLayout>
In my onCreate() I call the Views by their ID and set the texts and the drawables. Depending on a meassured result, there are two scenarios:
1) Show the meassured result (let's say 42.9 GW/ms)
TextView id_result: '42.9'
TextView id_unit: 'GW/ms'
ImageView id_image: #drawable/image1_1
ImageView id_image2: #drawable/image2_1
Everything is fine
2) The result is too high
TextView id_result: 'HI'
TextView id_unit: visibility GONE
ImageView id_image: visibility GONE
ImageView id_image2: visibility GONE
Now here is the problem. I expected the text 'HI' to be shown centered on screen. Instead, only "H" is shown. When I set android:maxLines="2", it is revealed, that Android wraps the text after 'H'. 'I' sits on the second line. Why is this the case? '42.9' is longer and doesn't get wrapped.
Does anyone has a solution?

Android Nested LinearLayout, Gravity not having expected results

Having a problem with nested LinearLayouts to use as a custom row in a ListView.
App is a kind of internal corporate phone book and this row will be repeated in the ListView.
The "Details Witheld" on the right looks fine when gravity is set to left for it, but if gravity is set to center then it appears half off the page.
I'd guess my parent LinearLayout is too wide for the screen but it's set as fill_parent so I don't see how.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
Full XML source is below.
Aligned Left looks like this and I'd like Details to be a space in so it's centered above the Witheld.
Aligned Center looks like this
Full XML Source
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- Left side name & post / parish -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="80"
android:orientation="vertical" >
<TextView
android:id="#+id/txtrowname"
android:layout_marginBottom="2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Smtih, Mr John"
android:textSize="24sp"
android:textColor="#color/darkblue_text"
android:textStyle="normal" />
<TextView
android:id="#+id/txtrowpost"
android:layout_marginBottom="2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12 Some Street, SomeTown. SS11 1SS"
android:textSize="12sp"
android:textColor="#000000"
android:textStyle="normal" />
</LinearLayout>
<!-- Details withheld on right -->
<TextView
android:id="#+id/txtrowwitheld"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:text="Details Witheld"
android:layout_weight="20"
android:textSize="18sp"
android:gravity="center"
android:lines="2"
android:maxLines="2"
android:textColor="#FF0000"
android:layout_marginBottom="2dp"
android:textStyle="normal" />
</LinearLayout>
Image with layout_gravity as suggested by Ramesh
try layout_gravity in place of gravity that will work and dont use line="2". it will automatically fit into 2 lines if width exceeds.
I ended up just making do without it being centered.

Checking if a string is overflowing from my view in android?

I have a listview in Android which is dynamically loaded. The list view holds an image and on it is shows the description of the image which should ideally take up 20% of the images' height. How do I check if my textview which is on top of the image view in my list is taking a max height of 20% of the image?
As you can see from the screenshot, "Harvey's - -check in for a chance to win a free burger
Picker, ON" is fitting in the textview in 2 lines, sometimes if my android device is small it exceeds the background (black colored) view and looks bad. I need to stop that so I decided I must truncate the string if it overflows on my textview.
How do I truncate the strings on the textview so that once that 20% of the imageview height has been reached the image description ends with "..."
The xml I have at the moment is given below. Note that com.parse.myPackage.AspectRatioImageView is just a custom view for an imageview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFFFF" >
<com.parse.myPackage.AspectRatioImageView
android:id="#+id/campaignImageLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/blank" />
<FrameLayout
android:id="#+id/frameLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/frameLayout2"
android:layout_alignParentLeft="true"
android:background="#color/fadedBlack">
<TextView
android:id="#+id/campaignNameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading..."
android:layout_marginLeft="10dp"
android:textColor="#color/white"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</FrameLayout>
<FrameLayout
android:id="#+id/frameLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/campaignImageLabel"
android:layout_alignParentLeft="true"
android:background="#color/fadedBlack">
<TextView
android:id="#+id/campaignLocationLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Location Loading..."
android:textColor="#color/white"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</FrameLayout>
Try this on your TextView
android:maxLines="2"
android:ellipsize="end"
This will truncate your text and place "..." at the end if it is longer than 2 lines
You can use :
android:ellipsize="end"
android:maxLines="1"
Change value of maxlines to check manually that what makes it 20% of your imageview's height. ellipsize will take care of the '...' termination.

TextView width confusion

I'm creating a detail view using the GridLayout and textviews. My GridLayout will have about 6 rows and 2 columns. On my first row, I have two textviews. The first (placard) has an background image which I'm centering text on top of it. My image is about 150 px wide. The second textview (material) can have anything from one word to 20 words. I would like the text to wrap (up to 5 lines).
My problems is that the text in the second textview (material) is going off the screen. I have tried many different settings trying to get the width to adjust without success. Here is my XML layout:
<?xml version="1.0" encoding="UTF-8"?>
<android.support.v7.widget.GridLayout
xmlns:grid="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alignmentMode="alignBounds"
android:background="#color/lemon"
grid:columnCount="2"
grid:rowCount="6" >
<TextView
android:id="#+id/placard"
grid:layout_gravity="right"
grid:layout_column="0"
grid:layout_row="0"
android:padding="5sp"
android:gravity="center"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="#+id/material"
grid:layout_column="1"
grid:layout_row="0"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:ellipsize="none"
android:scrollHorizontally="false"
android:padding="5sp"
android:singleLine="false"
android:maxLines="5"
android:textColor="#0000ff"
android:textSize="12sp"
android:textStyle="bold" />
</android.support.v7.widget.GridLayout>
Any suggestions would be appreciated. Thanks in advanced.
There is a link I found that explains how to solve this problem... Basically, when you are using the support version of GridLayout you need to specify your own namespace.
http://www.mentby.com/scott-olcott/gridlayout-text-clipping-using-support-library.html

Android layout space-filling problem

I'm having real difficulty coming up with a layout which works. I have a view which fills the width of the screen. It contains three sub-views:
Some text
A number in parentheses after the main text
A button
The button is right-aligned, and the text items are left-aligned one after the other, as shown:
| Some heading text (n) [button] |
The problem is controlling what happens when the text is too long. I want it like this, so that the number is always visible just to the right of the main text. The main text should be truncated if needed so the other two views remain visible.
| Some very very long headin... (n) [button] |
The closest I've got which succesfully truncates the main text results in the (n) always being right-aligned next to the button even when the main text is short enough to fit. That's not what I want.
How would you approach this?
I'm not posting any of my current XML yet, lest it prejudice anyone's suggestions.
I do not believe there's any xml layout for that. My guess is that you will need to extend TextView and measure the text length inside onDraw(...), adjusting the text accordingly through some iteration (i.e., removing one character at a time until the text fits the canvas)
I just found another question that is quite similar to yours: Ellipsize only a section in a TextView . No other answer than ellipsize in the middle.
Another thoughts:
I'm wondering if it would work to have one textview with the main text (ellipsize left, wrap_content) and another with the number in the parenthesis (wrap_content), both inside an horizontal linear layout. That layout would be inside a relative layout and layout_toLeftOf the button, which would be wrap_content, layout_alignParentRight.
Does it make any sense? I don't have Eclipse now to test it myself. Not sure if the (n) textview would be lost behind the button or not with a long text.
Alternatively (and less interesting), you can setup one single relative layout with the two textviews all layout_toRightOf and the button aligned to the right (layout_alignParentRight) and set the max witdth ot the first textview (android:maxWidth). You would need to set up different layouts for different screens, though.
An example with a fixed max width that will work as required:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click me"
android:id="#+id/bt1"
android:layout_alignParentRight="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="short text"
android:lines="1"
android:ellipsize="end"
android:id="#+id/t1"
android:layout_alignParentLeft="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="(n)"
android:lines="1"
android:id="#+id/n1"
android:layout_toRightOf="#id/t1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click me"
android:id="#+id/bt2"
android:layout_below="#id/bt1"
android:layout_alignParentRight="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="very long text that will not fit in any layout, regardless of the size of the screen"
android:lines="1"
android:ellipsize="end"
android:id="#+id/t2"
android:layout_below="#id/bt1"
android:layout_alignParentLeft="true"
android:maxWidth="220dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="(n)"
android:lines="1"
android:id="#+id/n2"
android:layout_below="#id/bt1"
android:layout_toRightOf="#id/t2"
/>
</RelativeLayout>
Try a linearlayout, set the weight of the text view as 1,, and set ellipsis as TruncateAt.MIDDLE. Check this layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:id="#+id/text" android:layout_width="0dp"
android:layout_height="wrap_content" android:lines="1"
android:ellipsize="middle" android:gravity="left"
android:layout_weight="1" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
The key is the ordering of the items as this is the order they are measured, in order to ensure your button and (n) text get enough space in the overall layout:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
/>
<TextView
android:id="#+id/middle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_toLeftOf="#id/button"
android:text="(n)"
/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/middle"
android:layout_alignParentLeft="true"
android:gravity="left"
android:ellipsize="end"
android:text="Some really long to text to make this flow over"
android:lines="1"
android:maxLines="1"
/>
</RelativeLayout>

Categories

Resources