Android, can't make text vertically center in TextView - android

I have a problem with making verticaly center text in TextView.
Android Studio in preview showing text correctly, in center like I want:
But when I will put app to device (Htc One m8, Android 6) then text is bottom (not in center of orange box (I set TextView to orange for better see where is problem).
Below text of xml layout, can see find where is a problem? ( I removed part of code, just added main relative and linear and line where is a problem)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="#dimen/main_relative_layout_height_medium"
android:layout_margin="#dimen/main_relative_layout_margin_medium"
android:orientation="vertical"
android:background="#color/widget_relative_layout_background_color"
android:paddingTop="#dimen/main_relative_padding_top_medium">
<!--android:updatePeriodMillis="86400"-->
<LinearLayout
android:id="#+id/contentContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="#dimen/main_linear_layout_padding_medium"
android:paddingTop="#dimen/main_linear_layout_padding_top_medium"
android:paddingBottom="#dimen/main_linear_layout_padding_bottom_medium"
android:background="#color/widget_linear_layout_background_color"
android:weightSum="15"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
...
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="0dp">
<LinearLayout
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="#dimen/times_row_size_medium"
android:paddingTop="0dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:layout_width="#dimen/icon_width_size_medium"
android:layout_height="wrap_content"
android:id="#+id/firstWidgetImageView"
android:layout_column="1"
android:src="#drawable/ic_time_to_departure_white"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:text="02:41"
android:layout_column="0"
android:textColor="#color/widgetTextColor"
android:id="#+id/firstTimeTextView"
android:textSize="#dimen/first_time_text_size_medium"
android:background="#color/application_button_color" />
</LinearLayout>
...
</LinearLayout>
</FrameLayout>

You have used center_horizontal in your TextView change it to center and you should be fine.
Code example:
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="02:41"
android:layout_column="0"
android:textColor="#color/widgetTextColor"
android:id="#+id/firstTimeTextView"
android:textSize="#dimen/first_time_text_size_medium"
android:background="#color/application_button_color" />

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="02:41"
android:layout_column="0"
android:textColor="#color/widgetTextColor"
android:id="#+id/firstTimeTextView"
android:textSize="#dimen/first_time_text_size_medium"
android:background="#color/application_button_color" />
Please take a look at the above code . You need to replace gravity from center_horizontal to center .

Related

Linear Layout behind ImageView in Relative Layout

Let me explain i have a xml like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgCircle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/img_circle" />
<LinearLayout
android:id="#+id/llFirstScreen"
android:layout_alignTop="#id/imgCircle"
android:layout_alignBottom="#id/imgCircle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/imgCircle"
android:layout_toEndOf="#id/imgCircle"
android:background="#drawable/layout_view_round_corner"
android:orientation="vertical">
<TextView
android:id="#+id/mapCurrentLocation"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="TextView1"
android:textColor="#color/lblTextColor"
android:textSize="#dimen/lblTextLarge"
android:textStyle="normal" >
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="TextView2"
android:textColor="#color/lblHintColor2"
android:textSize="#dimen/lblTextMedium"
android:textStyle="normal" >
</TextView>
</LinearLayout>
</RelativeLayout>
And this gives me Image and TextViews next to it
But i want to accomplish something like this
that TextView Background is extended (padding maybe) at the half of the Rounded Image (behind it) so that it looks like its "part" of the image. Or I was thinking of Making a view with background white that goes behind TextView so that it looks that way. But wanted to ask here maybe if there is a better way to do this.
I hope i was clear in explaining this (That is why I added pictures).
I have tried to use demo with your xml file and you can check below code. I had to give static height and width of the ImageView to achieve result.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/llFirstScreen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/imgCarCircle"
android:layout_alignTop="#id/imgCarCircle"
android:layout_marginLeft="30dp"
android:background="#color/colorAccent"
android:orientation="vertical"
android:paddingLeft="40dp">
<TextView
android:id="#+id/mapCurrentLocation"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="text1"
android:textStyle="normal"></TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="text2"
android:textStyle="normal"></TextView>
</LinearLayout>
<ImageView
android:id="#+id/imgCarCircle"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#mipmap/ic_launcher_round" />
</RelativeLayout>
try these:
first solution: change this
<LinearLayout
android:id="#+id/llFirstScreen"
android:layout_alignTop="#id/imgCircle"
android:layout_alignBottom="#id/imgCircle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/imgCircle"
android:layout_toEndOf="#id/imgCircle"
android:background="#drawable/layout_view_round_corner"
android:orientation="vertical">
to this:
<LinearLayout
android:id="#+id/llFirstScreen"
android:layout_alignTop="#id/imgCircle"
android:layout_alignBottom="#id/imgCircle"
android:layout_width="match_parent"
android:layout_marginLeft="20dp" // or just figure your image width and set this programatically
android:layout_height="wrap_content"
android:background="#drawable/layout_view_round_corner"
android:orientation="vertical">
second solution: it to use AbsoluteLayout which is a bit hard to handle.

Android relative layout, positioning something in the middle of 2 views [duplicate]

This question already has answers here:
issue with "#id" vs "#+id" in positioning in Relative Layout
(3 answers)
Closed 7 years ago.
I have 2 ImageButtons and a Seerkbar inside a relative layout and I am trying to position them so that :
1) first imageButton is on the left, it's left side and the parent's left side align.
2) second imageButton is on the right, it's right side and the parent's right side align.
3) the Seekbar is in the middle, and it covers all of the remaining space.
It should look something like this:
I tried this:
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton android:id="#+id/image_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#drawable/image_1"/>
<SeekBar android:id="#+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/image_1"
android:layout_toLeftOf="#id/image_2"
android:max="95"/>
<ImageButton android:id="#+id/image_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/image_2"/>
</RelativeLayout>
This works fine on my intellij device preview, BUT fails to compile because of the line:
android:layout_toLeftOf="#id/image_2"
as I am using the id/image_2 before declaring it. Is there a solution to this ?
TRY THIS....
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton android:id="#+id/image_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#drawable/image_1"/>
<SeekBar android:id="#+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/image_1"
android:layout_toLeftOf="#+id/image_2"
/>
<ImageButton android:id="#+id/image_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/image_2"/>
</RelativeLayout>
I think you forgot to put + sign before id like this:
android:layout_toRightOf="#+id/image_1"
android:layout_toLeftOf="#+id/image_2"
This would help you deal with different resolution, give it a try
i have put the imageview instead of seekbar lol
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
android:gravity="center"
android:background="#FFF"
android:layout_margin="1dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="40"
android:background="#aef"
android:layout_margin="4dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView"
android:background="#984"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="100dip"
android:layout_weight="20"
android:background="#fec"
android:layout_margin="4dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView2"
android:background="#594"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="40"
android:background="#9ef"
android:layout_margin="4dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/imageView3"
android:background="#994"/>
</LinearLayout>
</LinearLayout>
I don't recommend in the RelativeLayout to reference two components such as toRightOf and toLeftOf. Always try to reference each to only one. In your case you can also use LinearLayout with weights for a uniformed distribution for the chils as the following
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageButton
android:id="#+id/image_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
<SeekBar
android:id="#+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:max="95" />
<ImageButton
android:id="#+id/image_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
</LinearLayout>

