Why wont the parent change the height to wrap the text view? - android

I am baffled as to why this parent relative layout with id main_layout* will not adjust its height for the view with id is_admin.
Here is the code. Put in to an xml file and preview in Android Studio to see the problem:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:paddingRight="#dimen/user_item_rellayout_paddingrightleft"
android:paddingLeft="#dimen/user_item_rellayout_paddingrightleft"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:id="#+id/user_container">
<RelativeLayout
android:id="#+id/main_layout"
android:layout_centerVertical="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:visibility="visible"
android:layout_above="#+id/profile_image_layout"
android:layout_marginBottom="#dimen/user_item_isadmin_marginbottom"
android:textColor="#color/black"
android:layout_centerHorizontal="true"
android:textAllCaps="true"
android:id="#+id/is_admin"
android:text="ADMIN"
android:textSize="#dimen/user_item_isadmin_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RelativeLayout
android:id="#+id/profile_image_layout"
android:layout_centerHorizontal="true"
android:layout_width="60dp"
android:layout_height="60dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_image"
android:src="#color/delim_grey"
android:layout_width="60dp"
android:layout_height="60dp" />
<TextView
android:id="#+id/profile_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
<de.hdodenhof.circleimageview.CircleImageView
android:visibility="invisible"
android:id="#+id/sent_icon"
android:layout_centerInParent="true"
android:src="#drawable/sent_tick"
android:background="#drawable/circle_green"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="10dp"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentRight="true"
android:id="#+id/delete_button"
android:background="#drawable/circle_white"
>
</LinearLayout>
</RelativeLayout>
<TextView
fontPath="font/AvenirNext-Medium-06.ttf"
android:layout_marginTop="#dimen/user_item_username_margintop"
android:textColor="#color/black"
android:layout_centerHorizontal="true"
android:layout_below="#+id/profile_image_layout"
android:textAllCaps="true"
android:id="#+id/username"
android:text="TOTO"
android:textSize="11dp"
android:gravity="center"
android:maxLines="1"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:ellipsize="end"/>
<TextView
android:layout_marginTop="#dimen/user_item_fullname_margintop"
android:layout_centerHorizontal="true"
android:textSize="8dp"
android:layout_below="#+id/username"
android:id="#+id/fullname"
android:maxLines="2"
android:text="toto titi"
android:textColor="#color/hintGrey"
android:gravity="center"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:ellipsize="end"/>
</RelativeLayout>
</RelativeLayout>

Because you've set the property android:layout_above="#+id/profile_image_layout" to the view with id android:id="#+id/is_admin" and profile_image_layout is at the top of the view.
It's fine when you set it to android:layout_below="#id/fullname"

Related

RecyclerView Cutting Off in the last data in LinearLayout

