How to change the color of the floating button and insert a text into it

Asked

Viewed 608 times

0

I would like to change the color of the floating button and put a small text but do not know how to do, could help me

  • Which floating button? Try to post a [mcve] that helps you understand your difficulty, or at least the relevant part of your current code.

2 answers

1

better if you can do it programmatically, for example, at any event of your Activity you can do:

public class MinhaActivity extends AppCompatActivity {
    Button meubotao;
    // ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        meubotao = (Buttom) findViewById(R.id.meubotaonoxml);
        // aqui voce pode mudar a cor original, uma imagem, um shape num xml, em fim o que voce quiser
        if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
           meubotao.setBackgroundDrawable(getResources().getDrawable(R.drawable.meufundo) );
        } else {
           meubotao.setBackground( getResources().getDrawable(R.drawable.meufundo));
        }
        // especificamente nesse caso, eu verifico a versão do android para evitar problemas
        // você pode usar o mesmo recurso em qualquer evento que voce quiser, pois a forma para setar o background de um compnente no android via código é essa
        // se voce quiser mudar a cor, basta colocar a cor que voce deseja usando uma referencia no seu "/values/colors.xml" ou mesmo a cor como uma string usando parsecolor:
        meubotao.setBackground(getColor(getApplicatioContext(), Color.parseColor("#FFFFFF")));
    }
}

I hope it helps

0

You need to add the line backgroundTint, example:

<android.support.design.widget.FloatingActionButton
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_add"
    app:backgroundTint="@color/orange"
    app:borderWidth="0dp"
    app:elevation="6dp"
    app:fabSize="normal" >

Remember that this line does not start with android: rather with app:

To add a text in place of the image, it is a little more complicated, you need to include it within a Framelayout.

Example:

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right">

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:color/transparent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@android:string/ok"
        android:elevation="16dp"
        android:textColor="@android:color/white"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</FrameLayout>

Browser other questions tagged

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