1. Download our Official Android App: Forums for Android!

Apps What's wrong with my dynamically declared layouts?

Discussion in 'Android Development' started by MuhammedRefaat, Jan 5, 2014.

  1. MuhammedRefaat

    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    9
    Joined:
    Nov 25, 2013

    Nov 25, 2013
    9
    0
    25
    My android app supposed to have a LinearLayout contained in a HorizontalScrollView, now i have to declare a number of another contained layouts that are contained in that main layout but i don't know the number or the data of these layouts as I take this data from the server i connect to after the app launching, So, the main layout that contained in the HorizontalScrollView is statically created in the XML file and it's contained layouts must created throw run time by the java code. So, i take them from the server database and put them in a two-dimentional array and then make a loop to loop throw that array and create a layouts equal to that array size, I don't know if this way is right or not but it gives me nothing, and this is my code :

    XML:

    [HIGH] <LinearLayout
    android:id="@+id/ver1"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@android:color/transparent"
    android:eek:rientation="vertical" >
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/rooms"
    android:textColor="@android:color/white"
    android:textSize="17sp" />
    <HorizontalScrollView
    android:id="@+id/scroll_rooms"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    <LinearLayout
    android:id="@+id/ver_rooms"
    android:baselineAligned="false"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:eek:rientation="horizontal">
    </LinearLayout>
    </HorizontalScrollView>
    </LinearLayout>[/HIGH]

    JAVA:

    [HIGH]
    private void declare_dynamics() {

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.homeview, null);

    LinearLayout ver_rooms = (LinearLayout) v.findViewById(R.id.ver_rooms);
    List<LinearLayout> inner_ver = new ArrayList<LinearLayout>();

    LinearLayout.LayoutParams lp_ineer_ver = new LinearLayout.LayoutParams(
    300, 300);
    LinearLayout.LayoutParams lp_icon = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT, 300, 1);
    lp_icon.setMargins(10, 15, 5, 0);

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    metrics.widthPixels / 4, LayoutParams.MATCH_PARENT);
    params.setMargins(10, 15, 5, 0);

    for (int i = 0; i < (Integer) ((Rooms.size() / 2) + 1); i++) {
    LinearLayout temp_inner_ver = new LinearLayout(this);
    temp_inner_ver.setLayoutParams(lp_ineer_ver);
    temp_inner_ver.setOrientation(LinearLayout.VERTICAL);
    temp_inner_ver.setWeightSum(2);
    temp_inner_ver.setPadding(7, 7, 7, 7);
    temp_inner_ver.setBackgroundColor(Color.BLUE);
    inner_ver.add(temp_inner_ver);
    }

    int cmp = 0;
    for (int j = 0; j < inner_ver.size(); j++) {
    LinearLayout icon1 = new LinearLayout(this);
    LinearLayout icon2 = new LinearLayout(this);
    icon1.setLayoutParams(params);
    icon2.setLayoutParams(params);
    icon1.setBackgroundResource(R.drawable.ac_overlay);
    icon2.setBackgroundResource(R.drawable.ac_overlay);
    icon1.setOrientation(LinearLayout.HORIZONTAL);
    icon2.setOrientation(LinearLayout.HORIZONTAL);
    icon1.setTag(NORMAL);
    icon2.setTag(NORMAL);

    ImageButton test = new ImageButton(this);
    test.setLayoutParams(lp_icon);
    test.setImageResource(R.id.tvshow_img);
    icon1.addView(test);
    icon1.setBackgroundColor(Color.RED);

    inner_ver.get(j).addView(icon1);
    cmp++;
    if (cmp != Rooms.size()) {
    inner_ver.get(j).addView(icon2);
    cmp++;
    }
    ver_rooms.addView(inner_ver.get(j));
    }

    }[/HIGH]And that's the array called Rooms:
    [HIGH][0] room_id, 0, 14, 15, 18, 37, 38
    [1] room_name, Home, Living Room, Master Bedroom, Kitchen, Office Room, Entrance
    [2] room_types_type_id, 1, 4, 6, 8, 15, 12
    [3] sys_room_id, 0, 2, 3, 6, 5, 1
    [4] sys_zone_id, 1, 1, 1, 1, 2, 1
    [5] type_cats_cat_id, 4, 1, 1, 2, 1, 3
    [6] room_position, 0, 1, 2, 3, 4, 5
    [7] locked, false, false, false, false, false, false[/HIGH]
     

    Advertisement

  2. alostpacket

    alostpacket Over Macho Grande?
    Rank:
    None
    Points:
    513
    Posts:
    7,972
    Joined:
    Nov 29, 2009

    Nov 29, 2009
    7,972
    3,603
    513
    Android App Developer
    NY
    at least this line is wrong:

    [HIGH]
    LinearLayout.LayoutParams params
    = new LinearLayout.LayoutParams( metrics.widthPixels / 4,
    LayoutParams.MATCH_PARENT);
    [/HIGH]You are passing an int flag MATCH_PARENT along with the pixel width, you are confusing different overloaded constructors.


    If I were you i would create all layouts in XML and inflate them as needed, then you can add them programtically.

    ex:
    [HIGH]
    for (i=0; i< 5; i++)
    {
    View itemView = inflater.inflate(R.layout.my_item, null);
    myContainerView.addView(itemView);
    }
    [/HIGH]This way you dont have to deal with generating the Layout params yourself. I generally avoid generating Layout Params unless I absolutely have to.


    hth
     
  3. MuhammedRefaat

    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    9
    Joined:
    Nov 25, 2013

    Nov 25, 2013
    9
    0
    25
    but i can't create the layouts through the XML as i don't know it's number or data
     

Share This Page

Loading...