Jono
Jono

Reputation: 18108

Custom component Android LinearLayout not visible

Custom LinearLayout component view not displaying content

Hi i have created a custom component view that extends LinearLayout and when i use this view on my xml, it doesnt become visible on the screen.

Here is the main view that is using this custom component

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/contentLinearLayout"
    android:layout_width="@dimen/width"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ABB0B6" />

    <com.jon.ui.EnableView
        android:id="@+id/enableContainter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

Here is the EnableView custom component code:

package com.jon.ui;



/**
 */
public class EnableView extends LinearLayout {

    private static View enableView;

    private static Button btnContactlessInfoAction;
    private static LinearLayout btnMakeContactlessAction;
    private static View makeContactlessView;


    private static View.OnClickListener enableContaclessBtnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    };

    private static View.OnClickListener contactlessHelpBtnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    };
    private static ViewGroup viewContainer;
    private final Context mContext;

    public EnableContactlessView(Context context) {
        super(context);
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.custom_component, this);
        setOrientation(LinearLayout.VERTICAL);
    }

    public EnableContactlessView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.custom_component, this);
        setOrientation(LinearLayout.VERTICAL);
    }


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

    }

    public void initiate() {

           btnMakeContactlessAction = (LinearLayout) this.findViewById(R.id.btn);
            btnContactlessInfoAction = (Button) this.findViewById(R.id.info_btn);
            btnContactlessInfoAction.setOnClickListener(contactlessHelpBtnClick);


    }




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

    }
}

Below is the View's layout xml i use to inflate from EnableView class called custom_component.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/top_margin"
    android:gravity="center_horizontal"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="9dp"
        android:layout_weight="3"
        android:background="@color/button_colour"
        android:gravity="center"
        android:paddingBottom="9dp"
        android:paddingTop="9dp">


        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="15dp"
            android:src="@drawable/icon_symbol" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="activate and enable"
            android:textColor="@android:color/white" />
    </LinearLayout>


    <Button
        android:id="@+id/info_btn"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_gravity="center_vertical|end"
        android:background="@drawable/icon" />


</LinearLayout>

Upvotes: 0

Views: 221

Answers (1)

Francesc
Francesc

Reputation: 29260

Do not override onLayout if you're leaving it empty, remove that function. onLayout is used to measure the parent and its children, if you leave it empty nothing is getting measured.

Upvotes: 3

Related Questions