Newbie
Newbie

Reputation: 1714

align View programmatically doesn't work

Here is my layout:


and here is my function which create the layouts and views

    public void createUI(int count)
    {
        ScrollView scrollView = new ScrollView(this);
        ScrollView.LayoutParams svp = new ScrollView.LayoutParams(
                ScrollView.LayoutParams.WRAP_CONTENT, ScrollView.LayoutParams.WRAP_CONTENT);
        scrollView.setLayoutParams(svp);

        LinearLayout linearLayout = new LinearLayout(this);
        LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setLayoutParams(llp);

        int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 110, getResources().getDisplayMetrics());
        int ivSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, getResources().getDisplayMetrics());

        for (int i = 0; i < 2; ++i)
        {
            RelativeLayout relativeLayout = new RelativeLayout(this);
            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT, height);
            relativeLayout.setLayoutParams(rlp);

            ImageView imageView = new ImageView(this);
            imageView.setId(R.id.txtImage);
            imageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.myborder));
            RelativeLayout.LayoutParams imagelp = new RelativeLayout.LayoutParams(ivSize, ivSize);
            imagelp.addRule(RelativeLayout.ALIGN_PARENT_START);
            imageView.setLayoutParams(imagelp);


            TextView txtName = new TextView(this);
            imageView.setId(R.id.txtName);
            txtName.setText("Abu Baka");
            RelativeLayout.LayoutParams namelp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            namelp.addRule(RelativeLayout.RIGHT_OF, R.id.txtImage);
            txtName.setLayoutParams(namelp);

            relativeLayout.addView(imageView);
            relativeLayout.addView(txtName);
            linearLayout.addView(relativeLayout);
        }
        scrollView.addView(linearLayout);
        setContentView(scrollView);
    }

I set the imageView id programmaticallyimageView.setId(R.id.txtImage); and set rule to the txtNamenamelp.addRule(RelativeLayout.RIGHT_OF, R.id.txtImage);, but it not working.
Here is my output:
enter image description here

The word Abu Baka suppose to be right of the imageView, but it's not...How to solve this? or is there any wrong with my code?
This is my ids.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="txtImage" type="id"/>
    <item name="txtName" type="id"/>
    <item name="txtSince" type="id"/>
    <item name="txtComment" type="id"/>
</resources>

Upvotes: 0

Views: 177

Answers (3)

Bharatesh
Bharatesh

Reputation: 9009

This line worked for me, use

namelp.addRule(RelativeLayout.RIGHT_OF, imageView.getId());

instead of

namelp.addRule(RelativeLayout.RIGHT_OF, R.id.txtImage);

FYI : As I was testing with 2.3 version, made another change (for u not required I guess)

imagelp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

Upvotes: 0

Gagan
Gagan

Reputation: 1497

I would have done this like below:

RelativeLayout.LayoutParams namelp = (RelativeLayout.LayoutParams) txtName.getLayoutParams();
namelp.addRule(RelativeLayout.RIGHT_OF, R.id.txtImage);
txtName.setLayoutParams(namelp);

Also, you have missed setting the id for txtName.

Upvotes: 0

Thomas R.
Thomas R.

Reputation: 8073

When you add your child views to its parent, invoke the method with the layout params:

relativeLayout.addView(imageView, imageView.getLayoutParams());
relativeLayout.addView(txtName, , txtName.getLayoutParams());
linearLayout.addView(relativeLayout, relativeLayout.getLayoutParams());

Be sure that you already set them on the views or add new layout params in addView() method.

Upvotes: 0

Related Questions