Align a textview to left in a LinearLayout(horizontal) - java

I'm trying to make my Textview aligned to left have tried. And android:gravity="left" but nothing happened.
This is my XML code:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="Email:"
android:textSize="20dp"/>
<EditText
android:id="#+id/editTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="#drawable/bg"
android:hint="Enter Address"/>
</LinearLayout>

Try this use code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="Email:"
android:textSize="20dp"/>
<EditText
android:id="#+id/editTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView11"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_toEndOf="#+id/textView11"
android:layout_weight="1"
android:background="#drawable/bg"
android:hint="Enter Address"/></RelativeLayout>

The gravity attribute is used to set the gravity of the content of the View.
The layout_gravity attribute is used to set the gravity of the view itself with regards to its parent.
So if you wanted to set the gravity of the text in your TextView then you'd use gravity. If you want to set the gravity of the TextView inside the LinearLayout you should use layout_gravity.

try this use RelativeLayout insted of LinearLayout and make your TextView android:layout_alignParentLeft="true"
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:text="Email:"
android:textSize="20dp"/>
<EditText
android:id="#+id/editTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:layout_alignParentRight="true"
android:background="#drawable/bg"
android:hint="Enter Address"/>
</RelativeLayout>

If you want to use Linear Layout then use :
<TextView
android:id="#+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_alignParentLeft="true"
android:text="Email:"
android:textSize="20dp"/>

Related

TextView overlaps with another textview

I want the text to be displayed to the right of the name,but if the name is too large, the text on the right doesn't fit on the screen. I used the following solution, but it causes the first textview to stretch beyond the size of the text in it.
What I have:
screenshot
What I want:
concept
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="true"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignWithParentIfMissing="true"
android:layout_weight="1"
android:fontFamily="#font/montserrat"
android:singleLine="true"
android:text="1"
android:textColor="#color/colorText"
android:textSize="20sp" />
<TextView
android:id="#+id/readed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginStart="10dp"
android:background="#drawable/back_new"
android:fontFamily="#font/montserrat"
android:gravity="center"
android:keepScreenOn="true"
android:paddingHorizontal="5dp"
android:paddingVertical="3dp"
android:singleLine="true"
android:text="554"
android:textColor="#color/colorWhite"
android:textSize="18sp" />
</LinearLayout>
just remove android:weighSum="1" and android:layout_weight attribute in two textViews
enter code here<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="true"
android:orientation="horizontal">
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignWithParentIfMissing="true"
android:fontFamily="#font/montserrat"
android:singleLine="true"
android:text="3"
android:textColor="#color/colorText"
android:textSize="20sp" />
<TextView
android:id="#+id/readed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginStart="10dp"
android:background="#drawable/back_new"
android:fontFamily="#font/montserrat"
android:gravity="center"
android:keepScreenOn="true"
android:paddingHorizontal="5dp"
android:paddingVertical="3dp"
android:singleLine="true"
android:text="554"
android:textColor="#color/colorWhite"
android:textSize="18sp" />
</LinearLayout>

Android/XML: RelativeLayout design

basically, I don't understand how to use RelativeLayout. I don't know how to design it. I always use LinearLayout with Table inside of it. Now I use RelativeLayout for my design. I want to know, how to add text "Sorry, no list available" Below is my current code of XML
<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:id="#+id/haha"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyHistoryList">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:gravity="center"
android:longClickable="false"
android:maxLines="1"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
<TextView
android:id="#+id/tvSuggestion"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".80"
android:gravity="start"
android:text="Suggestion"
android:textAllCaps="true"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/colorAccent"
android:textStyle="bold"
app:fontFamily="#font/anaheim" />
<TextView
android:id="#+id/tvStatus"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".20"
android:gravity="start"
android:text="Status"
android:textAllCaps="true"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/colorAccent"
android:textStyle="bold"
app:fontFamily="#font/anaheim" />
</TableRow>
</LinearLayout>
<RelativeLayout
android:id="#+id/huhu"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recylcerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:visibility="gone" />
<ImageView
android:id="#+id/imgEmpty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:src="#drawable/empty"/>
</RelativeLayout>
</LinearLayout>
This is how it looks like
(source: fbcdn.net)
But, I want to add text "Sorry, no list available" below of the image (marginTop = 20dp). Can anyone help?
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_below="#id/imgEmpty"
android:text="Sorry, no list available"/>
Insert it below your ImageView of RelativeLayout!
Replace your Relative layout with this.
<RelativeLayout
android:id="#+id/huhu"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recylcerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:visibility="gone" />
<ImageView
android:id="#+id/imgEmpty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:src="#drawable/empty"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_below="#id/imgEmpty"
android:text="Sorry, no list available"/>
Add this below imgEmpty. Use android:layout_below to place the text below image,android:layout_centerInParent="true" to move text to center.
<TextView
android:layout_marginTop="20dp"
android:layout_centerInParent="true"
android:text = "Sorry, no list available"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imgEmpty"/>
Here the output
in relative layout you can use below/above to put view below/above its sibling
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imgEmpty"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:text="TextView"/>