I have a LinearLayout activity that contains two contents which are CardView 1 and CardView 2. Everything is doing well, also with NestedScrollView. But I have problem in the last RecycleView data, it is cutting off a little bit in very bottom.
I already tried many solution that I have searched for in Stack Overflow and this still doing bad. I'm new in this, please help
This is my xml:
<?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:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.yehezkiel.eclassapp.MateriActivity"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_nilai"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
My Custom RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="90dp"
android:background="#color/white"
android:padding="10dip">
<TextView
android:id="#+id/nama_nilai"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toStartOf="#+id/view_2"
android:gravity="center_vertical"
android:text="Pemrogaman Web Tugas 1"
android:textColor="#5e1b2c"
android:textSize="16sp"
android:textStyle="bold" />
<View
android:id="#+id/view_2"
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="14dp"
android:layout_toStartOf="#+id/imageView10"
android:background="#c0c0c0" />
<TextView
android:id="#+id/bobot_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/imageView8"
android:textAlignment="center"
android:layout_marginTop="0.5dp"
android:ellipsize="end"
android:gravity="center"
android:text="Bobot"
android:textColor="#555555"
android:textSize="8sp" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignEnd="#+id/bobot_layout"
android:layout_below="#+id/nama_nilai"
android:layout_marginTop="8dp"
app:srcCompat="#drawable/percentage" />
<TextView
android:id="#+id/bobot_nilai"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView8"
android:layout_alignTop="#+id/percent"
android:layout_marginStart="8dp"
android:layout_toEndOf="#+id/bobot_layout"
android:text="5.00" />
<TextView
android:id="#+id/percent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView8"
android:layout_toEndOf="#+id/bobot_nilai"
android:text="%"
android:layout_marginStart="2dp"/>
<ImageView
android:id="#+id/imageView9"
android:layout_width="23dp"
android:layout_height="23dp"
android:layout_alignBottom="#+id/percent"
android:layout_marginStart="9dp"
android:layout_toEndOf="#+id/percent"
app:srcCompat="#drawable/equality" />
<TextView
android:id="#+id/skala_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/imageView9"
android:layout_alignStart="#+id/imageView9"
android:layout_below="#+id/imageView9"
android:layout_marginTop="1dp"
android:ellipsize="end"
android:gravity="center"
android:text="Skala"
android:textAlignment="center"
android:textColor="#555555"
android:textSize="8sp" />
<TextView
android:id="#+id/skala_nilai"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/skala_layout"
android:layout_marginStart="8dp"
android:layout_toEndOf="#+id/imageView9"
android:text="100.00" />
<ImageView
android:id="#+id/imageView10"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_below="#+id/nama_nilai"
android:layout_toStartOf="#+id/nilai_nilai"
app:srcCompat="#drawable/score" />
<TextView
android:id="#+id/nilai_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/imageView10"
android:layout_toStartOf="#+id/nilai_nilai"
android:ellipsize="end"
android:text="Nilai"
android:textAlignment="center"
android:textColor="#555555"
android:textSize="12sp" />
<TextView
android:id="#+id/nilai_nilai"
android:layout_width="90dp"
android:layout_height="50dp"
android:layout_alignBottom="#+id/nilai_layout"
android:layout_alignParentEnd="true"
android:layout_below="#+id/information"
android:text="89.70"
android:padding="15dp"
android:textAlignment="center"
android:textSize="18sp" />
<ImageView
android:id="#+id/information"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignEnd="#+id/nilai_nilai"
android:layout_alignParentTop="true"
app:srcCompat="#drawable/information" />
</RelativeLayout>
I don't think it's overwritten but I think the textview is hiding behind the card itself since its in a relative layout.
Can you try adding layout below attribute in your cardview and assign the ID of the textview "static1"
Let me know how it goes.

Android App Layout collapses on physical device

