Im trying to place an icon thats 16x16 pix within a normal button that I have in a layout. This button also uses another png for its background. The problem I have is not placing it within the button but placing it where I want. When I add it using the following code it places the drawable at the bottom but exactly on the edge of the image which looks terrible.
<Button
android:id="#+id/clearform"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/helpText"
android:layout_alignParentBottom="true"
android:layout_marginBottom="19dp"
android:typeface="monospace"
android:textColor="#FFFF00"
android:drawableBottom="#drawable/cross"
What I would like is to create a button in my layout that possibly has some padding from the bottom but there is no such method or xml attribute that does such a thing the way I would like. Its only left right top or bottom and when I added padding it only placed padding between the bottom of the text in the button and the top of the drawable.
So how can I accurately place an image within the button itself.
I think you can use android:padding for aligning to a greater extent & use android:paddingTop/Right/Left/Bottom to get the exact postion.It would be much easier.
Related
I need a custom radio button with a custom background, centered text, an icon immediately before the text, but without the default indicator.
Currently, I have the following code:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="#null"
android:background="#drawable/bg_rbtn_custom"
android:drawableLeft="#drawable/icon"
android:text="Lorem ipsum…" />
The problem is the icon defined in drawableLeft, is pushed all the way to the left, i.e.
[icon]____Lorem ipsum…________
I need this:
____[icon]Lorem ipsum…________
The same thing happens when I use android:button="#drawable/icon; the icon is place at the left-most part of the View, and the text is then centred inside the left-over space (rather than being centred relative to the entire View). I am Android API 8, so I can't use drawableStart, so I need a way to duplicate its behaviour (at least I assume that's what it does). The text is dynamic, and will change at runtime, so I can't really hard code the padding.
My question is quite similar to this one, but that guy only needed a Button, but I need a RadioButton that'll work in a RadioGroup.
First thoughts would be to try
android:paddingLeft="#db"
where "#db" would be the number of pixels it would take to center everything
After trying your code and replacing the background and icon to local resources I had, I was unable to recreate the issue you are having. Could it be possible that the icon you are using has transparent pixels on its right edge?
I created a button with a 9patch image as a background. In the 9patch image I used the bottom and right lines to specify the content area/padding, to position the text on the button. This works well on a TextView, but on a Button the content lines seem to have no effect on the text position. Setting "paddingTop"/"paddingBottom" also seem to have no effect - only "padding" has any effect.
This is my layout XML:
<Button
android:layout_width="450dp"
android:layout_height="198dp"
android:text="Some text here"
android:gravity="center"
android:background="#drawable/my_button"/>
Is there any way to set the padding? Maybe I could use a different View instead of a Button? For example, I could try to just use a TextView and make it clickable, but I'm not sure what other side-effects this will have.
Explicitly setting android:padding="#null" solved my issue.
The issue seemed to be that a Button (or one of the styles in the theme I'm using) is setting the padding, which overrides all other padding in the button - padding in the background drawable, as well as paddingTop, paddingLeft etc.
My button layout is now defined like this:
<Button
android:layout_width="450dp"
android:layout_height="198dp"
android:text="Some text here"
android:gravity="center"
android:padding="#null"
android:background="#drawable/my_button"/>
My IDE (IntelliJ IDEA 11.1) picked up the #null as an error, even though it compiled. Setting it to -1px also worked.
If you use a 9-patch image as background, both the padding and the stretched areas are defined in the background.
It's quite graphic if you use the draw9patch tool: the padding is defined (indirectly) with the content area (lines on right and bottom):
http://developer.android.com/guide/developing/tools/draw9patch.html
One of the application in my mobile has a header like the one shown below in the snapshot. I like the design very much and would like to create one for my application. Clicking the home icon takes the user to landing page(Home page). I have only created buttons of squarish and rounder corners. But not sure, how to create one like the one given below. Any help would be highly appreciated.
Create the image in photoshop or whatever.
Then create an imageview and let it act like a button:
<ImageView
android:id="#+id/camera_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/button_camera"
android:onClick="onCameraButtonClick" />
onClick is introduced in Android 1.6
or a button and set the background:
<Button
android:id="#+id/camera_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_camera"
android:onClick="onCameraButtonClick" />
drawable/camera_button should be a state drawable so the user can see when it is being clicked/focused.
in your activity:
public void onCameraButtonClick(View v){
// Do Something
}
It's all here:
http://developer.android.com/reference/android/widget/Button.html
The way that I would do this is just create the buttons in photoshop/GIMP on the same canvas then save each as PNG, you can use the slice tool for this. Then I would just add an attributes to each button. There's attributes that let you set the left margin of say the right button to line up with the right margin of the left button. The backgrounds of the buttons could also be patch9 images, and the icons inside it could be the src attribute of the button view. The button with the camera would have SRC pointing to a PNG that has the camera icon as well as "Camera". And the button itself not having any text.
First of all, can I just say, I find laying out android UI's to be a frustrating experience? I used to think the XML layouts were simple and clean and awesome but every time I try to make anything with it I spend hours trying to do the simplest things!
In this particular instance I'm trying to make a simple horizontal bar that contains an image button of fixed size on the right and to the left of it I want an ImageView that takes up the rest of the available width. I see similar constructs all the time in the UI: the search box that appears at the top of the screen when searching, the text area and send button for composing text/googletalk messages, etc.
I've tried both a horizontal linear layout and a relative layout, and I can't get the button to look right in either one. My latest attempt has the following layout code:
It looks like this:
Using the hiearchyviewer indicates that both the imageview and the button have the same height (45px). And it shows the view dimensions and positions to be exactly what I'm looking for. Same height (differing widths of course since the ImageView is much wider). And they butt right up next to each other, centered in the Relative Layout. However the button as drawn on screen is obviously not taking up the full ImageButton view. I'm thinking it's something weird about the android system 9patch drawable used for the ImageButton background. But what do I know? I can't get it to look right no matter what I try.
How did you set up your RelativeLayout? Try to set it up like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<ImageButton android:layout_width="wrap_content" android:src="#drawable/icon" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:id="#+id/imgButton"></ImageButton>
<ImageView android:id="#+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:src="#drawable/red_button" android:scaleType="fitXY" android:layout_alignBottom="#+id/imgButton" android:layout_alignTop="#+id/imgButton" android:layout_toLeftOf="#+id/imgButton"></ImageView>
</RelativeLayout>
Hope this helps.
If dimensions are exactly how you are looking for , then in ImageButton and ImageView , use android:scaleType="fitXY" and check.
For simple case , I might use linearlayout with horizontal orientation with two buttons in it with proper weights.
I'm trying to create standard button in android with a background and some text in front but some fairly specific alignment. I want the text to be centered vertically and on the left with 20dp of padding. The alignment works but the padding doesn't. I know I could probably get the desired effect by putting a few spaces in the text but that seems like a hack and next I want to do a similar thing but with the text at the top so I would prefer a more elegant solution.
Here's what I have:
<Button
android:layout_width="312dp"
android:layout_height="95dp"
android:id="#+id/gv_music_button"
android:text="Music"
android:textSize="30sp"
android:paddingLeft="20dp"
android:gravity="left|center_vertical"
/>
My mistake, padding was working correctly. Just didn't appear to be.