Button invisble but enabled - android

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.

Related

Replacing a View in Android onClick

I'm still kind of new to android. I'm writing a Tic Tac Toe game as a bit of practice. I'm trying to figure out how to replace views when I click a button. I have 9 buttons in a GridView. When a user clicks one, I want that to change to a non-clickable TextView and back to Button when a user click's the reset Button at the bottom of the screen.. I use a flag to keep track of player's turn so it'll know whether or not place an x or o. Is this even possible or am I stretching here?
You'll soon find that there are really not that many things that are stretching it for Android.
This is certainly possible. For each grid in your GridView, put in two elements - the Button, and the TextView. Change the visibility of each. In other words, you don't actually replace one with the other - you just hide one, and show the other.
So you'd have two items like this:
<Button ... android:visibility="invisible"/>
<TextView ... android:visibility="visibile"/>
And have both of these match_parent, so that they fill each grid and are basically both on top of each other.
To change the visibility in the code:
button1.setVisibility(View.INVISIBLE);
textView1.setVisibility(View.VISIBLE);
I'm trying to give you as little actual code as possible so you play with this and write it yourself, but this should definitely put you in the right direction. Let me know if you need more guidance though.
You can do it two different ways
You can put both a button and a textview in each grid and interchange their visibility when you click on the button. For this, you can set the button and textview properties from the xml layout and you dont have to do much programatically
You can use a button alone and just change the look by changing the background drawable at runtime. Then you can make it unclickable by disabling it or changing its focusable property to false
You can even use an imageview and just change the drawable src and disable it on user click. Android is quite flexible and this is not even a stretch. If you give a little more detail of the specifics you want to achieve, I could advise which solution will be best fit

Android, an XML for EVERY image button?

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).

Android Custom Buttons: Alternative to Multiple Graphics?

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.

Easier alternative for showing Button as pressed or not than Selector?

I have an application with a large number of buttons across the various screens. Each of the buttons has a background image as well as text (so ImageButton is no good). I am aware that it's possible to have multiple versions of an image for the different states a button can be in (e.g. pressed and not pressed), that it's possible to put the different versions of the image in a selector, and then to set the background of the button to be the selector. However, this means I need to create two versions of each image (times four for each of the different densities I'm considering) and thus bloating the size of my apk as well as adding considerable time to produce the duplicate images.
Is this the only way to change the look of a button for when it's pressed or not (i.e. to set its background to be a selector which links to two different images), or is there some other easier way to change the look of a button, like specifying some built-in property of the button (its 'darkness' for example)?
Found one way of showing a View as pressed or not by wrapping the View in a FrameLayout, giving the FrameLayout a foreground drawable which is a selector consisting of a black-ish transparent colour (pressed state) and fully transparent colour (default state), and then setting the click listener on the FrameLayout instead of the View. Wrote it out in greater detail on my Blog here:
http://adilatwork.blogspot.co.uk/2012/12/android-show-view-as-pressed-without.html

android custom button

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.

Categories

Resources