The layouts and elements in Android Studio look exactly how I want it to be but as soon as I install the APK on my device, the layout collapses. All views are collapsed one on top of another which is not what was shown in Android Studio's preview...
I'm using linear layouts and relative layout. I had used android studio 1-2yrs ago and there was no such thing as constrained layout, now every time I create a layout I get an constrained error.
XML Main activity (only one):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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" tools:context="com.simultaneousequations.spacelaunchx.spacelaunchx.MainActivity">
<LinearLayout
android:id="#+id/linearLayoutTop"
android:layout_width="0dp"
android:layout_height="101dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="11dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="#+id/linearLayoutCenter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:id="#+id/relativeLayoutTop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorLightGrey">
<LinearLayout
android:id="#+id/linearLayoutTopImage"
android:layout_width="110dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/iridumNEXT40_50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:adjustViewBounds="false"
android:cropToPadding="false"
android:scaleType="fitStart"
app:srcCompat="#mipmap/iridiummission5" />
</LinearLayout>
<TextView
android:id="#+id/iridium5Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:layout_toEndOf="#+id/linearLayoutTopImage"
android:layout_toRightOf="#+id/linearLayoutTopImage"
android:fontFamily="sans-serif-condensed"
android:text="Iridium NEXT 40 50"
android:textAlignment="textStart"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/colorRedDragon"
android:textSize="16sp"
android:textStyle="bold|italic" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayoutCenter"
android:layout_width="400dp"
android:layout_height="101dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="101dp"
android:background="#color/colorLightGrey"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="109dp">
<LinearLayout
android:id="#+id/linearLayoutCenterImage"
android:layout_width="110dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/crs14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:scaleType="fitStart"
app:srcCompat="#mipmap/crs14" />
</LinearLayout>
<TextView
android:id="#+id/crs14Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
ndroid:layout_marginTop="16dp"
android:layout_toEndOf="#+id/linearLayoutCenterImage"
android:layout_toRightOf="#+id/linearLayoutCenterImage"
android:fontFamily="sans-serif-condensed"
android:text="SpaceX NASA - CRS-14"
android:textAlignment="center"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/colorRedDragon"
android:textSize="18sp"
android:textStyle="bold|italic" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayoutCenterBottom"
android:layout_width="400dp"
android:layout_height="101dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="101dp"
android:background="#color/colorLightGrey"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="109dp">
<LinearLayout
android:id="#+id/linearLayoutCenterBottomImage"
android:layout_width="110dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/bhu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:scaleType="fitCenter"
app:srcCompat="#mipmap/spacexlogo"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="101dp" />
</LinearLayout>
<TextView
android:id="#+id/bhuText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:layout_toEndOf="#+id/linearLayoutCenterBottomImage"
android:layout_toRightOf="#+id/linearLayoutCenterBottomImage"
android:fontFamily="sans-serif-condensed"
android:text="Bangabandhu-1"
android:textAlignment="center"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/colorRedDragon"
android:textSize="18sp"
android:textStyle="bold|italic" />
</RelativeLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
What am I doing wrong?
Seems like you're not familiar with ConstraintLayout.
That's OK. Don't use ConstraintLayout when you don't want to use it. Using this is not necessary.
What you have to do is just change the root layout to either LinearLayout, RelativeLayout or any other Layout you want to use as per your requirements.
I'm giving you the code that you should use in this case. In that code, I'm using RelativeLayout as the root layout, because you are using layout_alignParentBottom like attributes.
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.companys.appx.appx.MainActivity">
<LinearLayout
android:id="#+id/linearLayoutTop"
android:layout_width="0dp"
android:layout_height="101dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="11dp"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/relativeLayoutTop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorLightGrey">
<LinearLayout
android:id="#+id/linearLayoutTopImage"
android:layout_width="110dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/iridumNEXT40_50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:adjustViewBounds="false"
android:cropToPadding="false"
android:scaleType="fitStart"
app:srcCompat="#mipmap/iridiummission5" />
</LinearLayout>
<TextView
android:id="#+id/iridium5Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:layout_toEndOf="#+id/linearLayoutTopImage"
android:layout_toRightOf="#+id/linearLayoutTopImage"
android:fontFamily="sans-serif-condensed"
android:text="Iridium NEXT 40 50"
android:textAlignment="textStart"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/colorRedDragon"
android:textSize="16sp"
android:textStyle="bold|italic" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayoutCenter"
android:layout_width="400dp"
android:layout_height="101dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="101dp"
android:background="#color/colorLightGrey"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="109dp">
<LinearLayout
android:id="#+id/linearLayoutCenterImage"
android:layout_width="110dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/crs14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:scaleType="fitStart"
app:srcCompat="#mipmap/crs14" />
</LinearLayout>
<TextView
android:id="#+id/crs14Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:layout_toEndOf="#+id/linearLayoutCenterImage"
android:layout_toRightOf="#+id/linearLayoutCenterImage"
android:fontFamily="sans-serif-condensed"
android:text="SpaceX NASA - CRS-14"
android:textAlignment="center"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/colorRedDragon"
android:textSize="18sp"
android:textStyle="bold|italic" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayoutCenterBottom"
android:layout_width="400dp"
android:layout_height="101dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="101dp"
android:background="#color/colorLightGrey"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="109dp">
<LinearLayout
android:id="#+id/linearLayoutCenterBottomImage"
android:layout_width="110dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/bhu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:scaleType="fitCenter"
app:srcCompat="#mipmap/spacexlogo"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="101dp" />
</LinearLayout>
<TextView
android:id="#+id/bhuText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:layout_toEndOf="#+id/linearLayoutCenterBottomImage"
android:layout_toRightOf="#+id/linearLayoutCenterBottomImage"
android:fontFamily="sans-serif-condensed"
android:text="Bangabandhu-1"
android:textAlignment="center"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/colorRedDragon"
android:textSize="18sp"
android:textStyle="bold|italic" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
If there is any mistake(typo) in the code change it.
The only point is to use RelativeLayout as the root Layout instead of ConstraintLayout
In the above code, I've removed all the ConstraintLayout Attributes from the first LinearLayout because the root Layout is no more a ConstraintLayout.
This Trick will remove the problem. The problem was occurring because ContraintLayout has its own rules that you were not following. But now as you've changed the root Layout the problem will no longer exist.

