Display an image in Android Studio using ImageView in layout preview - android

I'm working on an Android app. I create an item layout for a recycler view and I'd like to display a preview of it in Android Studio using layout preview with tools namespace. To accomplish this task I:
Put in sampledata an image (sampledata\nre1.png). This is an important thing because I don't want to include the sample image in the apk.
Use tools namespace in the ImageView to display the image in the layout preview
An screenshot:
And the layout 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="wrap_content"
android:paddingLeft="8dp"
android:paddingTop="8dp"
android:paddingRight="8dp"
android:paddingBottom="8dp"
tools:context=".ui.adapters.BarcodeAdapter">
<ImageView
android:id="#+id/item_barcode_nre1_image"
android:layout_width="wrap_content"
tools:layout_width="120dp"
android:layout_height="112dp"
android:scaleType="fitXY"
android:layout_marginStart="#dimen/text_margin"
android:contentDescription="NRE1"
android:layout_marginTop="4dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/item_barcode_nre_label"
tools:src="#sample/nre1.png" />
<ImageView
android:id="#+id/item_barcode_nre2_image"
android:layout_width="wrap_content"
android:layout_height="112dp"
android:scaleType="fitXY"
android:layout_marginEnd="#dimen/text_margin"
android:contentDescription="NRE2"
android:adjustViewBounds="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/item_barcode_nre1_image"
tools:src="#sample/nre2.png" />
<ImageView
android:id="#+id/item_barcode_codice_fiscale_image"
android:layout_width="wrap_content"
android:layout_height="112dp"
android:scaleType="fitXY"
android:contentDescription="Codice fiscale"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/item_barcode_codice_fiscale_label"
tools:src="#sample/codice-fiscale.png" />
<TextView
android:id="#+id/item_barcode_nre_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/text_margin"
android:text="#string/item_qrcode_nre"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/item_barcode_nre1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:ellipsize="end"
android:maxLines="10"
android:textAppearance="#android:style/TextAppearance.Material.Body1"
app:layout_constraintEnd_toEndOf="#+id/item_barcode_nre1_image"
app:layout_constraintStart_toEndOf="#+id/item_barcode_nre_label"
app:layout_constraintStart_toStartOf="#+id/item_barcode_nre1_image"
app:layout_constraintTop_toBottomOf="#+id/item_barcode_nre1_image"
tools:text="1300A" />
<TextView
android:id="#+id/item_barcode_nre2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="10"
android:layout_marginTop="4dp"
android:textAppearance="#android:style/TextAppearance.Material.Body1"
app:layout_constraintEnd_toEndOf="#+id/item_barcode_nre2_image"
app:layout_constraintHorizontal_bias="0.482"
app:layout_constraintStart_toStartOf="#+id/item_barcode_nre2_image"
app:layout_constraintTop_toBottomOf="#+id/item_barcode_nre2_image"
tools:text="4003535379" />
<TextView
android:id="#+id/item_barcode_codice_fiscale_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/text_margin"
android:text="#string/item_qrcode_codice_fiscale"
app:layout_constraintStart_toStartOf="#+id/item_barcode_nre_label"
app:layout_constraintTop_toBottomOf="#+id/item_barcode_nre1" />
<TextView
android:id="#+id/item_barcode_codice_fiscale"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_marginTop="4dp"
android:textAppearance="#android:style/TextAppearance.Material.Body1"
app:layout_constraintEnd_toEndOf="#+id/item_barcode_codice_fiscale_image"
app:layout_constraintStart_toStartOf="#+id/item_barcode_codice_fiscale_image"
app:layout_constraintTop_toBottomOf="#+id/item_barcode_codice_fiscale_image"
tools:text="CF" />
</androidx.constraintlayout.widget.ConstraintLayout>
I think I do it correctly, but it does not work. Any idea? Tnx.

<ImageView
android:id="#+id/item_barcode_nre1_image"
android:layout_width="wrap_content"
tools:layout_width="120dp"
android:layout_height="112dp"
android:scaleType="fitXY"
android:contentDescription="NRE1"
android:layout_marginTop="4dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/item_barcode_nre_label"
tools:src="#drawable/ic_launcher_background" />
Working on my side. Let's try. Thanks

