【Android】ボタンの角を丸くする方法を徹底解説!カスタムドローアブルで実装する手順
このチュートリアルでは、Androidアプリでボタンの角を丸く表示する方法を、実際のコード例とともにわかりやすく解説します。カスタムドローアブル(Drawable)を活用することで、通常時・フォーカス時・押下時といったボタンの状態に応じた角丸デザインも柔軟に実現できます。
手順1:Android Studioで新規プロジェクトを作成する
まずはAndroid Studioを起動し、メニューから「File → New Project」を選択して新しいプロジェクトを作成しましょう。必要な項目をすべて入力し、プロジェクトのセットアップを完了させてください。
手順2:レイアウトファイル(activity_main.xml)を編集する
res/layout/activity_main.xml に以下のコードを追加します。ポイントは、Buttonタグの android:background 属性に、後ほど作成するカスタムドローアブル「mybutton」を指定している点です。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#ffffff"
android:background="@drawable/mybutton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Buttons" />
</androidx.constraintlayout.widget.ConstraintLayout>
手順3:MainActivity.java を編集する
src/MainActivity.java には、以下のように標準的なコードを記述します。特別な処理は不要で、レイアウトを読み込むだけでOKです。
package com.app.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
手順4:カスタムドローアブル(mybutton.xml)を作成する
ここが本記事の核心部分です。res/drawable フォルダに mybutton.xml を新規作成し、以下のコードを追加してください。<selector> 要素を使うことで、ボタンの状態(押下中・フォーカス中・通常時)ごとに異なる角丸スタイルを定義できます。
- <corners android:radius="3dip"> … 角の丸みの半径を指定します。数値を大きくするとより丸くなります。
- <stroke> … ボタンの枠線の太さと色を指定します。
- <gradient> … グラデーション背景を、<solid> は単色背景を設定します。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#345953" android:endColor="#689a92" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle">
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<solid android:color="#58857e"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e" />
</shape>
</item>
</selector>
手順5:AndroidManifest.xml を確認する
Manifests/AndroidManifest.xml が以下のようになっていることを確認してください。特別な権限は不要ですが、MainActivityがランチャーとして登録されている必要があります。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.app.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
アプリを実行して動作を確認しよう
それでは、実際にアプリを実行してみましょう。Android端末をPCにUSB接続した状態で、Android Studioのツールバーにある「Run」アイコンをクリックします。デバイス選択画面で自分のスマートフォンを選択すると、実機の画面に角丸ボタンが表示されるはずです。

角の丸みを調整したい場合は、android:radius の値を変更するだけでOKです。ぜひ色やグラデーションも自由にカスタマイズして、オリジナルのボタンデザインを作ってみてください。
-
Androidのホームボタンをダブルタップしてカメラを起動する方法
スマートフォンの中には、カメラアプリをワンタップで起動できる専用ボタンを搭載した機種もありますが、多くの機種ではアプリドロワー(またはホーム画面)から手動でカメラアプリを開く必要があります。Androidはカスタマイズ性の高さで知られており、実はホームボタンからカメラアプリを直接起動できるようにする方法があります。これを設定しておけば、端末のメニューをたどる手間が省け、従来の方法よりもはるかに素早くカメラを立ち上げられます。急いでいるときに決定的な瞬間を撮りたい——そんな場面でも、ホームボタンを押すだけでカメラアプリを起動できます。アプリドロワーから開くよりも格段に速いでしょう。ここでは、An
-
Androidの主要レイアウト徹底解説:種類・特徴・使い分けのポイント
AndroidアプリのUI設計において、LinearLayout、RelativeLayout、ConstraintLayout、TableLayout、FrameLayout など、選択できるレイアウトは実に多彩です。では、どれを使うのが最適なのでしょうか?各レイアウトの詳細に入る前に、まずはViewオブジェクトの階層構造と、Androidの描画プロセスについて確認しておきましょう。この基礎知識があると、各レイアウトの挙動やパフォーマンス特性がぐっと理解しやすくなります。ViewとViewGroupViewGroupは、あらゆるViewの親クラスであり、同時にレイアウトの基底クラスでもありま