Replacing a View in Android onClick - android

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

Related

Button invisble but enabled

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.

Sliding drawer looks like semi circle

Android provides the SlidingDrawer by default looks like below image -
Image http://www.gru.at/android/wp-content/uploads/2010/09/s_open.png
Can we customize this SlidingDrawer looks like semi-circle type with buttons included. For, example take a look at below image -
Anyone done the SlidingDrawer like above one. I've googled it. Didn't get any nice solution for this. Anyone has idea/example blog for this?
So make sure you have Gimp and 9-Patch at the ready for a lot of trial and error...
Then what you wanna do is...
Add suitable layout to hold buttons where the current ImageView Handle is, like Horizontal LinearLayout or RelativeLayout
Cut the id of the current ImageView Handle and add it to this new Layout you just added
add your buttons to the LinearLayout/RelativeLayout (you can test it now, the buttons and the handle should all slide open at the same time)
Now add Button listeners for each of the buttons
Set the SlidingDrawer to android:allowSingleTap = "false" (tit for tat, you cant have the single tap option anymore, just the slide)
Use a ButtonSelector.xml to have different layouts for buttons be pressed or not pressed.
This is the hard part. Edit the drawables for the buttons, and handle so that they will fit together nicely if the screen is huge, small, landscape, portrait. I recommend using 9-Patch in conjunction with Gimp.
Pat yourself on the back because you now have a totally sweet custom sliding drawer.
Make sure you post pictures and state any problems or issues you ran into as I have never fully implemented this, but I did get to step 6 to make sure it was possible.
Last but not least, Good Luck...
Thanks for RelativeLayout and FrameLayout These layouts makes my requirement comportable with what i need. I've done like below steps -
First, i've splitted the full image into three pieces. And, merge these images with FrameLayout And, gave onclickListner to my center of image(Because, it was contain that arrow marks up/down)
And, i've used the Animation for just to open that View as SlidingDrawer
These steps helps me lot.

Image Puzzle game help

I made an app in Android, in which I put twelve buttons. When you click on a button, it will show you an image on the button. It's like an image puzzle game, with two buttons containing one image.
If the images match, then the game continues, but if the images do not match, it will disappear.
The only thing that I don't understand is what code and logic I should use if the image does not match and it will disappear.
Please help, thanks.
If you want to make something disappear and take up no space, you can set its visibility to 8 (which is invisible). Here I'm making everything inside a layout invisible.
LinearLayout buttonbox = (LinearLayout) findViewById(R.id.storybox);
buttonbox.setVisibility(8);
You can change the visibility of any kind of view. Here's the documentation.

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.

creating a custom image based layout on android

Is it possible to create a layout based on (background) images? For example, there is well know app called Appie that uses this picture as a homescreen:
I might be able to recreate the layout with a TableLayout, but this will be difficult to get it perfectly aligned with the buttons in the image. The default layout options make it very difficult, or maybe impossible, to allow for selection of the buttons on the image (especially when the buttons are in an arc-path).
Can anyone tell me how this is done?
I had some issues positioning a badge on the corner of a view. You can check my solved question:
Positioning a badge bubble on the left upper side of a button
About how it can be done. I would do it with a RelativeLayout and TableLayout as you mentioned, but to be completely sure, you can use apktool to see how the xml are done but it might be ilegal to do it.

Categories

Resources