Related

How to fix the resizable area of button (image) in Android Studio being bigger than the button itself?

!!Beginner alert!!
I designed my app in Adobe XD. Now I started to introduce the design in Android Studio. However, whenever I use a resource from the resource manager and drop it in the designer, the resizable area is way bigger than the image which i think will affect the clickable area of it. Everything looks good in Adobe XD, the resizable area is the border of the button. How can I do this in Android Studio? I will attach images below. Help is greatly appreciated.
How it's supposed to look (the look in Adobe XD)
The resizable area of the button in Adobe XD
The resizable area of the button in Android Studio
How can I make Android Studio have the same resizable area as Adobe XD?
I implemented the XML code 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="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
tools:context=".Dashboard">
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="10dp"
app:layout_constraintEnd_toStartOf="#+id/imageView5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/resmonevo" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
app:layout_constraintStart_toStartOf="#+id/imageView3"
app:layout_constraintTop_toBottomOf="#+id/imageView3"
app:srcCompat="#drawable/red_gradient_line" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="121dp"
app:layout_constraintBottom_toBottomOf="#+id/imageView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView3"
app:layout_constraintTop_toTopOf="#+id/imageView3"
app:srcCompat="#drawable/dashboard" />
<ImageView
android:id="#+id/imageView14"
android:layout_width="0dp"
android:layout_height="115dp"
android:layout_marginStart="5dp"
android:layout_marginTop="7dp"
android:layout_marginEnd="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView4"
app:srcCompat="#drawable/rectangle_210" />
<ImageView
android:id="#+id/imageView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/imageView18"
app:srcCompat="#drawable/rectangle_221" />
<ImageView
android:id="#+id/imageView18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="18dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toTopOf="#+id/textView2"
app:layout_constraintStart_toEndOf="#+id/imageView15"
app:srcCompat="#drawable/welcome_" />
<ImageView
android:id="#+id/imageView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="47dp"
android:layout_marginEnd="27dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/imageView14"
app:srcCompat="#drawable/how_are_you_today" />
<ImageView
android:id="#+id/imageView20"
android:layout_width="21dp"
android:layout_height="19dp"
app:layout_constraintBottom_toBottomOf="#+id/imageView15"
app:layout_constraintEnd_toEndOf="#+id/imageView15"
app:layout_constraintStart_toStartOf="#+id/imageView15"
app:layout_constraintTop_toTopOf="#+id/imageView15"
app:srcCompat="#drawable/ic_person_outline_24px" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="32dp"
android:layout_marginEnd="9dp"
android:fontFamily="#font/montserrat"
android:text="Username"
android:textColor="#FFFFFF"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="#+id/imageView19"
app:layout_constraintEnd_toStartOf="#+id/imageView19"
app:layout_constraintStart_toStartOf="#+id/imageView18"
app:layout_constraintTop_toBottomOf="#+id/imageView19" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="390dp"
android:layout_height="351dp"
android:adjustViewBounds="true"
app:srcCompat="#drawable/rectangle_187"
tools:layout_editor_absoluteX="-39dp"
tools:layout_editor_absoluteY="158dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
The XML code for the most recent button is :
<ImageView
android:id="#+id/imageView8"
android:layout_width="390dp"
android:layout_height="351dp"
android:adjustViewBounds="true"
app:srcCompat="#drawable/rectangle_187"
tools:layout_editor_absoluteX="-39dp"
tools:layout_editor_absoluteY="158dp" />
Here is how my mess looks
The problem what I can figure out is that you are using static size in width
I will suggest you to use Linear layout as parent and inside it have a Image view as child because you are statically defining the width of image view.
so try something like this:
Kindly use imageView in your case I have used MaterialTextView just for an instance
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal"
android:padding="12dp">
<com.google.android.material.textview.MaterialTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:fontFamily="#font/cantata_one"
android:text="Email"/>
<com.google.android.material.textview.MaterialTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.7"
android:id="#+id/layout_recycler_all_cust_email"
android:fontFamily="#font/capriola"
android:textColor="#706E6E"
android:text="Email "/>
</LinearLayout>
and in the above code, you can see i have assigned weightsum to the LinearLayout and accordingly wait to it's children so you should try this. This will make your UI Flexible according to the device size and in the android:layout_height="wrap_content" you can use your value like 350DP or something else.

