ImageButton in Android - android

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.

Related

Combining two drawables, one over another which have a different size

I have a game which shows the avatars of the users as drawables. The avatars have a width of 150dp. Now I want to place on the bottom center a red or green dot which indicates if a user is online or not. I have the red/green dot as drawable as well. The dots have a width/height auf 30dp. How do I combine them?
Until now I was loading those avatars using the picasso library. I would really like to continue doing so, will that be possible?
I have a #drawable/avatar and either a #drawable/red_dot or #drawable/green_dot
Not sure what kind of source code I could provide here :-)
Thank you for your help in advance!
Edit:
I have been experimenting with Layer List which I saved as avatar.xml inside the drawable directory, but this only shows two same sized drawables ... so not something I really desire:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/avatarPic"
android:drawable="#drawable/avatar"
android:width="150dp"
android:height="wrap_content"
/>
<item
android:id="#+id/avatarBadge"
android:drawable="#drawable/redDot"
android:width="30dp"
android:height="30dp"
/>
</layer-list>
Second Edit:
Just wanted to mention that I went with a RelativeLayout here.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/avatarPic"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:src="#drawable/avatar" >
</ImageView>
<ImageView
android:id="#+id/avatarBadge"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignBottom="#id/avatarPic"
android:layout_centerHorizontal="true"
android:layout_centerVertical="false"
android:layout_marginBottom="1dp"
android:adjustViewBounds="true"
android:src="#drawable/red_dot" >
</ImageView>
</RelativeLayout>
and in the main layout it is accessible through the <include> tag
A quick solution is to use a RelativeLayout.
Place the avatar ImageView as first child in the RelativeLayout
Plece the dot ImageView to be drawn over the image view bottom left with e.g.
layout_alignParentLeft="true"
layout_alignParentBottom="true"

Centering ToggleButton Image - With No Text

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

How to create really small buttons in android from code?

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" />

How to remove button background color

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.

Android ImageButton: image with tranparent background

i want to write an application where the user can change the color scheme. I have Imagebuttons and the images on the buttons are pngs and have transparent background. but when i place them on the buttons they are not transparent anymore. could anybody help me with some code?
thanks!
Anne
Are you ensuring that android:background="#null" is set? Otherwise you'll have the gray button background. For example:
<ImageButton
android:background="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_transparent_png"
/>
write your layout for ImageButton as:
<ImageButton
android:src="#drawable/your_image"
android:id="#+id/your_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"/>
Hope this helps!

Categories

Resources