Placing an image in button but it does not show image - java

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

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

Is it possible to make a TextView marquee stop and reverse when the whole text has been shown?

I have a TextView that represents the title of the current fragment and is located in the toolbar. When the user uses a big font size, the title sometimes becomes too long to be displayed completely. Hence I want to have it scroll to the right until the whole text has been shown, then stop for two seconds and then scroll back to the left where it stops as well. This should be repeated forever.
I have tried using marquee to accomplish this (see below), but there doesn't seem to be any option to make marquee stop scrolling, let alone dynamically reverse it. If there are any alternatives to marquee that have this option, I'd be happy to use them as well.
Currently the TextView looks like this:
<TextView
android:id="#+id/title_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:singleLine="true"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"/>
there is no such option, you have to end with custom solution. some sample with custom initialization can be found here (speeds up marquee, you have to modify code)

Issue with android studio and radiogroup

I'm trying to make a simple app in android studio but running into trouble with layout whenever I place down a RadioGroup. In my first image here I've created a RadioGroupand dragged two RadioButtons into it by dragging them to the radiogroup option on the component tree trying to drop them into the radiogroup on the layout thing is impossible as the radiogroup is completely invisible on it.
I drag a button below it, lining it up with the center, and the box that shows where the buttons going to go shows it being below the radiogroup. But once I release the mouse button, the button im attempting to place ends up here.
I've been fighting with android studio for a while now but if I get the button even remotely close to the RadioGroup it gets catapulted up the screen, and I don't have enough room if I place the button way down low for my other elements.
tl;dr
radiogroups are catapulting any elements underneath them above them. its probably a layout or gravity issue or something like that. but I don't know how to fix it and would like my buttons to not be underneath all the other elements kthx.
I'm not sure what is causing your issue, because you aren't providing enough information. But If you want the button to appear under your radio group, then in your xml file, give your radio group an id android:id="#+id/radioGroup" and in your button add android:layout_below="#id/radioGroup".
Try to android:layout_below="#+id/radioGroup" in your button.As your are using Relative Layout by default it takes you top while dragging.Here is full Button view.
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="23dp"
android:id="#+id/button"
android:layout_below="#+id/yourRadioGroupId"
android:layout_centerHorizontal="true" />

How do I change the color of the drawable in a button?

I have a button in the list of items rendered with recycler view with a drawable icon. Here is my code.
<Button
android:id="#+id/button_like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/ic_thumb_up_black_24dp"
android:text="#string/label_like"
android:textSize="11dp"
style="?android:attr/borderlessButtonStyle"/>
Now I want to change the color of the icon ic_thumb_up_black_24dp which is anydpi xml icon from Android material design icons.
I tried android:drawableTint="#android:color/secondary_text_dark" but it does not work.
android:textColor="#android:color/secondary_text_dark" works only on the text color but not on the icon. Can someone help me with this?

Android Studio Layout Auto Alignment

I am new to Android Studio, and no matter what I do whenever I add something such as a textview or button to it, I cannot make it the size I want it to. It always auto aligns itself to the sides or top. When I want to make a square it doesn't allow me to do it. It never allows me to put my button or textview where I want it to. How do I make it so I can place my stuff anywhere I want and whatever size I choose it to be?
You are probably using linear layout,You can use relative layout instead for start, although if you want to put a lot of buttons its getting harder to work with relative layout and linear layout is more organized go to you'r xml and there edit the in the first line where it says <LinearLayout... to <RelativeLayout> and don't forget to do the same at closing,I recommend you to watch a video on youtube on how to work with linear layouts.
In RelativeLayout, it enable the attributes like alignParentRight="true/false" for exact right position and so on...
layout_below="#id/tool/viewID_below_you_want_to_put" and same for other side
so you can use it
For Example,
<RelativeLayout
...>
<TextView
android:id="#+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_toRightOf="#id/txt1"
android:alignParentRight="true"
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
which will make your textview in left and then button in right of textview
If you want to set it in center tou can set gravity="center | center_vertical | center_horizontal"
also you can set size as you want in width or height like
android:layout_width="50dp"

Custom Button with picture and text?

I want to add a picture as well as text within a button as shown below.
If I am giving this as a background then it will deteriorate according to the screen size of different phones. So I want to custom draw custom layout where the image of info button will remain in same pixel and the info letters to be drawn as separate. So that I will get the exact outlook without any image distortion.
You can specify the background image, the '!' icon and the text separately like so:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:drawableLeft="#drawable/icon"
android:text="info" />
Use 9 patch for background, Use normal button, use drawable left for image on the left. That is the best way to go for your case.
Check this for 9-patch. That will let you create the background with rounded corners like you need without skewing them.
So finally you will have a normal button with the background that you got from 9-patch and the drawableLeft that you set the icon.
Check this out for defining button states in case you want a better user experience. You will still need to do all these and then define states for the button background and use it as the background.
You can use these three drawable for your backround name a file backround.xml in your res/drawable folder this file will handle your butoon backround on different state pressed,focused and normal:
these are three 9-patch drawable that you can use:
backround.xml is:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
     android:state_pressed="true" 
     android:state_enabled="true"
        android:drawable="#drawable/btn_orange" />
    <item 
     android:state_focused="true" 
     android:state_enabled="true"
        android:drawable="#drawable/btn_orange" />
    <item 
     android:state_enabled="true"
        android:drawable="#drawable/btn_black" />
<item 
android:state_enabled="false"
        android:drawable="#drawable/btn_red" />
</selector>
Your xml button will be like this:
<Button
android:id="#+id/my_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/myIcon"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:text="info"
android:background="#drawable/background" />
you need to arrange 3 things in your Button widget: background image, info icon and text. For these use these:
android:background="backgroundImage"
android:drawableLeft="info icon"
android:text="text"

Categories