Implementing rounded constraint layout - android

I have a ConstraintLayout with rounded corners:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:background="#drawable/backgrond_drawable"
app:layout_constraintWidth_percent="0.3"
app:layout_constraintDimensionRatio="1:1.5"
>
</androidx.constraintlayout.widget.ConstraintLayout>
and this is backgrond_drawable.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<shape android:shape="rectangle" >
<corners android:radius="20dip" />
<stroke android:width="1dip" android:color="#222222" />
<gradient android:angle="-90" android:startColor="#222222" android:endColor="#222222" />
</shape>
</item>
When I add a TextView to bottom of my ConstraintLayout it eliminate the roundness of corners:
what should i do?

YOu need to have a second drawable to make the lower corners of the textview rounded
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<shape android:shape="rectangle" >
<corners android:bottomLeftRadius="20dp" android:bottomRightRadius="20dp"/>
<stroke android:width="1dip" android:color="#222222" />
<gradient android:angle="-90" android:startColor="#222222" android:endColor="#222222" />
</shape>
</item>
and set this drawable as the textView's background.
Or you could use a card layout and then use the textview
for eg
<CardView.
.
.
.
cardCornerRadius:"20dp"
>
<ContraintLayout>
<TextView>
</TextView>
</ContraintLayout>
</CardView>

Set your TextView background transparent it will fix your issue; I hope 🤞 You can try the below approach.
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="100dp"
android:background="#drawable/backgrond_drawable"
app:layout_constraintDimensionRatio="1:1.5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.3">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:gravity="center"
android:text="Testing text"
android:textColor="#color/white"
app:backgroundTint="#color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Related

How to make five different colours width equal in my drawable that the layout will use as a background?