TextView height stretches in parent with gravity right. why?

I am making chat activity and having issues with right chat bubble. As you can see in the image below. TextView stretches height wise. According to my thinking it should be stretching width wise first then if space is full it should stretch height wise. Please suggest me if it is possible with xml or not. I have seen java code solution but i want to find a solution in xml first.
layout.xml
<?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="wrap_content"
android:gravity="right"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="20" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="80"
android:gravity="right">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:background="#drawable/ic_bubble_sent"
android:gravity="right">
<TextView
android:id="#+id/txt_msg_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/txt_message_time"
android:layout_toStartOf="#+id/txt_message_time"
android:textSize="16sp"
android:text="Lorem"
android:visibility="visible" />
<TextView
android:id="#+id/txt_message_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txt_msg_content"
android:layout_marginTop="-9dp"
android:text="00:33"
android:textColor="#color/colorAppGray"
android:textSize="12sp"
android:textStyle="normal" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Replace your two TextView with this,
<TextView
android:id="#+id/txt_msg_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="Lorem"
android:visibility="visible"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/txt_message_time"
android:layout_toStartOf="#+id/txt_message_time"
android:layout_marginRight="32dp"
android:layout_marginEnd="32dp" />
<TextView
android:id="#+id/txt_message_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:33"
android:textSize="12sp"
android:textStyle="normal"
android:layout_alignBaseline="#+id/txt_msg_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
I removed all unnecessary alignments from first TextView.
UPDATE
The overall layout looks like,
<?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="wrap_content"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="20" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="80"
android:gravity="right">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
>
<TextView
android:id="#+id/txt_msg_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="Lorem"
android:visibility="visible"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/txt_message_time"
android:layout_toStartOf="#+id/txt_message_time"
android:layout_marginRight="32dp"
android:layout_marginEnd="32dp" />
<TextView
android:id="#+id/txt_message_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:33"
android:textSize="12sp"
android:textStyle="normal"
android:layout_alignBaseline="#+id/txt_msg_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Note : I removed your background for testing.

Why in my layout I am unable to align the ImageView and TextView in a line?

I have the following layout:-
<?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="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin"
android:layout_marginLeft="#dimen/default_dbl_margin"
android:layout_marginRight="#dimen/default_dbl_margin">
<ImageView
android:id="#+id/lblIcon"
android:src="#drawable/ic_status_confirmed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/default_dbl_margin"
android:layout_marginTop="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin" />
<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/relativeLayout">
<TextView
android:id="#+id/lblFirstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="FirstLine"
android:singleLine="true"
android:ellipsize="end"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/lblFirstLine"
android:layout_alignParentLeft="true"
android:layout_marginTop="#dimen/default_margin">
<ImageView
android:layout_width="14sp"
android:layout_height="14sp"
android:id="#+id/imgTimeIcon"
android:src="#drawable/ic_action_time" />
<TextView
android:id="#+id/lblSecondLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2:00pm"
android:textSize="14sp"
android:layout_gravity="center_vertical"
android:textColor="#android:color/secondary_text_dark" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/newIndicator"
android:src="#drawable/new_indicator"
android:layout_marginRight="#dimen/default_quad_margin"/>
<ImageButton
android:id="#+id/btnChat"
android:src="#drawable/ic_action_chat"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
However the layout looks like below:
Notice that the time TextView is slightly displaced downwards. I don't understand why?
specify gravity for the parent linear layout and change the image width and height from sp to dp
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_below="#+id/lblFirstLine"
android:layout_alignParentLeft="true"
android:layout_marginTop="#dimen/default_margin">
<ImageView
android:layout_width="14dp"
android:layout_height="14dp"
android:id="#+id/imgTimeIcon"
android:src="#drawable/ic_action_time" />
<TextView
android:id="#+id/lblSecondLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2:00pm"
android:textSize="14sp"
android:textColor="#android:color/secondary_text_dark" />
</LinearLayout>
</LinearLayout>
i Hope it will help
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_vertical">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="#+id/imgTimeIcon"
android:gravity="center"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/lblSecondLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="2:00pm"
android:textColor="#android:color/secondary_text_dark"
android:textSize="14sp" />
</LinearLayout>
Adding android:layout_gravity="center_vertical" to android:id="#+id/imgTimeIcon"" ImageView should solve your problem
Also don't use sp except on fonts. Use dpinstead
Instead of:
<ImageView
android:layout_width="14sp"
android:layout_height="14sp"
android:id="#+id/imgTimeIcon"
android:src="#drawable/ic_action_time" />
Use:
<ImageView
android:layout_width="14dp"
android:layout_height="14dp"
android:id="#+id/imgTimeIcon"
android:layout_gravity="center_vertical"
android:src="#drawable/ic_action_time" />
Use RelativeLayout for the ImageView and TextView:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/lblFirstLine"
android:layout_alignParentLeft="true"
android:layout_marginTop="#dimen/default_margin">
<ImageView
android:layout_width="14sp"
android:layout_height="14sp"
android:id="#+id/imgTimeIcon"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_time" />
<TextView
android:id="#+id/lblSecondLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2:00pm"
android:textSize="14sp"
android:layout_toRightOf="#+id/imgTimeIcon"
android:layout_centerVertical="true"
android:textColor="#android:color/secondary_text_dark" />
</LinearLayout>
</LinearLayout>

