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.
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.
My layout file currently displays an image, but I would like clicking certain parts of the image to call different things, such as changing the content view/switching activities. Is there a way to make an onClickListener for certain parts of the screen?
Try creating an image map or you can use this link
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).
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.
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.