0

So I have a Java class that creates a dynamic relative layout and adds it to a linear layout on a main activity. My buttons show up on top of each other in the android emulator and I am not sure why. From what I have gathered researching I have everything right.

public class Feed {
private String status;
private Uri image;
private boolean imageContent;

public Feed(String status) {
    this.status = status;
    imageContent = false;
}

public Feed(String status, Uri image) {
    this.status = status;
    this.image = image;
    imageContent = true;
}

public void showFeed(Context page, LinearLayout main) {
    RelativeLayout sample = new RelativeLayout(page);
    sample.setBackgroundResource(R.drawable.sample);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);


    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    final Button like = new Button(page);
    like.setId(1);
    like.setText("like");
    like.setTextColor(page.getResources().getColor(R.color.yellow));
    like.setBackgroundColor(Color.TRANSPARENT);
    sample.addView(like, params);

    final Button comment = new Button(page);
    comment.setId(2);
    comment.setText("comment");
    comment.setTextColor(page.getResources().getColor(R.color.yellow));
    comment.setBackgroundColor(Color.TRANSPARENT);
    params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                                              RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    params.addRule(RelativeLayout.ALIGN_RIGHT, like.getId());
    comment.setLayoutParams(params);
    sample.addView(comment);

    final Button pin = new Button(page);
    pin.setId(4);
    pin.setText("pin");
    pin.setTextColor(page.getResources().getColor(R.color.yellow));
    pin.setBackgroundColor(Color.TRANSPARENT);
    params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                                              RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    sample.addView(pin, params);


    final Button share = new Button(page);
    share.setId(3);
    share.setText("share");
    share.setTextColor(page.getResources().getColor(R.color.yellow));
    share.setBackgroundColor(Color.TRANSPARENT);
    params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                                              RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    params.addRule(RelativeLayout.ALIGN_LEFT, pin.getId());
    params.setMargins(0, 10, 0, 0);
    sample.addView(share, params);

    TextView statusText = new TextView(page);
    statusText.setId(5);
    statusText.setTextColor(page.getResources().getColor(R.color.white));
    statusText.setText(status);
    params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_TOP, like.getId());
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    sample.addView(statusText, params);

    if(imageContent) {
        ImageView statusPhoto = new ImageView(page);
        statusPhoto.setId(5);
        statusPhoto.setImageURI(image);
        params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_TOP, statusText.getId());
        sample.addView(statusPhoto, params);

    }
    main.addView(sample, params);
}

}

1 Answer 1

1

Create a new params object for each view. Using the same one for all of them causes you to change the values for each view every time, because they're stored by reference.

Sign up to request clarification or add additional context in comments.

1 Comment

I created a params1 object and set all the rules and it puts it on top of the other button(comment on top of like). It is weird because when I don't set any new rules it puts it in the top left corner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.