Advertisement
Guest User

Untitled

a guest
Apr 13th, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. package edu.ucsb.cs.cs185.mastergberry.colormeprogrammatically;
  2.  
  3. import android.app.Activity;
  4. import android.graphics.Color;
  5. import android.os.Bundle;
  6. import android.view.Gravity;
  7. import android.view.Menu;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.view.ViewGroup;
  11. import android.view.ViewGroup.LayoutParams;
  12. import android.widget.Button;
  13. import android.widget.LinearLayout;
  14.  
  15. public class ColorMeActivity extends Activity {
  16.  
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.        
  21.         final LinearLayout layout = new LinearLayout(this);
  22.         View.OnClickListener mButtonListener;
  23.        
  24.         mButtonListener = new OnClickListener() {
  25.             public void onClick(View v) {
  26.                 switch (v.getId()) {
  27.                     case 101:
  28.                         layout.setBackgroundColor(Color.RED);
  29.                         break;
  30.                     case 102:
  31.                         layout.setBackgroundColor(Color.GREEN);
  32.                         break;
  33.                     case 103:
  34.                         layout.setBackgroundColor(Color.BLUE);
  35.                         break;
  36.                     case 104:
  37.                         layout.setBackgroundColor(Color.YELLOW);
  38.                         break;
  39.                
  40.                 }
  41.             }
  42.         };
  43.        
  44.         layout.setLayoutParams(new LayoutParams(
  45.                 LayoutParams.WRAP_CONTENT,
  46.                 LayoutParams.MATCH_PARENT));
  47.         layout.setWeightSum(4);
  48.         layout.setOrientation(LinearLayout.VERTICAL);
  49.         //layout.setGravity(Gravity.CENTER_HORIZONTAL);
  50.        
  51.         //ViewGroup vg = new ViewGroup(layout);
  52.        
  53.         Button redButton = new Button(this);
  54.         redButton.setText("Red");
  55.         LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
  56.                 ViewGroup.LayoutParams.MATCH_PARENT,
  57.                 ViewGroup.LayoutParams.MATCH_PARENT,
  58.                 1f);
  59.         //redButton.setWidth(0);
  60.         redButton.setId(101);
  61.         redButton.setLayoutParams(p);
  62.         redButton.setOnClickListener(mButtonListener);
  63.         layout.addView(redButton);
  64.        
  65.         Button greenButton = new Button(this);
  66.         greenButton.setText("Green");
  67.         greenButton.setLayoutParams(p);
  68.         //greenButton.setWidth(0);
  69.         greenButton.setId(102);
  70.         greenButton.setOnClickListener(mButtonListener);
  71.         layout.addView(greenButton);
  72.        
  73.         Button blueButton = new Button(this);
  74.         blueButton.setText("Blue");
  75.         blueButton.setLayoutParams(p);
  76.         //blueButton.setWidth(0);
  77.         blueButton.setId(103);
  78.         blueButton.setOnClickListener(mButtonListener);
  79.         layout.addView(blueButton);
  80.        
  81.         Button yellowButton = new Button(this);
  82.         yellowButton.setText("Yellow");
  83.         yellowButton.setLayoutParams(p);
  84.         //yellowButton.setWidth(0);
  85.         yellowButton.setId(104);
  86.         yellowButton.setOnClickListener(mButtonListener);
  87.         layout.addView(yellowButton);
  88.        
  89.        
  90.         setContentView(layout);
  91.     }
  92.  
  93.     @Override
  94.     public boolean onCreateOptionsMenu(Menu menu) {
  95.         // Inflate the menu; this adds items to the action bar if it is present.
  96.         getMenuInflater().inflate(R.menu.color_me, menu);
  97.         return true;
  98.     }
  99.    
  100.    
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement