Dynamically set the widths (weights) of Views inside a LinearLayout - java

I have a LinearLayout that contains four differently-colored Views. Each colored view represents the proportion of a workout's cumulative time spent in various heart-rate zones (blue -> green -> orange -> red).
I can get the desired effect by changing the android:layout_weight=".35" attribute in the various views in the XML. Setting these values does exactly what I want (when I don't override them in code). I think I'm just not accessing these properties correctly in code.
I have tried hard-coding nice, round sets of numbers and they don't look correct. The first one in the list appears much larger than it ought to be.
My layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/ex_history_item"
android:layout_height="160dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:theme="#style/AppTheme.CurrentStyle.HabitCard"
xmlns:android="http://schemas.android.com/apk/res/android" >
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="4dp"
android:id="#+id/cardio_bar"
android:visibility="visible"
android:weightSum="2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
>
<View
android:id="#+id/cardio_none"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".15"
android:backgroundTint="#color/cardio_zone_none"
/>
<View
android:id="#+id/cardio_fatburn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".35"
android:backgroundTint="#color/cardio_zone_fatburn"
/>
<View
android:id="#+id/cardio_cardio"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".35"
android:backgroundTint="#color/cardio_zone_cardio"
/>
<View
android:id="#+id/cardio_peak"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".15"
android:backgroundTint="#color/cardio_zone_peak"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/accent"
android:layout_width="35dp"
android:layout_height="match_parent"
android:background="#color/cardio"
android:backgroundTint="#00000000"
android:backgroundTintMode="add"
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="#+id/ex_image"
app:layout_constraintTop_toBottomOf="#id/cardio_bar">
<TextView
android:id="#+id/bodypartLabel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:foregroundGravity="fill"
android:rotation="90"
android:text="Bodypart"
tools:text="Bodypart" />
</LinearLayout>
<ImageView
android:id="#+id/ex_image"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_margin="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="#+id/accent"
app:layout_constraintTop_toBottomOf="#+id/exercise_name"
>
</ImageView>
<TextView
android:id="#+id/exercise_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exercise Name"
android:layout_marginLeft="5dp"
android:layout_marginTop="3dp"
android:textSize="18sp"
app:layout_constraintLeft_toRightOf="#+id/accent"
app:layout_constraintTop_toBottomOf="#+id/cardio_bar" />
<TableLayout
android:id="#+id/cardio_table"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="25dp"
app:layout_constraintBottom_toBottomOf="#+id/ex_image"
app:layout_constraintRight_toRightOf="parent"
>
<TableRow>
<TextView android:text="Duration"/>
<TextView android:id="#+id/duration" android:text="0"/>
</TableRow>
<TableRow>
<TextView android:text="Distance"/>
<TextView android:id="#+id/distance" android:text="0"/>
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/weights_table"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="25dp"
app:layout_constraintBottom_toBottomOf="#+id/ex_image"
app:layout_constraintRight_toRightOf="parent"
>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
My code for changing them is the following
private final LinearLayout cardioBar;
private final View cardioZoneNone;
private final View cardioZoneFatburn;
private final View cardioZoneCardio;
private final View cardioZonePeak;
Float total = (float)exercise.exercise.getHrTotalDuration();
holder.cardioBar.setWeightSum(total);
holder.cardioZoneNone.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,(float)exercise.exercise.getHrNone()));
holder.cardioZoneFatburn.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,(float)exercise.exercise.getHrFatburn()));
holder.cardioZoneCardio.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,(float)exercise.exercise.getHrCardio()));
holder.cardioZonePeak.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,(float)exercise.exercise.getHrPeak()));
I have even tried hard-coding some values in place of the ones in my objects. Strangely, it seems that setting them to 0.25 (with a weightsum of 1.0) looks correct, but most others set the cardioZoneNone much, much larger than it ought to be.
When I log the values, the numbers look appropriate.

I had read that the width should only be set to zero when using weight attribute in XML, but not when setting weight from code. Perhaps this was true when that was written, but setting width to zero does seem to fix my issue

Related

There is a jerk when I set visibility GONE for this button

