How to dynamically set drawableTop for Button (android) - android

I have a Button:
<Button
android:id="#+id/btnCap2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/btn_s9cap2"
android:background="#null"
android:layout_gravity="center_horizontal"
android:drawablePadding="10dp"
android:padding="20dp"
android:text="#string/menu_btn_s9cap2"
android:textColor="#color/black"
android:textStyle="bold" />
I need to adjust drawableTop dynamically . How can I do this ?
I know how to adjust background but this does not solve my problem.
btnCap2 = (Button) findViewById(R.id.btnCap2);
drawable = getResources().getDrawable(R.drawable.btn_s9cap2);
btnCap2.setBackground(drawable);
btnCap2.setOnClickListener(this);

Use
btnCap2.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);

Related

How to change icon size in button android

I have a button which have both text and image. The image size it is taking as actual size of image. How can i change the size of image in button?
<Button
android:id="#+id/button3"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:drawableTop="#drawable/home_icon"
android:text="Settings"
/>
Set
android:scaleType="fitXY"
Also wrap_content is the preferred Parameter for width and height so that the OS will automatically adjust depending upon the screen size.
Please refer to
Android - Image Button Scale
and
Adjusting the size of an ImageButton in Android
You should use a ImageButton and specify the image in android:src, and set android:scaletype to fitXY
Set Drawable in your code
Drawable drawable = getResources().getDrawable(R.drawable.icon);
drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*0.10),
(int)(drawable.getIntrinsicHeight()*0.10));
ScaleDrawable sd = new ScaleDrawable(drawable, 0, sWidth,sHeight);
Button btn = findViewbyId(R.id.btnId);
btn.setCompoundDrawables(sd.getDrawable(), null, null, null); //set drawableLeft/Right for example
Control the size deterministically with specific measurements. For example:
<Button
android:id="#+id/button3"
android:layout_weight="1"
android:layout_height="50dp"
android:layout_width="50dp"
android:background="#drawable/home_icon"
android:text="Settings"
/>
For even more control over the button's design use a container such as a RelativeLayout to hold the image and the text. Then assign an onClickListener to the container to turn it into a clickable button. Here's an example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/snazzy_button_with_image_above_text"
android:layout_width="wrap_content"
android:layout_height="64dp"
android:layout_gravity="center"
android:layout_marginLeft="1dp"
android:background="#color/SkyBlue"
android:paddingBottom="2dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:paddingTop="1dp" >
<ImageView
android:id="#+id/your_image"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/white"
android:contentDescription="image inside button"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/your_text"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/your_image"
android:layout_alignParentLeft="true"
android:layout_gravity="center"
android:layout_marginBottom="0dp"
android:layout_marginTop="0dp"
android:background="#color/translucent_black"
android:gravity="bottom|center_horizontal"
android:paddingTop="0dp"
android:text="Your Button Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/white"
android:textSize="10sp" />
</RelativeLayout>
In the code assign the onClickListener as such:
yourButton = (RelativeLayout) findViewById(R.id.snazzy_button_with_image_above_text);
yourButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View clickedButton) {
//button click action here
}
Set android:background="#drawable/image_name"
and give width and height like
android:layout_height="10dp"
android:layout_height="10dp"
or you can try using ImageButton instead of Button because ImageButton has more image rendering options than Button
You can refer here
Simple way: add next line to dimens.xml
This parameter change size all icons on buttons in AlertDialog
<resources>
<dimen name="abc_alert_dialog_button_dimen">24dp</dimen>
</resources>

Android button with icon and text

I have some buttons like this in my app:
<Button
android:id="#+id/bSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="Search"
android:textSize="24sp" />
I'm trying to create a same button with text and a icon.
android:drawableLeft doesn't work for me (Maybe it would, but i don't know how to set a max height to the icon).
So i created a LinearLayout with a ImageView and a TextView and made it act like a button:
<LinearLayout
android:id="#+id/bSearch2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_default"
android:clickable="true"
android:padding="16dp"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:adjustViewBounds="true"
android:maxHeight="30dp"
android:maxWidth="30dp"
android:scaleType="fitCenter"
android:src="#drawable/search_icon" />
<TextView
android:id="#+id/tvSearchCaption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="24sp"
android:paddingRight="30dp"
android:gravity="center"
android:text="Search" />
</LinearLayout>
My new button is exactly what i want (font size, icon and text placement).
But it doesn't look like my default buttons:
So i tried, to change the background and the text color of my new Button:
Button Search = (Button) findViewById(R.id.bSearch);
LinearLayout bSearch2 = (LinearLayout) findViewById(R.id.bSearch2);
bSearch2.setBackground(bSearch.getBackground());
TextView tvSearchCaption = (TextView)findViewById(R.id.tvSearchCaption);
tvSearchCaption.setTextColor(bSearch.getTextColors().getDefaultColor());
This gives a strange result, my old button, gets messed up:
When i change the order of these two buttons in the XML, so the "new button" goes first, it makes another strange result:
Now i noticed, that when i try to press the old button, the new one gets pressed.
Any ideas?
Try this one.
<Button
android:id="#+id/bSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="Search"
android:drawableLeft="#android:drawable/ic_menu_search"
android:textSize="24sp"/>
To add an image to left, right, top or bottom, you can use attributes like this:
android:drawableLeft
android:drawableRight
android:drawableTop
android:drawableBottom
The sample code is given above. You can also achieve this using relative layout.
You can use the Material Components Library and the MaterialButton component.
Use the app:icon and app:iconGravity="start" attributes.
Something like:
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.Icon"
app:icon="#drawable/..."
app:iconGravity="start"
../>
For anyone looking to do this dynamically then setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) on the buttons object will assist.
Sample
Button search = (Button) findViewById(R.id.yoursearchbutton);
search.setCompoundDrawablesWithIntrinsicBounds('your_drawable',null,null,null);
This is what you really want.
<Button
android:id="#+id/settings"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#color/colorAccent"
android:drawableStart="#drawable/ic_settings_black_24dp"
android:paddingStart="40dp"
android:paddingEnd="40dp"
android:text="settings"
android:textColor="#FFF" />
#Liem Vo's answer is correct if you are using android.widget.Button without any overriding. If you are overriding your theme using MaterialComponents, this will not solve the issue.
So if you are
Using com.google.android.material.button.MaterialButton
or
Overriding AppTheme using MaterialComponents
Use app:icon parameter.
<Button
android:id="#+id/bSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="Search"
android:textSize="24sp"
app:icon="#android:drawable/ic_menu_search" />
What about like this?
<Button
android:id="#+id/yourID"
android:drawableStart="#drawable/ic_flag"
android:textColor="#FFFFFF"
android:textSize="12sp"
android:textStyle="bold"
android:textAlignment="textStart"
android:background="#drawable/btn_background"
android:fontFamily="#font/YourFont"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Text" />
try It:-
position-> left,right,top,bottom
android:drawableposition="#android:drawable/-yourImage-"
android:drawabletop="-Yourfilesrc-"
android:drawablebottom="-Yourfilesrc-"
android:drawableleft="-Yourfilesrc-"
android:drawableright="-Yourfilesrc-"
I was experiencing the same problem with pure Button view. In my case XML solution was not usefull, because I have some logic to show/hide this icon.
There is one more solution using pure Kotlin:
(bSearch as? MaterialButton)?.let {
icon = AppCompatResources.getDrawable(context, R.drawable.search_icon)
iconGravity = MaterialButton.ICON_GRAVITY_START
}
P.S. We can cast Button to MaterialButton as fair as we can use icon param in Button: icon param is not actually Button param - it is sneaky MaterialButton param somehow working with Buttons.

