Is it possible to have a round button without changing the android:background attribute?

Asked

Viewed 1,682 times

0

I have in the directory res>drawable>fundo.xml the following code:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#ff0000"></solid>
</shape>

Where do I define in android:background="@drawable/fundo" of xml button res>layout>minha_lista.xml:

<Button
  android:id="@+id/botao_alunos"
  android:text="+"
  android:textSize="15sp"
  android:textColor="#fff"
  android:layout_width="56dp"
  android:layout_height="56dp"
  android:background="@drawable/fundo"
/>

To leave it with rounded shape to follow:

inserir a descrição da imagem aqui

However, what makes me a little confused is changing the attribute background and also change its format, I believe that what confuses me is trying to associate this background attribute to stylization front-end(thing I shouldn’t do), but I had a question:

Is there any way to avoid using the.xml background as the button background and just using the Button attributes could be performed the same change as your Shape?

  • 1

    would not be better to use a Floating Action Button?

  • I can round it with the Floating Actionscript Button?

2 answers

2


Is there any way to avoid using the.xml background as the button background and just using the Button attributes could be performed the same change as your Shape?

Not.

A button, like any View, is a rectangle. It will always occupy a rectangular area whose dimensions are defined in android:layout_width and android:layout_height.

What the View "shows" in this rectangle, in the case of a Button, is the Drawable indicated by android:background. The format is not changed, it will always be a rectangle.

It is possible to use/create another type that derives from Button or Imagebutton (as is the case with Floatingactionbutton(1)), however it will always be the content of your android:background or android:src, in the case of the second, which will give the "shape" (appearance) to the button.

(1)A Floatingactionbutton is not a round button. It will only be round if the Drawable indicated in android:src has a round "shape".

  • 1

    Thanks for the help @ramaral!

0

A floating action button (FAB) is a circular button that triggers the main action in your application’s user interface. This page shows you how to add FAB to your layout, customize some of your appearances and respond to the ringtones.

To learn more about how to create a floating action button in your app according to material design guidelines: https://material.io/design/components/buttons-floating-action-button.html

inserir a descrição da imagem aqui

Add the floating action button to your layout

The following code shows how the FloatingActionButton should appear in your layout file:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:src="@drawable/ic_my_icon"
    android:layout_margin="16dp" />

By default, the FAB is colored by colorAccent, that you can customize with the theme color palette.

You can configure other FAB properties using XML attributes or corresponding methods, such as the following:

The size of the FAB, using the app:fabSize by the xml or method setSize().

The ripple color of the FAB, using the app:rippleColor by the xml or method setRippleColor().

The FAB icon, using the android:src by the xml or method setImageDrawable().

  • Nice guy, I’ll take a look later. Beauty?

  • 1

    @Andréfilipe Note that a Floatingactionbutton is not a round button. It will only be round if the Drawable indicated in android:src has a round "shape".

  • Well remembered, I just noticed it now little at the time of implementation. By itself it is not round... Thank you @ramaral, very clarified my doubts!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.