Allan Macmillan
Allan Macmillan

Reputation: 1491

Relative Layout not displaying pictures correctly

I have a RelativeLayout with 4 ImageViews that should have 2 on top, one on each side of the Layout, and 2 ImageViews below the first two, one on each side of the screen.

I have 2 problems, the two ImageViews that are supposed to be below, do not go below. And The 2 ImageViews that are aligned to be on the right of the screen should be one on top of the other but seem to be about 25dp out.

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);

    RelativeLayout tl = (RelativeLayout)v.findViewById(R.id.l1);
    tl.setBackgroundColor(Color.WHITE);

    ImageView imageTopL = new ImageView(getActivity());
    imageTopL.setImageResource(R.drawable.bell_dl_256);
    imageTopL.setPadding(50, 0, 0, 0);
    imageTopL.setId(0);

    ImageView imageBottomL = new ImageView(getActivity());
    imageBottomL.setImageResource(R.drawable.bell_dl_256);
    imageBottomL.setPadding(50, 0, 0, 0);
    imageBottomL.setId(1);

    ImageView imageBottomR = new ImageView(getActivity());
    imageBottomR.setImageResource(R.drawable.bell_dr_256);
    imageBottomR.setPadding(0, 0, 50, 0);
    imageBottomR.setId(2);

    ImageView imageTopR = new ImageView(getActivity());
    imageTopR.setImageResource(R.drawable.bell_dr_256);
    imageTopR.setPadding(0, 0, 50, 0);
    imageTopR.setId(3);

    tl.addView(imageTopL);
    tl.addView(imageTopR);
    tl.addView(imageBottomL);
    tl.addView(imageBottomR);

    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)imageTopL.getLayoutParams();
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

    params = (RelativeLayout.LayoutParams)imageTopR.getLayoutParams();
    params.addRule(RelativeLayout.RIGHT_OF, imageTopL.getId());
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

    params = (RelativeLayout.LayoutParams)imageBottomL.getLayoutParams();
    params.addRule(RelativeLayout.BELOW, imageTopL.getId());
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);


    params = (RelativeLayout.LayoutParams)imageBottomR.getLayoutParams();
    params.addRule(RelativeLayout.BELOW, imageTopL.getId());
    params.addRule(RelativeLayout.RIGHT_OF, imageBottomL.getId());
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

    return v;

Upvotes: 0

Views: 257

Answers (1)

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

// try this
  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);

        RelativeLayout tl = (RelativeLayout) v.findViewById(R.id.l1);
        tl.setBackgroundColor(Color.WHITE);

        ImageView imageTopL = new ImageView(this);
        imageTopL.setId(1);
        imageTopL.setImageResource(R.drawable.ic_launcher);
        RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        imageTopL.setAdjustViewBounds(true);
        params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        params1.setMargins(50,10,0,0);
        imageTopL.setLayoutParams(params1);
        imageTopL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(ImageTextListViewActivity.this,"Top Left",Toast.LENGTH_SHORT).show();
            }
        });


        ImageView imageTopR = new ImageView(this);
        imageTopR.setId(2);
        imageTopR.setImageResource(R.drawable.ic_launcher);
        imageTopL.setAdjustViewBounds(true);
        RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        params2.setMargins(0,10,50,0);
        imageTopR.setLayoutParams(params2);
        imageTopR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(ImageTextListViewActivity.this,"Top Right",Toast.LENGTH_SHORT).show();
            }
        });


        ImageView imageBottomL = new ImageView(this);
        imageBottomL.setId(3);
        imageBottomL.setImageResource(R.drawable.ic_launcher);
        imageTopL.setAdjustViewBounds(true);
        RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        params3.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        params3.addRule(RelativeLayout.BELOW, imageTopL.getId());
        params3.setMargins(50,10,0,0);
        imageBottomL.setLayoutParams(params3);
        imageBottomL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(ImageTextListViewActivity.this,"Bottom Left",Toast.LENGTH_SHORT).show();
            }
        });

        ImageView imageBottomR = new ImageView(this);
        imageBottomR.setId(4);
        imageBottomR.setImageResource(R.drawable.ic_launcher);
        imageTopL.setAdjustViewBounds(true);
        RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        params4.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        params4.addRule(RelativeLayout.BELOW, imageTopR.getId());
        params4.setMargins(0,10,50,0);
        imageBottomR.setLayoutParams(params4);
        imageBottomR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(ImageTextListViewActivity.this,"Bottom Right",Toast.LENGTH_SHORT).show();
            }
        });

        tl.addView(imageTopL);
        tl.addView(imageTopR);
        tl.addView(imageBottomL);
        tl.addView(imageBottomR);

        return v;
    }

Upvotes: 1

Related Questions