Center a TableLayout within a LinearLayout - android

Android newbie here.
<LinearLayout
android:layout_below="#+id/mMainTop"
android:layout_above="#+id/mMainBottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
// some TextView
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mSubTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
how can I center my TableLayout vertically with reference to the remaining space left in the parent LinearLayout?
I hope someone can understand what I mean.

Try and make android:layout_gravity="center" to android:layout_gravity="center_vertical" in your TableLayout
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mSubTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">

add the following attributes to LinearLayout
android:layout_height="match_parent"
android:gravity="cneter_vertically"

You should set height the LinearLayout match_parent

Related

Horizontal Centering TextView in RelativeLayout doesn't work

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"

set TextView in HorizontalScrollView to the right

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>

Align objects into middle of layout

I am dealing with ADK and I have a problem. I use LinearLayout, and I want to put my objects into middle and center of layout.
Here is my code
<?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:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal|center_vertical"
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal|center_vertical"
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal|center_vertical"
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal|center_vertical"
</LinearLayout>
Still it goes to the TOP CENTER or to left. Should I use something else instead of LinearLayout? Or is there special code for centering and middling?
Thank you for helping!
Note: Unnecessary parts are removed.
You can use RelativeLayout with nested LinearLayout. Put all your view into LinearLayout without applying centering and set centerInParent="true".
But in your case just remove layout_gravity attribute from your views and add gravity="center" to the root view.
And also you forget to close your view's tags;)
So the final layout will be 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"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
You should consider these things
Because LinearLayout just adds views one by one, your LinearLayout android:layout_width and android:layout_height should be "fill_parent" to show your views correctly
android:layout_gravity should be "center_horizontal|center_vertical"
Try adding android:layout_weight for your objects

Need help centering an EditText in a Linear Layout

I'm trying to center an edittext vertically and horizontally in a linear layout, but it is not working. Very simple problem really.
<?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:background="#drawable/login">
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:background="#drawable/loginbutton"
android:text="Username"
android:layout_gravity="center"
android:textSize="25dp"
android:gravity="center_horizontal|center_vertical"
android:textColor="#000000"
android:layout_margin="10dp"
android:id="#+id/username">
</EditText>
I'll try changing to a relativelayout in the mean time.
LinearLayout do not support centering in the direction it stacks items as far as I know. Unless the LinearLayout is crucial (which in your case it shouldn't be, as you also need the centering), I would recommend you switch to a RelativeLayout.
With a RelativeLayout, you can set the attribute android:layout_centerInParent="true" on the EditText to get it centred.
Using gravity affects the gravity of the contents of said UI item, this means (in this case) that the text inside the EditText would be centered. In order to center the UI item itself you need to define layout_gravity as this is the gravity a UI item holds within his parent. The code posted by the OP will do the horizontal center but not the vertical center.
As an alternative you can use RelativeLayout
<?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"
android:background="#drawable/login"
android:orientation="vertical" >
<EditText
android:id="#+id/username"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/loginbutton"
android:layout_centerInParent="true"
android:text="Username"
android:textColor="#000000"
android:textSize="25dp" >
</EditText>
</RelativeLayout>
It is very simple. Add android:gravity="center" with the linear layout.
<LinearLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
Try this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/login"
android:layout_centerVertical="true"
android:layout_centerInParent="true">

Android - Center TextView Horizontally in LinearLayout

I have the following basic layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/title_bar_background">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceLarge"
android:padding="10dp"
android:text="HELLO WORLD" />
</LinearLayout>
<LinearLayout>
It seems like the xml is correct but the text is aligned to the left. The textview takes up the entire width of the parent and the textview is set to be centered. Not sure what the problem is...
What's happening is that since the the TextView is filling the whole width of the inner LinearLayout it is already in the horizontal center of the layout. When you use android:layout_gravity it places the widget, as a whole, in the gravity specified. Instead of placing the whole widget center what you're really trying to do is place the content in the center which can be accomplished with android:gravity="center_horizontal" and the android:layout_gravity attribute can be removed.
If you set <TextView> in center in <Linearlayout> then first put android:layout_width="fill_parent" compulsory
No need of using any other gravity
<LinearLayout
android:layout_toRightOf="#+id/linear_profile"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="It's.hhhhhhhh...."
android:textColor="#color/Black"
/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/title_bar_background">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:padding="10dp"
android:text="HELLO WORLD" />
</LinearLayout>
Use android:gravity="center" in TextView instead of layout_gravity.
Just use: android:layout_centerHorizontal="true"
It will put the whole textview in the center

Categories

Resources