package com.tommymacwilliam.androidwalkthroughapp1;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Button;
import android.widget.Toast;

public class AndroidWalkthroughApp1 extends Activity implements View.OnClickListener {
	
	final int TOP_ID = 3;
	final int BOTTOM_ID = 4;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // create two layouts to hold buttons
        LinearLayout top = new LinearLayout(this);
        top.setId(TOP_ID);
        LinearLayout bottom = new LinearLayout(this);
        bottom.setId(BOTTOM_ID);
        
        // create buttons in a loop
        for (int i = 0; i < 2; i++) {
        	Button button = new Button(this);
        	button.setText("Button " + i);
        	// R.id won't be generated for us, so we need to create one
        	button.setId(i);
        	
        	// add our event handler (less memory than an anonymous inner class)
        	button.setOnClickListener(this);
        	
        	// add generated button to view
        	if (i == 0) {
        		top.addView(button);
        	}
        	else {
        		bottom.addView(button);
        	}
        }
        
        // add generated layouts to root layout view
        LinearLayout root = (LinearLayout)this.findViewById(R.id.root_layout);
        root.addView(top);
        root.addView(bottom);
    }
    
    @Override
	public void onClick(View v) {
		// show a message with the button's ID
		Toast toast = Toast.makeText(AndroidWalkthroughApp1.this, "You clicked button " + v.getId(), Toast.LENGTH_LONG);
		toast.show();
		
		// get the parent layout and remove the clicked button
		LinearLayout parentLayout = (LinearLayout)v.getParent();
		parentLayout.removeView(v);
	}
}