0

I may being going about this the wrong way, but I pretty much have the code almost there so I don't think I am too far off. I am trying to create Horizontal Linearlayout with two vertical Linearlayouts inside programmatically. I have created it in the xml to test it, but as I load data from the db I need to dynamically create the rows.

Here is what my xml looks like and what I am trying to create programmatically:

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_marginLeft="25dip"
            android:layout_marginRight="10dip"
            android:background="@drawable/rounded_edges_white">

            <ImageView
                android:layout_width="120dp"
                android:layout_height="80dp"
                android:id="@+id/imageView3"
                android:contentDescription="personIcon"
                android:src="@drawable/person_icon"
                android:layout_gravity="center" />
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#f1f1f1"
                android:layout_marginBottom="10dip" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Danielle"
                android:id="@+id/textView81"
                android:layout_gravity="center_horizontal" />
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="fill_parent"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="25dip"
            android:background="@drawable/rounded_edges_white">

            <ImageView
                android:layout_width="120dp"
                android:layout_height="80dp"
                android:id="@+id/imageView2"
                android:contentDescription="personIcon"
                android:src="@drawable/person_icon"
                android:layout_gravity="center" />
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#f1f1f1"
                android:layout_marginBottom="10dip" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Danielle"
                android:id="@+id/textView8"
                android:layout_gravity="center_horizontal" />
        </LinearLayout>
    </LinearLayout>

So i can just duplicate that xml to get the layout I want. Here is the code I've created:

    public void buildHome(){
        DatabaseHandler db = new DatabaseHandler(this);
        List<Person> people = db.getAllPeople();

        LinearLayout parentLayout = (LinearLayout) this.findViewById(R.id.personList);\
        float padding25 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 25, getResources().getDisplayMetrics());
        float padding10 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
        float size120 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 120, getResources().getDisplayMetrics());
        float size80 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics());
        float size = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
        float mBottom = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics());


        int count = 0;
        int totalCount = 1;
        LinearLayout rowLayout = null;
        LinearLayout childLayout = null;
        for (final Person peep : people) {
            if(count == 2) count = 0;
            Log.d("count", ""+count);
            if(count == 0) {
                Log.d("creating row", "true");
                //create row layout container
                rowLayout = new LinearLayout(this);
                rowLayout.setOrientation(LinearLayout.HORIZONTAL);
                LinearLayout.LayoutParams rowParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                rowLayout.setTag(totalCount);
                rowLayout.setLayoutParams(rowParams);
                rowLayout.setBaselineAligned(false);
            }

            //create row layout container
            childLayout = new LinearLayout(this);
            childLayout.setOrientation(LinearLayout.VERTICAL);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f);

            //left or right col
            if(count == 0) layoutParams.setMargins(Math.round(padding25), Math.round(padding10), Math.round(padding10), 0);
            else layoutParams.setMargins(Math.round(padding10), Math.round(padding10), Math.round(padding25), 0);
            childLayout.setLayoutParams(layoutParams);

            childLayout.setTag(peep.getId());
            childLayout.setClickable(true);
            childLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(Main.this);
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putString("personID", "" + peep.getId());
                    editor.apply();

                    Intent intent = new Intent(Main.this, List.class);
                    startActivityForResult(intent, 1);
                }
            });
            childLayout.setBackgroundResource(R.drawable.rounded_edges_white);

            //set icon
            ImageView icon = new ImageView(this);
            LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(Math.round(size120), Math.round(size80));
            imageParams.gravity = Gravity.CENTER_HORIZONTAL;
            icon.setLayoutParams(imageParams);
            icon.setContentDescription("personIcon");
            icon.setImageResource(R.drawable.person_icon);
            childLayout.addView(icon);

            //horizontal line
            View horizontalLine = new View(this);
            horizontalLine.setBackgroundColor(Color.LTGRAY);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, Math.round(size));
            params.setMargins(0, 0 , 0, Math.round(mBottom));
            horizontalLine.setLayoutParams(params);
            childLayout.addView(horizontalLine);

            //Set name title
            TextView titleView = new TextView(this);
            titleView.setText(peep.getName());
            LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            titleParams.gravity = Gravity.CENTER_HORIZONTAL;
            titleView.setLayoutParams(titleParams);
            childLayout.addView(titleView);

            //add to row
            rowLayout.addView(childLayout);

            //add to parent view
            if(count == 1){
                Log.d("add to parent", "true");
                parentLayout.addView(rowLayout);
            }
            count++;
            totalCount++;
        }

        Log.d("size", ""+people.size());
        if(people.size() % 2 != 0) {
            Log.d("only 1", "true");
            parentLayout.addView(rowLayout);
        }
    }

