Underline within a Textview in android studio XML - android

I want to put a horizontal line within a textview in android studio. I want to create a textview layout like this.

You can use this code to get TextView with a line .
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Device"
android:layout_centerVertical="true"
android:layout_margin="3dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/tv_textview"
android:layout_margin="3dp"
/>
</RelativeLayout>

create linear layout with match_parent width
set orientation vertical
put text view child with wrap_content width
put view with 0dp width and weight = 1
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#000"
android:gravity="bottom" />

Draw custom drawable and set to drawableEnd to textview
Drawable can something like this..
and do not forgot to set textview width match parent

Do it Programitically Like..
(Use _ for Making a Line)
yourTextView.setText("Device_________________________");
yourTextView.setTextColor("Your Color Here");
and In XML file Set
Android:width = "MatchParent".
for your Text View.
Do this only if you are creating static layout.

Related

Scrollview doesn't show scrollbar

my Android TextView xml is very simple like below, I used style ScrollView but it won't show scrollbar, even when the text is exceeding the bottom and there is no way for me to see what's beyond. How to enable the scrollbar please?
I think "match_parent" has issue but don't know how to fix. Thanks!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
<TextView
android:id="#+id/textResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/buttonRecord"
android:layout_marginTop="16dp"
android:background="#drawable/back"
android:editable="true"
android:height="150dp"
android:text=""
android:enabled="false"
android:scrollbars="vertical"
style="#android:style/Widget.ScrollView"
android:layout_alignRight="#+id/buttonRecord"
android:layout_alignEnd="#+id/buttonRecord"
android:layout_alignLeft="#+id/buttonRecord"
android:layout_alignStart="#+id/buttonRecord" />
</RelativeLayout>
If you want the content to scroll, perhaps try giving it a fixed height? Right now the height is set to wrap_content due to layout_height. I don't think you should be using android:height at all
If you want a fixed height TextView to scroll vertically with scrollbar,
define your XML layout file:
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:text="#string/text_string" />
define your Java class file:
TextView tv = (TextView) findViewById(R.id.textView);
tv.setMovementMethod(new ScrollingMovementMethod());

Android allow layout cut off higher child

How i can insert an textview with height=100dp in parent layout with height=60 and display cropped textview?
I want to resize parent layout without resizing textview inside.
Set a negative value to the layout_marginTop of your TextView, and after resizing the parent you should also set that layout_marginTop to zero. The layout is like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_marginTop="-40dp"
android:layout_height="100dp"
android:text="text" />
</LinearLayout>

TextView above ImageView with dynamic height

I have a ScrollView with some elements.
My problem is that i can't set textview's above ImageView's with dynamical height.
I have 3 ImageView with different height, they can be visible or gone.
Above them i need to put text with background color, this text need to be at the bottom part of the ImageView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/pic"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:id="#+id/third_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:padding="8dp"
android:layout_alignParentBottom="true"
android:layout_above="#id/third_picture"
android:gravity="center_horizontal"
android:background="#android:color/white"
android:text="example text">
</TextView>
I tried a lot, but my View was always broken.
Please help, i need some proffesional advise.
You can do this in textview:
android:layout_alignBaseline="#id/pic"
This will align the baseline of textview with basline of imageview
You can set your picture as a background for a layout (e.g linear layout ) rather than using an ImageView and then put the textview inside it and place it where ever you want.
<LinearLayout
android:background="your picture" >
<!-- align it to the bottom -->
<TextView
/>
</LinearLayout>

display textview above a button without hardcoding the height in xml

I am displaying a textview(I intend this to fill the entire screen excluding the button below it) and a button(small one at the bottom) in an activity. I want textview to be placed aove the button. I don't want to hardcode any height/width.
Hence
For button I have kept height ad width as word_wrap
For textview I have kept width as fill parent.
What I want to know is that, is there anyway by whcih I can specify textview height to be screenheight-button height. I want to this in xml file but not dnamically inside my code.
To achieve this you have to create something like this :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/button1"
android:text="TextView" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
and you have a textview which will stay above your button and will fit the entire screen.
Use RelativeLayout, set textview's dimensions as fill_parent, then place button below it,
set button's android:layout_below="#+id/textview" and button's dimensions as wrap_content.
Check the result in visual designer or in device, it should work
You can use layout_weight attribute
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:text="TextView" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>

Android: Align one element below and in the middle of another

I have an ImageView and TextView. I want to place the TextView below the image view but with the middle of the TextView aligned with the middle of the ImageView (along the horizontal axis). If the text in the TextView changes to something much larger or smaller, the middle of the text always needs to remain aligned with the middle of the ImageView. Is this possible in xml?
Yes, you are simply looking to contain both elements in a vertical LinearLayout which has android:gravity set to center_horizontal.
Something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
... >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
... />
</LinearLayout>
Because the TextView's width is wrap_content, setting its gravity shouldn't be necessary. I would do it just for safety (and additionally, I may set the width to match_parent as well).
You can make use of RealtiveLayout as follows..
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
... >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... />
<TextView
android:layout_below="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
If you want your textview to be of single line only then add following properties to TextView
android:ellipsezed="end"
android:singleLine="true"
Use a given layout and add it to any of your existing layout you get result what your looking for ..
Hope this explanation works for you..
set TextView android:layout_width="fill_parent" and set android:gravity="center" in TextView.
I think it help you.
Thanks

Categories

Resources