Button testButton = new Button(context); testButton.setText("Test"); StateListDrawable sld = new StateListDrawable(); GradientDrawable drawable = new GradientDrawable( Orientation.TOP_BOTTOM, new int[] { Color.BLUE, Color.RED }); sld.addState(new int[] { android.R.attr.state_pressed }, drawable); sld.addState(StateSet.WILD_CARD, new ColorDrawable(Color.YELLOW)); testButton.setBackgroundDrawable(sld); mainLayout.addView(testButton);
Drawable d = getResources().getDrawable(R.drawable.ic_launcher); Button one = new Button(this); // mediocre one.setBackgroundDrawable(d); Button two = new Button(this); // better two.setBackgroundResource(R.drawable.ic_launcher);
LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); for (int i = 0; i < 5; i++) { Button b = new Button(this); b.setText("button #" + i); b.setBackgroundDrawable(new SLD()); ll.addView(b); } setContentView(ll);
public class MainActivity extends Activity implements OnClickListener{ Button myButton; boolean flag = false; RelativeLayout rl; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rl = (RelativeLayout) findViewById(R.id.relative); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) new LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); myButton = new Button(this); myButton.setOnClickListener(this); if(flag){ myButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.record_button_pressed)); }else{ myButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.record_button_normal)); } rl.addView(myButton,params); }
// Activity.onCreate method LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); int NUM = 5; final int[] cnt = new int[NUM]; Random r = new Random(); for (int i = 0; i < NUM; i++) { cnt[i] = r.nextInt(20); Button b = new Button(this); b.setText("Click me"); b.setTextSize(18); b.setTag(i); Drawable[] layers = {b.getBackground()}; Drawable d = new DecoratedTextViewDrawable(b, layers, cnt[i]); b.setBackgroundDrawable(d); OnClickListener l = new OnClickListener() { @Override public void onClick(View v) { DecoratedTextViewDrawable d = (DecoratedTextViewDrawable) v.getBackground(); int idx = (Integer) v.getTag(); d.setCnt(++cnt[idx]); } }; b.setOnClickListener(l); ll.addView(b); } setContentView(ll);
public class MyScreen extends Activity { private Button btnNext; private Drawable background; btnNext = (Button) this.findViewById(R.id.btnNext); background = btnNext.getBackground(); //stores the original Drawable characteristic ... //disable button if (!canGoNext) { btnNext.setEnabled(false); btnNext.setBackgroundResource(R.drawable.gray); //make it gray } //enable button else { btnNext.setEnabled(true); btnNext.setBackgroundDrawable(background); //make it selectableItemBackground again } }
Button fbBtn = (Button)findViewById(R.id.fbLogin) ; if( fbBtn != null ) { fbBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.com_facebook_loginbutton_blue) ) ; fbBtn.setText( R.string.prompt_login_yes ) ; fbBtn.setTextColor( getResources().getColor(R.color.com_facebook_loginview_text_color) ) ; fbBtn.setTextSize( 16 ) ; fbBtn.setVisibility(View.VISIBLE) ; }
Button btnIconTxt=new Button(context); btnIconTxt.setId(id); //this aligns a image to the left of ur text view(This the important line). btnIconTxt.setCompoundDrawablesWithIntrinsicBounds(new BitmapDrawable(context.getResources(),bitmapOfYourImage), null, null, null); btnIconTxt.setText(message); btnIconTxt.setTextSize(TypedValue.COMPLEX_UNIT_PX, sizeInPx); btnIconTxt.setWidth(widthInPx); btnIconTxt.setTextColor(Color.RED); int currentApi = Build.VERSION.SDK_INT; if(currentApi >= 16) btnIconTxt.setBackground(btnColor); else btnIconTxt.setBackgroundDrawable(btnColor); RelativeLayout.LayoutParams SDISP_params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); btnIconTxt.setLayoutParams(params1); //U can set the x and y margin for this button using //params1.leftMargin = xPixels; // params1.topMargin = yPixels; yourParentLayout.addView(btnIconTxt);
bMarkerBtn.setBackgroundDrawable(drawable);
private Drawable buttonDrawable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button btn=(Button) findViewById(R.id.button1); buttonDrawable=btn.getBackground(); //Setting the image. btn.setBackgroundResource(R.drawable.ic_launcher); btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { /*Removing image and setting text*/ btn.setBackgroundDrawable(buttonDrawable); btn.setText("My Button"); } }); }
private void setButtonStyle(Button b, String text) { LayoutParams param = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f); b.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.blue_button)); b.setGravity(Gravity.CENTER); b.setText(text); b.setLayoutParams(param); b.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20); b.setTypeface(null, Typeface.BOLD); b.setShadowLayer(2, 1, 1, R.color.button_shadow_colour); b.setTextColor(context.getResources().getColor(R.color.button_text_colour)); }
btn.setBackgroundDrawable(new ColorDrawable(Color.parseColor(btnBackgroundColor )));
for (int i=0; i<=9; i++) { TableRow tr = new TableRow(this); for (int j=0; j<=9; j++) { Button button = new Button (this); button.setHeight(30); button.setWidth(30); button.setBackgroundDrawable(d); tr.addView(button, 30,30); if (bommaker[i][j]) { isBomb = true; } else { isBomb = false; } final Vakje vakjeObject = new Vakje(isBomb); //veld[i][j] = new Vakje(isBomb); button.setOnClickListener( new onClickListener() { public void onClick(View v) { vakjeObject.yourMethod(); } }); } layout.addView(tr); }
btnText.setBackgroundDrawable(getResources().getDrawable( R.drawable.text_mode_selector)); btnText.setLayoutParams(btnParams); btnSketch.setBackgroundDrawable(getResources().getDrawable( R.drawable.sketch_selector)); btnSketch.setLayoutParams(btnParams); btnColor.setBackgroundDrawable(getResources().getDrawable( R.drawable.color_selector)); btnColor.setLayoutParams(btnParams); btnUndo.setBackgroundDrawable(getResources().getDrawable( R.drawable.undo_selector)); btnUndo.setLayoutParams(btnParams); btnRedo.setBackgroundDrawable(getResources().getDrawable( R.drawable.redo_selector)); btnRedo.setLayoutParams(btnParams);