RelativeLayout doesn't obey parent size (with layout_weight)

I haven't been able to find a similar problem to the one I'm having on StackOverflow - basically, I have a simple XML layout with a vertical LinerLayout which uses layout_weight to make a 10/50/40 split, which is what I want (as seen below):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".1"
android:text="#string/title"
android:textColor="#color/black"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".5"
android:orientation="vertical"
android:background="#color/green">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:fillViewport="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
</HorizontalScrollView>
<ImageView
android:id="#+id/preview_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/level_preview_back"
android:focusable="false" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".4"
android:text="#string/title"
android:textColor="#color/black"
android:textStyle="bold" />
</LinearLayout>
BUT - I want the HorizontalScrollView and the ImageView to be overlayed (i.e. take up the exact same space on on top of the other), so I insert a RelativeLayout as such:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".1"
android:text="#string/title"
android:textColor="#color/black"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".5"
android:orientation="vertical"
android:background="#color/green">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:fillViewport="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
</HorizontalScrollView>
<ImageView
android:id="#+id/preview_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/level_preview_back"
android:focusable="false" />
</RelativeLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".4"
android:text="#string/title"
android:textColor="#color/black"
android:textStyle="bold" />
</LinearLayout>
But when I do this, the layout_weight (10/50/40 split) breaks and suddently the LinearLayout that now contains the RelativeLayout takes up virtually all the screen in the previews.
Anyone experienced this problem or know a good workaround? I've also tried replacing the LinearLayout with the RelativeLayout directly and I have the same problem.
EDIT: Looking further into this issue, there seems to be more to it. As I commented below, I was able to get around the problem by wrapping LinearLayouts around the TextViews, though I'm not sure why that worked. Updating my SDK and plugin didn't help (though I did need to do it....)
Moving further along with my task, I hit a second problem - I was implementing a second version of the overlapped ScrollView & ImageView (which were done with a FrameLayout as suggested by Karsten) but the layout_weight problems began anew.
Testing various arrangements, it seems that the issue is very much related to the image I'm using, which is quite large in resolution. Replacing them with a smaller image corrects the issues - it looks like using large images in either ImageViews or as backgrounds to LinearLayouts tends to ruin the layouts for the entire screen.
EDIT #2: I've actually loaded the XML on to two different devices and it renders exactly as it should.... This seems to be a bug with the Eclipse XML layout viewer?
(Please excuse the poor XML formatting - the Eclipse Android xml editor is frustrating in that regard...)
The XML you've posted works fine for me in the Eclipse preview. Are you running the latest version of the Android plugin?
Note that you can replace the middle LinearLayout and RelativeLayout with a single FrameLayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".1"
android:text="string/title"
android:textStyle="bold" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".5"
android:background="#00ff00" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:fillViewport="true" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
</HorizontalScrollView>
<ImageView
android:id="#+id/preview_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:focusable="false" />
</FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".4"
android:text="string/title2"
android:textStyle="bold" />
</LinearLayout>

