Custom circle button android

Android Custom Button With Centered Progress Indicator

Recently, I’ve got a simple task in a project I am working on.
I had to create a designed button with a centered progress indicator inside of it.

The button should have 3 states:

In Enabled state, it should show text while in Loading state it should show a Circular Progress Indicator (which can be native or any custom view such as Lottie animation view) and in Disabled state it should show text but grayed out.

Here is the final look according to the design!

What sounds like a simple task turned out to be not so straight forward and easy to do!
As I’ve looked for a way of how I can implement such view, I thought that I will just create a custom view that extends some Android Button class, but if you will look on the Button classes you will (or not) be surprised that those actually extends nothing else but a TextView!!
So how I can extend a View class and then add and manipulate additional Views on it?
And also, the biggest part of this of course is the styling and theming!

After all, I want to make my custom buttons to be reusable, easy to use and with different styles like rounded corners, ripple effect and any color.

When I realised that extending Button is not an option, I thought that extending a ViewGroup like a RelativeLayout or ConstraintLayout will be much better. A ViewGroup has child views inside of it, they can be manipulated and it’s easy to change their visibility and state.
I thought, if a TextView can behave as a button then then so a ViewGroup can, it’s all matter of styling and theming.
Finally I’ve chosen RelativeLayout, I like it, it’s simple and easy to work with.

First Layout First

I’ve started with designing my custom button layout, which looks like this:

As you can see, its just a simple RelativeLayout with a centered TextView and for the progress indicator I’ve chosen to use LottieAnimationView.

One important thing here, you may look on line 13 in the XML, the TextView android: textAppearance attribute which set to be
“?android:attr/textAppearanceButton”, this attribute is the one that sets buttons default text appearance right from the app theme! If we will override it in our app theme, it will effect this TextView here. We will see this further down in the styling and theming part of this article.

Show Me The Code

Now that we have the layout ready, its time to see the code that it is associated with:

Let’s go over the code and see all about it, and that is so simple.

  1. First, notice as I mentioned that the ProgressButton class extends RelativeLayout
  2. On lines 10–14 the init block, first, inflating the layout and then on lines 12–13 initialising the childs views using findViewById
  3. Next, on line 14, calling the loadAttr() method for getting styleables attributes for the button initial state! I’ve defined a few styleables so they can be set in the custom view xml. The attributes are:
  • text — Setting the button text
  • loading — For setting initial loading state from xml
  • enabled — For setting initial enabled/disabled state
  • lottieResId — For setting any lottie animation resource from xml

4. On lines 30–34, applying the initial values to the child views, and also setting the whole view enable state. Later, this enable state will change the appearance thanks to a style and selector background drawable.

Читайте также:  Необходимые сервисы для android

Next, on lines 37–46, setLoading method for changing the button loading states! When it is loading, the TextView is GONE and the progressBar is VISIBLE, when its not loading, the TextView is VISIBLE and the progressBar is GONE.
Notice also that when it is in loading state I am setting it so it will be not clickable.

On lines 48, a method for setting the text in code, for any purposes.

Last but not the leaset, on lines 52–55 overriding the setEnabled method for setting the whole view (The parent RelativeLayout) enable state and also the enable state of the TextView, this is important as you may want to have different text colors when the button is disabled.
Thanks to the “?android:attr/textAppearanceButton” attribute I’ve mentioned earlier, we can have any text styling for different states. I will show all styling in the next sections.

Do It With Style

After complete with layout and code, all we have to do now is connect a set of styling and theming. This part is a little bit tricky, but here is how I’ve managed it.
BTW, I really encourage you to go over this article, which explains how text appearance, theming and styling works in Android

Define a Theme in style.xml

The above xml defines two attributes which will be applied as a theme to the button custom view.
As we mentioned earlier, the textAppearanceButton attribute will effect the text that is in the TextView that in the button layout.

Next, the colorControlHighlight attribute will effect the view ripple effect when clicked. This is also an attribute that comes from the app theme which we override just for this custom view.

The textAppearance styling:

In the above xml we set the text appearance, first we define a base TextAppearance.Body.White block with the font family, text style, size and the base color.
Next, a specific TextAppearance.ProgressButton.Black block for the black button, this block inherit from the base block but overrides the android:textColor attribute, as the text color should be different when it is disabled so I defined a selector for it.

Last but not the least, the selector for the black button text color, with different color for disabled state.

What Is Your Background?

Of course, any view should have a background right?
So is our nice little custom button, the way we do it is by defining some drawable with shape and selector just we do in any other view!

First, lets define the background drawable:

Notice that the ripple color attribute is colorControlHighligh, thats right! We’ve override it in our theme! Cool!
Now, we need to define the shape and the selector:

The above three xml’s completes the view background!!

Now that we have a theme and background ready we should combine them both together! This will be done in another style block:

Notice that in the above style xml, we defined an “android:theme” attribute and gave it the theme that has been defined earlier. This will apply that theme to the view.
Next, we’ve set the “android:background” attribute with the drawable which includes the shape and the selector!

For any new color or shape, we will have to create a bunch of styling xml as described above! But when using it we should take only the last style block which combine both the theme and the background. You may look in the example in the next section!

Thats it!! Everything is ready for use!

I’ve Built It! I Want To Use It!!

All right! This is it! It’s time to use our nice little button in a Fragment or Activity. First, let’s put it in a layout!

Читайте также:  Самый точный навигатор для андроида

Using the new ProgressButton is simple as just putting it in a layout and just set the style for the preferred color!

Now in our Fragment, we can use binding to access it and use it!

Summary

