Create a custom compund buttons with divided circle shape - android

How would you go on implementing such a compound view :
note that it contains 2 buttons, A and B.

If you want to do this in a single view, then you could draw filled path (arc) for A and B. For touch detection, you might override the onTouch to know if the user tapped on A region or B region. You could expose this outside the view via an Interface. The advantage here is that you could easily dynamically change the A and B region. The partition can be easily modified.
But if you are not after dynamic change then you could simply have a linearLayout with vertical orientation and have two buttons with the background of a sector. (just like Frank N. Stein suggested in the comment)

Related

How avoid the click transfer between brother views?

I have three layout layers and over the blue one, two views (EditText and a simply view), so let's put some context:
When I set on click listener only to yellow layer and touch inside the blue one for example, the click is listened only in the yellow layer, the click is transfer. ClickTransferBetweenChilds
When I set on Click listener in all layers the click listener is triggered when I touch anywhere inside each layer. ClickNotTransferWhenSetClickListener
This is the default android behavior and I call this layer stack, parent-child views, there's the following schema:
Yellow - is parent of -> Red - is parent of -> Blue.
Now focus on the blue layer, over the blue layer are super posed two elements, the EditText and the View, the EditText is over the View, and initially with the default android behavior when I touch the upper left side, the keyboard is called (because the EditText is below) but I want to avoid this behavior. Please call it brother propagation.
I make a simply solution please refer to line 71 Gist in MainActivity.java, but we can't implement in production because I can only touch the Layout things (the yellow one - Parent - at possible).
TL/DR: I want to avoid this behavior ClickTransferBetweenBrothers how to avoid transfer click between brothers keeping the click transfer between parent-child views.

Android Layout: How to get a particular ancestor of a view

I have a layout similar to the capture. The green rectangles are placed in horizontal scrollers and when the user touches one, another activity is launched. The blue rectangles are other layers, like the horizontal scrollers.
My problem: when the user touches a green rectangle, I need to know in which RED rectangle the touched green rectangle is placed in.
Is there any easy way to get a particular ancestor of a view? I would like to be able to add and remove more "blue rectangles", so I don't want something like view.getParent().getParent().getParent() .
Thanks!
Two ways,
Simpler one:
Set the red ones as tag to all the child it contains, now when a child receive Touch you can easily figure out who is the parent among red, by getTag method.
Little Complex but cooler:
a. Maintain a list of all the red view
b. Implement on Touch on green one
c. Get the x,y when you receive touch
d. Once you receive Touch, loop through list of red view, getting Hit Rect for each red view, once you get the hit rect, just check if it contains ur received x,y. Once you found match return. Wollah you have your parent view.
Hope it help.

How many views are required as I have a view showing drawing required, do I still need surfaceview

I have already created a circle with the use of a View and have not used SurfaceView at all. I want to create buttons which when clicked on show images from the drawables. But I have read on the net that a SurfaceView is required to allow UI elements to be placed on top. Is this true, can someone please help me, as I am confused on this.
Thanks.
It's not very clear what you want to do, but if you want to place UI elements on top of each other without using SurfaceView you can you a RelativeLayout, this layout allows you to have views on top of each other, do you can have an ImageView with a drawable appearing over a button for example.
If you just want to change the background/src images of a button when clicked (for example to create a 3d effect of clicking), you can check out selectors, these allows you to specify different drawables for pressed/normal states.
If you want to create buttons on a SurfaceView, I suggest you render Bitmaps that will represent buttons. You will have to programmatically check if the touch coordinates are in the bounds of that bitmap tough, to register a button click.
I hope this helps.

Circular Buttons Group View

This is a snapshot from an app called "Noom Weight Loss Coach":
I was stunned by this circular view in this app. It can have some buttons (six in this snapshot but they can be more or less) and they can be rotated and have different colors.
I have a couple of questions:
Is there an existing library that provides this circular view?
If not, from where you would start if you want to build one? I am just interested in the circular look of the buttons. The rotation is not important.
I would recomend to use custom buttons. Derive your class from View, redefine onDraw() for drawing buttons state changes and listen for onTouch events.

Android: Dynamically vary number of buttons in a custom View

I am learning Android by implementing a clone of Mastermind. I want to break up the screen (or View) into three parts: the board with the users guesses so far and feedback, a series of control buttons, and a series of buttons to pick the color of the next peg.
My instinct is to do this is a modular way. The layout files uses nested LinearLayouts (I know not the most efficient thing to do, but this is an educational experience.)
The "board" is a custom View where I do a lot of drawing with a Canvas. The buttons on the bottom are declared in the layout file. Notice the orange strip to the right?
Right now that is another custom View. I want to add a variable number of buttons to that custom View based on the number of colors the player can choose from. A button press would select the color for the next peg in the player's guess. (There are 3 versions of the game, easy, medium, and hard each with a different number of colors.)
So, how do I add a variable number of buttons to the custom View I am creating? Or am I approaching this in the wrong way? Should I use a prebuilt layout? If so, which one and how could I dynamically change the number of buttons in the layout?
Thanks for any help. Cheers!
You can do this in two ways:
Using a predefined layout and setting initially the property
"visibility" of all the buttons to "gone", then programatically you
can set the "visibility" of the buttons you need to "visible". The
"gone" property makes the button invisible and also not consumes
space in the layout.
Adding dinamically buttons to the main layout, first you will have to
create or "inflate" them.
The second options is more powerful, but also more difficult if you are learning.

Categories

Resources