Buttons in Android Studio: Create, Style and Handle Clicks (Java)
Learn buttons from scratch: add them in XML, style them, and make them do something when tapped. A perfect first lesson if you are just starting with Android.
In Android Studio choose New Project → Empty Views Activity, with Language Java and Minimum SDK API 24. Material buttons come with the project; nothing to add.
2. Add a button and handle taps
In activity_main.xml, add a Button with an android:id and android:text. In MainActivity, find it with findViewById and give it a click listener:
A plurals resource picks "1 time" or "2 times" for you.
3. Long press
setOnLongClickListener runs when the button is held. Return true to say you handled it, so a normal click doesn't follow. Here it resets the counter.
4. Five styles
Material 3 has five button styles, from most to least important: Filled, Tonal, Elevated, Outlined and Text. Use Filled for the main action on a screen and lighter styles for the rest. Each is just a different style; the code is the same.
5. One listener for many buttons
Instead of five listeners, make one and attach it to all five. The v passed in is the button that was tapped, so ((Button) v).getText() tells you which.
6. Add an icon
Use MaterialButton with app:icon and app:iconGravity="textStart". In code, setIconResource swaps the icon, here from an outline heart to a filled one.
7. Your own shape and colour
app:cornerRadius rounds the corners (28dp makes a pill), android:backgroundTint sets the colour, and app:strokeColor with app:strokeWidth adds a border.
8. Turn a button on and off
Start with android:enabled="false". When the checkbox changes, call setEnabled(checked). A disabled button is greyed out and ignores taps.
9. Pick one with a toggle group
MaterialButtonToggleGroup with app:singleSelection="true" works like tabs: only one button stays selected. addOnButtonCheckedListener tells you which.
10. Run it
Try each section from top to bottom.
Tips
Make buttons at least 48dp tall so they're easy to tap; these are 56dp.
Keep button text short and clear: say what happens, like Save or Continue.
What's improved over the video
All five Material 3 styles side by side, plurals for the counter, long press, icons, a disabled state and a toggle group, with every repeated setting written once as a style.
Files affected
Check what to add and what to replace before copying code.
One example of each: taps and long press, one listener for many buttons, icon, pill, enable/disable and a toggle group.
MainActivity.java
packagecom.example.buttons;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.CheckBox;importandroid.widget.TextView;importandroidx.appcompat.app.AppCompatActivity;importcom.google.android.material.button.MaterialButton;importcom.google.android.material.button.MaterialButtonToggleGroup;importcom.google.android.material.snackbar.Snackbar;/**
* Everything about buttons on one screen: handling taps, the Material 3 styles, icons,
* custom shapes and colours, turning a button on and off, and a group of toggle buttons.
*/publicclassMainActivityextendsAppCompatActivity{privateint taps =0;privateboolean liked =false;@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);Screens.setUp(this,R.layout.activity_main);View root =findViewById(R.id.main);// 1. The basic button: count taps, and reset with a long press.Button tapMe =findViewById(R.id.btnTap);TextView tapCount =findViewById(R.id.textTapCount);
tapMe.setOnClickListener(v ->{
taps++;
tapCount.setText(getResources().getQuantityString(R.plurals.taps, taps, taps));});
tapMe.setOnLongClickListener(v ->{
taps =0;
tapCount.setText(R.string.tap_hint);returntrue;// true means "handled", so a normal click doesn't follow.});// 2. Five styles, one listener: the tapped button tells us which one it was.View.OnClickListener showStyle = v ->Snackbar.make(root,getString(R.string.you_tapped,((Button) v).getText()),Snackbar.LENGTH_SHORT).show();for(int id :newint[]{R.id.btnFilled,R.id.btnTonal,R.id.btnElevated,R.id.btnOutlined,R.id.btnText}){findViewById(id).setOnClickListener(showStyle);}// 3. An icon that changes when tapped.MaterialButton like =findViewById(R.id.btnLike);
like.setOnClickListener(v ->{
liked =!liked;
like.setIconResource(liked ?R.drawable.ic_heart :R.drawable.ic_heart_outline);
like.setText(liked ?R.string.liked :R.string.like);});// 4. A custom pill button.findViewById(R.id.btnPill).setOnClickListener(v ->Snackbar.make(root,R.string.pill_tapped,Snackbar.LENGTH_SHORT).show());// 5. A button that stays off until the box is ticked.Button next =findViewById(R.id.btnContinue);CheckBox agree =findViewById(R.id.checkAgree);
agree.setOnCheckedChangeListener((box, checked)-> next.setEnabled(checked));
next.setOnClickListener(v ->Snackbar.make(root,R.string.continued,Snackbar.LENGTH_SHORT).show());// 6. A toggle group: like tabs, only one button is selected at a time.MaterialButtonToggleGroup range =findViewById(R.id.groupRange);TextView rangeText =findViewById(R.id.textRange);
range.addOnButtonCheckedListener((group, checkedId, isChecked)->{if(!isChecked)return;
rangeText.setText(getString(R.string.showing,((Button)findViewById(checkedId)).getText()));});}}