Customizing a textview - android

I want to create a label like this:
Requirements are:
The rectangle should grow with the text
the length of the red line is dependent of a property
What I tried was to create a 9ng image for the background of the text and use the padding property for the length of the red line. The red line is drawn by overriding the onDraw method.
The result is that the red line is correct but the background is expanded including the padding like this:
What's the best way to do this? Building a custom view with a label included? a custom view drawn 100% programmatically (in this case, what's the method to resize a 9ng image?)?
Thanks
Julien

try this one
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="The text"
android:textColor="#000000"
android:background="#drawable/<your_drawable>"
android:textSize="16sp" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:background="#f00"
android:layout_weight="1" />
</LinearLayout>
Hope it will help you

Best way to do this is probably using two different controllers.
You set a simple textView with your rectangle as background and with the width set to wrap-content.
After this you create the line positioning her where you want (to the right of the controller or to the end of the page or where you please) and you resize it as you want.
For any doubt I'm here

Related

How to set text leading or shift a text position on a TextView

How do you set a text leading on a TextView? Or how do you shift a text position relative to a view that it is in?
I want the height of the textview to be less than what wrap_content would set at default without cutting the text from the bottom.
I've tried using the layout_height tag on its xml layout but it would clip the text from the bottom just like the image I've provided on the link below, and I also have tried using the lineSpacingExtra tag but it seems to only work for the distance/leading between two lines.
Here is my xml file:
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:text="clipped text"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:lineSpacingExtra="-18dp"
android:text="a text longer than usual to demonstrate line spacing"/>
Image for clarity: https://i.stack.imgur.com/QGV4B.png
Why you can use wrap_content? Or can you share what are you trying to achieve?
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clipped text"/>
Did you try to do it using padding or paddingTop ? Try it with different values to get your desired result.
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:paddingTop="0dp"
android:text="clipped text"/>

TextView: Remove spacing and padding on top and bottom

I have one textview which size is 50 dp. I got this result like the picture below.
I want to remove bottom spaces in my textView ,because its height depend on the text size. And I want to receive background only into my quotation mark.
Is there any way to remove the unused spaces in my textView?
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="top"
android:text="“"
android:fontFamily="#font/arialbd"
android:background="#ff0"
android:textColor="#E8E9EF"
android:includeFontPadding="false"
android:textSize="50dp" />
Try this:
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="top|center"
android:text="“"
android:fontFamily="#font/arialbd"
android:background="#ff0"
android:textColor="#E8E9EF"
android:includeFontPadding="false"
android:textSize="50dp"
android:gravity="top" />
This solution works on my case. To remove bottom padding. Set the layout_height the same size as your textSize or 1dp or 2dp less.
Set includeFontPadding="false". Set android:lineSpacingExtra="0dp"
In your case you have to implement custom textView. check this example for reference.
U can try setting your bottom margin to a negative value.
Something like this:
android:layout_marginBottom="-10dp"
Let me know if its works, that is the simplest way but can be anothers...

How to center a text vertically in a narrow textview in Android?

I have a TextView which has a height of 30px and a textsize of 40px. Since the text is taller than the View, I only want to display the middle part of the text.Like this:
But with android:gravity="center_vertical", I only can display the upper part of the text with some bottom part cut off.
And this is my code:
<TextView
android:layout_width="wrap_content"
android:layout_height="30px"
android:textSize="40px"
android:text="ABCDEFG"
android:gravity="center_vertical"
/>
Anyone knows of any way to do that? Thanks!
Can't you simply wrap it in a LinearView set to the appropriate size and adjust the margin within the textview to about half (depending on padding) the font size?
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:background="#android:color/white">
<TextView
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:text="Sliced in half"
android:textSize="60dp"
android:layout_marginTop="-30dp"/>
</LinearLayout>
Resulting in:
http://i.stack.imgur.com/jEdxx.png
I highly doubt whether you can do that. All the options that I have come across while dealing with TextView are for when the Text is smaller than the View. This is the first question that I have come across which has the requirement otherwise.
One option which I had used before was android:scrollHorizontally="true" where in you specify whether the text is allowed to be wider than the View.
Interesting question. It would be great if you could add some code though.

Limit the text width of TextView (Not the background image)

I have a TextView with a background that declared as:
<TextView
android:id="#+id/questionText"
android:layout_width="250dp"
android:layout_height="100dp"
android:gravity="center"
android:textSize="26px"
android:textColor="#FFFFFF"
android:background="#drawable/blue"
/>
the problem is that the background image is like a frame, so i want the
text inside to have a margin from the background image dimensions.
Is this possible?
10X alot,
have a gr8 weekend :)
What you want sounds like padding.
<TextView
android:id="#+id/questionText"
android:layout_width="250dp"
android:layout_height="100dp"
android:gravity="center"
android:textSize="26px"
android:textColor="#FFFFFF"
android:background="#drawable/blue"
android:padding="5dp"
/>
This would put a 5 pixel border around the actual text showing the background through.
Incidentally, layout_margin is also a parameter and is used to put space between the entire view, background included, and other views.
I'd recommend to make 9-patch drawable. It defines where the text can be without specifying padding in the control.

How to setup this layout in android?

In Android I'm trying to get a layout looking like this image using xml.
What I've been trying to do is to separate the view vertical into two parts. One for the blue part and one for the white. For the blue part I use a shape as background to get the gradient and for the white part just a white background. It works good for the "Title" and "Some text" part. But as the image view should overlay both layouts it isn't working.
I can't figure out how to setup the xml layout to get it working, any ideas?
I would do it like this:
<RelativeLayout>
<TextView
android:id="#+id/title"
android:layout_alignParentTop="true"
android:text="title"
/>
<TextView
android:layout_below="#id/title"
android:layout_alignParentBottom="true"
android:text="some text here"
/>
<ImageView
android:layout_alignParentLeft="true"
android:text="title"
/>
</RelativeLayout>
Of course, you'll need to set the layout width, height, ... on those elements.
I think that you'll find the answer here: set the absolute position of a view in Android

Categories

Resources