As you may see, such a simple looking task requires some effort to implement in the traditionally Android and View native framework. I wonder if maybe Jetpack Compose will bring new possibilities for making Android UI manipulation much easy with the ideas of Widgets and composition!

Источник

Custom circle button android

Like Button is a library that allows you to create a button with animation effects similar to Twitter’s heart when you like something.

Table of Contents

Add this in your root build.gradle file (not your module build.gradle file):

Add this to your module’s build.gradle file:

Like Button XML

To use this like button in your layout simply copy and paste the xml below. This provides the default heart button.

There are several other attributes that can be used to configure the button’s behaviour and appearance. They are shown below and will be explained in the sections that follow long with their java counterparts.

To set the inital liked state of the button you simply use the setLiked functionality via XML or Java. This will show the button in the liked state with the drawable that you selected.

You can also set if the button is to be enabled or disabled. Once disabled, the animation, listener or any other functionality of the button won’t be triggered.

Like Event Listener

To listen to events from your like button, simply implement the listener that’s triggered once the button is tapped.

The libary is bundled with three icons that you can use. A heart,thumb and a star.

To set the respective icon via xml simply use the word in the icon type attribute.

If you would like to set the icon via Java then simply call the set icon method on the button.

To set the icon size via xml simply use this attribute

If you are doing it programmatically you can set it with either the method for pixels or dp.

Note, it’s very important that the size of the button is set even if you are planning to use custom drawables as described below as the library uses this value to determine the width and height of the effects that occur when the button is tapped.

In order to use custom icons instead of the ones bundled with the library you simply set the drawables that represent the liked and unliked state of the button.

Circle Color Config

If you watch the animation closely you will notice that there’s a circle that begins from the center of the icon and and then it animates away from the center before the exploding dots animate. These colours can be changed to suit the theme of your icon.

Dots Color Config

The dots that represent the outer animation can be coloured also.

To change the size of the dots that also contributes to the size of the overall like button view you can use the animation scale factor attribute via XML or it’s Java equivalent

This library was made by possible based on code and design inspiration from these sources:

Please fork repository and contribute using pull requests.

Any contributions, large or small, major features, bug fixes, additional language translations, unit/integration tests are welcomed and appreciated but will be thoroughly reviewed and discussed.

Читайте также:  Mirrorlink skoda как подключить андроид

Источник

How to create custom design for Button background in Kotlin Android

Button Background in Kotlin Android

There is a default style for a Button in Android, but it may not fit your application theme. In this Android Tutorial, we shall present you on ways to create custom design for Button background.

In the following screenshot will have custom Button designs that we shall create in this tutorial.

Create Custom Button Background in Kotlin Android

Following are the steps in creating a custom background drawable for a Button in Android.

Step 1: Create a drawable xml file in /app/res/drawable/ folder. Say button_background.xml

Step 2: Mention the shape of drawable, and its properties in the xml. An example would be as shown in the following.

button_background.xml

Step 3: Create a button in the layout xml file and provide the drawable, button_background.xml as background. Example code snippet is shown in the following.

Step 4: You may change some properties of the Button like padding, margin, textColor etc., for a proper look and to suit your application theme.

Example Kotlin Android Application details for customizing Button Backgrounds

Following are the layout file, Activity file and drawable resources that could generate the buttons shown at the starting of the tutorial.

res/drawable/btn_solid_color.xml

res/drawable/btn_gradient.xml

res/drawable/btn_center_gradient.xml

res/drawable/btn_edge_color.xml

res/drawable/btn_round_edge.xml

res/drawable/btn_oval.xml

res/drawable/btn_oval_gradient.xml

Till now are the drawable resources that we are going to use as background for buttons. You may choose only one or some of these in your project, and discard the rest. Accordingly make changes in the layout file.

You may also change the colors, or other values like stroke widths, border radius, etc., based on the requirement.

activity_custom_button.xml

CustomButtonActivity.kt

Run this application, and you would get the following output on your Android Screen.

Conclusion

In this Kotlin Android Tutorial, we have learnt how to set custom backgrounds to Button widget in Android using Kotlin.

Источник

Custom circle button android

CIRCLE MENU [JAVA]

A simple, elegant UI menu with a circular layout and material design animations

We specialize in the designing and coding of custom UI for Mobile Apps and Websites.

Stay tuned for the latest updates:

  • Android 4.1 Jelly Bean (API lvl 16) or greater
  • Your favorite IDE

​ Just download the package from here and add it to your project classpath, or just use the maven repo:

Place the CircleMenuView in your layout and set the icons and colors of the buttons, as shown below.

Example of arrays colors and icons in res\values\buttons.xml :

Or use the constructor

to add CircleMenuView and configure the buttons programmatically (in the code).

Next, connect the event handler CircleMenuView.EventListener as shown below, and override the methods you need.

You can use open(boolean animate) and close(boolean animate) methods, to open and close menu programmatically

Here are the attributes you can specify through XML or related setters:

  • button_icons — Array of buttons icons.
  • button_colors — Array of buttons colors.
  • icon_menu — Menu default icon.
  • icon_close — Menu closed icon.
  • icon_color — Menu icon color.
  • duration_ring — Ring effect duration.
  • duration_open — Menu opening animation duration.
  • duration_close — Menu closing animation duration.
  • distance — Distance between center button and buttons

🗂 Check this library on other language:

Circle Menu Android is released under the MIT license. See LICENSE for details.

If you use the open-source library in your project, please make sure to credit and backlink to www.ramotion.com

📱 Get the Showroom App for Android to give it a try

Try this UI component and more like this in our Android app. Contact us if interested.

About

⭕️ CircleMenu is a simple, elegant UI menu with a circular layout and material design animations. Android UI library made by @Ramotion

Источник

Оцените статью