Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.ucsb.cs.cs185.mastergberry.colormeprogrammatically;
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.Button;
- import android.widget.LinearLayout;
- public class ColorMeActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- final LinearLayout layout = new LinearLayout(this);
- View.OnClickListener mButtonListener;
- mButtonListener = new OnClickListener() {
- public void onClick(View v) {
- switch (v.getId()) {
- case 101:
- layout.setBackgroundColor(Color.RED);
- break;
- case 102:
- layout.setBackgroundColor(Color.GREEN);
- break;
- case 103:
- layout.setBackgroundColor(Color.BLUE);
- break;
- case 104:
- layout.setBackgroundColor(Color.YELLOW);
- break;
- }
- }
- };
- layout.setLayoutParams(new LayoutParams(
- LayoutParams.WRAP_CONTENT,
- LayoutParams.MATCH_PARENT));
- layout.setWeightSum(4);
- layout.setOrientation(LinearLayout.VERTICAL);
- //layout.setGravity(Gravity.CENTER_HORIZONTAL);
- //ViewGroup vg = new ViewGroup(layout);
- Button redButton = new Button(this);
- redButton.setText("Red");
- LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
- ViewGroup.LayoutParams.MATCH_PARENT,
- ViewGroup.LayoutParams.MATCH_PARENT,
- 1f);
- //redButton.setWidth(0);
- redButton.setId(101);
- redButton.setLayoutParams(p);
- redButton.setOnClickListener(mButtonListener);
- layout.addView(redButton);
- Button greenButton = new Button(this);
- greenButton.setText("Green");
- greenButton.setLayoutParams(p);
- //greenButton.setWidth(0);
- greenButton.setId(102);
- greenButton.setOnClickListener(mButtonListener);
- layout.addView(greenButton);
- Button blueButton = new Button(this);
- blueButton.setText("Blue");
- blueButton.setLayoutParams(p);
- //blueButton.setWidth(0);
- blueButton.setId(103);
- blueButton.setOnClickListener(mButtonListener);
- layout.addView(blueButton);
- Button yellowButton = new Button(this);
- yellowButton.setText("Yellow");
- yellowButton.setLayoutParams(p);
- //yellowButton.setWidth(0);
- yellowButton.setId(104);
- yellowButton.setOnClickListener(mButtonListener);
- layout.addView(yellowButton);
- setContentView(layout);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.color_me, menu);
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement