Android : Childs of a relative layout either overlap each other or only the last or the first one is visible

StackOverflow https://stackoverflow.com/questions/22949674

  •  30-06-2023
  •  | 
  •  

Question

I am using this code to get the contents from a source and hence using dynamic initialisation.

The problem is that the views made by the createcomment(), overlap each other.

What can be done so that overlapping does not occur??

createdynamic

    void createdymanic()
    {
        rl= new RelativeLayout(getActivity());

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

        RelativeLayout.LayoutParams ivprofile = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams textname = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT,
                            RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams textdate = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams textbody = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams imagelike = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams imagecomment = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams imageshare = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams editcomment = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);



        // set picture of the person
        ivprofilepic= new ImageView(getActivity());
        ivprofile.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        ivprofile.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        ivprofile.setMargins(5,5,0,0);
        ivprofilepic.setId(1);
        ivprofilepic.setImageResource(R.drawable.ic_launcher);



        tvname= new TextView(getActivity());
        textname.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        textname.addRule(RelativeLayout.RIGHT_OF, ivprofilepic.getId());
        textname.setMargins(8, 5, 0, 0);
        tvname.setId(2);
        tvname.setText("Name");
        tvname.setTextColor(getResources().getColor(R.color.textcolor));
        tvname.setTextSize(18);

        tvdate= new TextView(getActivity());
        textdate.addRule(RelativeLayout.ALIGN_BOTTOM, ivprofilepic.getId());
        textdate.addRule(RelativeLayout.ALIGN_LEFT, tvname.getId());
        textdate.addRule(RelativeLayout.BELOW, tvname.getId());
        tvdate.setId(3);
        tvname.setText("Date");


        tvcontent= new TextView(getActivity());
        textbody.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        textbody.addRule(RelativeLayout.BELOW, ivprofilepic.getId());
        textbody.setMargins(5,0,0,0);
        tvcontent.setId(4);
        tvcontent.setText("Content");


        iblike= new Button(getActivity());

        imagelike.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        imagelike.addRule(RelativeLayout.BELOW, tvcontent.getId());
        imagelike.setMargins(5, 7, 0, 0);
        iblike.setId(5);
        iblike.setText("like");


        ibcomment= new Button(getActivity());
        imagecomment.addRule(RelativeLayout.ALIGN_TOP, iblike.getId());
        imagecomment.addRule(RelativeLayout.CENTER_HORIZONTAL);
        ibcomment.setId(6);
        ibcomment.setText("comment");


        ibshare= new Button(getActivity());
        imageshare.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        imageshare.addRule(RelativeLayout.ALIGN_TOP, iblike.getId());
        imageshare.setMargins(0,0,0,5);
        ibshare.setId(7);
        ibshare.setText("share");

        etcommentbody= new EditText(getActivity());
        editcomment.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        editcomment.addRule(RelativeLayout.BELOW, iblike.getId());
        editcomment.setMargins(5,8,0,0);
        etcommentbody.setId(8);
        etcommentbody.setHint("Add a comment...");

        rl.addView(ivprofilepic, ivprofile);
        rl.addView(tvname, textname);
        rl.addView(tvdate ,textdate);
        rl.addView(tvcontent, textbody);
        rl.addView(iblike, imagelike);
        rl.addView(ibcomment, imagecomment);
        rl.addView(ibshare, imageshare);
        rl.addView(etcommentbody, editcomment);

        mainlayout.addView(rl, 0, rlp);

        tvname.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                //profile pe le jaana h yaha se
                //profile class banegi
            }
        });

        iblike.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new Caller().execute("3", api, id);
            }
        });

        ibcomment.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String a=etcommentbody.getText().toString();
                new Caller().execute("4", api, id,a);
            }
        });

        ibshare.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
//              h.share(user_id, post_id);
            }
        });
    }

createcomments

    void createcomment()
    {
        RelativeLayout clayout= new RelativeLayout(getActivity());
        RelativeLayout.LayoutParams clp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        //clp.addRule(RelativeLayout.BELOW, etcommentbody.getId());
        clp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        RelativeLayout.LayoutParams ivcommentprofile = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams textcommentname = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams textcommentdate = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams textcommentbody = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);


        ivcommentprofilepic= new ImageView(getActivity());
        ivcommentprofile.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        ivcommentprofile.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        ivcommentprofile.setMargins(5,5,0,0);
        ivcommentprofilepic.setId(101);
        ivcommentprofilepic.setImageResource(R.drawable.ic_launcher);




        tvcommentname= new TextView(getActivity());
        textcommentname.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        textcommentname.addRule(RelativeLayout.RIGHT_OF, ivcommentprofilepic.getId());
        textcommentname.setMargins(5,5,0,0);
        tvcommentname.setId(102);
        tvcommentname.setText("Name");
        tvcommentname.setTextColor(getResources().getColor(R.color.textcolor));
        tvcommentname.setTextSize(18);

        tvcommentdate= new TextView(getActivity());
        textcommentdate.addRule(RelativeLayout.ALIGN_BOTTOM, ivcommentprofilepic.getId());
        textcommentdate.addRule(RelativeLayout.RIGHT_OF, ivcommentprofilepic.getId());
        textcommentdate.addRule(RelativeLayout.BELOW, tvcommentname.getId());
        textcommentdate.setMargins(5, 3, 0, 0);
        tvcommentdate.setId(3);
        tvcommentdate.setText("Date");


        tvcommentcontent= new TextView(getActivity());
        textcommentbody.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        textcommentbody.addRule(RelativeLayout.BELOW, ivcommentprofilepic.getId());
        textcommentbody.setMargins(5,0,0,0);
        tvcommentcontent.setId(4);
        tvcommentcontent.setText("Content");



        clayout.addView(ivcommentprofilepic, ivcommentprofile);
        clayout.addView(tvcommentname, textcommentname);
        clayout.addView(tvcommentdate ,textcommentdate);
        clayout.addView(tvcommentcontent, textcommentbody);
        rl.addView(clayout, clp);

    }

I use the following statements

for(ka=0;ka<jsarray.length();ka++){
    createdymanic(ka);

    // setting on click listener and all

    for(int i=0;i<comment_array.length();i++){
    createcomment(i);

        // detting onclicklistener and all
    }
}
Was it helpful?

Solution 2

Sorry for creating such a lame question.

The problem was that the default value of commentlayout's orientation is horizontal by default.

The following code solved the problem :

commentlayout= new LinearLayout(getActivity());
commentlayout.setOrientation(LinearLayout.VERTICAL);

OTHER TIPS

It looks like your last text view you're adding (tvcommentcontent) doesn't have a right bound, so your third (tvcommentdate) and your 4th view (tvcommentcontent) you're adding are overlapping. Try adding

textcommentbody.addRule(RelativeLayout.LEFT_OF, tvcommentdate.getId());

right above your

textcommentbody.setMargins(5,0,0,0);

line. This should make sure the last view stayes on the left of the third view. (I made a sweet diagram with MS paint to show this better but apparently new users can't add images :) )

I hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top