0

how to avoid overlapping of dynamically positioned views?

I have a RelativeLayout , and i am adding views dynamically(at runtime) at particular position(x,y coordinates) but the problem is the views are overlapping. How to avoid this.

Thanks in advance.enter image description here

    <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


        <RelativeLayout
            android:id="@+id/rl_main"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </RelativeLayout>
         <LinearLayout
    android:layout_width="fill_parent"
    android:id="@+id/ll_mainBottom"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    </LinearLayout>

        </LinearLayout>
       </ScrollView>

javacode

 ll_main = (RelativeLayout) findViewById(R.id.rl_main);

            if (views[3].equals("textView")) {


                TextView tv_new = new TextView(TenMinActivity.this);
                // location
                int x = Integer.parseInt(views[12]);
                int y = Integer.parseInt(views[13]);

                String bgColor = "#" + views[4];
                String fgColor = "#" + Views[5];

            tv_new.setBackgroundColor(Color.parseColor(bgColor)); // Bg Color
        tv_new.setTextColor(Color.parseColor(fgColor)); // Text color


                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            width, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    params.leftMargin = x;
    params.topMargin = y;

                ll_main.addView(tv_new, params);
}else if(views[3].equals("edittext")){

    ....

}
Vikram Deshmukh
  • 12,304
  • 4
  • 36
  • 38
LMK
  • 2,882
  • 5
  • 28
  • 52
  • what about changing relative layout to linear – Renjith K N May 15 '13 at 05:14
  • @RenjithKN Sir , if i am using LinearLayout im unable to set view at particular positions.It is displaying all views vertically. ex.Name(Textview)
    (EditText)
    Age(Tv)
    – LMK May 15 '13 at 05:18

4 Answers4

1

Give

android:paddingLeft=""

and to each element so that the ones to the left most of the screen will have bit of space. and the ones to the right,give

android:paddingBottom=""

First,check the first 2 elements that is the name text and your text field after that you can proceed to the rest.

I can guide you better,if you post your code

Check this,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Name"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="21dp"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10"
        android:padding="10dp"
        android:paddingl="10dp" >

        <requestFocus />
    </EditText>

</RelativeLayout>
user
  • 1,001
  • 4
  • 21
  • 45
  • @latif mohammad khan,do you have any sort of compulsion to code in java directly..why can't you code in the layouts? – user May 15 '13 at 05:27
  • Sir ,actually we get all this views(view BG color,View Textsize,textcolor,....... ,x-coordinate y-coordinate position ) dynamically. I(in code) have to check weather this is EditText,Radio,..etc and add to layout at given position. @user – LMK May 15 '13 at 05:36
  • @latif mohammad khan, Ok then,I would suggest you to implement it as we do it for the list view...create a layout and fix the positions and then check the conditions and then add the values dynamically. – user May 15 '13 at 05:53
0

You need to learn how layouts work, first try to make the same layout in xml. Then you will learn that this kind of layout can easily be made using LinearLayout.

You can use parent LinearLayout and add sublayouts to it. Now you say if you use LinearLayout all subviews are shown vertically. It shows vertically because you are adding them one by one. If you take a RelativeLayout and add your TextView and EditText to it and then add that RelativeLayout to your LinearLayout, you will get the desired result.

<LinearLayout>
    <RelativeLayout>
        <TextView />
        <EditText />//use layout_margin or layout_toLeftOf for moving to to right
    </RelativeLayout>

    <RelativeLayout>
        <TextView/>
        <EditText/>
    </RelativeLayout>
</LinearLayout> 
Naveen
  • 1,703
  • 13
  • 22
  • Sir ,actually we get all this views(view BG color,View Textsize,textcolor,....... ,x-coordinate y-coordinate position ) dynamically. I(in code) have to check weather this is EditText,Radio,..etc and add to layout at given position,I should write through code only @naveen – LMK May 15 '13 at 06:11
0

Please use Layout params like layout_below layout_above toRightof and ToLeftof when you add a view to container relative layout

https://stackoverflow.com/a/5191159/1911784

Community
  • 1
  • 1
OMAK
  • 1,031
  • 9
  • 25
  • Sir,I cant use this because I have to insert at particular position in the layout(at runtime) ie(x,y coordinates given). – LMK May 15 '13 at 06:38
  • @latifmohammadkhan In Android there are many devices with different width and height so please don't use x ,y coordinate, it will make many alignment issues in different devices. so try to use layout params thats better – OMAK May 15 '13 at 06:50
  • Yes, in some devices the view is good. could you please send code (as u said :use layout params thats better) @user1911784 I have used RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( width, android.view.ViewGroup.LayoutParams.WRAP_CONTENT); params.leftMargin = x; params.topMargin = y; plz see my code given. – LMK May 15 '13 at 07:08
0

you an use below code to add views in your layout

RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            TextView text2 = new TextView(context);         
                        newParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                        newParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                   newParams.addRule(RelativeLayout.ALIGN_BOTTOM, text1.getId());
            text2.setLayoutParams(newParams);
            layout.addView(text2);

where layout is your main layout.

you can use other params as well - like rightof, leftof etc

Pankaj Kushwaha
  • 300
  • 1
  • 13
  • Sir,I cant use this because I have to insert at particular position in the layout(at runtime) ie(x,y coordinates given). Please see the code given. – LMK May 15 '13 at 06:51