I am using android:animateLayoutChanges="true"
here I am sharing my card view layout :
<?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"
android:layout_width="280dp"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="#+id/cvRegisteredPharmacy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginBottom="30dp"
app:cardBackgroundColor="#color/colorAccent"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical">
<ImageView
android:id="#+id/ivPharmacy"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_weight="0.25"
android:background="#color/colorWhite" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:id="#+id/tvPharmacyName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Pharmacy Name"
android:textColor="#color/colorWhite"
android:textSize="16sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RatingBar
android:id="#+id/rbPharmacyRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
style="?android:attr/ratingBarStyleSmall"
android:rating="3.5"
android:progressTint="#color/colorPrimary"
android:progressBackgroundTint="#color/colorGrey" />
<TextView
android:id="#+id/tvPharmacyInfo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:text="3.5"
android:textColor="#color/colorWhite"
android:textSize="12sp" />
</LinearLayout>
<TextView
android:id="#+id/tvPharmacyAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Pharmacy Address, 1/2 Main St NW, Monteral, QC"
android:textColor="#color/colorWhite"
android:textSize="14sp" />
</LinearLayout>
<Button
android:id="#+id/btnSelectPharmacy"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
android:background="#drawable/btn_bg_white"
android:text="Select Pharmacy"
android:textColor="#color/colorAccent"
android:visibility="gone" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
Here is the complete layout that I've made
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="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=".activities.PharmacyMapActivity">
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvPharmaciesList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Also, I am sharing the simple code where I am setting visibility of this button :
if (holder.btnSelectPharmacy.getVisibility() == View.VISIBLE){
holder.btnSelectPharmacy.setVisibility(View.GONE);
} else {
holder.btnSelectPharmacy.setVisibility(View.VISIBLE);
}
I've tried many solutions so far including view animation, Object animator, Expandable layout (through library) but no luck so far!
looking forward for your suggestions.
I've a workaround for this to lift the card up but then I want to make my card expanded from upside exactly as like as in the above picture.
While using automatic layout animation on the child node, the result will be, that the parent node just snaps to layout_alignParentBottom & wrap_content once the child node is View.GONE.
Combining two animations could make it less jerky. I mean, the CardView would need it's height animated; either synchronously or once the child node is View.GONE. Actually, when animating the child node to height 0dp, this would also animate the parent node's height, since it's wrap_content; setting a static value for the height would also prevent it from snapping alike that. Also animating to View.INVISBLE and then animating it's height would prevent the parent node from snapping. Sorry for not providing a ready-made solution, but explaining why it behaves alike that and how it might work is still better than no answer.

How to align a TextView according to its length

i have an interface with two ImageButtons and a TextView among them. The TextView contains a number and the two ImageButtons control that number, adding or removing 1.
The problem is when the number becomes two digits: the TextView width increase and one of two ImageButtons moves.
As you can see when the number is one digit there isn't any type of problem, but when it becomes two digits the ImageButton moves from his place. I already tried with android:layout_gravity and with android:gravity but they doesn't function. There is a way to keep the TextView "centered" and make sure the ImageButton doesn't move?
<?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:autofit="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="5dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="#+id/foodName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="10dp"
android:gravity="center_vertical"
android:hint="food_name"
android:maxLines="2"
android:textAllCaps="false"
android:textColor="#android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:gravity="end"
android:orientation="horizontal"
android:padding="5dp">
<ImageButton
android:id="#+id/addFood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
app:srcCompat="#drawable/ic_add_black_40dp" />
<TextView
android:id="#+id/foodQuantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|center_horizontal"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:gravity="center|center_horizontal"
android:maxLength="2"
android:text="0"
android:textColor="#707070"
android:textSize="20sp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/removeFood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
app:srcCompat="#drawable/ic_remove_black_40dp" />
</LinearLayout>
</LinearLayout>
This is my code and this is the layout of a RecyclerView member, because I created a personalized element.
Thanks a lot.
Since you have wrap_content on your foodQuantity TextView, addFood is going to move to accommodate for the extra spacing required to add multiple digits. If you want to keep the position of the button fixed, and make sure the buttons don't move, then you are going to have to give your TextView a fixed width to accommodate for the maximum number of characters you can expect.
I would suggest you to use android:layout_weight so you can define which element is filling the remaining space if necessary. So when setting the TextView a layout_weight of 1 and the ImageButtons a layout_weight of 0 you can afterwards set the android:gravity of the TextView that is defining the gravity inside of the TextView to center and it will always be centered.
BUT: you need to set the width of the LinearLayout to match_parent so it fills the parent. Without that the LinearLayout will never get some remaining space since it always shrinks to the size that all elements need together
...
...
<LinearLayout
android:id="#+id/layout_count"
android:layout_width="200dp"
or android:layout_width="match_parent
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_complete" />
<TextView
android:layout_width="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_height="wrap_content"
android:text="1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_complete" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_complete" />
<TextView
android:layout_width="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_height="wrap_content"
android:text="1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_complete" />
</LinearLayout>
</LinearLayout>

