package com.example.addview01;

import android.os.Bundle;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
        
       final LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain);
        
       LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        
       //Create four 
       for(int j=0;j<=4;j++) 
       {   
           LinearLayout ll = new LinearLayout(this);
           ll.setOrientation(LinearLayout.HORIZONTAL);
            
           TextView product = new TextView(this);
           product.setText(" Product"+j+"    ");
           ll.addView(product);
            
           TextView price = new TextView(this);
           price.setText("  $"+j+"     ");
           ll.addView(price);
            
           final Button btn = new Button(this);
           btn.setId(j+1);
           btn.setText("Add To Cart");
           btn.setLayoutParams(params);
                
           final int index = j;
           btn.setOnClickListener(new OnClickListener()
           {
               public void onClick(View v)
               {
                    Log.i("TAG", "index :" + index);
                    Toast.makeText(getApplicationContext(), "Clicked Button Index :" + index, Toast.LENGTH_LONG).show();
               }
           });
                
               ll.addView(btn);
               lm.addView(ll);  
       }
   }
}

