Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining layout_weight and maxHeight

Tags:

android

I'm quite new to Android programming and I'm stuck on an simple problem.

I have a basic homepage with some buttons and a logo. I use a LinearLayout and the layout_weight attribute to distribute all the components.

Here is my code :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="iut.project.findmeapp.MainActivity" >

    <ImageView
        android:id="@+id/activityMainAppLogoIv"
        android:layout_width="match_parent"
        android:layout_height="@dimen/null_height"
        android:contentDescription="@string/description_logo"
        android:layout_weight="1"
        android:maxHeight="@dimen/img_maxHeight"
        android:layout_margin="@dimen/img_margin"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/activityMainMyEventsBtn"
        android:layout_width="match_parent"
        android:layout_height="@dimen/null_height"
        android:layout_marginTop="@dimen/button_margin_vertical"
        android:layout_marginBottom="@dimen/button_margin_vertical"
        android:layout_weight="1"
        android:text="@string/my_events" />

    <Button
        android:id="@+id/activityMainMyContactsBtn"
        android:layout_width="match_parent"
        android:layout_height="@dimen/null_height"
        android:layout_marginTop="@dimen/button_margin_vertical"
        android:layout_marginBottom="@dimen/button_margin_vertical"
        android:layout_weight="1"
        android:text="@string/my_contacts" />

    <Button
        android:id="@+id/activityMainLastNotifBtn"
        android:layout_width="match_parent"
        android:layout_height="@dimen/null_height"
        android:layout_marginTop="@dimen/button_margin_vertical"
        android:layout_marginBottom="@dimen/button_margin_vertical"
        android:layout_weight="1"
        android:text="@string/last_notification" />

    <TextView
        android:id="@+id/activityMainCopyrightTv"
        android:layout_width="match_parent"
        android:layout_height="@dimen/null_height"
        android:gravity="center"
        android:layout_weight="0.5"
        android:layout_margin="@dimen/tw_margin"
        android:text="@string/copyright" />

</LinearLayout>

(Note : @dimen/null_height is 0dp)

My problem is that I want the image to scale with the size of the screen, but I don't want it pixelated : I have set a maximum height of 200px.

The following lines don't seem to work, because the image has no limit and totally ignores the maxHeight attribute.

android:layout_weight="1"
android:maxHeight="@dimen/img_maxHeight"

Actually it looks like this (example image) :

Homepage

It works perfectly, but when I set maxHeight to 10px for example, nothing changes.

How can I achieve this ? I highly prefer to use a LinearLayout.

Thank you in advance.

like image 915
Chostakovitch Avatar asked Apr 17 '15 09:04

Chostakovitch


People also ask

What does layout_weight mean in android?

LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen.

How do you add weight in relative layout?

RelativeLayouts do not support weight. You need to use a LinearLayout as a parent container if you want to use weights.

How to make LinearLayout in android studio?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .

Which attribute would you use to set the height of a view to be half of the screen android?

android:layout_height 50% of the screen size.


1 Answers

Thanks to Evripidis Drakos for pointing me in the right direction. Just need a check to only set the max height if it's actually required.

public class MaxHeightImageView extends ImageView {

public static final int MAX_HEIGHT_NOT_SET = -1;
public static final int INDEX_MAX_HEIGHT = 0;
private static final int[] STYLE_ATTRIBUTE_ARRAY = {
        android.R.attr.maxHeight,
};

private int myMaxHeight;

public MaxHeightImageView(Context context) {
    super(context);
    init(context, null);
}

public MaxHeightImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs);
}

public MaxHeightImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MaxHeightImageView(Context context, AttributeSet attrs, int defStyleAttr,
        int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int size = MeasureSpec.getSize(heightMeasureSpec);
    if(MAX_HEIGHT_NOT_SET != myMaxHeight && size > myMaxHeight) {
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(myMaxHeight, MeasureSpec.AT_MOST);

    }

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

private void init(Context context, AttributeSet attrs) {
    if(context != null) {
        TypedArray ta = context.obtainStyledAttributes(attrs,
                STYLE_ATTRIBUTE_ARRAY);
        myMaxHeight = ta.getDimensionPixelSize(INDEX_MAX_HEIGHT, 0);
    } else {
        myMaxHeight = MAX_HEIGHT_NOT_SET;
    }
}
}
like image 50
Stimsoni Avatar answered Oct 16 '22 18:10

Stimsoni