I want to add a circular progress icon like
Inside a chip view in android. Some thing like this as the end product
Excuse the background of the progress bar indicator. I am not able to find any thing regarding this. Any suggestion?
You can achieve this behaviour by setting programmatically the ChipIcon using an androidx.swiperefreshlayout.widget.CircularProgressDrawable if you want to align the loading indicator in the left side of your Chip or you can use the CloseIcon if you want to align it on the right side of the Chip.
Align CircularProgressDrawable on the left side of chip:
1.In your chip xml layout define the chipIconSize and padding attributes based on your needs like below:
<com.google.android.material.chip.Chip
android:id="#+id/chip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chip"
app:chipIconSize="25dp"
app:iconStartPadding="5dp"
app:iconEndPadding="5dp"
app:chipBackgroundColor="#android:color/holo_blue_bright" />
2.Create programmatically your CircularProgressDrawable like below:
CircularProgressDrawable cpDrawable = new CircularProgressDrawable(this);
cpDrawable.setStyle(CircularProgressDrawable.DEFAULT);
cpDrawable.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
cpDrawable.start();
3.And set the above CircularProgressDrawable to your ChipIcon like below:
chip = findViewById(R.id.chip);
chip.setChipIcon(cpDrawable);
chip.setChipIconVisible(true);
Align CircularProgressDrawable on the right side of chip:
1.In your chip xml layout define the closeIconSize and closeIcon paddings attributes based on your needs like below:
<com.google.android.material.chip.Chip
android:id="#+id/chip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chip"
app:closeIconSize="25dp"
app:closeIconStartPadding="5dp"
app:closeIconEndPadding="5dp"
app:chipBackgroundColor="#android:color/holo_blue_bright" />
2.Create programmatically your CircularProgressDrawable like above and set it on Chip CloseIcon like below:
chip = findViewById(R.id.chip);
chip.setCloseIcon(cpDrawable);
chip.setCloseIconVisible(true);
Results will be like below:
I created a layout using relative layout, you can check this i hope you get this
<?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="80dp"
android:background="#drawable/dummy_bg"
android:splitMotionEvents="true">
<TextView
android:id="#+id/tvlabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:text="Electronics"
android:textColor="#color/black"
android:textSize="25sp" />
<ProgressBar
android:id="#+id/progrressciruclar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_toRightOf="#+id/tvlabel" />
</RelativeLayout>
also create this dummy_bg in your drawables folder and paste the following code
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<stroke android:width="3dp"
android:color="#FFFFFF" />
<corners android:radius="50dp"/>
<padding android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
</shape>
Related
I am trying to input an image in a regular button, however it does not show up. My code looks like this
<Button
android:text="#string/buttonexit"
android:layout_width="300dp"
android:layout_height="50dp"
android:id="#+id/buttonexit"
android:layout_marginLeft="50dp"
android:background="#drawable/wood"/>
I also hoped and included src instead of background, however it still did not work.
<Button
android:text="#string/buttonexit"
android:layout_width="300dp"
android:layout_height="50dp"
android:id="#+id/buttonexit"
android:layout_marginLeft="50dp"
android:src="#drawable/wood"/>
Lastly I also tried to make an image button, however once I added an image I cannot adjust the corners anymore.
You can use AppCompatButton instead of Button.
For a Button, the primary color defined in the app theme overrides any custom background that you want to apply to a Button, but it is not the case for the AppCompatButton.
For example:
<androidx.appcompat.widget.AppCompatButton
android:text="test"
android:layout_width="300dp"
android:layout_height="50dp"
android:id="#+id/buttonexit"
android:layout_marginLeft="50dp"
android:background="#drawable/bg"/>
Besides, you can also use ImageButton to achieve this function.
You can add an image by property android:src and adjust the corners with property android:background by creating a shape xml file.
You can refer to the following code:
1.create file button_shape.xml in folder drawable:
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android = "http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--background color-->
<solid android:color="#20a4e5"/>
<!--radius-->
<corners android:radius="10dip"/>
<!--Sets the width and color of the border line-->
<stroke android:width="2dp" android:color="#fffff0" />
</shape>
2.set it for android:background of ImageButton
<ImageButton
android:text="test"
android:layout_width="300dp"
android:layout_height="80dp"
android:layout_marginLeft="50dp"
android:background="#drawable/button_shape"
android:src="#drawable/grass"/>
How can I make an EditText with a drop-down in Android?
For reference i have added an image. I am trying to combine an edittext with spinner but I am not able to achieve the same.
Can anyone add suggestion or xml mockup?
Well, You can use a LinearLayout with a horizontal orientation and create a custom drawable for the rounded border.
Implementation
First, you need to draw the rounded border by creating a new Drawable resource file.
In your res/drawables folder create rounded_border.xml
rounded_border.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="4dp" />
<stroke android:width="2dp" android:color="#2E2E2E" />
</shape>
</item>
</selector>
Then, you can add the LinearLayout to your layout as follows.
your_layout.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#drawable/rounded_border"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#null"
android:paddingHorizontal="16dp"
android:text="(504) 596-3245"
android:layout_weight="2"/>
<View
android:layout_width="2dp"
android:background="#2E2E2E"
android:layout_height="match_parent"/>
<Spinner
android:layout_width="0dp"
android:layout_height="match_parent"
tools:listitem="#layout/spinner_item_text"
android:layout_weight="1"/>
</LinearLayout>
Remarks
You can Customize the border width, color, and radius from the rounded_border.xml.
Final Results
could refer this lib demo : https://github.com/z2wenfa/SpinnerEditText
could use the lib directly
dependencies {
compile 'com.github.z2wenfa:SpinnerEditText:1.0.1'
}
or could refer: https://github.com/z2wenfa/SpinnerEditText/blob/master/spinneredittext-lib/src/main/java/com/z2wenfa/spinneredittext/SpinnerEditText.java
What I want to do:
What I have:
I want to make a circular icon with initials in it. In my scenario initials can have one or two letters. Like you can see my solution work fine for two letters but not to one. How can I solve this for both cases?
There is my code from the XML file:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="20dp"
android:textAllCaps="true"
android:textColor="#color/white"
android:background="#drawable/shape_rounded_blue_button"
android:layout_gravity="center_horizontal"
android:gravity="center"
local:MvxBind="Text Initials"/>
And there is shape_rounded_blue_button:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/blue"/>
<corners android:radius="30dp" />
</shape>
As I wanted to implement the same thing, I made some research on GitHub and found this library:
https://github.com/amulyakhare/TextDrawable
It does exactly what you want and has some neat features as auto-coloring. You can use it with an ImageView and create a drawable, e.g. like this:
val drawable = TextDrawable.builder()
.beginConfig()
.width(bubbleSize)
.height(bubbleSize)
.endConfig()
.buildRound("FH", MATERIAL.getColor("FH"))
theImageView.setImageDrawable(drawable)
And that looks like this:
Your text will automatically be centered within the bubble, so it's no problem to have one or two characters.
You need to create a drawable resource file under res>drawable and save it as bg_circle.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#424242"/>
<size android:width="120dp" android:height="120dp"/>
</shape>
Now make icon_container in your layout.xml file as:
<RelativeLayout
android:id="#+id/icon_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="#dimen/icon_width_height"
android:layout_height="#dimen/icon_width_height"
android:src="#drawable/bg_circle" />
<TextView
android:id="#+id/icon_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#android:color/white"
android:textSize="#dimen/icon_text" />
</RelativeLayout>
For complete code implementation visit Implementing Gmail like icon with text and random colors
You need to create a circle shape.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#color/base30"/>
For better effects(shadow and appearance) you can use TextDrawable.
I need to display the contact name initial letter inside circle like in lollipop contacts. I didn't find any solution.
The circle should different colour for each user.
You can follow this. If you wanna save time you can use this library.
Edit
The link may be invalid any point of time. So I want to add the details below this.
repositories{
maven {
url 'http://dl.bintray.com/amulyakhare/maven'
}
}
dependencies {
compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
}
You can add which ever methodology you are following.
Basically you have to add one ImageView with same height and width and add this TextDrawable as a background of your view.
TextDrawable drawable = TextDrawable.builder()
.buildRect("A", Color.RED);
ImageView image = (ImageView) findViewById(R.id.image_view);
image.setImageDrawable(drawable);
The library has support for circle,square and rectangle drawables.
I didn't see any library for this but my solution is draw a circle in run time in a predefined layout in your xml use this :
How to draw circle by canvas in Android?
Then in your list adapter choose first letter of each name string using substring(0,1)
,and using setText() you can set list item textView to first letter.
If you need source code let me know to put it for you.
UPDATE:
I recently used very easy approach for this in one of my apps,
you should make a layout like this in your layout folder:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rlWeekDay"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="right"
android:layout_margin="3dp"
android:background="#drawable/circle_shape">
<TextView
android:id="#+id/tvWeekDayFirstLetter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text=" E "
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:textSize="20sp" />
</RelativeLayout>
And your shape in drawable folder as circle_shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval"
android:dither="true">
<corners
android:bottomRightRadius="100dp"
android:bottomLeftRadius="100dp"
android:topLeftRadius="100dp"
android:topRightRadius="100dp"/>
<solid android:color="#ccc" />
</shape>
</item>
<item android:bottom="3dp">
<shape android:shape="oval"
android:dither="true">
<solid android:color="#color/white"/> <!-- this one is ths color of the Rounded Button -->
<corners
android:bottomRightRadius="100dp"
android:bottomLeftRadius="100dp"
android:topLeftRadius="100dp"
android:topRightRadius="100dp"/>
</shape>
</item>
</layer-list>
Then in your listView or recyclerview use this as the item to inflate.like this
Simple and small code to do this -
<com.google.android.material.button.MaterialButton
android:id="#+id/"
android:layout_width="40dp"
android:layout_height="50dp"
android:text="A"
android:clickable="false"
app:backgroundTint="#color/Blue200"
android:padding="0dp"
app:cornerRadius="50dp"
/>
By using a Material Card View and a Textview, it would be easy to create such a view.
<com.google.android.material.card.MaterialCardView
android:layout_width="50dp"
android:layout_height="50dp"
app:cardBackgroundColor="#color/red"
app:cardCornerRadius="50dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/initialTv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="AB" />
</com.google.android.material.card.MaterialCardView>
When creating a PopupWindow it shows a border like in the following image:
How do I remove it?
Try to add this line :
mPopup.setBackgroundDrawable(new BitmapDrawable());
You can create one custom style and put that border the same color on background, try something like:
New | Android XML File.
myborder.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dip"
android:color="#android:color/darker_gray" />
<solid
android:color="#android:color/background_dark" />
<padding
android:left="7dip"
android:top="7dip"
android:right="7dip"
android:bottom="7dip" />
<corners
android:radius="6dip" />
</shape>
Using the drawable Android XML file in a layout
Layout.xml
<LinearLayout
android:orientation="vertical"
android:background="#drawable/myborder"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Text"
/>
<!-- ..................... -->
You need to create a custom layout and set border of parent layout,
i'll giving you a Logical idea for doing this.
Your layout must be like below.
dialog_layout.xml
<RelativeLayout>
<LinerLayout> <!-- You can **Set/Remove** all background properties of this LinearLayout-->
<!-- Here are all child element like EditText/ Or TedxView-->
</LinerLayout>
</RelativeLayout>
Here are link for border :
Border