0

I want to let the user add buttons So that each row are only four buttons. so I wrote the following function:

private void addContact() {
        //numButton Count how many buttons there are in line
        if(numButton==0){
            LinearLayout linearLayout =new LinearLayout(this);
            linearLayout.setOrientation(0);//horizontal
            ImageButton imageButton =new ImageButton(this);
            imageButton.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.button1));
            linearLayout.addView(imageButton);
            LinearLayout linearbase= (LinearLayout)findViewById(R.id.linearBase);
            linearbase.addView(linearLayout);
            numButton++;
        }
        else if(numButton<4)
        {
            LinearLayout linearlayout= ----####Here I do not know what to write!!!!###
            ImageButton imageButton =new ImageButton(this);
            imageButton.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.button1));
            linearlayout.addView(imageButton);
            numButton++;
        }
        else 
        {
            numButton=0;
        }
    }

I marked the lines of code my problem Specifically my problem is how to put the new button into the linearlayout that defined in the previous call to this function? Second question: how to keep the new situation even when you close the application?

  • This might help: [Dynamically creating Buttons and setting onClickListener](http://stackoverflow.com/questions/4401028/dynamically-creating-buttons-and-setting-onclicklistener) I found it in the **Related** column (over there -->). As for keeping the data after closing the app, this is a good reference. [Data Storage](http://developer.android.com/guide/topics/data/data-storage.html) – Sam Apr 26 '12 at 22:28

2 Answers2

0

When you create the LinearLayout the first time, give it an id. Then you can get it the second time with findViewById(). To answer your second question, store a boolean in SharedPreferences.

Yusuf X
  • 14,513
  • 5
  • 35
  • 47
0

Declare your LinearLayouts outside your addContact method, or else they only exist inside that method. I think something like this should work (I haven't tested it):

class myclass{

    private LinearLayout linearLayout;
    private LinearLayout linearbase;
    private int numButton;

    @Override
    public void onFinishInflate() {
        super.onFinishInflate();

        linearbase= (LinearLayout)findViewById(R.id.linearBase);
        LinearLayout linearLayout =new LinearLayout(this);
        linearLayout.setOrientation(0);//horizontal

        numButton=0;
    }

    private void addContact() {
        //numButton Count how many buttons there are in line
        if(numButton==0){

            ImageButton imageButton =new ImageButton(this);
            imageButton.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.button1));
            linearLayout.addView(imageButton);
            linearbase.addView(linearLayout);
            numButton++;
        }
        else if(numButton<4)
        {
            linearLayout= new LinearLayout(this);
            ImageButton imageButton =new ImageButton(this);
            imageButton.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.button1));
            linearlayout.addView(imageButton);
            numButton++;
        }
        else 
        {
            numButton=0;
        }
    }   
}
ForeverWintr
  • 5,492
  • 2
  • 36
  • 65