How to add an Icon to a Button in Android from Java? - java

I want to add an icon to a Button that I created from Java, after hours I didn't find a solution yet...
I have something like this:
Button button = new Button(context);
And I'd like to have something like this:
button.setIcon(Icon myicon);
Is this possible? ;)

You can use the MaterialButton:
MaterialButton button = new MaterialButton(this);
button.setIcon(ContextCompat.getDrawable(this,R.drawable.ic_add_24px));
button.setText("Button");

If you want a Button with image then in XML you have ImageButton that have attribute of drawableLeft/drawableRight..etc.
So you can set in from there. In java I think you can set in the same way by calling setResourceDrawable.

you could also setIcon an XML file, Please try this
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:drawableRight="#drawable/ic_icon" <!--also have drawableLeft:-->
android:drawablePadding="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

Related

Icon not appearing on fab

I have a fab(floating action button), here is it's XML code
<android.support.design.widget.FloatingActionButton
android:id="#+id/floatingActionButton3"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="28dp"
android:layout_marginTop="32dp"
android:clickable="true"
android:focusable="true"
app:backgroundTint="#color/vert"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_check_circle_black_18dp" />
As you can see in the code I want the icon "ic_check_circle_black_18dp" to be on my fab. When I click on "design" I can see the icon on my button, but when I launch my app on my phone (7.1) there are no icons.
I already checked the following questions :
AppCompat 23.2 use VectorDrawableCompat with RemoteViews (AppWidget) on API<21
Why is my floating action button not displaying its image properly?
I can't find any solution to my problem
Well, i avoided the problem by using ImageButton instead of Floating action button and it works.

Can't get rid of the ugly border around ImageButton

I'm working on an Android App which has a few (Next, Back, ... etc) Buttons which are all round and transparent and I use ImageButton for those Buttons, however the problem is there is always a white border around the Button (I use black background so it is very ugly) and the button never appears to be round is appears as some sort of a square-ish shape.
Here is what I tried so far :
Setting the background of the ImageButton in the activity_mypage.xml file to
android:background="#android:color/transparent"
and to
android:background="?android:selectableItemBackground"
and to
android:background="?android:selectableItemBackgroundBorderless"
even to
android:background="#null"
but nothing seems to work on the .xml side.
and I tried the following on the MyPage.java file :
myBtn.setBackgroundResource(0);
and also
myBtn.setBackground(null);
Nothing seems to work anything I do keeps resulting the same (although it removes a gray border from around the Button but none of them makes the Button completely transparent)
This is a screenshot of the button before applying any of above :
And this is a screenshot of the same Button after applying any of those attributes (doesn't really matter if I applied it only to the xml or only to the java file or to both because all results are the same):
This is my xml code for one of my Buttons in xml :
<ImageButton
android:id="#+id/my_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="26dp"
android:layout_marginEnd="37dp"
android:layout_marginRight="37dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:srcCompat="#mipmap/my_btn"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintRight_creator="1"
android:background="#android:color/transparent"
/>
And here is the code for it in the .java file :
ImageButton myBtn= (ImageButton) findViewById(R.id.my_btn);
myBtn.setBackground(null);
myBtn.setBackgroundResource(0);
I'm really lost at this every result I found for this query has suggested one of the above methods but none of these seems to work for me..
Just change
android:background="#android:color/transparent"
to
android:background="#null"
Try with this
android:src="#mipmap/yourimage"
instead of
app:srcCompat="#mipmap/my_btn"
add this to your button
android:background="#drawable/template"
and create a file name it template in your drawable folder
and add this code to it
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="#drawable/btn2imagePressed"></item>
<item android:drawable="#drawable/btn1imageNotPressed"></item> </selector>
You can use ImageView instead of ImageButton, so background issue will not occur
<ImageView
android:id="#+id/my_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="26dp"
android:layout_marginEnd="37dp"
android:layout_marginRight="37dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:src="#mipmap/my_btn"
/>
So after reading some of the posted answers and playing around with the suggestions in my code I noticed that the icon was incorrectly loaded into my mipmap directory instead of drawables and that is because when I imported the icons I selected New -> Image Asset instead of Vector Asset which caused my 'icon' to lose its transparency ...
Thanks to everyone for their replies and answers.

Android adding image to button

I am trying to add an image to a button(image button or normal button) but not in a specific res/drawable folder.
I want this image as a ImageView object or drawable object etc, but I couldn't.
You can set an icon for Button by adding following attribute in XML for Button like this:
android:drawableLeft="#drawable/button_icon"
If you want to know how to do it programmatically, follow this article:
How to programmatically set drawableLeft on Android button?
Or just add 'background':
<Button
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/qrcode"/>
But i think it's better if you use ImageButton, see this example:
<ImageButton
android:id="#+id/searchImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/your_image_drawable" />
Use ImageButton and add src in your background:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/yourImageNameHere"

Android- Cannot Cast from View to Button

Okay, I'm new to both java and android programming, but I've come across the most frustrating error ever and I can't seem to be able to find the solution. here is where the error is at.
final Button button = (Button) findViewById(R.id.activity_button);
And here is the XML of the button itself.
<Button
android:id="#+id/button_main"
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/button_text" />
I'm not seeing the problem, I imported import com.awesometech.uselessbutton.R; which didn't help. Any suggestions? I'm using Eclipse.
Change to
final Button button = (Button) findViewById(R.id.button_main);
You have
<Button
android:id="#+id/button_main" // id is button_main not activity_button
Probably activity_button id belongs to another view and you are casting it to a Button.
In your main Activity, in the findViewbyId() method, you pass the ID of your Button which you declared in you xml.
So your button code is :-
<Button
android:id="#+id/button_main"
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/button_text" />
here , id is button_main
so in you Class, you need to do
final Button button = (Button) findViewById(R.id.button_main);//Button ID goes here

how can i have a list of icons in my android app;

can any one suggest how can i have a list of icons like browser icon,email icon and contacts icon upon clicking on those it should lead to android browser,email and contacts apps respectively...right now i have done it, upon clicking buttons. Now i want icons(with image and text) instead of buttons...
Check this out:
http://developer.android.com/resources/tutorials/views/hello-formstuff.html#CustomButton
This will show you had to put the images you want in res/drawable/ and then load them up as buttons in your app.
There is no widget icon.
You can do it yourself using a LinearLayout an ImageView and a TextView.
The xml should be something like this:
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/your_image"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/your_icon_text"/>
</LinearLayout>
You can use Android Image Button and set its property "Clickable=true" and do the required work on its click event listener
I thought you want to have ImageButton , clicking on that you want to open particular application/browser/contact list.
ImageButton - Displays a button with an image (instead of text) that can be pressed or clicked by the user
<ImageButton
android:id="#+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon/>
If you wants ImageButton with Background button then set android:background attribute, as below:
<ImageButton
android:id="#+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:background="#drawable/background_image"/>
If you want an image and text in your button, the way I do it is create a 9 patch, so I can define where the text will go using the padding and stretchable areas. Then follow the same instructions as for the Custom Button already mentioned, but use a standard button, not an Image Button.

Categories

Resources