이 예제는 Android 알림(Notification)에서 텍스트 콘텐츠가 화면에 흐르는 마키(marquee) 효과를 구현하는 방법을 보여줍니다. 커스텀 알림 레이아웃을 활용하면 텍스트 스크롤 효과는 물론, 알림의 배경색 변경처럼 다양한 디자인 요소도 자유롭게 적용할 수 있습니다.
1단계 — 새 프로젝트 만들기
Android Studio에서 File → New Project로 이동해 새 프로젝트를 생성하고, 필요한 모든 세부 정보를 입력합니다.
2단계 — res/layout/activity_main.xml에 코드 추가
메인 레이아웃에는 알림을 생성하는 버튼 하나를 배치합니다.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="16dp"
android:onClick="createNotification"
android:text="create notification" />
</RelativeLayout>3단계 — res/layout/custom_notification_layout.xml에 코드 추가
알림에 사용할 커스텀 레이아웃입니다. android:background 속성으로 알림의 배경색을 지정하고, TextView에 ellipsize="marquee"와 singleLine="true"를 설정해 긴 텍스트가 한 줄로 흐르도록 만듭니다.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="64dp"
android:background="@color/colorAccent"
android:padding="10dp">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentStart="true"
android:layout_marginEnd="10dp"
android:contentDescription="@string/app_name"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/image"
android:text="Testing"
android:textColor="#000"
android:textSize="13sp" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_toEndOf="@id/image"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."
android:textColor="#000"
android:textSize="13sp" />
</RelativeLayout>4단계 — src/MainActivity에 코드 추가
버튼을 클릭하면 커스텀 레이아웃이 적용된 알림을 생성하도록 MainActivity를 작성합니다. Android 8.0(Oreo) 이상에서는 반드시 알림 채널을 먼저 생성해야 한다는 점에 유의하세요.
package app.tutorialspoint.com.notifyme;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RemoteViews;
public class MainActivity extends AppCompatActivity {
public static final String NOTIFICATION_CHANNEL_ID = "10001";
private final static String default_notification_channel_id = "default";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void createNotification(View view) {
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this, default_notification_channel_id);
mBuilder.setContent(contentView);
mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
mBuilder.setAutoCancel(true);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
assert mNotificationManager != null;
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
}
}5단계 — AndroidManifest.xml에 코드 추가
마지막으로 매니페스트 파일에 진동 권한과 메인 액티비티 설정을 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.tutorialspoint.com.notifyme">
<uses-permission android:name="android.permission.VIBRATE" />
<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 스마트폰이 컴퓨터에 연결되어 있다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤 툴바의 Run 아이콘을 클릭하고, 목록에서 본인의 모바일 기기를 선택하세요. 앱이 설치되면 기기에 기본 화면이 표시됩니다.
화면 중앙의 버튼을 누르면 커스텀 레이아웃이 적용된 알림이 생성되며, 지정한 배경색 위에서 긴 텍스트가 좌우로 흐르는 마키 효과를 확인할 수 있습니다.