Here is what the log says:

10-21 17:48:41.586 24022-24022/com.example.app D/count: 0
10-21 17:48:41.586 24022-24022/com.example.app D/creating row: true
10-21 17:48:41.587 24022-24022/com.example.app D/count: 1
10-21 17:48:41.598 24022-24022/com.example.app D/add to parent: true
10-21 17:48:41.598 24022-24022/com.example.app D/count: 0
10-21 17:48:41.598 24022-24022/com.example.app D/creating row: true
10-21 17:48:41.598 24022-24022/com.example.app D/after the first row: true
10-21 17:48:41.599 24022-24022/com.example.app D/count: 1
10-21 17:48:41.599 24022-24022/com.example.app D/add to parent: true
10-21 17:48:41.600 24022-24022/com.example.app D/size: 4

SO its hitting all the right places and saying its creating row and adding to parent. This is the closest post to what I am doing linear layout inside linear layout is adding but is not visible

but he isn't really dynamically adding the extra rows. He has layout2 and 3 if he goes over.

This is what i am aiming for(repeating as many times as needed):

            |---------------|   |---------------|
            |               |   |               |
            |               |   |               |
            |               |   |               |
            |               |   |               |
            |---------------|   |---------------|

            |---------------|   |---------------|
            |               |   |               |
            |               |   |               |
            |               |   |               |
            |               |   |               |
            |---------------|   |---------------|

I have 4 items in the database but only row 1 with 2 items shows.

Thanks

Community
  • 1
  • 1
Pablo
  • 45
  • 2
  • 8
  • 1
    Actually even if you're pulling your data from your database, you won't need to create the layout dynamically. You can use a custom listview to show your database data very neatly. – Razgriz Oct 22 '15 at 00:33
  • I'm interested, how would I go about doing that? – Pablo Oct 22 '15 at 09:02
  • You can look up tutorials in making CustomListViews with Custom Objects. Since you want 2 columns per row, I suggest you do a GridView and specify the column count to 2. Making Custom GridViews would be very very similar to making custom listviews. I'll try to write an example later, I just need to check my code. – Razgriz Oct 22 '15 at 09:15
  • I seem to be having trouble looking for my code. :/ You can try to google up on how to do Custom GridViews, I'm sure you'll find examples where a custom object (reflecting your database entries) was used, as well as a custom gridView Adapter. – Razgriz Oct 22 '15 at 12:37
  • While trying to figure this out I saw some gridview stuff, I'll see if I can figure it out – Pablo Oct 22 '15 at 14:57
  • If you're having problems, you can ask another question (to avoid being off topic in this question) and link me to that question. I'd be glad to help. – Razgriz Oct 23 '15 at 03:37
  • I found a tutorial that pushed I'm in the right direction with custom grid view. It seemed like I was so close with this code since it was showing two of the items that I wanted to finish it. Oh well – Pablo Oct 23 '15 at 08:15
  • If you're having problems finishing it, I can help. Post another question and link it here. – Razgriz Oct 23 '15 at 08:31

1 Answers1

0

it has to do with your code

//add to parent view
if(count == 1){
  Log.d("add to parent", "true");
     parentLayout.addView(rowLayout);
  }
 count++;
 totalCount++;

you should change it to

if(count % 2 == 1) {

this will add the row every 2 items

William Ku
  • 798
  • 5
  • 17
  • When I change it to if(count %= 1) it says boolean required. My log file shows that it creates a row every 2. – Pablo Oct 22 '15 at 09:01
  • sorry, the line should be if (count % 2 == 1) – William Ku Oct 22 '15 at 16:59
  • That does the same as what I had before. Either way it goes into the code for the second row, the second row just doesn't show. I put a log.d in there to make sure it was going in the if statement. I feel like it is adding it, but the items are invisible or just not being shown?? – Pablo Oct 22 '15 at 17:08
  • you should remove the line if(count == 2) count = 0; – William Ku Oct 22 '15 at 17:13
  • But that's not the issue. The code inside the if(count % 2 == 1) is running at the right time. See my log above. Log.d("add to parent", "true"); runs twice in the right order. I removed the if(count ==2) count =0; and it has errors with adding the child when it already has a parent. – Pablo Oct 22 '15 at 17:18
  • hmm, guess it will be more straightforward to use a GridLayout (2x2) then. It will save all the checkings .. – William Ku Oct 22 '15 at 17:26