Adding Layout Weight dynamically - android-layout

For my project I want 30 images in my page, and I want to arrange them in such a way that there are 3 images in one horizontal linear layout.
Since I cannot use density pixels as it may look different on different divices, I want to use layout_weights. The way we write in our XML files :
<ImageButton
android:id="#+id/my_image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
**android:layout_weight="0.33"**
android:background="#android:color/transparent"
android:src="#drawable/ic_launcher" />
I want to know what will be the Exact Equivalent of the XML code in Java Language..

You can set the weight using the LayoutParams of your view (the last param)
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
myView.setLayoutParams(params);

Related

HorizontalGridView from lean back i want to keep item in center

I have list of item, i am using HorizontalGridView to display that item, i want to keep the HorizontalGridView in center of the screen but its not working.
Following is my code:
<android.support.v17.leanback.widget.HorizontalGridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:layout_centerHorizontal="true"/>
But it occupies complete width and height even-though there is less item
Note: I am developing for android tv using leanback library
It's better to use RecyclerView if you want to center and wrap content. Actually, HorizontalGridView extends RecyclerView, so it will be easy to change to:
<androidx.recyclerview.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:layout_centerHorizontal="true"/>

Make same space between buttons in a HorizontalScrollView

This is a solution in order to add same space between views in the layout:
make same space between button in linearlayout
But let say we have many items and we need to put them in HorizontalScrollView for handset devices.
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<LinearLayout
android:id="#+id/shareLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnRequestAPrescriptionRefill"
android:layout_marginTop="10dp" >
<Button
android:id="#+id/btnFacebook"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/btn_facebook" />
<View
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1" />
...
</LinearLayout>
</HorizontalScrollView>
This solution works fine for handsets but in the tablet where we have a big screen size, the same space between buttons is not working. Buttons are just sticking together without no space that we created by:
<View
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1" />
Is there any solution?
A scroll view adapts it's size to its content view (the linear layout) but when the screen is large enough you want it the other way around, you want the content view to adapt its size to the scroll view. This is a little contradictory.
The only way to do this is specifying a minWidth property for your LinearLayout.
android:minWidth="200dp"
This is a pain in the butt because you probably need to set it at runtime, or have many versions of your XML for different screen resolutions.
Maybe another approach for large screen resolutions would be providing a different XML without the scrollview.

what element type and layout for should I use for app landing page navigation buttons?

I'm pretty new to Android Development, i'm following some Youtube tutorials currently. I'd like to create a basic mobile tourist application, but i'm stuck as to how to create the main menu layout.
Each Favorite 1 - 3, and Options 1 - 9 should be icons. (I'm not sure which form element should be used for this).
QuestionHow can I create the favorite, and option icons in the layout.xml file, what element is best suited, and what layout(s) should I use?
In HTML, i'd create a table with 3 columns, 4 rows and set the table width and height to be 100%, and make the td valign and align central.
Here you could make horizontal linearLayouts of vertical LineraLayouts of Image+TextView pairs.
The second range linear layouts would have
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
Inside second range layouts you could use for image and text/view:
...for images:
android:layout_width="match_parent"
android:layout_weight="3"
android:layout_height="0dp"
... for texts
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
And the screen is nicely divided.

How can I get ImageView to scale an image and align top right?

I've spent 3 hours trying to get this to work without success so heopfully someone here can help.
I have a FrameLayout that is being loaded via inflate from an XML file and in it I have the following layout:
<LinearLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:id="#+id/element_image"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_marginBottom="10dip"
android:layout_marginLeft="10dip"
android:scaleType="fitStart"
android:background="#888888" >
</ImageView>
<Button android:id="#+id/element_button"
style="#style/Button"
android:layout_gravity="right"
android:text="#string/element_button_label"
/>
</LinearLayout>
I have an image loaded from a BLOB which I want to display in this ImageView. In the GalleryAdaptor I load the image like this:
ImageView image = (ImageView) v.findViewById(R.id.element_image);
byte [] bitmapData = cursor.getBlob(4);
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length);
image.setImageBitmap(bitmap);
When I use android:scaleType="fitStart" in the layout above the image is not scaled to the height or width of the ImageView but is about a third of it's width (which I can see from the grey background I have given it) and is centered.
If I use android:scaleType="fitXY" the image height is scaled but the width is not. Again the image is centered.
Can someone give me some pointers on:
How to get the image to scale both height and width wise
How to align the image top right
Answering my own question here - but looks like this was just an issue with the source image resolution. Android (2.2 at least) does not seem to want to scale these images up when fitStart or fitEnd is used - but when fitXY is used it does scale them.

Where is a reference to the android XML UI layout?

I am looking for a spec or reference of all the possible options for the various XML layout attribute settings that typically come with an android UI. Google seem to be good at burying it. This is similar to this question but remains in-effectively answered.
Such as what are my options available to me for the TextView layout_width definition ? There must be a complete definition published ... somehwere....
layout_* attributes aren't directly part of the view they appear on, which is why you won't find them in TextView's documentation. (TextView is not a ViewGroup.) They are arguments to the parent view, also known as LayoutParams. Take a look at the "Known Subclasses" sections at the top of the linked page for a list of them. They're instructions about how a ViewGroup should arrange each child view, and each parent type can recognize different ones depending on what kinds of layout options it supports.
For example, LinearLayout.LayoutParams supports the android:layout_weight parameter. Children of a LinearLayout can specify weight to request a proportion of the remaining space after all children have been measured. You can give equal weight to two sibling TextViews with a base width of 0 to give them each half of the available space within the parent.
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello" />
<TextView android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="World" />
</LinearLayout>
Normally developer.android.com is your site. Maybe this helps:
http://developer.android.com/reference/android/view/View.html
If you use Eclipse, then the autocomplete suggestions may help you as well in adding the right parameter.
...and the options you have for layout_width are
wrap_content (as large as the content of the View)
fill_parent (extends to the whole size - width or height - of its parent)
Layout parameters are pretty well described in the documentation for ViewGroup.LayoutParams and its subclasses. For the truly strong of heart, you can always browse the source for attr.xml.

Resources