Tsikon
Tsikon

Reputation: 334

Views within custom layout won't show

I'm creating a custom linear layout to hold two image views. I first thought that the layout is not showing at all but I then set its background to black to check if the layout is being inflated and it did. Then understood that the problem is the image views, they simply not showing.

Thanks in advance :-)

This is the class:

public class LoginIcons extends LinearLayout {

    private ImageView mImageViewLogo;
    private ImageView mImageViewIcons;
    private View mView;
    boolean test = false;

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

    public LoginIcons(Context context) {
        super(context);
        init();
    }

    private void init() {

        setOrientation(LinearLayout.VERTICAL);
        mImageViewLogo = new ImageView(this.getContext());
        mImageViewIcons = new ImageView(this.getContext());
        mView = new View(getContext());

        LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        mImageViewLogo.setLayoutParams(params);

        params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.topMargin = (int)LocalSettingsHelper.dpToPx(getContext(), 20);
        mImageViewIcons.setLayoutParams(params);

        params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.topMargin = (int)LocalSettingsHelper.dpToPx(getContext(), 10);
        mView.setLayoutParams(params);

        mImageViewLogo.setImageResource(R.drawable.splash_logo_placeholder);        
        mImageViewIcons.setImageResource(R.drawable.login_icons);

        mImageViewIcons.setBackgroundColor(Color.BLACK);
        mImageViewLogo.setBackgroundColor(Color.BLACK);

        addView(mView);
        addView(mImageViewLogo);
        addView(mImageViewIcons);

    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        setViewsDimensions();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    private void setViewsDimensions() {

        LinearLayout parent = (LinearLayout) mImageViewLogo.getParent();

        int screenWidth = LocalSettingsHelper.getScreenWidth(getContext());

        // 0.375 percent
        int imgDim = (int) ((double) screenWidth * 0.32);
        int iconsWidth = (int) ((double) screenWidth * 0.5);

        LinearLayout.LayoutParams params = (LayoutParams) mImageViewLogo
                .getLayoutParams();
        params.width = imgDim;
        params.height = imgDim;
        mImageViewLogo.setLayoutParams(params);

        params = (LayoutParams) mImageViewIcons.getLayoutParams();
        params.width = iconsWidth;
        mImageViewIcons.setLayoutParams(params);
    }
}

And this is the xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/HeaderBackground"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button_ok"
        style="@style/LoginOkButtonStyle"
        android:background="@drawable/blue_button_background_selector"
        android:text="@string/ok" />

    <com.example.me.entities.views.LoginIcons
        android:id="@+id/loginIcons"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </com.example.me.entities.views.LoginIcons>

</LinearLayout>

Upvotes: 1

Views: 166

Answers (1)

Tsikon
Tsikon

Reputation: 334

It seems that the View object acted like match_parent and had height of nearly 400 pixels. This caused the image views to drop outside the boundaries of the screen. Once I removed the View the image views turned up.

Upvotes: 0

Related Questions