Android layout webview between another two views - android

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.)

Related

Bottom margin Android LinearLayout item in XML ignored, but works in Java code

I am developing an app with an activity with member reactions on a hike event. The reactions are the yellow "balloons" which are made using a LinearLayout. Each item is constructed from a XML file (listitem_deelnemerreactie.xml) which defines the layout for a reaction item. The top level of this layout file is a LinearLayout my itself.
I want some spacing between the separate elements, as well as some right margin. The most straightforward way to so this should be: setting a bottom and right margin on the top-level LinearLAyout element of the listitem_deelnemerreactie.xml layout file.
But setting the bottom margin on the LinearLayout has no effect on the vertical spacing, though the right margin does have an effect.
The only way to be able to set a vertical margin appears to be: setting is in the Java code, after attaching the inflated view to the container.
See the two images for the effect and the code.
Though setting the margins in the code is a working workaround, I still think it is strange this cannot be achieved in the XML. Why is the bottom margin attribute ignored while the right margin is not?
Any ideas?
Have you tried to set an android:padding="10dp" for example on your elements to spaced them ?

Android: RemoveAllItems from view doesn't refresh size

I have a ScrollView that contains a LinearLayout.
In code I add items to the LinearLayout and it nicely expands.
When I remove, in code, all the items from the LinearLayout, the LinearLayout stays the same size, it's empty though.
How do I get the ScrollView to reclaim the height that the LinearLayout used when it had items and shrink back the LinearLayout to use the least height as it is now empty?
I have tried (in C#):
linearLayout.RemoveAllViewsInLayout();
linearLayout.PostInvalidate();
scrollView.PostInvalidate();
Java or C# answers are fine.
I only have Java experience with Android, so I would try using:
linearLayout.requestLayout();
This should cause another layout pass, and assuming your LinearLayout is set to WRAP_CONTENT it will measure all the children and size itself to fit the smallest space that the children occupy (or minHeight if you have that set and it is larger).
This trick seems to work. The result is that the linearlayout ids resized and the scrollview as well:
linearLayout.RemoveAllViewsInLayout();
linearLayout.Visibility = ViewStates.Gone;
scrollView.SmoothScrollTo(0, 0);
linearLayout.Visibility = ViewStates.Visible;
There must be a better way, but this works.

Imageview floating left

How can I create ImageView floating to the left of the textView, something like html:
<div>
<img src="src" style="float:left"> Text here.....
</div>
You have two options:
With LinearLayout, setting the orientation to horizontal so the image is first and then come the rest.
With RelativeLayout, there you can indicate the position of a element relative to another with the attributes of the style android:layout_toLeftOft, android:layout_toRightOf, android:layout_below or android:layout_alignParentTop, ...
However, it is not so flexible as CSS for some actions and, for example, wrapping text around an image is not so easy to achieve.
There's really no concept of floating elements in Android, but you can easily put an image to the left of some text using the drawableLeft attribute of the TextView. Example:
Otherwise, for more complicated layouts, parent views determine how their children are laid out. For example, instead of the concept of a div which simply wraps it children and uses the float and display attributes of the children to determine how things look, Android has more complex parent views (ViewGroups as they are called, since that's the super class) to control things.
Check out the docs for LinearLayout and RelativeLayout for some examples.
The positioning of views depend on the kind of layout you use. In case if you are using a RelativeLayout and you want to float your view(in your case the ImageView) which is within this Relativelayout, you can use the attribute of the ImageView (android:layout_toLeftOf="") specifying the view id of your textview between the double quote.

Is there a way in Android XML to make a row of buttons with widths set to widest button?

Is there a way to declare a row of buttons in XML so that all the buttons have the same width, which is equal to the wrap_content width of the widest button? I'm familiar with the trick of setting all the widths to 0 and assign them all a weight of 1, but that won't work if the parent layout width is set to wrap_content. I don't want to set the parent width to fill_parent because I don't want the buttons stretched more than necessary.
The only way I can think of doing this is in code (either with onMeasure logic in each button that communicates with the other buttons or with a custom layout class).
I think you'd have to do this in code.
Creating a custom layout class would be the way to go. Override onMeasure() and make it look something like this:
Call setLayoutParams on all children to set their layout_widths to WRAP_CONTENT.
Call super.onMeasure()
Iterate child views to find the one with the biggest getMeasuredWidth().
Iterate all other child views calling setLayoutParams() with the widest pixel width.
Call super.onMeasure() again. :)
That should work but I won't stake my reputation on it... happy to help you further if it doesn't.
To get the buttons in a row in the XML you need to add the buttons with in a LinearLayout and change the orietnation to horizontal i.e.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
</LinearLayout>
As for getting the widest button and changing all the other buttons to match I am not sure as a guess you would have to have some sort of method within your activity to get the widest button and then programaticaly set all the other buttons to be the same.
Just find out what your widest button is, but it in a view with a horizontal width to match, and then use layout_width="match_parent" or "fill_parent" in < 2.3.
It'll make them all use the width assigned.
If you want to do it programatically, you need to iterate over all the sections, find the max, than iterate again and set it.

How to resize the RelativeLayout or any other Layout in Android?

I have a RelativeLayout defined in xml and I call the setContentView(R.layout.relativeLAyout) for displaying in Activity.
Now, if I want to resize this RelativeLayout then can it be done and if yes, then can someone let me know how?
The inner components can be resized relatively to the parent.
Is this actually possible?
Regards
Sunil
well if you use Eclipse, in the XML file, there at the bottom just switch to Graphical Layout.
and from there you can drag and drop payouts, Buttons etc :D
then in properties you can select add the pixels (200px ) on width/height
As to the inner components not resizing, make sure that both android:layout_width and and android:layout_height are set to fill_parent - then they should take up the space given to the parent.

Categories

Resources