ActionBarSherlock with custom titlebar - weight distribution no working

I'm trying to implement a custom title bar with ActionBarSherlock. The title bar should have two buttons in its left and right edges, and a textview in the center. The textview should occupy the space between the two buttons.
I've tried setting the layout_weight, but for some reason it had no effect at all.
This is the custom layout I have in res/layout
<LinearLayout 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="horizontal" >
<Button
android:id="#+id/btn_left"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Left" />
<TextView
android:id="#+id/header"
android:layout_width="0dip"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_weight="4"
android:text="Header Text" />
<Button
android:id="#+id/btn_right"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Right" />
</LinearLayout>
Right now all the elements just float to the left and I have no idea why, what am I doing wrong?
Instead of using a LinerLayout try using a RelativeLayout.
Edit: made center text view strech to buttons (make sure to leave TextView match_parent..
<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">
<Button android:id="#+id/leftButton"
android:layout_alignParentLeft="true"....
<TextView android:layout_centerHorizontal="true"
android:layout_toLeftOf="#id/rightButton"
android:layout_toRightOf="#id/leftButton" ....
<Button android:id="#+id/rightButton"
android:layout_alignParentRight="true"....
</RelativeLayout>

Android - stretched EditText above Button

I want to put a scrollable EditText above a row of buttons such that the text component fills all the vertical space left free by the buttons. This is proving surprisingly difficult, not least because the ADT is buggy: it suggests that this layout will do and it does in Eclipse but not in the real application!
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/view1"/>
<LinearLayout android:id="#id/view1"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<!-- buttons go here -->
</LinearLayout>
</RelativeLayout>
In this layout the text stretches to the full screen, as you might expect, i.e., it goes under the button but the ADT graphical layout tool shows it stopping just above the button. OTOH if you give it a wrap_content height then of course it doesn't stretch. layout_weight is of no help either.
So, is there a way? You must test your answer in the emulator or on a real device, not merely in Eclipse.
Precision: what makes me think this is not working and that the text widget extends under the buttons is the shape of the scrollbar at the bottom (see pic. below): it should be rounded at both ends and instead it looks like the bottom extends farther. If you compares its shape at the bottom with the shape it has at the top (which unfortunately I was unable to capture: it disappears too fast) it is very clearly different. OTOH the text widget boundary looks as expected. This is very confusing. Maybe just a minor bug in Android?
try this code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:inputType="textMultiLine" >
<requestFocus></requestFocus>
</EditText>
<LinearLayout
android:layout_height="wrap_content"
android:id="#+id/linearLayout1"
android:layout_width="match_parent">
<Button
android:text="Button"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/button1"
android:layout_width="0dp"></Button>
<Button
android:text="Button"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:layout_width="0dp"></Button>
<Button
android:text="Button"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/button3"
android:layout_width="0dp"></Button>
</LinearLayout>
</LinearLayout>
This should work
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:id="#+id/view1" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button android:text="Button" android:layout_weight="1"
android:layout_height="wrap_content" android:id="#+id/button1"
android:layout_width="0dp"/>
<Button android:text="Button" android:layout_weight="1"
android:layout_height="wrap_content" android:id="#+id/button2"
android:layout_width="0dp"/>
<Button android:text="Button" android:layout_weight="1"
android:layout_height="wrap_content" android:id="#+id/button3"
android:layout_width="0dp"/>
</LinearLayout>
<EditText android:id="#+id/text1" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_above="#id/view1"
android:gravity="top"/>
</RelativeLayout>
I did the same thing with TextView and a Button at the bottom.I tried to put EditText in place of TextView there and it works fine for me. You can try this -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/button"
android:text="Add New Template"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_above="#id/add_acronym"
/>
</RelativeLayout>
Here is the snapshot-

Categories

Resources