I need to create the same buttons like in the picture below. Button
's must be with the text inside.
When I was making XML
layout I encountered with a problem of Button
's touch area. Each next button is cover previous button with a rectangular Button
's area.
Is it right to place hexagons in the XML markup like I done, to implement hexagons like in picture? Please, help me to solve problem with touch area and if possible tell me how to create a layout right, because I'm not sure that since I'm doing is correctly.
Here is part of my test layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="?attr/actionBarSize">
<Button
android:id="@+id/button1"
android:layout_width="130dp"
android:layout_height="134dp"
android:background="@drawable/hexagon_shape_img"
android:text="Home page"
android:textSize="@dimen/small_text" />
<Button
android:id="@+id/button2"
android:layout_width="130dp"
android:layout_height="134dp"
android:layout_marginLeft="65dp"
android:layout_marginTop="-20dp"
android:background="@drawable/hexagon_shape_img"
android:text="Tavern"
android:textSize="@dimen/small_text" />
</LinearLayout>
Ok, this turned out to be more complicated than I thought. The basic problem is that the highest Z-ordered button always gets the touch event, but doesn't pass it on the the lower Z-orders even if it doesn't dispose of the event. To get both buttons to see the event, a work-around is needed. Here's the basic approach:
1: Create a container in the Fragment/Activity that currently holds your hex buttons
2: Create a Fragment that contains the buttons and another button on top of all of them with alpha=0
3: Add a getOverlay():View method that returns that alpha=0 button
4: implement a hitTest():Button method in the fragment
5: In the main Activity/Fragment, set up listeners to handle the touch events of the overlay button
I made (and tested) an Application to demonstrate the concept. I'm going to leave the hitTest to you as it's rather tedious
activity_main.xml:
fragment_buttons.xml:
MainActivity.java:
Buttons.java:
Good luck.
ETA: Sample hitTest