I've got next problem.
There is ImageView and button panel below it. I want ImageView to be as large as it needs, but if there is no enough space button panel should be present on screen and Imageview should be shrinked. What is the best solution for that?
Have a look to the android:layout_weight attribute.
What does android:layout_weight mean?
In case there is only 2 views in one layout (ie. Imageview and button) you can put both view in linearlayout and give value 1 for imageview weight layout (and doesn't give any value for the button)
Related
What's the best approach to have a layout with the following:
Spinner (default height)
WebView (all the space between the two views (Spinner and Button))
Button (default height)
How is it possible to specify the height of the WebView to take all the space between the two (i.e. if they are resized later, the WebView will automatically be adjusted).
Thanks!
Use a RelativeLayout as the parent layout. Align the Spinner to the top of the parent. Align the Button to the bottom of the parent. For the WebView, set the height as match_parent and then use the android:layout_below and android:layout_above attributes to make sure that it always lies between the two views.
(I would've given the code but I don't want to. You might just copy paste with zero learning.)
I have a relative layout, inside of it is a ImageView that is centered in its parent.
I want to add a share up to the image. If I put this attribute android:layout_alignTop="#+id/myImage" the button will appear over the image. I want to be upper than this.
Use
android:layout_below="#+id/shareId"
in your <ImageView> tag. Or
android:layout_above="#+id/myImage"
in your share's View tag depending on what works better for you. If this doesn't fix it then please post the xml and a picture of what you want/have if possible.
RelativeLayout Docs shows the different properties that can be used.
I have a simple problem but could not solve it with clean solution .
Suppose there are two textviews horizontally next to each other. I want to make them such that
second textview come just right to first and first should grow as much as it can without putting second textview out of screen (means second textview should always be visible).
Hint:you could always use Relative layout and try scaling them in the Graphical layout.
One solution would be, putting the two textViews in a LinearLayout and assign each textView a weight attribute. The LinearLayout divides the space between those views accordingly to the weight. For example if the first textview has weight 3 and the second has weight 2, the width for the first textview would be 3/5 superviews width and the second would be 2/5.
Another solution would be to put the second textView with a fixed width aligned right to the parent RelativeLayout, and the first view to be leftof the second view with the width 'match_parent'
How do I go about implementing a button bar with buttons of different shapes and heights? As an example (please excuse my poor drawing skills for this quick mockup):
The example bar has 3 buttons, with the middle button (3) a different shape and height than the other 2 buttons (1,2). The button bar will be a view that is included and merged into other views so as to seem to float on top of the parent view.
I was thinking of implementing buttons 1 and 2 into a layout, and then button 3 as another layout that I then merge with the first two button's layout.
like my previous comrades said, you need some kind of layout or container that can have a background (if you wish for button #3 to hoover above it) then use relative layout for mixing the two the disadvantage of this other than complexity is that youcannot relate to the other two buttons since they reside in a different layout.
More elegant solution may be to have a special background drawable that can:
have a method setCurrentHeight() that will specify the height the actual viewable section should have the rest will be filled with transparent color.
override it's own draw so just before it's drawing it will have a callback called, call back you can register yourself to.
then you can register the callback in your activity to take the current position of the #3 button and set the height accordingly, this way you are still with one layout with special drawable as background.
A customized LevelDrawable might do the trick.
I would layout this bar as follows:
A RelativeLayout as a container for the rest, with height set to wrap_content and alignparentbottom = true
An ImageView for the bar
2 Buttons with a transparent background (Button 1 and 2)
Button 3 is a custom Button with a custom Image of a trapezoid as background
So you will have a Layout similar to this:
<RelativeLayout
...>
<ImageView
.../>
<Button
... Button 1 />
<Button
... Button 2 />
<Button
... Button 3 />
</RelativeLayout>
I don't exactly know that this will work, and I can't test it, but you might give something like this a try; I believe it can all be done elegantly through XML.
Have a RelativeLayout (id:mainLayout) that will contain all of your views, wrap_content for both dimensions.
Have a blank View as your first child that will serve as your background bar
Set the View's background color/image to what you want; layout_width to fill_parent; layout_height to wrap_content; layout_alignTop="#id/leftButton"; layout_alignBottom="#id/leftButton".
Add an ImageButton for your center button (id:bigButton), wrap_content for both dimensions; layout_centerInParent="true".
Add an ImageButton for your left button (id:leftButton), wrap_content for both dimensions; layout_toLeftOf="#id/bigButton"; layout_centerInParent="true".
Add an ImageButton for your right button (id:rightButton), wrap_content for both dimensions; layout_toRightOf="#id/bigButton"; layout_centerInParent="true".
In my head, I believe this works, but I could be off. Regardless, something to think about, and I hope it helps you find a solution. :)
Better you can tablelayout with different button styles or relative layout for button "3"
I'm adding a TextView to a parent LinearLayout RelativeLayout programmtically. There is enough space for the text to be displayed horizontally but for some reason the text is displayed vertically. Does anyone know what's going on here?
It's really hard to say what is the problem without looking at your XML and layout code. In my experience, sometimes it happens when the parent layout has height set as WRAP_CONTENT and the views inside it have wrong weight config, for example one has weight but WRAP_CONTENT and the other MATCH_PARENT (Sorry, I don't remember the case exactly). I suggest you check your LayoutParams carefully or set a fixed width and height for the TextView to see what is the problem.
You can also post your code here so we can have a look at it