Home > front end >  Placing an image in button but it does not show image
Placing an image in button but it does not show image

Time:03-27

I created a button and I used android:background="@drawable/background" to change the image however it only displays the default design of a button (purple). How do I set it to the image I have saved in my drawable folder?

I know there is Image Button but I wanted to write a text on the button, but there is no feature where I can write a text on top if I used Image Button

CodePudding user response:

You have use an ImageView and create a clicker method that allow you to click on the image, you don't have to use a button in you want to click on an image

CodePudding user response:

As you have mentioned, you can't put a text if you have used ImageButton.

As far as I understood, you want to have view which has a text on above of image and image on bottom of the text.

If you use android:background feature on Button, button's background will be your drawable image and it won't be as you have expected.

If you are going to use Button for sure, you can use android:drawableBottom feature to show your drawable image on below of your text. And you can basically add button text by using android:text feature.

My advice to you, you should use TextView instead of Button for that case. Because if you do it with the Button as I have mentioned above, your button will have background and you can make transparent background for your button by using below code.

<Button
    android:id="@ id/buttonMoreTime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:drawableBottom="@drawable/ic_baseline_home_24"
    android:text="@string/app_name"
    android:textColor="@color/black" /> 

This code will make your Button has a transparent background.

android:background="@android:color/transparent"

But this is a little bit hacky solution. If it won't have background and if you do not have to use Button in your case for sure, you can use TextView for that and basically to show text and drawable on bottom, you can code your TextView basically as below.

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    app:drawableBottomCompat="@drawable/ic_baseline_home_24" />

Note: These are very simple xml examples to solve your main problem about showing image on bottom of your text, in same view. If you need anything more, let me know and I would be glad to help.

  • Related