I have created five different colours in my drawable, however, when the background shows on the android handheld the colour width is not equal for each colour.
My goal is to make each colour width equal in the drawable level.
This is what I like to achieve:
Drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="50dp">
<shape android:shape="rectangle">
<size android:height="250dp" android:width="50dp"/>
<solid android:color="#color/red_background_70_lighter"/>
</shape>
</item>
<item android:left="50dp">
<shape android:shape="rectangle">
<size android:height="250dp" android:width="50dp"/>
<solid android:color="#android:color/holo_green_light"/>
</shape>
</item>
<item android:left="100dp">
<shape android:shape="rectangle">
<size android:height="250dp" android:width="50dp"/>
<solid android:color="#android:color/holo_orange_dark"/>
</shape>
</item>
<item android:left="150dp">
<shape android:shape="rectangle">
<size android:height="250dp" android:width="50dp"/>
<solid android:color="#android:color/holo_blue_dark"/>
</shape>
</item>
<item android:left="200dp">
<shape android:shape="rectangle">
<size android:height="250dp" android:width="50dp"/>
<solid android:color="#android:color/darker_gray"/>
</shape>
</item>
<item android:top="-2dp" android:right="0dp" android:left="0dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#android:color/holo_orange_light" />
</shape>
</item>
</layer-list>
This is what I have on the Android HH at the moment:
Try this:
1.. create 9 patch png image (containing equal width rectangles) ..
2.. set created file as background..
Hope this works..
for using 9 patch.. here is the link
https://developer.android.com/studio/write/draw9patch
.. as you mentioned it will slow down the recycler view... here is another apprach.. nest constraint layout in your view (I have nested it in Relative Layout..
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:id="#+id/rootLayout"
tools:context=".MainActivity">
android:layout_width="match_parent"
android:layout_height="400dp">
<LinearLayout
android:id="#+id/dropables"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"></LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/dropables"
android:layout_marginTop="5dp"
android:padding="5dp">
<TextView
android:id="#+id/holder1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#color/blue"
app:layout_constraintHeight_default="percent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.25" />
<TextView
android:id="#+id/holder2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#color/red"
app:layout_constraintHeight_default="percent"
app:layout_constraintLeft_toRightOf="#id/holder1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.25"/>
<TextView
android:id="#+id/holder3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#color/blueShade"
app:layout_constraintHeight_default="percent"
app:layout_constraintLeft_toRightOf="#id/holder2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.25"/>
<TextView
android:id="#+id/holder4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#color/black"
app:layout_constraintHeight_default="percent"
app:layout_constraintLeft_toRightOf="#id/holder3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.25"/>
<TextView
android:id="#+id/Actucal_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintHeight_default="percent"
app:layout_constraintLeft_toLeftOf="parent"
android:text="Your Text"
android:textColor="#color/white"
android:textAlignment="center"
android:textSize="40dp"
android:paddingTop="20dp"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
I have used text view as holder for background color .. if u want to change color of specific strip. change color of that "holder" programmatically.

How to make upper shadow for button in android?

I want to make a button like this. I used cardview to give the radius and drop shadow effect. But only dropshadow does not gives an edgy style like in the image above.
Here is my code for creating this type of button:
<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginEnd="10dp"
app:cardCornerRadius="20dp"
app:cardBackgroundColor="#color/tuna2"
app:strokeColor="#color/gun_powder"
app:cardElevation="6dp"
app:contentPadding="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" >
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView28"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/transfer"
android:textColor="#color/white"
android:layout_marginStart="30dp"
android:layout_marginEnd="37dp"
android:fontFamily="#font/dm_sans"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
app:layout_constraintBottom_toBottomOf="#+id/textView28"
app:layout_constraintStart_toEndOf="#+id/textView28"
app:layout_constraintTop_toTopOf="#+id/textView28"
app:srcCompat="#drawable/ic_arrow_right" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
This code creates a button like in the image below:
How can I make the button look like the sun is hitting from above?
Try This
drawable/custom_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#41445F"/>
<corners android:radius="20dp"/>
<size android:width="100dp" android:height="40dp"/>
</shape>
</item>
<item android:top="2dp">
<shape android:shape="rectangle">
<solid android:color="#303348"/>
<corners android:radius="20dp"/>
<size android:width="100dp" android:height="40dp"/>
</shape>
</item>
</layer-list>
and set background in ConstraintLayout layout like this
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/custom_bg">
output like this

TextView with rounded corners displays correctly in xml file but not app?

I have a Textview on which I am trying to add rounded corners on.
My TextView:
<TextView
android:id="#+id/textview_answer_one"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="#drawable/rounded_corners"
android:elevation="4dp"
android:gravity="center"
android:text=""
app:layout_constraintBottom_toTopOf="#+id/guideline5"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline" />
and my Drawable file:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<shape>
<corners android:radius="15dp" />
<solid android:color="#ffffff"/>
</shape>
</item>
</layer-list>
When I check in the XML Editor (I don't know what it's called) in Android Studio, you can clearly see that the TextView have rounded corners. However, when I run the app on my phone the TextView does not have rounded corners. How come? Any suggestions on what I might be doing wrong?
Try This Shape :
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="20dp"/>
<padding android:left="10dp" android:right="10dp" android:top="10dp" android:bottom="10dp"/>
<solid android:color="#CCCCCC"/>
</shape>
Example Xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TextView
android:background="#drawable/r"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View"
android:textSize="20dp"
android:textColor="#FFFFFF"
android:gravity="center_horizontal"
/>
</LinearLayout>

Is there a way to create this on XML ? 2 colors box

Is there a way to create these two boxes with XML only (beside the icon of course)
The tob box has rounded bordered around it and shadow but inside it divided to two parts which one is pink background and one is white background (without stroke between them)
Create 2 XML drawables
rect_colored.xml
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/colorAccent"/>
<corners android:bottomRightRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
rect_white.xml
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:color="#color/colorPrimaryDark"
android:width="2dp"/>
<corners android:radius="5dp"/>
</shape>
Position the drawables appropriately
This will work differently with different root views. I'm using ConstraintLayout for simplicity
<android.support.constraint.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">
<FrameLayout
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:background="#drawable/rect_white"
android:id="#+id/frameLayout"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp">
<!--Content here-->
</FrameLayout>
<FrameLayout
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginBottom="0dp"
android:layout_marginRight="2dp"
android:layout_marginTop="0dp"
android:background="#drawable/rect_colored"
app:layout_constraintBottom_toBottomOf="#+id/frameLayout"
app:layout_constraintRight_toRightOf="#+id/frameLayout"
app:layout_constraintTop_toTopOf="#+id/frameLayout">
<!--Content here-->
</FrameLayout>
</android.support.constraint.ConstraintLayout>
The output
Just use a LayerList with the shapes you need nested. May take a little tweaking, but it is simple enough. You can even add in your bitmaps for the left icon if you like. Just make your layers, and adjust from the sides and handle your corners.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="50dp">
<shape
android:shape="rectangle" >
<solid android:color="#0000FF" />
<corners android:bottomRightRadius="20dp" android:topRightRadius="20dp" />
</shape>
</item>
<item android:right="50dp">
<shape
android:shape="rectangle" >
<solid android:color="#ffffff" />
<corners android:bottomLeftRadius="20dp" android:topLeftRadius="20dp" />
</shape>
</item>
</layer-list>
Here simply you create a one xml file & drawable file to create shape & border.
MyActivity.Xml
<LinearLayout
android:id="#+id/ll_mobile_img"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<RelativeLayout
android:id="#+id/rr_verification_code"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_weight="0.3">
<EditText
android:id="#+id/et_verification_code"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/ractangle_mobile_white_border"
android:ems="10"
android:hint="Enter Code"
android:drawableLeft="#mipmap/ic_launcher"
android:imeOptions="actionGo"
android:inputType="number|textAutoComplete"
android:maxLength="6"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingTop="16dp"
android:singleLine="true"
android:textColor="#android:color/white"
android:textColorHint="#android:color/white"
android:textSize="18sp" />
</RelativeLayout>
<LinearLayout
android:id="#+id/ll_country_img"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#android:color/white"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="#+id/tv_verification_code_img"
android:layout_width="38dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/globe"
android:textColor="#color/blue_font_color"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
Drawable:- ractangle_mobile_white_border.xml
[![<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item>
<shape>
<gradient android:angle="360" android:endColor="#android:color/transparent" android:startColor="#android:color/transparent" />
<stroke android:width="1dp" android:color="#android:color/white" />
<corners android:bottomRightRadius="1dp" android:topRightRadius="1dp"/>
<padding android:bottom="3dp" android:left="0dp" android:right="3dp" android:top="3dp" />
</shape>
</item>
</layer-list>
</item>
</selector>
Hope this helps you.

Background EditText and ImageView are black?

I'm creating a custom border to LinearLayout, to do it I'm using shape . The problem is when I set #drawable/custom_linear_border all EditText has background black. This problem occur with Android 2.3 because superior this problem does not occur.
How could I solve it ?
custom_linear_border
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="2dip"
android:color="#android:color/darker_gray" />
</shape>
LinearLayout
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/custom_linearlayout_border"
android:layout_marginTop="50dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:src="#drawable/icon_login"
/>
<EditText
android:id="#+id/etEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="13dp"
android:layout_gravity="center_vertical"
android:hint="Email"
android:inputType="textEmailAddress"
android:background="#00000000"
/>
</LinearLayout>
//Use this drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#android:color/transparent" />
<stroke
android:width="2dip"
android:color="#android:color/darker_gray" />
</shape>

Categories

Resources