Android Image Rounded Top - android

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

Related

Image View doesn't match parent's rounded cornered background

<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>

Changing image background colour

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)

I would like to create a textView surrounded rounded circle the diagram as shown below

I would like to create a textView surrounded rounded circle the diagram as shown below:
Image Link :- https://i.stack.imgur.com/E0cMB.png
Can anyone of you let me know how to do this?
Check this how to implement the above image requirement.
Your Layout data should be like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#004473">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="5dp"
android:background="#drawable/linear_layout_corner_shape"
android:orientation="horizontal">
<TextView
android:layout_width="60dp"
android:layout_height="60dp"
android:gravity="center"
android:text="01"
android:background="#drawable/textview_circle_shape"
android:textSize="20sp"
android:textColor="#000000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:text="Lorum Ipusum"
android:textSize="20sp"
android:textColor="#FFFFFF"/>
</LinearLayout>
</LinearLayout>
Drawable files data:
linear_layout_corner_shape.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#004473"></solid>
<stroke
android:width="2dp"
android:color="#FFFFFF"></stroke>
<corners android:radius="30dp"></corners>
textview_circle_shape.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#FFFFFF"></solid>

Add image in imageButton

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...

how to restrict image to show insider border

my screen not show image fit on border is goes out of border seeimage http://imgur.com/VMwFMzf is show tomoattow and cheexz image out of frame help me how do i fixed it?? i want to show first two images inside border like third image bread which correctly show inside border
holder.txtText = (TextView) convertView.findViewById(R.id.title2);
holder.imgThumb = (ImageView) convertView.findViewById(R.id.image2);
holder.txtText.setText(fifthscreen.Category_name.get(position));
imageLoader.DisplayImage(fifthscreen.Category_image.get(position),
activity, holder.imgThumb);
<com.schoollunchapp.HorizontalListView
android:id="#+id/listview2"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_below="#+id/test_button_text5"
android:background="#ffffff"/>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/image2"
android:layout_width="90dp"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#drawable/imagebgborder"
android:src="#drawable/icon"
/>
<TextView
android:id="#+id/title2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:padding="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:gravity="center_horizontal"
/>
</LinearLayout>
<!--imageborder.xml--->
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<stroke android:width="1dp" android:color="#000000" />
<padding android:left="2dp" android:top="2dp" android:right="2dp"
android:bottom="2dp"
/>
<corners android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp" android:topRightRadius="10dp"/>
</shape>
Use this code
<ImageView
android:id="#+id/imageview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
Then follow below points:
Set your image to Imageview as 'src' and not as 'background'.
Crop your image width and height related to your Frame height and width.

Categories

Resources