being unique
being unique

Reputation: 9

How Do I create multiple gridview programatically

public class Dues extends Activity {

    static final String[] alphabets = new String[] { 
            "A", "B", "C", "D"};

    static final String[] alphabets1 = new String[] { 
            "E", "F", "G", "H"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dues);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, alphabets);

        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, alphabets1);

        RelativeLayout relativeLayout = new RelativeLayout(this);

        RelativeLayout.LayoutParams relativeLayoutParams = new 
        RelativeLayout.LayoutParams(
                                    RelativeLayout.LayoutParams.FILL_PARENT,
                                   RelativeLayout.LayoutParams.FILL_PARENT);

        GridView gridView= new GridView(this);

        gridView.setLayoutParams(new 
                      GridView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        gridView.setNumColumns(4);

        gridView.setAdapter(adapter);

        Drawable myIcon = getResources().getDrawable(R.drawable.bg); 

        gridView.setBackground(myIcon);

        GridView gridView1 = new GridView(this);

        gridView1.setLayoutParams(new
                        GridView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

        gridView1.setNumColumns(4);

        gridView1.setAdapter(adapter);


        Drawable myIcon = getResources().getDrawable(R.drawable.bg); 

        gridView1.setBackground(myIcon);

    }
}       

So this code is creating 2 gridviews but its overlapping. Please Help me. ABCD and EFGH both are overlapping each other. Like this I want to add multiple Gridview Using Scrollview . Can anyone help me with this please. I want it to be visible one below the other.

The number of gridview is dynamic , there is nor fixed number, Hence I want it to programmatically created and not through xml file. also I want it to be greenish transparent , so anyone could help me with that too

Upvotes: 0

Views: 66

Answers (1)

Ricardo
Ricardo

Reputation: 9656

A GridView is scrollable, therefore you cannot achieve what you want with a regular ScrollView. Another problem is that you are creating two GridView, but you are not adding them to any parent. What you need to do is to create a NestedScrollView with a LinearLayout in it with orientation=vertical and add each GridView to it. Apply match_parent to both the NestedScrollView and LinearLayout height and width. Another note, don't use LayoutParams.FILL_PARENT for the GridView height, use WRAP_CONTENT.

Upvotes: 1

Related Questions