Android default Button class provides a really big button.
There is a lot of wasted space around. Now I want to create really small buttons, that are only slightly bigger than the text/caption. How to do this from code?
So far I found the following method, but it is still providing too big buttons and wasting too much space:
A. Create an XML (smallbutton.xml) in the res/layout:
<?xml version="1.0" encoding="utf-8"?>
style="?android:attr/buttonStyleSmall"
android:text="color"
android:padding="0dp"
android:layout_margin="0dp"
android:textSize="8dp"
android:maxHeight="2dp"
android:maxWidth="2dp"
/>
B. From your code inflate this and add to the view:
Button pickBackColor = (Button) this.getLayoutInflater().inflate(R.layout.smalbutton,null);
...
LinearLayout linLayout = new LinearLayout(this);
linLayout.addView(pickBackColor);
Now the problem is that the button is still too big, it uses too much space around (on the left, right, above, under) of the text.
Any hint how to decrease the size of the button further?
Buttons have a minimal height and width (typically of 48dp respectively 64dp by default). So add
android:minHeight="0dp"
android:minWidth="0dp"
to get really small buttons:
For your information there is also the default style provided by Android that defines smaller buttons:
style="?android:attr/buttonStyleSmall"
You can also if necessary modify this default style and define custom attributes in your file styles.xml. Here is an example with the modifications described in the question:
<style name="MyButtonStyleSmall" parent="android:Widget.Button.Small">
<!-- Customizations -->
<item name="android:minHeight">0dp</item>
<item name="android:minWidth">0dp</item>
</style>
Then you just need to call it in the XML:
<Button
android:id="#+id/small_button"
android:text="#string/example"
android:contentDescription="#string/example_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/MyButtonStyleSmall" />
in .xml file...
you need to pass small value to layout_height and layout_width in xml file...... try this.
android:layout_width="50dp" android:layout_height="50dp"
Change value as per your requirement...
try this in your code:
pickBackColor.setWidth(VALUE);
pickBackColor.setHeight(VALUE);
<com.google.android.material.button.MaterialButton
android:id="#+id/food_item_minus_button"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="#drawable/custom_small_button_bg"
android:drawableLeft="#drawable/ic_minus"
android:paddingStart="6dp"
android:paddingLeft="6dp" />
Related
I want to have button with icon and text near icon both in center. I managed to do that with padding but since android has a lot of different screen sizes, it is not working well on small screen sizes. This is my code:
<Button
android:id="#+id/scanbtn"
android:text="#string/scan_btn_text"
android:textAlignment="center"
android:fontFamily="#font/open_sans_light"
android:drawableStart="#drawable/ic_photo_camera"
android:paddingStart="100dp"
android:paddingEnd="120dp"
android:textAllCaps="false"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_marginEnd="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="25dp"
android:background="#drawable/border"
android:textColor="#color/white"
android:textSize="19sp"
android:transitionName="use/scanbtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="parent" />
This is my button. Please show me other (better) way to make this button be same on all screen sizes
Just wrap a TextView by FrameLayout and use the Framelayout as a button.
<FrameLayout
android:background="#E06666"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:background="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/icon"
android:text="Scan"
android:layout_gravity="center"
android:gravity="center"
/>
</FrameLayout>
Bellow is my result:
Maintain the button padding across various screen size
--- Create different values folders
values, values-sw350dp, values-sw480dp, values-sw600dp, values-sw720dp
-- Add a file dimens.xml in each folder
-- Add a dimension with same name but different values on each file
<!-- Add this to dimens.xml in values folder -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="button_padding_start">100dp</dimen>
</resources>
<!-- Add this to dimens.xml in values-sw350dp folder -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="button_padding_start">150dp</dimen>
</resources>
<!-- Do the same for all other folders and keep increasing the value of button_padding_start -->
Then reference the value from your layout instead of using a fixed dp and let android select the best choice based on the screen size to maintain similar look across various device.
<!-- paddingEnd may not be necessary -->
android:paddingStart="#dimen/button_padding_start"
Learn more about supporting different screens here
Use the following code and remove all paddings from button XML:
android:drawableLeft="#drawable/image
Hope you get the answer. Please select as right answer.
Just use the MaterialButton with the app:iconGravity attribute.
Use the textStart or textEnd value.
<!-- Icon gravity textStart -->
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.Icon"
app:icon="#drawable/...."
app:iconGravity="textStart"
android:text="#string/..."
.../>
<!-- Icon gravity textEnd -->
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.Icon"
app:icon="#drawable/..."
app:iconGravity="textEnd"
android:text="#string/...."/>
Here is my ToggleButton:
<ToggleButton
android:id="#+id/bSmenuTopItems"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:background="#drawable/master_button_selector"
android:drawableLeft="#drawable/flame_icon" />
I have no text in this image, I need a ToggleButton due to Active State.
EDIT: I think question was misunderstood. There is a drawable inside the Toggle Button (flame_icon) and it is set as background. I want it to be centered. There is no Text, just an image. I need a Toggle Button because I need to have an Active State when selected.
There is only drawableLeft, drawableRight, drawableTop, etc. I want a draweableMiddle that doesn't seem to exisit.
I revised the answer to answer your revised question.
The drawableLeft, drawableRight, and drawableTop button, as far as I can tell, control where the image is placed relative to the selector (a/k/a on/off) indicator. Top will place it above the indicator with left and right placing it to a specific side respectively. I do not believe you can remove the selector indicator as that would defeat the purpose of using a ToggleButton.
I was able to center 2 drawable in 2 ToggleButtons using the following layout. To center the images within the ToggleButton I used drawableTop so that the images appeared over the selection indicator. I then set both textOn and textOff to be an empty string. If you do not set this, then the default on/off text appears above the selector indicator and pushes the drawable to the side.
<LinearLayout
android:id="#+id/buttonContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<ToggleButton
android:id="#+id/bSmenuTopItems"
android:layout_width="0"
android:layout_weight="1"
android:layout_height="75dp"
android:checked="false"
android:drawableTop="#drawable/flag"
android:textOn=""
android:textOff=""
android:layout_gravity="center"
android:textColor="#cecece" />
<ToggleButton
android:id="#+id/bSmenuTopItems2"
android:layout_width="0"
android:layout_height="75dp"
android:layout_weight="1"
android:checked="false"
android:textOn=""
android:textOff=""
android:drawableTop="#drawable/chaticon"
android:layout_gravity="center"
android:textColor="#cecece" />
</LinearLayout>
All you should have to do is adjust the height of the button to position your icon relative to the selector icon where you want it. My only other suggestion would be to control the size of the image you are using. If you can just adjust the dimensions of the image relative to the button, placing it with drawableTop should center it automatically.
I didn't have any luck with the accepted answer but maybe my scenario is subtly different. The other solution I've seen is to use the drawablePadding attribute to push the topDrawable toward the center of the button. That works to an extent but it assumes that the button's dimensions are fixed and even then, it's difficult to center the icon perfectly.
This is what I came up with instead. Don't specify the icon on the button itself. Instead, set the button's background to a layer-list drawable that draws the icon you want over the selector you want, like so:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/master_button_selector" />
<item>
<bitmap android:src="#drawable/flame_icon" android:gravity="center" />
</item>
</layer-list>
I used the android:drawableTop as described by Rarw, but as my button was tall and wide, it just hung at the top. Not really centred...so I fiddled around with the paddingTop and it might work here what I got:
<ToggleButton
android:id="#+id/togglebutton1"
android:paddingTop="50dp"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_weight="1"
android:drawableTop="#drawable/music_collection_small"
android:textOn=""
android:textOff=""
/>
I think it will work for my purposes
Maybe this will help you. Look at the bottom of the page for the solution from
zapl.
P.S didn't know how to link the specific answer, new here XD
I searched a lot but I didn't find how to remove the background color from the button which is appearing on the right and left side of button. Can anybody help?
My screen looks like
No matter what I try I am not able to remove the black portion.
Code:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btn_base"
android:text="#string/base"
android:layout_margin="2dp"
android:textColor="#000000"
android:background="#drawable/selector_button"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="30dp"/>
<Button
android:id="#+id/btn_care"
android:text="#string/care"
android:layout_margin="2dp"
android:textColor="#000000"
android:background="#drawable/selector_button"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="30dp"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btn_daily_prize"
android:layout_margin="2dp"
android:textColor="#000000"
android:background="#drawable/selector_button"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="30dp"
android:text="#string/daily_prize" />
<Button
android:id="#+id/btn_winner"
android:layout_margin="2dp"
android:textColor="#000000"
android:background="#drawable/selector_button"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="30dp"
android:text="#string/winner" />
</LinearLayout>
</LinearLayout>
#Drawable/selector_button
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true"
android:drawable="#drawable/pressed"> </item>
<item android:state_focused="true"
android:drawable="#drawable/focused"/>
<item android:drawable="#drawable/change"></item>
</selector>
this is the image used with name change.9.png
according to your scenario i can surely say that either you are not using a 9-patch image (an image with extension like .9.png ) or the 1 pixel borders of 9-patch at left and top are not drawn in correct manner. thats why the edges and the side border shade get expanded with the button with long width. either you should show what 9-patch button background you have used or try some correct 9-patch and check results for that.
Why don't you use some other control element like TextView instead of buttons? I just saw that TextView has onClickListner and so you can use it as sort-of button, though I have not done it; button is meant to aid you defining your layout, but as this seems to only be a problem for you, just do not use it).
By the way I seriously recommend you to use android styles, as you copy-paste a lot of attributes. If you use Eclipse for development, open your layout xml, select the item you want to extract the style of, press ctrl + 1 and then select extract style. That way you should avoid copy-pasting all these style attributes.
Try removing the android:textColor attribute. These can be misleading and sometimes alter the colour of more than just the text. If the text is supposed to be black then you don't need it.
I would to know how to have a border to separate my buttons
in my xml file.
Could someone please help me?
Use a LinearLayout with no children and a height of 1 DP (assuming you are separating them vertically)
<Button android:id="#+id/button1" />
<LinearLayout android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#color/white"/> <!-- Or #FFFFFF -->
<Button android:id="#+id/button2" />
You can play with other options but this should give you a basic idea of what (I think) you're looking for. Try elaborating and showing some sample code for future question so we can understand your problem and help more!
<Button android:id="#+id/button1"
android:layout_marginRight= "2dp" />
<Button android:id="#+id/button2"
android:layout_marginRight= "2dp" />
This sets a padding of 2dp on the right side of the button inside a linearlayout.
This is preferable to calling another transparent view or a transparent linearlayout because you save yourself from creating unnecessary views by using the built in layout tools of your linearlayout.
Can anybody tell me how to resize the imageButton to fit the image exactly? This is the code that I tried, but the image is placed at the position that I am locating using android:scaleType, but I am not able to reduce the size of imageButton. Please help me out in rectifying this issue. The code that I tried is:
<ImageButton>
android:id="#+id/Button01"
android:scaleType="fitXY" // i have tried all the values for this attr
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cropToPadding="false"
android:paddingLeft="10dp"
android:src="#drawable/eye"> // this is the image(eye)
</ImageButton>
android:background="#drawable/eye"
works automatically.
android:src="#drawable/eye"
was what I used with all the problems of resizing the image the the width and height of the button...
you are setting the image with the property "src"
android:src="#drawable/eye">
use "background" property instead "src" property:
android:background="#drawable/eye"
like:
<ImageButton
android:id="#+id/Button01"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cropToPadding="false"
android:paddingLeft="10dp"
android:background="#drawable/eye"> // this is the image(eye)
</ImageButton>
You're probably going to have to resize the button programmatically. You'll need to explicitly load the image in your onCreate() method, and resize the button there:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageButton myButton = (ImageButton) findViewById(R.id.button);
Bitmap image = BitmapFactory.decodeResource(R.drawable.eye);
myButton.setBitmap(image);
myButton.setMinimumWidth(image.getWidth());
myButton.setMinimumHeight(image.getHeight());
...
}
It's not guaranteed to work, according to the specifications for setMinimumX (since the width and height are still dependent on the parent view), but it should work pretty well for almost every situation.
Try to use ScaleType centerInside.
ScaleTypes are not properly rendered in Eclipse Layout designer, so test in your running app.
Did you try to give the layout_width and layout_height like the following? Since you are setting with wrap_content, the image button expands to the size of source image's height and width.
<blink>
<ImageButton>
android:id="#+id/Button01"
android:scaleType="fitXY"
android:layout_width="80dip"
android:layout_height="80dip"
android:cropToPadding="false"
android:paddingLeft="10dp"
android:src="#drawable/eye">
</ImageButton>
</blink>
You don't have to use it using src attribute
Wrong way (The image won't fit the button)
android:src="#drawable/myimage"
Right way is to use background atttribute
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/skin" />
where skin is an xml
skin.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- <item android:drawable="#drawable/button_disabled" android:state_enabled="false"/> -->
<item android:drawable="#drawable/button_pressed" android:state_pressed="true"/>
<!-- <item android:drawable="#drawable/button_focused" android:state_focused="true"/> -->
<item android:drawable="#drawable/button_normal"/>
</selector>
using button_pressed.png and button_normal.png
This will also help you in creating your skinned button with 4 states of pressed , normal , disabled and focussed.
Make sure to keep same sizes of all pngs
You can also set background is transparent. So the button looks like fit your icon.
<ImageButton
android:id="#+id/Button01"
android:scaleType="fitcenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cropToPadding="false"
android:paddingLeft="10dp"
android:background="#android:color/transparent"
android:src="#drawable/eye" />
I think you already solved this problem, and as other answers suggested
android:background="#drawable/eye"
is available. But I prefer
android:src="#drawable/eye"
android:background="00000000" // transparent
and it works well too.(of course former code will set image as a background and the other will set image as a image) But according to your selected answer, I guess you meant 9-patch.