Multiline TextView and Singleline TextView in one line

I want to get next result in my layout:
[left textview] [space] [right textview]
For this purpose I use it:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="left"
android:id="#+id/left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="right"
android:id="#+id/right"/>
</RelativeLayout>
It's okay. But if left TextView contains a large text, the result will be next:
[left textview] (under left textview [right textview]. So left override right. But I want that left TextView doesn't override right, and when left TextView reaches right TextView the Left TextView go down into multiline. So I want to get next result:
some-text-text-left-multiline
some-text-text-left-multiline text-in-right-text-view
some-text-text-left-multiline
Use a linear layout (horizontal) and place textview with the same android:layout_weight inside to make a 50/50 split
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
This expands the left until reaching the right using android:layout_toLeftOf
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/right"
android:text="this is a long text on the left which should be multiline and it is" />
<TextView
android:id="#+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="some text" />
</RelativeLayout>
Do this to your layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="right"
android:minWidth="50dp"
android:maxWidth="100dp"
android:text="right" />
<TextView
android:id="#+id/left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="left"
android:layout_toLeftOf="#+id/right"
android:text="left" />
</RelativeLayout>

Gray line inside cardview :)?

How do I remove the gray line (you can see on the screen) at the bottom of CardView?
Archive with source-code
I reviewed everything. I don't understand what the problem is.
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="contact det"
android:gravity="center_vertical"
android:textColor="#android:color/background_dark"
android:textSize="14dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<View
android:layout_height="1dp"
android:layout_width="match_parent"
android:background="#color/accent"
android:layout_below="#+id/linearLayout"/>
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:gravity="center_vertical"
android:textSize="10dp"
android:layout_below="#+id/linearLayout"
android:layout_alignLeft="#+id/txtSurname"
android:layout_alignStart="#+id/txtSurname"/>
<TextView
android:id="#+id/txtSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname"
android:gravity="center_vertical"
android:textSize="10dp"
android:layout_below="#+id/txtName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:id="#+id/txtEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textSize="10dp"
android:layout_above="#+id/txtAdd"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<TextView
android:id="#+id/txtAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:textSize="10dp"
android:layout_alignTop="#+id/txtSurname"
android:layout_alignLeft="#+id/txtEmail"
android:layout_alignStart="#+id/txtEmail"/>
</RelativeLayout>
Its my item CardView.
when
card_view:cardCornerRadius="2dp"
all is good, but when I write another value and line appears!
Just use “com.google.android.material.card.MaterialCardView” it will avoid issues connected to CardView
Try this -
android:divider="#null"
Or from your code -
getListView().setDivider(null);
getListView().setDividerHeight(0);

How can I align my ImageView with my TextView in a LinearLayout?

I have the following ImageView and TextView:
Here is the XML:
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/headerLinearLay" android:orientation="horizontal">
<ImageView android:src="#drawable/icon" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/avatarImageView"></ImageView>
<TextView android:layout_height="wrap_content" android:id="#+id/usernameTextView" android:text="TextView" android:layout_width="wrap_content" android:paddingLeft="4px"></TextView>
</LinearLayout>
How can I make the image and the text be positioned at the same height? I also want the ImageView to be in the corner
Try this:
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/headerLinearLay">
<ImageView
android:id="#+id/avatarImageView"
android:src="#drawable/icon"
android:layout_height="wrap_content"
android:layout_width="wrap_content"></ImageView>
<TextView
android:id="#+id/usernameTextView"
android:text="TextView"
android:paddingLeft="4dp"
android:layout_toRightOf="#id/avatarImageView"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"></TextView>
</RelativeLayout>
Here is a simple solution that works well. Essential here is to add: android:layout_gravity="center_vertical" to TextView
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon" />
<TextView
android:id="#+id/usernameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_weight="0.04" />
<ImageView
android:id="#+id/imageView"
android:layout_width="204dp"
android:layout_height="200dp"
app:srcCompat="#mipmap/ic_launcher" />

Categories

Resources