Problems inflating a custom LinearLayout into a custom class that extends LinearLayout

68 Views Asked by At

I'm trying to inflate a LinearLayout containing a few different Textviews and a Button into a custom class that extends LinearLayout. Here is the XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#173BBC"
android:gravity="center_vertical"
android:minWidth="200dp"
android:minHeight="40dp"
android:orientation="horizontal"
android:padding="10dp"
android:visibility="visible">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dp"
    android:layout_weight="1"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView12"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/compte" />

        <TextView
            android:id="@+id/textViewCompteT"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView14"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/valeur" />

        <TextView
            android:id="@+id/textViewValeurT"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView" />
    </LinearLayout>

</LinearLayout>

<Button
    android:id="@+id/buttonDeleteTransaction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:drawableTop="@android:drawable/ic_menu_delete"
    android:minWidth="48dp"
    android:paddingLeft="30dp"
    android:paddingTop="10dp"
    android:paddingRight="30dp"
    android:paddingBottom="0dp"
    android:textSize="0sp"
    app:cornerRadius="20dp"/>

</LinearLayout>

And here is my class: (please don't take care about the names of the variables, they are bad ;) )

public class TransactionTwo extends LinearLayout {
private Context context;
private LinearLayout container;
private LinearLayout content;
private LayoutParams layoutParams;
private LayoutParams contentLayoutParams;


public TransactionTwo(Context context, Activity parent){
    super(context);
    activity = (Details) parent;
    //here i define my layouts to make them have the fine shape.

    layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(10, 10, 10, 10);
    contentLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    this.setLayoutParams(layoutParams);
    this.setOrientation(LinearLayout.VERTICAL);
    this.setBackgroundColor(Color.RED);
    container = new LinearLayout(context);
    container.setOrientation(LinearLayout.VERTICAL);
    container.setLayoutParams(contentLayoutParams);

}

public void createViews(){
    //here i try to force the display of the inflated layout (which doesn't appear on screen)
    content = (LinearLayout) inflate(context, R.layout.modele_transaction2, container);
    content.setVisibility(View.VISIBLE);
    content.setLayoutParams(contentLayoutParams);
    container.setMinimumHeight(100);

    this.addView(container);
    

    //then i find a few textviews and set their text (which works correctly even if i can't see it)...
}

@Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {

}

Everything seems to work correctly (at least Android doesn't raise any error), but the only trouble is that I can see the LinearLayout "TransactionTwo" having the size of the LinearLayout that I inflate in it, but I can't see the inflated one... And when I ask for the height of content and container it always answer 0. So it is as if the inflated layout was only giving its size without appearing...

If anyone knows what is wrong in my code, that would be really appreciated because I'm stucked on this issue for a long time now ;)

Thank you!

1

There are 1 best solutions below

0
Bat BRT On

In fact it might have been a bug of Android Studio, because I made a copy of my java class and it worked this time...