Android RelativeLayout align element on another elements border

Im trying to make a text view to align to the left border of another imageview in a RelativeLayout, but I'm having trouble and I cannot achieve this. Can anyone tell me how can I achieve this?
Here is my current XML 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="wrap_content"
android:layout_marginBottom="15dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/main_item_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:contentDescription="#string/app_name"
android:scaleType="fitXY" />
<RelativeLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/blackalha"
android:gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:id="#+id/main_item_cont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/main_item_nmb"
android:paddingLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
<ImageView
android:id="#+id/main_item_ic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#color/app_purple"
android:src="#drawable/bacal" />
<TextView
android:id="#+id/main_item_nmb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/main_item_ic"
android:background="#drawable/saric"
android:gravity="center"
android:textColor="#333333" />
</RelativeLayout>
<TextView
android:id="#+id/main_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/main_item_bg"
android:layout_margin="5dp"
android:maxWidth="200dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</RelativeLayout>
Try this
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="#+id/main_item_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:contentDescription="#string/app_name"
android:scaleType="fitXY" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dip"
android:gravity="center"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<TextView
android:id="#+id/main_item_cont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center"
android:paddingLeft="5dp"
android:text="dsgffdufsdfdfsdfdsfsdfsdfdsfdf"
android:inputType="textMultiLine"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
<ImageView
android:id="#+id/main_item_ic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dip"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/main_item_cont"
android:background="#android:color/black"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/main_item_nmb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/main_item_cont"
android:layout_toRightOf="#+id/main_item_cont"
android:background="#android:color/white"
android:text="10"
android:textColor="#android:color/white" />
</RelativeLayout>
<TextView
android:id="#+id/main_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/main_item_bg"
android:layout_margin="5dp"
android:maxWidth="200dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
As #Salauyou mentioned, try with android:layout_alignLeft because it "Makes the left edge of this view match the left edge of the given anchor view ID"
and android:layout_toLeftOf means "Positions the right edge of this view to the left of the given anchor view ID."
Now, this is what I achieved:
I used this picture in drawable-mdpi (as the badge) - note the trick of the dual background:
and the standard launcher icon, since I didn't have a wine glass icon handy.
This is my layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:layout_margin="0dp"
android:padding="0dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:padding="4dp"
>
<TextView
android:id="#+id/txtIcon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:gravity="center"
android:padding="4dp"
android:background="#6b2339"
android:drawableRight="#drawable/ic_launcher"
/>
<TextView
android:id="#+id/txtBadge"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_toLeftOf="#id/txtIcon"
android:gravity="center"
android:padding="4dp"
android:background="#drawable/wines_crossing_badge"
android:text="8"
android:textColor="#312c23"
android:textSize="16sp"
/>
<TextView
android:id="#+id/txtDesc"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/txtBadge"
android:background="#312c23"
android:gravity="left|center_vertical"
android:padding="4dp"
android:text="Available Androids in stock for rental"
android:textColor="#fff"
android:textSize="16sp"
/>
</RelativeLayout>
</RelativeLayout>

Categories

Resources