How to align code vertically and horizontally? - android

I am writing this code, but my code isn't automatically aligning vertically.
<?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">
<TextView
android:id="#+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quantity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
</LinearLayout>
How the output should look like:
Quantity
0
While it is showing:
Quantity0

Note that you've not set the orientation in the LinerLayout, change that to this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
As the default orientation is horizontal, both your TextViews are in the same line.
Once you set the orientation to be vertical, it will look like this:
Quantity
0

Firstly, you can add orientation attribute to LinearLayout then set orientation vertical as below:
<?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:layout_orientation="vertical">
<TextView
android:id="#+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quantity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
</LinearLayout>
If you don't set orientation, in XML by default orientation of LinearLayout is horizontal. So,If you need vertical views in LinearLayout then set orientation attribute as vertical.

Use <Space/> tag to add space between views, or you can adjust by mentioning margin and padding as well. But space will be a good option as you don't need to have a concern while RTL and LTR layouts in app.
<?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">
<TextView
android:id="#+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quantity"/>
<Space
android:layout_width="4dp" // change according to your need.
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
</LinearLayout>

The easiest way would be adding space at the end in Quantity TextView, that means your code will be:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quantity "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
</LinearLayout>

Related

Cannot see textview

I am trying to make a layout with this code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
>
<android.support.v7.widget.CardView
app:cardCornerRadius="#dimen/cardview_corner_radius"
app:cardElevation="2dp"
card_view:cardUseCompatPadding="true"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#f27181"
android:id="#+id/charity_overview_card"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.sharesmile.share.views.LBTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#color/white"
android:padding="9dp"
android:text="Women Empowerment"
android:id="#+id/iv_charity_category_title"
/>
<ImageView
android:background="#color/colorPrimaryDark"
android:layout_width="128dp"
android:layout_height="126dp"
android:id="#+id/iv_charity_overview"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<com.sharesmile.share.views.LBTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_charity_amount"
android:textColor="#color/black"
android:textSize="15sp"
android:text="Test"
/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
but the last textview is not visible, even in the preview it is not showing, it shows like this :
As you can see the textview is not visible, i haven't given any weight, still facing this issue.
Also when i increase the size of the textview the value is visible as you can see here :
LinearLayout inside other parent LinearLayout with dynamic size will produce this type of issues. You should avoid nesting layout inside layout as much as possibile.
This issue can be simply fixed by removing the parent layout orientation.
For improvement
<CardView>
<LinearLayout> vertical
//Add 'N' child items here
</LinearLayout>
</CardView>
Change the following properties of your main LinearLayout, Make height and width to match_parent
android:layout_width="match_parent"
android:layout_height="match_parent"

How to change buttons layout from horizontal to vertical?

I'm translating a chinese app and I want to change the bottom buttons layout from horizontal to vertical in order to fit correctly the translations. I've been playing with the orientation and gravity from the LinearLayout and CheckedTextView but the buttons stay always in horizontal.
This is the xml code,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
android:background="#drawable/homepage_ground" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:nav_stick="http://schemas.android.com/apk/res-auto">
<include layout="#layout/title_bar_transparent_white" />
<RelativeLayout android:layout_width="fill_parent" android:layout_height="0.0px" android:layout_weight="1.0">
<com.rockrobo.ui.RRNavigateStick android:id="#id/navigate_stick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" />
<TextView android:id="#id/rc_starting" android:visibility="gone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10.0dip" android:text="#string/rc_starting_up" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" />
</RelativeLayout>
<LinearLayout android:layout_gravity="center" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="#dimen/rc_bot_margin">
<CheckedTextView android:textColor="#color/rc_button_text_color" android:gravity="center" android:id="#id/spot_clean" android:background="#drawable/rc_button_bg" android:clickable="true" android:layout_width="#dimen/rc_button_width" android:layout_height="#dimen/rc_button_height" android:layout_marginRight="#dimen/rc_bot_bar_gap" android:text="#string/rr_spot" />
<CheckedTextView android:textColor="#color/rc_button_text_color" android:gravity="center" android:id="#id/clean" android:background="#drawable/rc_button_bg" android:clickable="true" android:layout_width="#dimen/rc_button_width" android:layout_height="#dimen/rc_button_height" android:layout_marginRight="#dimen/rc_bot_bar_gap" android:text="#string/rr_clean" />
<CheckedTextView android:textColor="#color/rc_button_text_color" android:gravity="center" android:id="#id/charge" android:background="#drawable/rc_button_bg" android:clickable="true" android:layout_width="#dimen/rc_button_width" android:layout_height="#dimen/rc_button_height" android:text="#string/rr_charge" />
</LinearLayout>
Could anybody help? Thanks.
Hi try changing linear layout width to match parent and orientation to vertical

