final Drawable positiveButtonDrawable = getResources().getDrawable(R.drawable.alert_dialog_button_light_green); if (Build.VERSION.SDK_INT >= 16) { negativeButton.setBackground(negativeButtonDrawable); positiveButton.setBackground(positiveButtonDrawable); } else { negativeButton.setBackgroundDrawable(negativeButtonDrawable);
Button button = (Button) findViewById(R.id.camButton); if(camPressed){ button.setBackground(getResources().getDrawable(R.drawable.cam_pressed)) } else{ //other stuff }
... Button brownieButton = (Button)findViewById(R.id.myBrownieButton); switch (brownieType) { // brownieType would be an int indicating which State List is needed case 0: brownieButton.setBackground(getResources().getDrawable(R.id.brownie_selector0)); break; case 1: brownieButton.setBackground(getResources().getDrawable(R.id.brownie_selector1)); break; ... }
public void setIcontoButton(Button targetButton, Drawable icon) { int sdk = android.os.Build.VERSION.SDK_INT; if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) { targetButton.setBackgroundDrawable(icon) } else { targetButton.setBackground(icon); } }
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Button b = (Button) view.findViewById(R.id.buttonPlay); b.setBackground(c.getResources().getDrawable(R.drawable.ic_action_play)); your_adapter.notifyDataSetChanged(); } });
public static void setBacground(Button buttonA, Drawable draw) { int sdk = android.os.Build.VERSION.SDK_INT; if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) { buttonA.setBackgroundDrawable( draw); } else { buttonA.setBackground( draw); } }
// assuming "mainMenuButtons" (or whatever) contains your first four objects // note I don't know what color (84, 209, 241) actually is for(Button btn : mainMenuButtons) { btn.setBackground(new java.awt.Color(84, 209, 241)); }
Button bProfile; // your Button Bitmap bitmap; // your bitmap if(android.os.Build.VERSION.SDK_INT < 16) { bProfile.setBackgroundDrawable(new BitmapDrawable(getResources(), bitmap)); } else { bProfile.setBackground(new BitmapDrawable(getResources(),bitmap)); }
public void Favorites(View V) { Button star = (Button) findViewById(R.id.buttonStar); if(star.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.btn_star_off).getConstantState())) { star.setBackground(R.drawable.btn_star_on); } else { star.setBackground(R.drawable.btn_star_off); } }
Button bProfile; // your Button Bitmap bitmap; // your bitmap if(android.os.Build.VERSION.SDK_INT < 16) { bProfile.setBackgroundDrawable(new BitmapDrawable(getResources(), bitmap)); } else { bProfile.setBackground(new BitmapDrawable(getResources(),bitmap)); }
Button b = (Button) findViewById(R.id.button); if (b instanceof AppCompatButton) { ((AppCompatButton)b).setSupportBackgroundTintList(null); } Drawable d = b.getBackground(); d = DrawableCompat.wrap(d); DrawableCompat.setTintList(d, getResources().getColorStateList(...)); b.setBackground(d); // or setBackgroundDrawable on older platforms
Button b = (Button) findViewById(R.id.button); Drawable d = b.getBackground(); d = DrawableCompat.wrap(d); DrawableCompat.setTintList(d, getResources().getColorStateList(...)); b.setBackground(d); // or setBackgroundDrawable on older platforms
public void actionPerformed(ActionEvent e) { button_change_clicked(); button_change.setBackground(colors[current_value]); } });
final Iterator<Button> buttonI = buttons.iterator(); final Iterator<Territory> territoryI = territories.iterator(); while (territoryI.hasNext() && buttonI.hasNext()) { final Button button = buttonI.next(); final Territory territory = territoryI.next(); button.setBackground(territory.getOwner().getColor()); button.setLabel(territory.units()); }
Button button = new Button(getContext()); Bitmap backgroundBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_button); BitmapDrawable backgroundDrawable = new BitmapDrawable(getResources(), backgroundBitmap); backgroundDrawable.setGravity(Gravity.CENTER); // also LEFT, CENTER_VERTICAL, etc. backgroundDrawable.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP)); button.setBackground(backgroundDrawable);
Button button = new Button(null, imageView); // quadratic grey background button.setBackground(new Background(new BackgroundFill(Color.GREY, CornerRadii.EMPTY, Insets.EMPTY))); // no padding button.setPadding(Insets.EMPTY);
StateListDrawable content = new StateListDrawable(); Drawable pressedDrawable = new BitmapDrawable(getResources(),bitmap); //Your other drawables (focused, .....) content.addState(new int[] { android.R.attr.state_pressed }, pressedDrawable); //Add the other drawables the same way Button button = (Button) view.findViewById(R.id.my_button); button.setBackground(content);
private void configDot(Button b, int size, int margin){ b.setBackground(getResources().getDrawable(R.drawable.roundbutton)); LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(getPxFromDp(size), getPxFromDp(size)); p.gravity = Gravity.CENTER_HORIZONTAL; p.setMargins(0, margin, 0, margin); b.setLayoutParams(p); } private int getPxFromDp(int dp){ return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics()); }
MyDrawable drawable = new MyDrawable(...); MyDrawable drawablePressed = new MyDrawable(...); MyDrawable drawableFocused = new MyDrawable(...); StateListDrawable stateDrawable = new StateListDrawable(); stateDrawable.addState(new int[]{android.R.attr.state_pressed}, drawablePressed); stateDrawable.addState(new int[]{android.R.attr.state_focused}, drawableFocused); stateDrawable.addState(new int[]{}, drawable); Button button = (Button) findViewById(R.id.button); button.setBackground(stateDrawable);
public void init() { setBackground(Color.cyan); // see Graphics chapter. Panel p = new Panel(); p.setBackground(Color.red); p.add(applyB = new Button("Apply")); applyB.setBackground(Color.white); p.add(exitB = new Button("Exit")); exitB.setForeground(Color.red); add(p); // add (connect) "p" to "this", the Applet } }