I have a LinearLayout that contains multiple ImageButtons and TextViews. Something like this:
+---------+-----------+-------------------+-------+ <- LinearLayout
| i | i | text here | i |
+---------+-----------+-------------------+-------+
These items are hard to tap. I want to increase their tappable area. It seems as if TouchDelegate is the proper way to do this.
However, view.getParent().setTouchDelegate(new TouchDelegate(r, child)) is a 1:1 mapping, not a 1:Many mapping. So how would one solve this problem when a View has multiple children that each need to be tappable beyond their bounds?
Related
I have three views that are intended to look as follows:
+-----++-------------------------+
| 1 || |
+-----+| 3 |
+-----+| |
| 2 || |
+-----++-------------------------+
So far, so good. However...
Sometimes (3) is very small, and I want it centred in the vertical space used for (1) and (2).
+-----+
| 1 |+-------------------------+
+-----+| 3 |
+-----+| |
| 2 |+-------------------------+
+-----+
Other times, (3) is large, and I want (1) to align with the top (3), and for (2) to align with the bottom of (3):
+-----++-------------------------+
| 1 || |
+-----+| |
| 3 |
| |
+-----+| |
| 2 || |
+-----++-------------------------+
I have tried:
an outer Relative layout: (1) and (2) overlap in the "small-3" case
a linear layout containing (1) and (2) (with and without weights): the alignment at top/bottom does not work in the "big-3".
an outer linear layout (with various height settings): I can not get case (2) and (3) to work with the same settings.
To give a little context, (1) and (2) are buttons and (3) is a text block of varying size.
At this point I assume I am missing some very basic setting (or widget) that will make this work as intended.
Note: I have not included source code because there have, literally, been over a dozen different configurations tried and none worked.
Just asking the question helped...pretty sure the answer is to use ConstraintLayout.
Use ConstraintLayout and use the design tab instead of writing the code yourself when using the ConstraintLayout, its pretty easy to achieve what you want. Dont use RelativeLayout or LinearLayout for this.
Say we have the below vertical LinearLayout (WRAP_CONTENT on both layout width and height) with two childs Button with varying height.
{ LinearLayout
* empty space *
| |
| Button 1 |
|__________|
|Button 2| }
Why animating Button 1 "y" using ObjectAnimator, producing this:
{ LinearLayout
| |
| Button 1 |
|__________|
* empty space *
|Button 2| }
hence leaving a space there instead of moving Button2 below Button1 as well?
As for the code (nothing fancy): ObjectAnimator.ofFloat(button1, "y", delta).start();
I tried using ValueAnimator (with setY in OnAnimationUpdate() and then upon seeing the same thing,
Setting LinearLayout to invalidate during OnAnimationUpdate() also did the same thing.
tried with TranslateAnimation with fillEnabled and fillAfter being true. Both still producing this behavior.
What am I missing here? I have used LayoutAnimation but so far, I only used to animate them if a child is added or removed. Not if their bounds change.
Can anyone clue me in to the right method/terms here (or the correct way to achieve this effect)?
Note that I also actually wanted Button1 to be clipped by the parent container, if I animate beyond the parent bounds (though I not sure if it makes any difference for Button2). English isn't my first language so pardon the grammar or terms. Thanks.
I have a UI Design from my designer, and it exists of a background map with several buttons on it, positioned non-linear all over the map. Currently I position them in a RelativeLayout that is as large as the map, and use margin-left and margin-top etc in dip.
This works ok, but I also need to account for users with very small screens, that cause the map to scale down. My relative layout scales with it, but the margin values ofcourse not.
So I am wondering, how should I do this? I would prefer to layout these buttons using percentages like
left="30%"
top="50%"
Is there anything in Android that makes such a thing possible? Otherwise I have to come up with a custom layout class for that.
Visual Representation: (Ofcourse they don't actually are on 6 lines, and partially overlap in x or y position). It's actually a real (abstract) map of a building with location markers that you can press as buttons.
-------------------------
| x x |
| x |
| |
| x |
| x |
| x x|
-------------------------
Here is a complicated way that does not require a custom ViewGroup. Suppose you want a button at left 30%, top 40%
FrameLayout
View with background, match parent
LinearLayout orientation=horizontal, match parent
View layout_width=0dp, layout_weight=30, height=match_parent
LinearLayout orientation=vertical, width=0dp, weight=70, hieght=match
View layout_height=0dp, layout_weight=40, width=match_parent
FrameLayout layout_height=0dp, layout_weight=60
Button
I use Dimension resource files put in the relevant layout- buckets so I can change margins/paddings/sizes depending on device size.
(http://developer.android.com/guide/topics/resources/more-resources.html#Dimension)
(Storing dimensions in xml file in Android)
I came up with this very annoying layout issue.
Basically the UI component I'm looking for is not that complex, but the behavior needs to be exact.
In all cases I have 2 or 3 texts I need to layout horizontally.
In best case scenario all of these 3 fit in one line, so layout is not a problem.
| text 1 < empty space> | text 2 | < empty space> text 3 |
But when I hit an issue that text1 needs more than 1 lines, I should set that column 0 stretchable for TableLayout at the same time text2 might also need more than 1 lines so that column also needs to be stretchable and I would need the result to be like this:
| text1 many lines taking 40% | text2 takes 20% | text 3 many lines takes 40% |
And when this issue comes I haven't found a way to set TableLayout shrink and stretch columns. And also problem is that TableRow can contain 2 or 3 children.
I did few hour testing playing with LinearLayout and onGlobalLayout I calculated how much which is taking and adjusted weights of those views, but that's very complex and not working always, width on view3 sometimes just returned zero when view1 was very long.
I'm looking for some tips where to start here, should I do layouts in xml and inflate them instead programmically making them etc.
Okay I found out the solution, column 0 shrinkable, column 1 stretchable and tablerow span 2 for tablerow child number 2 if only 2 childs.
I need to make a heading with an ImageView and TextView that are centered together, like the following diagram.
--------------------------------
| |
| ----------- ---------- |
| -ImageView- -TextView- |
| ----------- ---------- |
| |
| -Other Content- |
| |
The ImageView must be scaled to the height of the TextView and together they must be centered as if they were one widget. Any ideas on how to accomplish this?
EDIT: you can use TableLayout for it, put the textview and Imagview in 2 columns of tablerow, then your imageview is adjust acccording to your textview's columns height.
use your textview's height as wrap_content and imageview's height fill_parent
If they must look as a single widget, then wrap them with a horizontal LinearLayout, and actually do treat it like a single widget. Centering this new 'widget' will do what you want.
Use RelativeLayout. Align ImageView's top and bottom with TextView's top and bottom. Set Top margins for textview. Set center horizontally to true in parent layout.