How to add space between two textviews

I am new to android development and I was just wondering how can I add space between two TextViews? Any help would be appreciated.
The code I have written so far
<?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="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/lbl_group_coworkers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coworkers" />
<TextView
android:id="#id/lbl_group_"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Family"/>
</LinearLayout>
You can use android:layout_marginTop="value"
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="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/lbl_group_coworkers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coworkers" />
<TextView
android:id="#id/lbl_group_"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Family"/>
</LinearLayout>
you can have margin between two textview.
add top margin to the second textview
like this
<TextView
android:id="#id/lbl_group_"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Family"/>
Just replace <LinearLayout> </LinearLayout>
with <RelativeLayout> </RelativeLayout>
then go in graphical layout and adjust space as you like.
add margin left right top bottom
android:layout_marginLeft="10dp"
try adding margin
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/lbl_group_coworkers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coworkers"
android:layout_margin="10dp"/>
<TextView
android:id="#+id/lbl_group"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Family"/>
</LinearLayout>
You can use
android:layout_margin="5dp"
or
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
But before you ask a lot more such questions, I'd suggest to read through the android dev guides ( http://developer.android.com/guide/components/fundamentals.html )
Good Luck and have fun developing...
You can use either padding or margin depending on what you need, here is a link of a guy who gives a good explanation between the two that will help you decide on which one you'd like to use: android margin vs padding
add android:layout_marginRight="..." to the first textView
set margin property in TextView...
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
and if all side set space then...
android:layout_margin="20dp"
Use GridLayout instead of LinearLayout
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:layout_gravity="left" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:layout_gravity="right" />
</GridLayout>
<?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"
android:background="#drawable/custom_border"
>
<TextView
android:text="#string/textView_reference_number"
style="#style/CustomTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView_refernce_number_label"
/>
<TextView
android:text="Reference Number Value"
style="#style/CustomTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView_refernce_number"
android:layout_marginTop="10dp"/>
</LinearLayout>
android:layout_marginTop XML attribute is used to specify extra space on the top side of this view. Thus android:layout_marginTop="10dp" on 2nd textView is specifying 10 dp of space between it and above view. #UDI Please refer below link for more details over the same https://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html

How to center a textview inside a linearlayout

