2

Issue about layout aliment.

i want to add view dynamically.

Logical code :

    linear1 = (LinearLayout) findViewById(R.id.parent);
    linear2 = (LinearLayout) findViewById(R.id.chiled);

    int len = 4;

    for (int i = 1; i <= len; i++) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        params1.gravity = Gravity.RIGHT;

        final int id_txt;
        ImageView iv = new ImageView(this);
        iv.setId(i);
        id_txt = iv.getId();
        iv.setBackgroundResource(R.drawable.ic_launcher);
        linear1.addView(iv, params);
        iv = ((ImageView) findViewById(id_txt));

        for (int j = 1; j < 2; j++) {
            final int id_;
            Button btn = new Button(this);
            btn.setId(i);
            id_ = btn.getId();
            btn.setText("button " + id_);
            linear2.addView(btn, params1);
            btn = ((Button) findViewById(id_));

            btn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Toast.makeText(v.getContext(),
                            "Button clicked index = " + id_,
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
        // btn.setBackgroundColor(Color.rgb(70, 80, 90));

        // linear1.addView(txt, params);

        // params.addRule(RelativeLayout.RIGHT_OF, txt.getId());

        iv.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Toast.makeText(view.getContext(),
                        "text clicked index = " + id_txt,
                        Toast.LENGTH_SHORT).show();
            }
        });
              }
    }

Xml Code :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/chiled"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

I want to add image in parent view and two button in child view dynamical.

I inspires to make this type view form

Android heterogeneous gridview like pinterest?

It should be like as

enter image description here

current output as

enter image description here

I don't know where is the problem.

One strange issues i am facing now in my editor if i see layout in code then its shows me android:orientation="vertical" and if i see in outline that shows android:orientation="horizontal" for each layout. how is it possible ?

Help me to solve out.Thanks

Community
  • 1
  • 1
Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85

2 Answers2

1

EDIT:

First off, start by adding your buttons to the xml. Along with the image view.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/extra_root"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/chiled"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Like" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Dislike" />
    </LinearLayout>

</LinearLayout>

You can then remove any concept of adding views, as they already exist.

package com.example.yul;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class extra extends Activity {
  Button like, dislike;
  LinearLayout root, sub;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.extra);

    like = (Button) findViewById(R.id.button1);
    dislike = (Button) findViewById(R.id.button2);

    // add button listeners here.

    ImageView iv = (ImageView) findViewById(R.id.image);

      iv.setBackgroundResource(R.drawable.ic_launcher);

    }
  }
}

This will only display the one image though, with buttons.

What you need to do is apply this xml, and code to a gridView or similar. As you will have id clashes otherwise.

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • so you are telling that create a layout dynamically also ? actually this is my main question http://stackoverflow.com/questions/13796618/how-to-manage-control-in-relative-layout-dynamically but i divide so it can be easy to find out solution. this is single vertical liner layout that same way i need to add two more so its looks like gallery. hope you get my concept. – Chintan Khetiya Dec 11 '12 at 11:54
  • @chintankhetiya It looks like the easiest way in your situation, so it will grow dynamically. The rest will function in the same way. Aka, root will still be added to screen in the same manor. Just remember to set correct orinetation on your linear layouts that you create dynamically. I assume you want it to go horizontal for one of them. Give it at try, and see what you think. – IAmGroot Dec 11 '12 at 12:04
  • have you work on this type of gallery ? and my concept is good or not ? – Chintan Khetiya Dec 11 '12 at 12:06
  • @chintankhetiya Im not entirely sure what you are trying to achieve. But you should maybe look at ListView. If yu are triyng to create a long list. http://developer.android.com/guide/topics/ui/layout/listview.html Itmight meet your requirements, and save loading speeds. – IAmGroot Dec 11 '12 at 12:28
  • No actually i need to view like as gallery. each image have two buttons like & dislike. its should not be view like grid .its looks like paintrest wall like http://stackoverflow.com/questions/11736658/android-heterogeneous-gridview-like-pinterest. i am using your way but its not working. and its very difficult to manage. – Chintan Khetiya Dec 11 '12 at 12:33
  • @chintankhetiya If you need just a like and dislike, why are you adding them dynamically? Just add the two buttons to the xml inside the child linearlayout. Apply findViewById to the buttons, then attach a button listener to them. You can then use your root xml, in place of any image you want, to tile them how you want. – IAmGroot Dec 11 '12 at 12:45
  • Doomsknight see this is my java code.http://pastie.org/5510814 and this is xml http://pastie.org/5510819. they give me force close. its according to you can you tell me where is the problem ? – Chintan Khetiya Dec 11 '12 at 12:59
  • @chintankhetiya Ive changed my answer, to reflect your code. However, you cannot just reuse the same xml, without causing id clashes. So you either need to use `listView` `gridView` or similar. Or create the whole thing dynamically. – IAmGroot Dec 11 '12 at 13:25
  • First of all thanks for reply. i have all ready coded same way. see my above two comments links. now i just want to know how can i add this group in grid. i am confused at that time.if you help me more then i will rich my destination. please. – Chintan Khetiya Dec 12 '12 at 05:16
0

Since the horizontal LinearLayout chiled is the first child of the vertical LinearLayout parent it is aligned as the first item. So adding Views to it will place them on top.

Try moving the chiled Layout to the root LinearLayout root.

Greeny
  • 1,931
  • 2
  • 17
  • 26