mike.b93
mike.b93

Reputation: 1999

Programmatically create Linear Layout with Style

I'm trying to create a LinearLayout programmatically but some things are just not working as expected.

This is the code:

LinearLayout djCard = (LinearLayout)getLayoutInflater().inflate(R.layout.cardtemplate, null);

                    ImageView djimage = new ImageView(this);
                    djimage.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 2));
                    djimage.setAdjustViewBounds(true);
                    djimage.setScaleType(ImageView.ScaleType.FIT_START);
                    bmpDj = BitmapFactory.decodeStream(am.open("djs/horger.jpg"));
                    djimage.setImageBitmap(bmpDj);

                    RobotoTextView djtext = new RobotoTextView(this);
                    djtext.setText(R.string.title_horger);
                    djtext.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 8));
                    djtext.setGravity(Gravity.CENTER_VERTICAL);
                    djtext.setTypeface(RobotoTypefaceManager.obtaintTypeface(this,12));
                    djtext.setTextSize(R.dimen.textSizeLarge);


                    djCard.addView(djimage);
                    djCard.addView(djtext);
                    container.addView(djCard);

cardtemplate:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="-7dp"
    android:clickable="true"
    android:orientation="horizontal"
    style="@style/nowCardStyle"/>

And look at this screenshot: The above card is what I have in xml, the other is my dynamically created card. Even the TextView (Custom one) is not showing...

img

This is my card layout in xml (which is what i want to have):

<LinearLayout
            android:id="@+id/djCard1"
            style="@style/nowCardStyle"
            android:clickable="true"
            android:padding="-7dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/djImg1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:scaleType="fitStart"
                android:adjustViewBounds="true"/>

            <com.mikebdev.douala.widget.RobotoTextView
                android:id="@+id/djText1"
                android:layout_gravity="center"
                android:layout_marginLeft="@dimen/activity_vertical_margin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:typeface="roboto_condensed_light"
                android:textSize="@dimen/textSizeLarge"
                android:layout_weight="8"/>

        </LinearLayout>

Upvotes: 0

Views: 481

Answers (1)

fifarunnerr
fifarunnerr

Reputation: 617

This might not be the answer to your question, but it might solve you problem:

You inflate cardtemplate.xml with the LinearLayout, and you try to add the ImageView and RobotoTextView programmatically. Why don't you inflate the second XML file you provided, with the ImageView and RobotoTextView included as children and retrieve those children from the parent?

For example:

  ImageView djimage = (ImageView) djCard.findViewById(R.id.djImg1);
    RobotoTextView djtext = (RobotoTextView) djCard.findViewById(R.id.djText1);

Then you could just inflate the layout from XML, and skip the hassle with trying to create a layout programmatically :)

Upvotes: 1

Related Questions