I can't place my textview in RelativeLayout in the center. I added this parameters
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
but it doesn't help.
My Layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/colorBackground"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="ru.myseolife.a99torrentsalpha2.MainActivity"
android:gravity="center"
tools:showIn="#layout/app_bar_main"
>
<TextView
android:id="#+id/nothing_found"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:text="#string/nothingfound"
/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<LinearLayout
android:id="#+id/linlaHeaderProgress"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone"
>
<ProgressBar
android:id="#+id/progressBar"
style="#style/Widget.AppCompat.ProgressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</RelativeLayout>
Anybody know what can it be? TextView is centering vertically, but horizontally it appears in the left side.
In your TextView layout_width is match_parent If you want to keep that and show it as centered you need to center the text inside the view not the TextView
So use android:gravity="center" inside your TextView or you can make TextView's width wrap_content and keep the way as it is.
Since it's a simple answer i'll tell you a trick > go to
settings > developer options > and tick Show layout boundaries then you can check the view bounds and get the idea where actually your view boundaries are !
the width of TextView should be "wrap_content"
Add
android:gravity="center"
to your textview, this will center the text since you are setting the width as match parent.
Add this:
android:gravity="center_horizontal"
Related
I have a simple construction; a TextView in a Scrollview, but I cannot make it work as I want.
The problems I get now;
The ScrollView keeps expanded when once the TextView was larger than the width of the screen, even when I set the text of TextView to nothing.
The TextView is on the left side.
I got the following:
<HorizontalScrollView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_gravity="right"
android:background="#drawable/background_scrollviews_display"
android:id="#+id/scrollViewSum">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right">
<TextView
style="#style/TextStyleDisplay"
android:textSize="#dimen/fontSizeTextViewSum"
android:paddingLeft="#dimen/paddingDisplayTextViews"
android:paddingRight="#dimen/paddingDisplayTextViews"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:gravity="right|center"
android:id="#+id/textViewSum"/>
</LinearLayout>
</HorizontalScrollView>
What I want;
The TextView on the right;
The ScrollView only expand when the TextView is larger than width of the screen;
I tried a lot, but I simply cannot find the right solution.
I hope someone can help me out!
I have a two clue to solve your problems:
1) make LinearLayout orientation Vertical, then you can position TextView on the right
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right">
<TextView
style="#style/TextStyleDisplay"
android:textSize="#dimen/fontSizeTextViewSum"
android:paddingLeft="#dimen/paddingDisplayTextViews"
android:paddingRight="#dimen/paddingDisplayTextViews"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:gravity="right|center"
android:id="#+id/textViewSum"/>
</LinearLayout>
2) make TextView wrap_content instead of match_parent
<TextView
style="#style/TextStyleDisplay"
android:textSize="#dimen/fontSizeTextViewSum"
android:paddingLeft="#dimen/paddingDisplayTextViews"
android:paddingRight="#dimen/paddingDisplayTextViews"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:gravity="right|center"
android:id="#+id/textViewSum"/>
make the textview width wrap_content
I have hard coded stuff like padding and text size etc. as I did't have your dimensions, you can change it later as per your requirements.
check if following code snipet helps.
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollViewSum"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textViewSum"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right|center"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="ldksjfkdlfkfldksjfkdlfkf"
android:textSize="20sp" />
</LinearLayout>
</HorizontalScrollView>
In my horizontal LinearLayout, I set the gravity in one view to be center_vertical, and then I tried to set layout_gravity for a second view, but that view is lined up with the centered text in the first view!
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top"
android:background="#drawable/border"
android:gravity="center_vertical"
android:text="layout_gravity=top gravity=center_vertical" >
</TextView>
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top"
android:background="#drawable/border"
android:gravity="top"
android:text="layout_gravity=top gravity=top" >
</TextView>
</LinearLayout>
And here is the same code but for a vertical layout. Notice that the desired behavior works correctly in the vertical layout.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_gravity="left"
android:background="#drawable/border"
android:gravity="center_horizontal"
android:text="layout_gravity=left gravity=center_horizontal" >
</TextView>
<TextView
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_gravity="right"
android:background="#drawable/border"
android:gravity="right"
android:text="layout_gravity=right gravity=right" >
</TextView>
</LinearLayout>
I can just use a RelativeLayout or maybe another nested LinearLayout to fix the problem. But I am asking this question because I would like to know if I am not understanding how gravity and layout_gravity works!! It is important to me that I understand how these fundamental attributes work. Thanks
If you want one centered and one top then you need to add a baseline aligned flag to the LinearLayout
android:baselineAligned="false"
It is default for the layout to align the children's baselines. This can cause issues such as this when the children use different values of gravity.
The difference with vertical is that the baselines can't be aligned as they can in horizontal.
See:
http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:baselineAligned
Additional info - This appears to explain the issue better than I can:
http://www.doubleencore.com/2013/10/shifty-baseline-alignment/
if you are trying something not "connected to each other" i.e. you want some space between two widgets then use a blank widget and use weights
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_weight="40"
android:gravity="center_vertical"
android:text="layout_gravity=top gravity=center_vertical" >
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="20" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_weight="40"
android:gravity="right"
android:text="layout_gravity=top gravity=top" >
</TextView>
</LinearLayout>
You werent clear about the output that you wanted , this is what i could interpret as your required output.. But i will explain you how this works ..
For more on weights you can go to http://developer.android.com/reference/android/widget/package-summary.html
There is something called as "widget" and "content of widget"
While talking of linearlayout with layout_gravity you set gravity of it self as a "widget" in the main View and with gravity you set gravity to the content in that linearlayout
Simmilarly with say textview , if you use layout_gravity (say equal to center) then the textview will be centered in its space (i.e. it will be centered in its width if layoutorientation is vertical , and it will be centered in its height if layout_orientation is horizontal)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:text="Second Activity" />
</LinearLayout>
See the same in vertical orientation
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:text="Second Activity" />
</LinearLayout>
Also now see about the gravity when you apply gravity to textview, the "TEXT" in the textview is centered (if gravity=center)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
**android:gravity="center"**
android:text="Second Activity" />
</LinearLayout>
Now you can realise that gravity sets the gravity to the content within the widget.. the "content" of a widget can be widgets itself..
LinearLayout is a widget , and within linear layout you add yet other types of widgets (button and text view)
So applying gravity=center to textview sets its content(i.e. text)at the center and applying gravity to linearlayout will center its content(i.e. textview) at the center
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:gravity="center"
android:text="Second Activity" />
</LinearLayout>
gravity is how the view itself positions its content within the area determined by its width and height. layout_gravity is how the parent view positions the child within the parent's area. Usually you can simply set gravity on the LinearLayout instead of setting layout_gravity on all of its children.
Do note that for LinearLayouts, gravity only has an effect on the axis that is not the orientation, e.g. top and bottom would be relevant to a horizontal LinearLayout, but left and right (or start and end) would not be relevant.
Hy everyone,
i have got a lot of text to diplay in one page, but i saw only first textview, other disapiard. Where is other textrview?
Code, i dont know why i see only first texttt and a lot offf blank page. And that isall:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="texttttttttt"
android:id="#+id/textView" style="#style/MetricPrefixTitle"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="texttttttttt"
android:id="#+id/textView1" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Valute"
android:id="#+id/textView" style="#style/MetricPrefixHeader"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="textttttttttttttt"
android:id="#+id/textView1" android:layout_gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="text"
android:id="#+id/textView" style="#style/MetricPrefixHeader"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="textttttttttttttttttt"
android:id="#+id/textView1" android:layout_gravity="center"/>
</LinearLayout>
</ScrollView>
You didn't set android:orientation attr in LinearLayout, the default one is horizontally so your textViews are positioned next to each other, if you scroll your text view from left to right you will see the other ones. What you want is to set android:orientation to vertical.
Besides the orientation, your layout_height-"match_parent" will cause your view to expand to the size the parent allows. You should use layout_height="wrap_content" when you want the view height to be the height of the contents.
I am creating a layout as below but the checkbox element is not visible on the scren where am i goign wrong ?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SeekBar android:id="#+id/seek"
android:layout_width="300dip"
android:layout_height="wrap_content"
android:progress="50"/>
...and a few more elements here.
</LinearLayout>
<CheckBox android:id="#+id/CheckBox"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="somestring" />
</LinearLayout>
The LinearLayout before the CheckBox has its height set to MATCH_PARENT(and it fills all the parent's height) and the parent LinearLayout of both has the orientation set to vertical so the CheckBox is pushed out of the screen.
Set the height of the LinearLayout containing the SeekBar to WRAP_CONTENT for example.
You need to set your inner Linear Layout's height and width to wrap content.
Try this.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<SeekBar
android:id="#+id/seek"
android:layout_width="300dip"
android:layout_height="wrap_content"
android:progress="50" />
</LinearLayout>
<CheckBox
android:id="#+id/CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="somestring" />
</LinearLayout>
Try This :
<?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="fill_parent"
android:orientation="vertical"
android:weightSum="10" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="9"
android:orientation="horizontal" >
<SeekBar
android:id="#+id/seek"
android:layout_width="300dip"
android:layout_height="wrap_content"
android:progress="50" />
</LinearLayout>
<CheckBox
android:id="#+id/CheckBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="somestring" />
</LinearLayout>
In your layout chose one element that you want to be flexible in size, say height wise. Then make its height="0dp" and weight="1". Make heights of rest of elements: height="wrap_content".
Also you may have given discreet size of some dp to your other elements, and hence their total height runs out of available screen space OR there are too many elements that they go beyond your screen height. In this case, wrap your layout in a ScrollView.
change your LinerLayout definition it cannot be android:layout_height="match_parent"
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1">
if you set match_parent for linearlayout it consumes all content so wrap_content is better.
But if you set it as I wrote checkbox will be at bottom page a linearlayout will consumes remaing part of screen.
I has the following XML:
<RelativeLayout android:id="#+id/cover_box"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView android:id="#+id/cover"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:id="#+id/download" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="#drawable/mark_download"
android:layout_centerInParent="true" android:layout_marginTop="90px" />
</RelativeLayout>
But it's look's like the marginTop is being ignored.
If you want the 2nd image to be 90dp under the center of the screen, you could replace it with a FrameLayout where you can control the padding to move your image downwards.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingTop="90dp">
<ImageView android:id="#+id/download"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/mark_download"/>
</FrameLayout>
When you use center in parent the view is put directly in the center. The margin top will only come into play if an object is within 90px of the top of your view. Thus pushing the centered view down to keep at least 90px of space on top of it. So it isn't being ignored but it is not having the effect that you think it should have.
I want the progressbar to shown under android:layout_centerInParent="true" so i have added a dummy TextView and set it to centerInParent .Then i put my progressbar under it. Now you can increase its distance from center in two ways . First by increasing marginTop in TextView and second increasing TextView height.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/widget"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash" >
<FrameLayout
android:id="#+id/splash_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<com.s3.tdd.interfaces.CircularProgressBar
android:id="#+id/circularprogressbar2"
style="#style/Widget.ProgressBar.Holo.CircularProgressBar"
android:layout_width="110dip"
android:layout_height="110dip"
android:layout_below="#+id/txt1"
android:layout_centerHorizontal="true" />
</RelativeLayout>
You can put your imageview inside of other ViewGroup (LinearLayout of RelativeLayout layout), leaving the margin of imageview, and doing android:centerInParent="true" for the ViewGroup:
<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">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:centerInParent="true">
<ImageView android:id="#+id/download" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="#drawable/mark_download" android:layout_marginTop="90px" />
</LinearLayout>
</RelativeLayout>
You can make a dummy view and center it to parent. Now align your view relative to dummy view using layout:alignComponent and give the marginTop. Now in the code move it according to the width of your view to center it.