I wish to change the image of a button when it is pressed. I am doing so in a selector XML file which is fine, and seems the same in all of the examples I have seen so far.
Am I correct though in saying that a separate XML file is required for EVERY button? I have one activity with 6 buttons, so does that mean in order to represent each of those buttons being pressed, I need 6 XML files?
That seems very cumbersome so maybe someone can shed some light on the best way to do it.
Here are the images I want to use for pressed:
Not pressed:
If you want all the buttons to look the same, you'll only need one XML file to define the appearance of the button. You can use that same appearance for as many imagebuttons as you like.
If you want the same background, but different icons you still only need one selector file, but the images you feed it should just be the red and blue squares, with no question mark. Then, as the src for the imagebutton, you would put the transparent image of just the question mark, just the clock, just the euro, etc
No. You just need to define your State Selector list once, and can reuse it for as many Views as you like (assuming you want them all to look the same).
Related
i designed my whole activity page in photoshop and opened it in eclipse.I want to put buttons on my fake image buttons. Doesnt it make any problem? I use the code below to make the button invisible but it disables button too. How can i fix this problem?
android:visibility="invisible"
You should not do like that (having a fake ImageButton on a View you want to make clickable), as many error can occur with doing it. Assuming you are trying to put this button on a whole ImageView, or a whole Layout, or at least a whole view, you should consider adding a click listener to this view. Try to find more information about how to use the View.setOnClickListener(View.OnClickListener) method. You will have less error to debug with it.
But if you still want to do, try instead using android:alpha property:
android:alpha="0"
It will make the button become invisible, but still present, and still active
This android:alpha attribute allows you to specify an opacity value for a color.
To be honest you shouldn't be doing that unless you are testing something or prototyping. Instead, you should cut your design in photoshop and export the images for your buttons.
Your approch will most likely cause the buttons to be positioned incorrectly above the image when run on different devices with different screen sizes.
The correct way to do this would be to use the ImageButton view with a StateListDrawable set as the source image.
You can read more about State Lists here:
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
It's very easy to declare it in XML and have different images for various button states (pressed, disabled, focused, ...).
To sum it all up. I would do the following things:
Export images for my buttons in all the different states that you prepared (i find neutral and pressed as an absolute minimum).
Import them as drawables in your project (be sure to generate different versions for every screen density you want to support).
Create a State List Drawable XML Resource as described here.
Set the resource created in step 3 as the android:src property of your ImageButton (setImageResource(int)).
Try android:background="#null" for your button. It will make that default gray background disappear, making your button invisible but still allowing you to add text etc if you want to.
That being said: I would not recomment a button here. I would prefer Pauls answer in your particular case.
I am designing a calculator in android, and for that I need buttons with two images on them, one image for the button main function and the other for the button second function. I cant collapse them into one image because those two functions of the button have to be separately replacable.
Currently I 'solved' this with coding a layer-list and using it as the android:src for an ImageButton, but this seems clumsy and has issues when resizing.
Is there a better solution?
I am trying to stick with xml, by the way
I found a workaround/a better ansatz. Instead of using ImageButton with two images, I am now using a custom font that contains vector images as characters, and write with the font into a normal Button.
I think all custom button tutorials I have been able to find for Android assume you are using three images for your button: a normal image, a pressed image, and a focused image.
Instead of essentially tripling the size of a given button's resources (and creating more work for the artist/UX guy), is it possible to only supply a normal button image, and for the other states, draw some sort of overlay over the existing button by extending the Button class?
Has anybody tried doing this with any success, or is it just accepted that all custom buttons need an image for each state and that is that?
You could by overriding the draw methods of the view in your custom button but it would be a simple process as you would have to also identify the different states yourself.
I think the correct answer to this question is essentially what #Luksprog said in the comments... It simply isn't worth it. Just make the extra art.
Image comments: Icon should go centered in the red bit. The text should go centered in the yellow bit.
I'm trying to create a button in android that looks pretty much like the above. I tried with the imagebutton, the normal button (with drawabletop and drawablebottom), but the text is not proper aligned then.
Do note that I prefer not to use shapes, as the final result will be ninepatch images (that come from photoshop). Although feel free an example there for documentation use.
Also note that it needs to be clickable.
I think i just need to create a custom XML for this below, and then somehow have a button make use of this. I just haven't figured out how/where i need to set the text and so on then.
Any advice on this is appreciated.
hiii....
While using Button... you can align your text as per your expectations using android:gravity="center"android:text="TestHere"`
Try using this... In my case this works f9... It does or doesn't help you plz let me know
You could create the lower part as a separate button, and the above part as an imageview. In your layout file, place the top part above the bottom part. And finally, in your code, create a on clicklistener and assign the same listener to both the button and the image. Of course this would only cause the bottom part to act like a button(looking like it's pressed), but perhaps that's acceptable.
As you want to use Nine patch, create nine patch such a way that the content area is only in the middle of the yellow portion. I'm sure you know how to select content area for nine patch.
I need to create some custom buttons as shown in the image below
what is the best approach to follow?
thanks
Abdul Khaliq
That's a hard one. I made a lot of custom views, and the first thing I would thinking of is, made one Button with that above image, and handle onTouch by yourself so you can distinguish which area the user hit. There you can also change the state of the button, like changing the image to a bevel one e.g. when the left button is hit.
Can you imagine this ansatz?
You can place two transparent "invisible" buttons over the top of a background in a LinearView. Like two ImageButtons with a transparent png inside.
It is also possible to make this background animated when buttons are clicked using android animation class.