Add number in circle within text in textview - java

The number 4 circle
As you see in the image there is number '4' in a circle within a text and it has a background color. It works as a reference when you click it opens a link in the browser. Is that something can be done in textview using spannable or any other way?

You can achieve this using Drawable.
Simply create background.xml in Drawable.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#color/colorAccent"/>
</shape>
And apply background in TextView,
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:gravity="center"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="14dp"
android:paddingBottom="14dp"
android:text="04"
It will generate the result like this,

Related

Button drawableStart draws outside of background border/corners

I want to make an a bit circular button with a drawableStart (without padding).
I added this to the button background:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#CC232323"/>
<corners android:radius="50sp"/>
</shape>
The button is:
<Button
android:id="#+id/btn_play"
android:layout_width="match_parent"
android:layout_height="50sp"
android:layout_marginStart="25sp"
android:layout_marginEnd="25sp"
android:layout_weight="1"
android:background="#drawable/btn_bg"
android:drawableStart="#drawable/icon_layer_list"
android:text="#string/sample_text"
android:textAlignment="textStart"
android:textAllCaps="false"
android:textColor="#color/colorWhite" />
The layer-list is:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/background_design"
android:height="50sp"
android:width="50sp"
/>
</layer-list>
the corner does work on the button it self but the drawableStart is not affected by them, so I get a button that on one side is with circular border and on the other side a square border of the drawableStart.
View (or Button in your case) is always rectangle. When you apply background with rounded corners, it is only background image rounded, not the View itself. If you need to have rounded corners in drawableStart, you need to have rounded corners in drawable you set for drawableStart.
I found a simple and easy solution, the idea of it is to wrap the button with a CardView and set it's cardCornersRadius to the wanted radius.
like so:
<androidx.cardview.widget.CardView
android:layout_width="150dp"
android:layout_height="150dp"
app:cardCornerRadius="250dp"
android:layout_gravity="center">
<Button
android:id="#+id/btn_play"
android:layout_width="match_parent"
android:layout_height="50sp"
android:layout_marginStart="25sp"
android:layout_marginEnd="25sp"
android:layout_weight="1"
android:background="#drawable/btn_bg"
android:drawableStart="#drawable/icon_layer_list"
android:text="#string/sample_text"
android:textAlignment="textStart"
android:textAllCaps="false"
android:textColor="#color/colorWhite"/>
</androidx.cardview.widget.CardView>
it is without a doubt the simplest way I've found in order to get the view act withthin the borderd, the CardView will not let the button or anything within it to draw outside of its borders / corners.
For more information about CardView (it's properties, dependency, etc...), go to This link.

Top rounded corner on an ImageView

How to add top rounded corners to an ImageView.
I've tried with the following method and it doesn't work.
imgView.setClipToOutline(true);
its better to create drawable file with radius just on top and set as a background.
You can create custom backgroung
<?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="0dp"
android:topLeftRadius="10dp"
android:bottomLeftRadius="0dp"/>
</shape>
set in imageview like this
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:background="#drawable/shape"/>
you can create a drawable for this or use this library for better result
https://github.com/vinc3m1/RoundedImageView

Bottom transparent shadow effect for ImageView

I want to add shadow effect to my ImageView, like a transparent from bottom side. Here two images with and without shadow effect. Has any possible ways to make like this? Or is it better to take shadow svg file from the internet and put on the original imageView?
You can put your image in the same space with a gradient image using a RelativeLayot like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/your_image"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/grad"/>
</RelativeLayout>
The "grad" image is this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="270"
android:endColor="#181818"
android:startColor="#00000000 " />
<corners android:radius="5dp" />
</shape>

How to draw a circle around a buttons text in Android (API 21+)

I have a row of buttons each with numbers on them. I show which one is selected by changing the background, but one of these number would be the "current" number (kind of like a current date) and I want to indicate that it is the current one even when it is selected (since it will have the same background as all other selected ones) by adding a circle around the text
Kind of like this (with 23 being both selected and the current one):
I tried doing this by adding a radius to the button and adding padding but was having a bit of trouble, but even if it worked it feels like there is probably a better way.
How do I make the circle around the text in the button?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/num_txt"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="0dp"
android:background="#drawable/bg_red"
android:gravity="center"
android:text="My name is NON"
android:textColor="#FFFFFF"
android:textSize="10dp" />
</RelativeLayout>
</RelativeLayout>
bg_red.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="10dip"/>
<stroke android:color="#FF0000" android:width="5dip"/>
<solid android:color="#FF0000"/>
</shape>
or try This or This

Android Rounded Corner Layout with Filled Tiled Image

I'm hoping there is an xml solution to this... I am trying to create a layout that has rounded corners and then filling that layout with an image (tiled, repeat). So basically I have a LinearLayout that has a drawble layered-list assigned to the background. Here is the test layout (xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/rate_buttons"
android:layout_width="100dp"
android:layout_height="50dp"
android:orientation="horizontal"
android:padding="0dp"
android:background="#drawable/test_rounded">
</LinearLayout>
</LinearLayout>
here is the test_rounded drawable xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item ><bitmap android:src="#drawable/my_tiled_image" android:tileMode="repeat"/></item>
<item >
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="3dip" android:color="#B1BCBE" />
<corners android:radius="20dip"/>
</shape>
</item>
</layer-list>
The image is overflowing at the corners of the shape/layout border. I get the following
As you can see the corners are overflowed. I heard about modifying the image part (blue pattern) in code but I have issues with that since I do modify it and animate it along the way. SO, if its at all possible to just fill in the layout with that image, please let me know. Thank you all for your help.
You can do this only programatically. But if you want to do this using xml, you have to add the background image in a layout above the rounded corner layout with the help of Relative layout and set margin. This will not give a perfect solution but will solve your problem.
The first is the XML way
Create an xml file in your respective drawable folder for the round border.
I have namedd it as popupborder.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="2dp" />
<corners android:radius="15dip" />
</shape>
The main layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="#drawable/popupborder" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp" >
<LinearLayout
android:id="#+id/property_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inner view with Background and margin"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
</LinearLayout>
</RelativeLayout>
i recommend you to use an image loader library such as universal image loader, picasso etc. Here is an example code for it:
String url = "file://" + your_file_path
com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(url, ivPicture, options);
You have to add
.displayer(new RoundedBitmapDisplayer(px))
to your Universal Image Loader display options.
Also there is another library (i think this is what you are looking for) https://github.com/vinc3m1/RoundedImageView
You can check this. But in second way you can get "Out of Memory Exception" cause of loading big sized image.
I hope this'll help you.

Categories

Resources