Cardview is not resizing itself when inner element is removed/hidden/resized

I have created cardview in following way. All fields get populated on response of a webservice. But cardview is not getting resized as per populated data.
For textview, it leaves some space below the data.
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cv2"
android:elevation="20dp"
android:layout_marginTop="1dp"
app:cardCornerRadius="0dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:id="#+id/Lin2"
android:layout_marginTop="30dp"
android:orientation="vertical"
android:animateLayoutChanges="true"
android:visibility="visible"
android:weightSum="100">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_username"
android:id="#+id/usernameTextView"
android:layout_marginTop="10dp"
android:layout_weight="20"
android:minHeight="50dp"
android:minLines="1" />
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:password="true"
android:hint="Password"
android:id="#+id/passwordTextView"
android:layout_weight="20"
android:minHeight="50dp"
android:minLines="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Update"
android:id="#+id/updateBtn"
android:background="#drawable/button_border"
android:textColor="#005AB4"
android:layout_weight="20"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="40"
android:id="#+id/credsNote"
android:textSize="14sp"
android:text=""
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:maxLines="7"
android:soundEffectsEnabled="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:orientation="horizontal"
android:id="#+id/cvLin2"
android:weightSum="100"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cv2Btn"
android:src="#drawable/cancel"
android:layout_weight="20"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cv2Title"
android:textSize="18sp"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:text="Credentials"
android:layout_weight="80"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/cv2ImgHazard"
android:src="#drawable/hazard_yellow"
android:layout_weight="20"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
Java code to populate the data
usernameTxt = (AutoCompleteTextView)findViewById(R.id.usernameTextView);
passwordTxt = (AutoCompleteTextView)findViewById(R.id.passwordTextView);
usernameTxt.setText(username);
passwordTxt.setText(password);
String message = "All set here!";
credsNote = (TextView)findViewById(R.id.credsNote);
credsNote.setText(message);
credsNote.setMinLines((1));
credsNote.setMaxLines((1));
int height_in_pixels = credsNote.getLineCount() * credsNote.getLineHeight(); //approx height text
credsNote.setHeight(height_in_pixels);
Drawable res = getResources().getDrawable(R.drawable.tick);
credStat = (ImageView)findViewById(R.id.cv2ImgHazard);
credStat.setImageDrawable(res);
Please check images below to get more idea about the problem.
How to resolve this issue?
Head part issue
Bottom space issue
I think you are using the to hide the element.
elment.setVisibility(View.INVISIBLE);
instead use:
elment.setVisibility(View.GONE);
It works like:
View.VISIBLE The view is visible.
View.INVISIBLE The view is invisible, but any spacing it would normally take up will still be used. Its "invisible"
View.GONE The view is gone, you can't see it and it doesn't take up the "spot".

Unable To Center Objects Within Two Relative Layouts

I'm attempting to split the UI for my horizontal layout into two halves each with it's own realative layout so I am able to center text and graphics within the two left and right halves of the screen. The problem is when I attmept to do so - the go button still ends up in the middle of the screen instead of centered on the right hand of the screen.
(If I can figure out how to center correctly I'm sure I'll be able to apply the same technique to the other elements to achieve the result I desire.
SOURCE:
<?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:layout_marginBottom="8sp"
android:layout_marginLeft="8sp"
android:layout_marginRight="8sp"
android:layout_marginTop="8sp"
android:background="#drawable/background"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/emblem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="24dp"
android:gravity="center"
android:scaleType="fitStart"
android:src="#drawable/apn_app_logo" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/start2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/go_button"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="#string/start_text2"
android:textColor="#000000"
android:textSize="18sp" />
<Button
android:id="#+id/go_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/apn_app_go_button" />
</RelativeLayout>
</RelativeLayout>
EDIT: (in response to Kuba's answer)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="horizontal"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/background"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/emblem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="24dp"
android:gravity="center"
android:scaleType="fitStart"
android:src="#drawable/apn_app_logo" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/background"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/go_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/apn_app_go_button"
android:gravity="center_horizontal" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/start_text2"
android:textColor="#000000"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
Instead of Relative layout, I would recommend using LinearLayout.
<LinearLayout 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"
android:orientation="horizontal"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#00dd00"
android:gravity="center"
>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#dd0000"
android:gravity="center_horizontal"
>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
In the inner LinearLayout you can either use android:gravity="center" to have its children centered vertically and horizontally. Or use use android:gravity="center_horizontal" to center in only horizontal way.
EDIT: Added the textview, which was discussed in the comments.
To have the textView appear underneath the button, you must change the orientation of the LinearLayout from horizontal to vertical. Please see the developer site .
The inner LinearLayout thus looks like this.
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:background="#dd0000"
android:gravity="center"
>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text here"
/>
</LinearLayout>
How can I use android:layout_below="#+id/go_button" in a linear layout?
You can't. These relationships only work in relative layout where you place views in relation to each other or the parent view. In linear layout items are stacked either vertically or horizontally.
Set android:gravity="center" in
<Button
android:id="#+id/go_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/apn_app_go_button" />
Maybe its work.

How can I align this layout correctly?

I have two textviews. I want one of them to be left aligned and the other to be right aligned. Pretty much so that it looks like a 2 column table.
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="#+id/profileLayout" android:orientation="vertical">
<LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:id="#+id/experience" android:orientation="horizontal">
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/experienceLabel" android:textStyle="bold" android:layout_weight="0" android:text="Experience"></TextView>
<LinearLayout android:id="#+id/linearLayout4" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"></LinearLayout>
<TextView android:text="TextView" android:layout_height="wrap_content" android:id="#+id/experienceTextView" android:layout_gravity="right" android:layout_weight="0" android:layout_width="wrap_content"></TextView>
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal">
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1"/>
<TextView android:layout_height="wrap_content" android:layout_weight="0" android:layout_width="wrap_content"/>
</LinearLayout>
Set the weight of the first one to 1 so it will eat up all of the extra space. Set the 2nd oen to wrap content, so it only takes up as much space as it needs.
But why do you have an extra LinearLayout around them? If you have nested LinearLayouts, you should probably be using a RelativeLayout instead.
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/experienceLabel" android:textStyle="bold" android:layout_weight="1" android:text="Experience"></TextView>
<TextView android:text="TextView" android:layout_height="wrap_content" android:id="#+id/experienceTextView" android:layout_weight="1" android:layout_width="wrap_content"></TextView>
</LinearLayout>
Use Relative Layout. About Relative Layout Link Example link
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content" android:id="#+id/experienceLabel"
android:textStyle="bold" android:layout_weight="0" android:text="Experience"
android:layout_alignParentLeft="true"/>
<TextView android:text="TextView" android:layout_height="wrap_content"
android:id="#+id/experienceTextView" android:layout_gravity="right"
android:layout_weight="0" android:layout_width="wrap_content"
android:layout_alignParentRight="true" />
Benefit of using RelativeLayout: it reduces your number of layout. Which will optimize your ui too.
I'm not entirely sure of the exactly layout you want, but you can try 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="wrap_content"
android:id="#+id/profileLayout"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:weightSum="2"
android:id="#+id/experience"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/experienceLabel"
android:textStyle="bold"
android:layout_weight="1"
android:text="Experience"/>
<TextView
android:text="TextView"
android:layout_height="wrap_content"
android:id="#+id/experienceTextView"
android:gravity="right"
android:layout_weight="1"
android:layout_width="wrap_content"/>
</LinearLayout>
</LinearLayout>
You were using layout_gravity rather than gravity also. layout_gravity tells the parent how you want to be laid out, gravity tells content how to be laid out.
Notice how I have a weightSum in the parent layout to 2 and then set the two children to a weight of 1 each. I also set the parent to fill_parent
You had and extra layout in there too - if you want spacing, set a margin on the second textview or use a view (a simpler object than a LinearLayout)

Categories