How to keep TextView as center inside linear layout
Clash Royale CLAN TAG#URR8PPP
How to keep TextView as center inside linear layout
This is my xml code i want keep center Textview
inside linear layout which contain textview
and view but i want text view shout center in linear layout bottom to view i.e button textview
should center from top view and bottom linearLayout
.
Textview
textview
textview
linearLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/no_vouchers_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="@+id/xxlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/header_height"
android:visibility="gone"
tools:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/margin_12"
android:orientation="vertical">
<TextView
android:id="@+id/xxmessage"
style="@style/Typeface.Body.TextMidGrey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lineSpacingExtra="@dimen/tfl_6"
tools:text="KJGSAHJDGJKSGDASAGDHJGAS" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="@dimen/margin_16"
android:background="@color/text_light_grey" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/button"
style="@style/Typeface.Body.Bold.White"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_16"
android:paddingBottom="@dimen/padding_12"
android:paddingTop="@dimen/padding_12"
android:background="@drawable/welcome_screen_button_selector"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/convert_points_to_vouchers"
android:textAllCaps="false" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
Possible duplicate of How to center a textview inside a linearlayout
– ADM
Aug 13 at 5:20
problem with me that inside linear layout there is view so i have keep center button textview with view and bottom layout in linear layout
– Anil Thakur
Aug 13 at 5:23
3 Answers
3
Use android:layout_width="match_parent"
&
android:gravity="center"
your TextView
it will work
android:layout_width="match_parent"
android:gravity="center"
TextView
Use this
<TextView
android:id="@+id/xxmessage"
style="@style/Typeface.Body.TextMidGrey"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:lineSpacingExtra="@dimen/tfl_6"
tools:text="KJGSAHJDGJKSGDASAGDHJGAS" />
Instead of this
<TextView
android:id="@+id/xxmessage"
style="@style/Typeface.Body.TextMidGrey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lineSpacingExtra="@dimen/tfl_6"
tools:text="KJGSAHJDGJKSGDASAGDHJGAS" />
SAMPLE CODE
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/no_vouchers_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="@+id/xxlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/header_height"
android:visibility="gone"
tools:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/margin_12"
android:orientation="vertical">
<TextView
android:id="@+id/xxmessage"
style="@style/Typeface.Body.TextMidGrey"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:lineSpacingExtra="@dimen/tfl_6"
tools:text="KJGSAHJDGJKSGDASAGDHJGAS" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="@dimen/margin_16"
android:background="@color/text_light_grey" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/button"
style="@style/Typeface.Body.Bold.White"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_16"
android:paddingBottom="@dimen/padding_12"
android:paddingTop="@dimen/padding_12"
android:background="@drawable/welcome_screen_button_selector"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/convert_points_to_vouchers"
android:textAllCaps="false" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
i am not talking about xxxmessage i am talking about button textview @Nilesh Rathod
– Anil Thakur
Aug 13 at 5:21
@AnilThakur Please check my updated answer
– Nilesh Rathod
Aug 13 at 5:23
still from view its take little more gap between button and view
– Anil Thakur
Aug 13 at 5:29
remove
android:layout_margin="@dimen/margin_12"
from LinearLayout
– Nilesh Rathod
Aug 13 at 5:35
android:layout_margin="@dimen/margin_12"
LinearLayout
if i will remove then upper text view will set to up
– Anil Thakur
Aug 13 at 5:55
Try this android:layout_gravity="center"
<TextView
android:id="@+id/xxmessage"
style="@style/Typeface.Body.TextMidGrey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:lineSpacingExtra="@dimen/tfl_6"
tools:text="KJGSAHJDGJKSGDASAGDHJGAS" />
Creating nested layout in android can result in varoious performance issues.
I would recommend you to use
ConstraintLayout instead of Linear layout.ConstraintLayout
is helpful in building responsive UI.
With ConstraintLayout
you can easily drag/drop any element and constrain them according to your activity.
ConstraintLayout
ConstraintLayout
i dont have to use ConstraintLayout.
– Anil Thakur
Aug 13 at 5:21
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Can you show your required UI pic. It will be easy to understand by pic than hundred of words.
– Khemraj
Aug 13 at 5:03