I cannot evenly space buttons - android-layout

I am using constraint layout in android studio, I want to evenly space buttons on main activity, it works fine on mobile view, but when I try to switch to tablet view it gets messed up, is there any way to evenly space buttons on both views?here is the problem in image

In case you want an example for how chainStyle may be used, you can look at one of our public samples. Or here is a paste from the link:
<androidx.cardview.widget.CardView
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="wrap_content"
android:layout_margin="#dimen/margin_std_half"
android:clickable="true"
app:cardBackgroundColor="#color/colorAccentLighter"
app:cardCornerRadius="#dimen/cardview_corner_radius"
app:contentPadding="#dimen/cardview_padding"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/sku_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#id/guideline"
app:layout_constraintEnd_toStartOf="#id/sku_price"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="This is a Title placeholder"/>
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/sku_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/sku_title"
app:layout_constraintTop_toTopOf="parent"
tools:text="$4.99"/>
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.25"/>
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/sku_image"
android:layout_width="wrap_content"
android:layout_height="68dp"
android:adjustViewBounds="true"
android:maxWidth="126dp"
android:scaleType="centerInside"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/sku_description"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#id/guideline"
/>
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/sku_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/margin_std_half"
android:layout_marginRight="#dimen/margin_std_half"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/sku_image"
app:layout_constraintTop_toTopOf="#id/guideline"
tools:text="This is a description placeholder, telling users how cool this item is"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

Related

Incorrect display of a custom dialog

Hi guys i have a problem with the view of a dialog window: in the preview of the screen, before sending the app in debug, I see the screen in a way; while, when I execute the app, my dialog window's layout doesn't respect the layout before the execution phase. Nel dialog I used a constraint layout; I don't know if this is the actual problem. I'm attaching the pictures of how it should be seen and the way it looks.
This is the XML ``
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<TextView
android:id="#+id/textView_titolo_dialog_contratta_tasso_di_interesse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="47dp"
android:layout_marginTop="10dp"
android:text="Contratta tasso di interesse
"
android:textColor="#color/black"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="81dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/icona_direttore_banca_per_dialog_ridimensionata" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="540dp"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="1dp">
<Button
android:id="#+id/button_dialog_contratta_parla"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/bottone_modificato_banca"
android:text="Parla"
app:backgroundTint="#null" />
</LinearLayout>
<ImageView
android:id="#+id/imageView_dialog_contratta_risposta_si"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginStart="95dp"
android:layout_marginTop="44dp"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/risposta_s__direttore_della_banca" />
<ImageView
android:id="#+id/imageView_dialog_contratta_risposta_no"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginStart="95dp"
android:layout_marginTop="44dp"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/risposta_no_direttore_della_banca" />
</androidx.constraintlayout.widget.ConstraintLayout>
Photo of incorrect view
Photo of the correct view

I want to make a TextView height "match_parent" whose parent height is "wrap_content" in XML (Android Studio)