Change drawable color on button on click

I have some buttons with a text and a drawable set in the xml with android:drawableLeft=...
I want to know how to change, on click, the color of both the text and the drawable. And it has to be general, because i have a lot of button with each time, a different drawable.
My XML :
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_articles"
style="#style/Main_button"
android:layout_marginLeft="5dp"
android:background="#color/green3"
android:onClick="myClickHandler"
android:text="Articles"
android:drawableLeft="#drawable/ic_article"
android:textColor="#drawable/btn_default" />
<Button
android:id="#+id/btn_encaiss"
style="#style/Main_button"
android:layout_marginLeft="5dp"
android:background="#color/green4"
android:onClick="myClickHandler"
android:text="Encaissement"
android:drawableLeft="#drawable/ic_encaiss"
android:textColor="#drawable/btn_default" />
<Button
android:id="#+id/btn_stats"
style="#style/Main_button"
android:layout_marginLeft="5dp"
android:background="#color/green1"
android:onClick="myClickHandler"
android:text="Statistiques"
android:drawableLeft="#drawable/ic_stats"
android:textColor="#drawable/btn_default" />
<Button
android:id="#+id/btn_clients"
style="#style/Main_button"
android:layout_marginLeft="5dp"
android:background="#color/green2"
android:onClick="myClickHandler"
android:text="Clients"
android:drawableLeft="#drawable/ic_clients"
android:textColor="#drawable/btn_default" />
</LinearLayout>
For now i just change text color on click. I want to change both drawable and text color without knowing the left drawble name.
Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
img.setBounds( 0, 0, 60, 60 );
txtVw.setCompoundDrawables( img, null, null, null );
With this code, you can change a left drawable programatically.
For the text color, please see Android documentation

How to style a image button without the grey default background?

I define a Button with this code
<ImageButton
android:id="#+id/btn_photo_lib"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startPhotoLibAction"
android:src="#drawable/library_blau" />
and the result is
Does someone know how to remove this grey area behind the image?
Thanks
Change ImageButton into ImageView or change android:src into android:background.
set background property of ImageButton instead of src.
There are 2 ways:
<ImageView
android:id="#+id/btn_photo_lib"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startPhotoLibAction"
android:src="#drawable/library_blau"
android:clickable="true" />
or:
<ImageButton
android:id="#+id/btn_photo_lib"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startPhotoLibAction"
android:background="#drawable/library_blau" />
You add android:background = "#android:color/transparent" as backgorund for your image button
<ImageButton
android:id="#+id/btn_photo_lib"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startPhotoLibAction"
android:background="#android:color/transparent"
android:src="#drawable/library_blau" />

ImageButton and TextView different states

I have a set of Views consisting in an ImageButton, an ImageView and a TextView.
The code looks like this:
<ImageButton
android:id="#+id/imagebutton_com"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/bean_bg"
/>
<ImageView
android:id="#+id/imageview_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/arrow_1"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
/>
<TextView
android:id="#+id/textview_wizard_main_button"
android:text="Soybeans"
android:layout_toRightOf="#id/imageview_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:textStyle="bold"
android:textColor="#color/white"
android:layout_margin="10dp"
/>
All works as expected, but I want to change the textColor of the TextView when the Button is focused.
I tried writing a selector xml file to do the same, but nothing worked.
Any suggestion?
You need to do it in code. Use OnFocusChangeListener on your ImageButton to achieve this.
Create a folder color under resources and add selector.xml file to that folder
change property of TextView namely
android:textColor = "#color/white"
to
android:textColor = "#color/selector"

Categories

Resources