I have the following layout i'd like to make the textview appear in the center and middle of the view. How can i acheive this?
I've tried adjusting the various gravity attributes when looking at the layout in graphical view, but nothing seems to change it. I'd like the textview in the center which i've done but halfway down the view.
thanks matt.
<?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"
android:background="#drawable/carefreebgscaledalphajpg" >
<TextView
android:id="#+id/textviewdatefornocallspage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="You have no calls for "
android:textSize="25sp" />
</LinearLayout>
set the gravity to center in your linearLayout tag :
<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"
android:background="#drawable/carefreebgscaledalphajpg" >
or use a RelativeLayout 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"
android:background="#drawable/carefreebgscaledalphajpg" >
<TextView
android:id="#+id/textviewdatefornocallspage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="You have no calls for "
android:textSize="25sp" />
</RelativeLayout>
Try this
simple i have tried to many answer but all answers are not working ......finally this methods works for me
<LinearLayout
android:layout_below="#+id/iv_divider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="#dimen/_10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/mycredits"
android:textColor="#color/colorGray"
android:gravity="center"
android:textSize="#dimen/_25dp" />
</LinearLayout>
Set android:gravity="center" attribute for TextView inside linear layout and keep the text view's height and width as wrap_content.
This Solution worked for me.
<TextView
...
android:layout_gravity="center"
....
/>
Set width of your textview to wrap_content, and set your LinearLayout to
android:gravity="center_horizontal"
Add android:gravity="center_horizontal" to your LinearLayout and alter your TextView's layout_width as android:layout_width="wrap_content"
<?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"
android:gravity="center_horizontal"
android:background="#drawable/carefreebgscaledalphajpg" >
<TextView
android:id="#+id/textviewdatefornocallspage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="You have no calls for "
android:textSize="25sp" />
</LinearLayout>
None of the other answers worked for me. I had to use android:textAlignment="center"
My layouts were as follows
<LinearLayout...
<RelativeLayout...
<RelativeLayout....
<TextView
android:layout_centerInParent="true"
android:textAlignment="center"
If in linearlayout your orientation vertical, you can put the textview in the "horizontal center" by android:layout_gravity="center". For centering textview vertically you need to set layout_height of textview to match_parent and set android:gravity to "center". If you want to use more than one view in this layout, you should use RelativeLayout and set
android:layout_centerInParent="true".
<LinearLayout
android:id="#+id/service_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/card_view"
android:layout_marginTop="10dp"
android:background="#color/app_bar_background"
android:orientation="vertical">
<TextView
android:id="#+id/list_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:gravity="center"
android:singleLine="true"
android:text="#string/Services"
android:textColor="#color/app_bar_text"
/>
</LinearLayout>
Sounds like you want android:layout_gravity for the TextView
see docs: developer.android.com
set android:gravity="center" in your Linear Layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".view.fragments.FragmentAncRecords">
<TextView
android:id="#+id/tvNoDataFound"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/poppins_regular"
android:visibility="visible"
android:gravity="center"
android:text="#string/no_record_available"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvAncRecords"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</LinearLayout>
center your texteView in RelativeLayout :
<?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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
Use Relative Layout, it always gives more accurate and flexible layout
<?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:orientation="vertical" >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>

Android: Layout List header

I have a header for a list that contains two image buttons. I want one of those image buttons to be on the left, and the other to be on the right. This is what my xml file looks like right now but it does not work, it just puts the two image buttons on the left together. I have also tried without the additional LinearLayouts but no luck.
<?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="horizontal"
android:gravity="fill_horizontal"
android:background="#drawable/list_header_background">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="left"
android:background="#drawable/list_header_background">
<ImageButton
android:id="#+id/refreshBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:src="#drawable/ic_refresh"
android:background="#drawable/list_header_background"
/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="right"
android:background="#drawable/list_header_background">
<ImageButton
android:id="#+id/battleBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:src="#drawable/ic_battle"
android:background="#drawable/list_header_background"
/>
</LinearLayout>
</LinearLayout>
As kabuko wrote, the RelativeLayout width is equal to the parent width. When we have a child view inside that RelativeLayout, we can use the android:layout_alignParentXXXXXX="true" parameter in order to align it (the child) accordingly to the parent.
<?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:orientation="horizontal"
android:background="#drawable/list_header_background">
<ImageButton
android:id="#+id/refreshBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:src="#drawable/ic_refresh"
android:layout_alignParentLeft="true"
android:background="#drawable/list_header_background"
/>
<ImageButton
android:id="#+id/battleBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:src="#drawable/ic_battle"
android:layout_alignParentRight="true"
android:background="#drawable/list_header_background"
/>
</RelativeLayout>
You probably want to use a RelativeLayout with layout_alignParentLeft and layout_alignParentRight on the buttons.

Categories

Resources