I have a shape in my drawables:
circle_item.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#ffffff" />
<size
android:width="120dp"
android:height="120dp" />
</shape>
Which is used like so:
icon.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/backgroundVw"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_gravity="center"
android:scaleType="fitXY"
android:background="#drawable/circle_item"/>
<ImageView
android:id="#+id/icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_margin="8dp"
android:src="#drawable/ic_home" />
</FrameLayout>
The image is changed via code like so:
binding.icon.setImageResource(item!!.Image)
Changing the image works perfectly, but I'm struggling to change the actual color property of circle_item through the ImageView
This is what I've tried so far:
binding.backgroundVw.background.colorFilter = BlendModeColorFilter(item!!.backgroundColor, BlendMode.SRC_IN)
But this just gives it a weird color which isn't defined anywhere in the project.
Try it
imageView.drawable.colorFilter = PorterDuffColorFilter(ContextCompat.getColor(context, R.color.colorBlack), PorterDuff.Mode.SRC_IN)
Related
<FrameLayout
android:id="#+id/flTodayTraining"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/fl_today_training_bg_margin_bottom"
android:layout_marginTop="#dimen/fl_today_training_bg_margin_top"
android:layout_marginHorizontal="#dimen/fl_today_training_bg_margin_horizontal"
android:background="#drawable/today_training_bg"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/tvDaysTrained"
>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="#dimen/iv_today_training_detail_height"
android:src="#drawable/male_upper_body"
android:scaleType="fitXY"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/upper_body"
android:textSize="#dimen/tv_today_training_detail_text_size"
android:textColor="#color/black"
android:paddingVertical="#dimen/tv_today_training_detail_padding_vertical"
android:gravity="center"
/>
</androidx.appcompat.widget.LinearLayoutCompat>
</FrameLayout>
That's the cornered background of the frame layout
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/white" />
<corners
android:radius="15dp"
/>
</shape>
As you can see in the image, it's rounded in the bottom, but not in the top, how do I make it rounded in all corners?
Please give me an answer that works on all version, not only api 31 +
Very simple. Just use cardView
see below XML code
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="160dp"
android:scaleType="centerCrop"
app:srcCompat="#drawable/fruit1" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg"
android:gravity="center"
android:paddingBottom="5dp"
android:text="Apple"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</androidx.cardview.widget.CardView>
and the result is below
pros-
you can easily give elevation to card view to show shadow
easily give it radius.
looks more attractive.
much more.
I hope it'll help you.
Instead of setting the background to the FrameLayout set it to the immediate ViewGroup. Also remove fitXy in the imageview
<FrameLayout
android:id="#+id/flTodayTraining"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/fl_today_training_bg_margin_bottom"
android:layout_marginTop="#dimen/fl_today_training_bg_margin_top"
android:layout_marginHorizontal="#dimen/fl_today_training_bg_margin_horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/tvDaysTrained"
>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/today_training_bg"<--set it to immediate viewgroup instead
android:orientation="vertical"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="#dimen/iv_today_training_detail_height"
android:foreground="#drawable/some_foreground" <-- insert the new background drawable you created
android:scaleType="centerCrop"
android:src="#drawable/male_upper_body" <-- removed fitXy
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/upper_body"
android:textSize="#dimen/tv_today_training_detail_text_size"
android:textColor="#color/black"
android:paddingVertical="#dimen/tv_today_training_detail_padding_vertical"
android:gravity="center"
/>
</androidx.appcompat.widget.LinearLayoutCompat>
</FrameLayout>
Create this background drawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke
android:width="8dp"
android:color="#color/white" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="#dimen/padding_margin_8" />
<stroke
android:width="8dp"
android:color="#color/white" />
</shape>
</item>
</layer-list>
I was trying to set a gradient and rounded shape in the button background. But, the gradient color is not showing in the button while shape is coming properly. Can you please tell me where I'm going wrong?
This is my Button xml code,
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="76dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent">
<ImageButton
android:id="#+id/verifyButton"
android:layout_width="300dp"
android:layout_height="70dp"
android:layout_marginBottom="20dp"
android:layout_marginEnd="50dp"
android:layout_marginStart="50dp"
android:layout_marginTop="20dp"
android:fontFamily="#font/aclonica"
android:background="#drawable/button"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/buttonText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/aclonica"
android:text="Verify"
android:textColor="#color/white"
android:textSize="25sp"
android:textStyle="bold"
android:layout_gravity="center"/>
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:visibility="gone" />
</FrameLayout>
And this is my drawable resource file xml code,
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#color/light_blue" android:endColor="#color/dark_blue"/>
<corners android:radius="50dp"/>
</shape>
Please help me with this. Thank you
I got the solution. By adding this code in the button xml file, it worked just fine.
app:backgroundTint="#null"
Add this to your drawable folder and add it ass background of the button.
round_gradient_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#eeffffff" />
<corners android:bottomRightRadius="16dp"
android:bottomLeftRadius="16dp"
android:topRightRadius="16dp"
android:topLeftRadius="16dp"/>
<gradient
android:startColor="#ffff00"
android:centerColor="#00ffff"
android:endColor="#ff0000"
android:angle="45" />
</shape>
Then go to your res/values/themes.xml
change
<item name="colorPrimary">#color/purple_500</item>
to
<item name="colorPrimary">#null</item>
I am using RecyclerView + GridLayout (3 columns). Now to make each square of the grid more "responsive", I want that each square will show some sort of divider, and that there will be a ripple effect within each square that the user clicks.
Edit : I added android:foreground="?attr/selectableItemBackground", but nothing happens.This is the single item xml code right now:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dip" >
<ImageView
android:foreground="?attr/selectableItemBackground"
android:id="#+id/icon"
android:layout_width="128dp"
android:layout_height="118dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginTop="12dp"
android:contentDescription="TODO"
android:src="#drawable/ic_launcher_background" />
</RelativeLayout>
This is how it currently look:
And this is how I want it to look:
This is how it works currently after fix, after adding:
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
you can create a ripple drawable like this if you want it to be customize:
ripple.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorPrimaryDark">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimaryLight" />
<!--<corners android:radius="#dimen/button_radius_large" />-->
</shape>
</item>
<item android:id="#android:id/background">
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="#color/background"
android:startColor="#color/background"
android:type="linear" />
<!--<corners android:radius="10dp" />-->
</shape>
</item>
and use it in your layout like this:
android:clickable="true"
android:background="#drawable/ripple"
OR
you can simply do it like this:
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
You can add divider by adding a GridItemDecoration to your recyclerview. i suggest you to use this.
a simple way for adding ripple when you click on items is adding a foreground with selectableItemBackground value to your item's view:
android:foreground="?attr/selectableItemBackground"
try this way for Ripple Effect in xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dip"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground">
<ImageView
android:id="#+id/icon"
android:layout_width="128dp"
android:layout_height="118dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginTop="12dp"
android:contentDescription="TODO"
android:src="#drawable/ic_launcher_background" />
</RelativeLayout>
and follow this link for Dividers in between items
How to add dividers and spaces between items in RecyclerView?
I trying to make a circular image button like WhatsApp.
I tried the below code, but I am only able to create a circular imageButton. The image did not show in the imageButton.
<ImageButton
android:id="#+id/imageButton"
android:layout_width="70dp"
android:paddingBottom="140dp"
android:src = "#drawable/camera"
android:scaleType="fitXY"
android:layout_height="70dp"
android:background="#drawable/round"
android:layout_gravity="center_horizontal" />
round.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="100dp" />
<stroke
android:width="5dp"
android:color="#090" />
</shape>
You added android:paddingBottom=140dp. Issue with that. Check this
<ImageButton
android:id="#+id/imageButton"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:background="#drawable/round"
android:tint="#android:color/black"
android:scaleType="fitXY"
android:padding="5dp"
android:src="#drawable/camera" />
You just missed solid tag in your custom shape file.
<solid android:color="#090" />
Edit:
Never try to give margin or padding which has a larger value. Just like you are using android:paddingBottom="140dp". This is not a recommended way.
Try this
<ImageButton
android:id="#+id/imageButton"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:background="#drawable/round"
android:scaleType="centerInside"
android:src="#mipmap/ic_launcher" />
Use a shape and fill with your desire color.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="100dp" />
<solid android:color="#090" /> // solid for filling empty space
</shape>
Use code in ImageButton
<ImageButton
android:id="#+id/imageButton"
android:layout_width="70dp"
android:paddingBottom="140dp"
android:src = "#drawable/camera"
android:scaleType="fitXY"
android:layout_height="70dp"
android:background="#drawable/round"
android:layout_gravity="center_horizontal" />
Add Solid Tag
<solid android:color="#090" />
and replace android:paddingBottom="140dp" with android:padding="whateversuitsyou"
For drawing a solid circle, try the code below:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#090"/>
<size
android:width="100dp"
android:height="100dp"/>
</shape>
Shape type oval is used for drawing circles.
WhatsApp used a floating action button, and you can implement it in two steps.
Add a dependency to your build.gradle file:
dependencies {
compile 'com.github.clans:fab:1.6.4'
}
Add com.github.clans.fab.FloatingActionButton to your layout XML file.
<com.github.clans.fab.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:src="#drawable/ic_message"
fab:fab_colorNormal="#color/app_primary"
fab:fab_colorPressed="#color/app_primary_pressed"
fab:fab_colorRipple="#color/app_ripple"/>
Download image of camera by clicking here.
And you are done.
Set width and height as below.
android:layout_width="match_parent"
android:layout_height="wrap_content"
This works for me. Try this...
I woudl like to add an image with rounded top corners to my imageView but it doesn't appears rounded.
my shape_round_top.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#android:color/darker_gray"/>
<corners android:topRightRadius="10dp"
android:bottomRightRadius="0.1dp"
android:topLeftRadius="10dp"
android:bottomLeftRadius="0.1dp"/>
</shape>
my layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="250dp"
android:background="#drawable/grid_bg">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="200dp">
<ImageView
android:layout_width="200dp"
android:layout_height="100dp"
android:scaleType="fitXY"
android:id="#+id/image1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="5dp"
android:background="#drawable/shape_round_top"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp" />
</RelativeLayout>
</LinearLayout>
I set my image programmatically
int id;
id = context.getResources().getIdentifier("imagename", "drawable", context.getPackageName());
image.setImageResource(id);
Are you typing the correct drawable name android:background="#drawable/shape_round_top"or android:background="#drawable/shape"?
Except this, everything looks fine