Inside the layout for my activity i have two TextView inside a RelativeLayout.
The one with id "descriptionfromDB" takes the text from a database while the other one will only show the text Description.
What I want is that the second one matches the height of the parent when the text of the one that gets modified become longer.
I tried to do it with android:layout_height="match_parent" for the first TextView (since parent height grows when the other TextView's text become longer) but with no results.
Here's an example of how it looks at the moment:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/description"
android:layout_width="100dp"
android:layout_height="match_parent"
android:textSize="18dp"
android:text="Description"
android:background="#drawable/text"
android:textColor="#color/blue_fontcolor"
android:padding="1dp"
android:textStyle="bold"
/>
<TextView
android:id="#+id/descriptionfromDB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
android:layout_toRightOf="#+id/description"
android:text=""
android:background="#drawable/text"
android:textColor="#color/blue_fontcolor"
android:padding="3dp"
/>
</RelativeLayout>
You can achieve the behavior you want using horizontal LinearLayout :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/description"
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="Description"
android:textSize="18sp" />
<TextView
android:id="#+id/descriptionfromDB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp" />
</LinearLayout>
Using weight would be a better idea since there are many different screen sizes. Below attributes determine that the first TextView should take 30% of the parent width and second one 70% which is the remaining width.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/description"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:text="Description"
android:textSize="18sp" />
<TextView
android:id="#+id/descriptionfromDB"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:textSize="16sp" />
</LinearLayout>
If you are going to add more views and nest this LinearLayout into another ViewGroup, since your layout will no longer be flat and also horizontal LinearLayout has double layout taxation, I recommend ConstraintLayout. Same behavior using ConstraintLayout :
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="Description"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/descriptionfromDB"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/description"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

How do I move my Button and Edittext freely in android studio

So I have 3 fields to get user input from and a button. When i try to move them the "centerinParent = true" inserts automatically even when i turn it off. I want to move those fields where I want (I use relative layout) and they just wont move What should I do?
activity code :
<?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=".MainActivity">
<EditText
android:id="#+id/numar_secunde_work"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginStart="36dp"
android:layout_marginTop="224dp"
android:ems="10"
android:hint="nr secunde workout"
android:inputType="number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="false"
android:layout_centerInParent="true"
android:layout_marginStart="41dp"
android:layout_marginBottom="236dp"
android:text="Start Workout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="#+id/numar_repetitii"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btn_start"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginStart="105dp"
android:layout_marginTop="249dp"
android:layout_marginBottom="50dp"
android:ems="10"
android:hint="nr repetitii"
android:inputType="number" />
<EditText
android:id="#+id/numar_pauza"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="103dp"
android:layout_marginTop="191dp"
android:ems="10"
android:hint="nr secunde pauza"
android:inputType="number" />
</RelativeLayout>

Tab indicator no longer at the bottom of AppBarLayout after migrating app to AndroidX. The tab is custom made tab

I migrated android app to AndroidX and then I noticed this
As you can see the tab indicator went up into the AppBarLayout. Here is the code
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay"
android:elevation="4dp">
<androidx.appcompat.widget.Toolbar
... // some code here relevant to toolbar
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="#layout/custom_tab" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="#layout/custom_tab" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="#layout/custom_tab"/>
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
The custom layout I created is below:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tab_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="CONTRIBUTIONS"
style="#style/TextAppearance.Design.Tab"/>
<TextView
android:id="#+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#id/tab_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="1000"
android:layout_marginBottom="8dp"
style="#style/TextAppearance.Design.Tab"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:text="#string/open_bracket"
app:layout_constraintBottom_toBottomOf="#+id/tv_number"
app:layout_constraintEnd_toStartOf="#+id/tv_number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/tv_number"
app:layout_constraintHorizontal_bias="1"
style="#style/TextAppearance.Design.Tab"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="#string/closing_bracket"
app:layout_constraintBottom_toBottomOf="#+id/tv_number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/tv_number"
app:layout_constraintTop_toTopOf="#+id/tv_number"
app:layout_constraintHorizontal_bias="0"
style="#style/TextAppearance.Design.Tab"/>
</androidx.constraintlayout.widget.ConstraintLayout>`
It looks fine in android 22 and below but not android 24 and above. However before this migration to androidx it was fine in all versions i.e the indicator was at bottom of appbarlayout not inside it.
You must set app:tabIndicatorColor="YOUR_COLOR" in TabLayout like this:
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#color/blue">
... // Your TabItems
</com.google.android.material.tabs.TabLayout>
if doesn't work, try to remove android:layout="#layout/custom_tab" from TabItems too.

Why Tab Layout's layout is neither scrollable or fit screen inside a fragment?

So i have a bottom navigation bar and in one of them it holds a tablayout consists of 3 tabs. Like the below image:
The Tablayout is inside a fragment, where it's not scrollable
The second issue other than not scrollable is, it doesn't fit the screen. Here is the android studio UI with blueprint image. (Please do compare with previous image and you will see the right most helloworld is not fitting in the screen and also couple of hello world is not scrollable!)
Tab Layout's Fragment not fitting the screen!
Partial of my home_fragment.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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tool"
>
<com.google.android.material.tabs.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMaxWidth="0dp"
app:tabMode="fixed"
app:tabPaddingBottom="-1dp"
app:tabPaddingEnd="-1dp"
app:tabPaddingStart="-1dp"
app:tabPaddingTop="-1dp"
app:layout_scrollFlags="scroll|enterAlways">
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tablayout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</LinearLayout>
And here is my Category.xml (where it doesn't fit the screen)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="88dp"
android:text="Hello World"
android:textSize="22dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="280dp"
android:layout_marginLeft="280dp"
android:layout_marginTop="56dp"
android:text="Hello World"
android:textSize="22dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="136dp"
android:text="Hello World"
android:textSize="22dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="188dp"
android:text="Hello World"
android:textSize="22dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="188dp"
android:text="Hello World"
android:textSize="22dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="78dp"
android:text="YYYYYYYYYYYUU"
android:textSize="22dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="48dp"
android:text="Hello World"
android:textSize="22dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="156dp"
android:layout_marginLeft="156dp"
android:layout_marginTop="56dp"
android:text="Hello World"
android:textSize="22dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="200dp"
android:layout_marginLeft="200dp"
android:text="Hello World"
android:textSize="22dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
I have tried almost all StackOverflow question regarding this problem, but nothing did work for me!May be i just screwed up somewhere, as i am a noob programmer! Any help good people?

Resources