Problem with layout constructing in Android

I have a layout for my recyclerview item:
<?xml version="1.0" encoding="utf-8"?>
<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_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:orientation="vertical">
<ImageView
android:id="#+id/iv_hotel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_launcher_background" />
<TextView
android:id="#+id/tv_hotel_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/iv_hotel"
tools:text="Belleclaire Hotel" />
<include
android:id="#+id/include"
layout="#layout/include_stars_set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="#+id/tv_hotel_name"
app:layout_constraintTop_toBottomOf="#+id/tv_hotel_name" />
<TextView
android:id="#+id/tv_hotel_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="#+id/include"
app:layout_constraintTop_toBottomOf="#+id/include"
tools:text="250 West 77th Street, Manhattan" />
<TextView
android:id="#+id/tv_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="#+id/tv_hotel_address"
app:layout_constraintTop_toBottomOf="#+id/tv_hotel_address"
tools:text="100.0" />
<TextView
android:id="#+id/tv_suites_available_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_suites_availability"
app:layout_constraintStart_toStartOf="#+id/tv_distance"
app:layout_constraintTop_toBottomOf="#+id/tv_distance" />
<TextView
android:id="#+id/tv_suites_available"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintStart_toEndOf="#+id/tv_suites_available_label"
app:layout_constraintTop_toTopOf="#+id/tv_suites_available_label" />
<ProgressBar
android:id="#+id/progress_hotel_image"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/iv_hotel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/iv_hotel" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
It seems like this:
enter image description here
However after building I have this view:
enter image description here
As you can see, I see only a half of my progressbar after I'm trying to dowload an image to my iv_hotel with a help of Glide.
How can I solve this problem? I'm a bit bad at xml guidelines understanding, so I'm asking for a help(
Use placeholder to show default image while a request is in progress
Glide.with(context)
.load(url)
.placeholder(R.drawable.placeholder)
.into(view);
if (!imageUrl.isNullOrEmpty()) {
imageView?.let { iv ->
Glide.with(iv.context)
.load(AppUtility.IMAGE_THUMB_URL.plus(imageUrl))
.apply(options)
.into(iv)
}
} else {
imageView?.setImageDrawable(error)
// set here your error img
}

Set buttons inline and break line to fit different devices

I want to add some buttons in a line and break it in a new line when they reach the end of the screen:
If you know CSS, what I need is similar to display: inline-block rule.
I am looking for a XML solution only, I want to avoid measuring the screen and buttons using java to emplace them below programatically.
This is my current code, the following buttons are inside a ConstraintLayout:
<Button
android:id="#+id/boton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:text="Imagen" />
<Button
android:id="#+id/boton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="#id/boton1"
android:text="Play" />
<Button
android:id="#+id/boton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="#id/boton2"
android:text="Audio" />
<Button
android:id="#+id/boton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="#id/boton3"
android:text="Play"
android:onClick="playVideo" />
<Button
android:id="#+id/boton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="#id/boton4"
android:text="Youtube" />
<Button
android:id="#+id/boton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="#id/boton5"
android:text="Raw" />
since last time, I found this library that you should enjoy ! github.com/google/flexbox-layout
In your case, you should use the flexWrap attribute :
This attribute controls whether the flex container is single-line or multi-line, and the direction of the cross axis. Possible values are:
nowrap (default)
wrap (use this)
wrap_reverse
You will be able to wrap your buttons.
Installation
Add the following dependency to your build.gradle file:
dependencies {
implementation 'com.google.android:flexbox:2.0.1'
}
Usage
FlexboxLayout that extends the ViewGroup like LinearLayout and RelativeLayout. You can specify the attributes from a layout XML like:
<com.google.android.flexbox.FlexboxLayout
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"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch" >
<TextView
android:id="#+id/textview1"
android:layout_width="120dp"
android:layout_height="80dp"
app:layout_flexBasisPercent="50%"
/>
<TextView
android:id="#+id/textview2"
android:layout_width="80dp"
android:layout_height="80dp"
app:layout_alignSelf="center"
/>
<TextView
android:id="#+id/textview3"
android:layout_width="160dp"
android:layout_height="80dp"
app:layout_alignSelf="flex_end"
/>
</com.google.android.flexbox.FlexboxLayout>
Have you tried using gridLayout ?
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="4">
<Button
android:id="#+id/boton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Imagen"
/>
<Button
android:id="#+id/boton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
/>
<Button
android:id="#+id/boton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Audio"
/>
<Button
android:id="#+id/boton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:onClick="playVideo"
/>
<Button
android:id="#+id/boton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Youtube"
/>
<Button
android:id="#+id/boton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Raw"
/>
</GridLayout>`

How to make themes?

i have a list item, in that i have certain text views and image views. The thing is that the design that i have got has three themes in it dark, brown and light. Now if I want to change the background of a list item to any of these three colors, how do I set the background. Like if i change the theme of the app from settings, the list item background should change. How am I suppose to make the themes with different backgrounds?
<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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.constraint.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="12dp"
android:paddingEnd="16dp"
android:paddingStart="16dp"
android:paddingTop="12dp">
<TextView
android:id="#+id/company_name_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/montserratlight"
android:text="Synergy Bizcon"
android:textColor="#color/textColorMediumThemeDark"
android:textSize="#dimen/fontMedium"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/schedule_imageview"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_marginTop="5dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/company_name_textview"
app:srcCompat="#drawable/ic_watch" />
<TextView
android:id="#+id/date_time_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_marginTop="4dp"
android:fontFamily="#font/montserratlight"
android:text="26 Apr 11:10 PM"
android:textColor="#color/textColorSmallThemeDark"
android:textSize="#dimen/fontExtraSmall"
app:layout_constraintStart_toEndOf="#id/schedule_imageview"
app:layout_constraintTop_toBottomOf="#id/company_name_textview" />
<TextView
android:id="#+id/pipe_textview"
android:layout_width="0.5dp"
android:layout_height="12dp"
android:layout_marginStart="6dp"
android:background="#color/textColorSmallThemeDark"
app:layout_constraintBottom_toBottomOf="#id/date_time_textview"
app:layout_constraintStart_toEndOf="#id/date_time_textview"
app:layout_constraintTop_toTopOf="#id/date_time_textview" />
<TextView
android:id="#+id/exhcange_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:fontFamily="#font/montserratlight"
android:text="BSE"
android:textColor="#color/textColorSmallThemeDark"
android:textSize="#dimen/fontExtraSmall"
app:layout_constraintBottom_toBottomOf="#id/pipe_textview"
app:layout_constraintStart_toEndOf="#id/pipe_textview"
app:layout_constraintTop_toTopOf="#id/pipe_textview" />
<TextView
android:id="#+id/rate_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:fontFamily="#font/montserratlight"
android:text="34567.58"
android:textColor="#color/textColorMediumThemeDark"
android:textSize="#dimen/fontMedium"
app:layout_constraintEnd_toStartOf="#id/add_imageview"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/points_change_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:fontFamily="#font/montserratlight"
android:text="-21.10(-0.02%)"
android:textColor="#color/textColorSmallThemeDark"
android:textSize="#dimen/fontExtraSmall"
app:layout_constraintEnd_toEndOf="#id/rate_textview"
app:layout_constraintTop_toBottomOf="#id/rate_textview" />
<ImageView
android:id="#+id/add_imageview"
android:layout_width="16dp"
android:layout_height="16dp"
android:tint="#color/iconsColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_add" />
</android.support.constraint.ConstraintLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.2dp"
android:background="#color/listItemDivider" />
</Linear Layout>
Like i have this list item, i want to change constraint layouts background to may be dark or brown or light. I would eventually use style to give the background, but i do not know how? Can someone help? Thanks

How to debug an unexpected Android layout?

My ConstraintLayout is showing some strange plotting of items on app. It is showing the elements correct positions on the preview screen of the android studio but while running app on the phone the positions of the elements are strange.
Here is the screen shot of app:
My layout is:
<?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"
android:background="#drawable/background"
>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="34dp"
android:layout_height="56dp"
android:text="appbar"
android:background="#android:color/transparent"
tools:layout_editor_absoluteY="25dp"
tools:layout_editor_absoluteX="8dp" />
<ImageView
android:id="#+id/imgBooks"
android:layout_width="272dp"
android:layout_height="302dp"
android:src="#drawable/books"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="56dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/toolbar" />
<ImageView
android:id="#+id/imgName"
android:layout_width="40dp"
android:layout_height="35dp"
app:srcCompat="#drawable/name"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="373dp"
android:layout_below="#+id/imgBooks"
android:layout_alignParentStart="true" />
<EditText
android:id="#+id/etEmail"
android:layout_width="320dp"
android:layout_height="43dp"
android:ems="10"
android:text="Email"
android:inputType="textEmailAddress"
tools:layout_editor_absoluteX="67dp"
tools:layout_editor_absoluteY="417dp"
android:fontFamily="AvenirLTStd Light"
android:textColor="#ffffff"
android:layout_above="#+id/etPhone"
android:layout_toEndOf="#+id/imgPhone" />
<ImageView
android:id="#+id/imgEmail"
android:layout_width="38dp"
android:layout_height="34dp"
app:srcCompat="#drawable/email"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="417dp"
android:layout_alignTop="#+id/etEmail"
android:layout_toStartOf="#+id/etEmail" />
<EditText
android:id="#+id/etName"
android:layout_width="320dp"
android:layout_height="43dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
tools:layout_editor_absoluteX="67dp"
tools:layout_editor_absoluteY="373dp"
android:fontFamily="AvenirLTStd Light"
android:textColor="#ffffff"
android:layout_above="#+id/etEmail"
android:layout_alignEnd="#+id/btn" />
<ImageView
android:id="#+id/imgPhone"
android:layout_width="35dp"
android:layout_height="31dp"
app:srcCompat="#drawable/phone"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="463dp"
android:layout_alignTop="#+id/etPhone"
android:layout_alignParentStart="true" />
<EditText
android:id="#+id/etPhone"
android:layout_width="320dp"
android:layout_height="43dp"
android:ems="10"
android:inputType="phone"
android:text="Phone"
tools:layout_editor_absoluteX="67dp"
tools:layout_editor_absoluteY="463dp"
android:fontFamily="AvenirLTStd Light"
android:textColor="#ffffff"
android:layout_above="#+id/btn"
android:layout_alignStart="#+id/etName"
android:layout_alignEnd="#+id/btn" />
<Button
android:id="#+id/btn"
android:layout_width="344dp"
android:layout_height="48dp"
android:text="Download Brochure"
android:layout_gravity="center_vertical|center_horizontal"
tools:layout_editor_absoluteX="20dp"
tools:layout_editor_absoluteY="514dp"
android:fontFamily="AvenirLTStd Light"
android:textColor="#ffffff"
android:background="#drawable/curves"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</android.support.constraint.ConstraintLayout>
The problem is you are not using constraints at all which is the whole point of constraint layout.
For example, if you don't use constraints on Download Brochure button and use only tools:layout_editor_absoluteX and tools:layout_editor_absoluteY attributes, the button stays at the position where you put it on the editor but when you compile the app, the button goes top left corner.
You need to add those to Download Brochure button, then set its location. So it will be on the same position in every possible situation.
app:layout_constraintLeft_toLeftOf="parent",
app:layout_constraintRight_toRightOf="parent"
But I suggest you to do it on editor while playing around with constraint layout which is much easier.
You can also watch this video. https://www.youtube.com/watch?v=sO9aX87hq9c. It helps a lot.

Categories

Resources