이 예제는 안드로이드 앱에서 버튼의 모서리를 둥글게 만드는 방법을 보여줍니다. 별도의 이미지 파일 없이 Drawable 리소스와 Shape XML만 활용하면, 버튼의 기본·눌림·포커스 상태별 스타일까지 한 번에 지정할 수 있습니다.
1단계: 새 프로젝트 생성
Android Studio에서 File → New Project를 선택한 후, 필요한 항목을 모두 입력하여 새 프로젝트를 생성합니다.
2단계: 레이아웃 파일 작성
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다. 버튼의 android:background 속성에 이후 작성할 커스텀 드로어블(@drawable/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 작성
src/MainActivity.java 파일에 아래 기본 코드를 추가합니다.
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단계: 둥근 버튼 드로어블 정의
res/drawable/mybutton.xml 파일을 새로 생성하고 아래 코드를 추가합니다. <selector> 태그 안에서 버튼의 세 가지 상태(눌림, 포커스, 기본)마다 각각의 <shape>를 정의하며, <corners> 태그의 radius 값으로 모서리의 둥글기를 조절할 수 있습니다. 원하는 곡률에 따라 radius 값을 늘리거나 줄이면 됩니다.
<?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단계: 매니페스트 설정
Manifests/AndroidManifest.xml 파일에 아래 코드를 추가합니다.
<?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 Studio에서 프로젝트의 액티비티 파일 중 하나를 열고 툴바의 Run 아이콘을 클릭하세요. 실행 옵션에서 자신의 모바일 기기를 선택하면, 기기 화면에 둥근 모서리가 적용된 버튼이 나타나는 것을 확인할 수 있습니다.
