Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách đặt icon cho ứng dụng Android

Hướng dẫn này minh họa quy trình thay đổi icon mặc định của ứng dụng Android bằng icon tùy chỉnh.

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project.

Bước 2: Chuẩn bị file icon

Icon nên có định dạng PNG, nền trong suốt, kích thước đề xuất:

  • 512x512 px — cho Google Play Store
  • Các kích thước mipmap: 48x48 (mdpi), 72x72 (hdpi), 96x96 (xhdpi), 144x144 (xxhdpi), 192x192 (xxxhdpi)

Bước 3: Thêm icon vào thư mục mipmap

Đường dẫn: app → res → mipmap (bao gồm: mipmap-mdpi, mipmap-hdpi, mipmap-xhdpi, mipmap-xxhdpi, mipmap-xxxhdpi, mipmap-anydpi-v26).

Copy file icon vào từng thư mục tương ứng với độ phân giải.

Bước 4: Đổi tên file icon (tùy chọn)

Đổi tên file icon thành tên có ý nghĩa, ví dụ: ic_app_icon.png.

Bước 5: Giao diện chính (res/layout/activity_main.xml)

<?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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Bước 6: Activity (src/MainActivity.java)

package com.example.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);
    }
}

Bước 7: Cập nhật Manifest (Manifests/AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.sample">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_app_icon"        <!-- Icon chính -->
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_app_icon_round" <!-- Icon bo tròn (Android 7.1+) -->
        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>

💡 Mẹo: Sử dụng Image Asset Studio

Android Studio cung cấp công cụ tích hợp: New → Image Asset. Chọn Launcher Icons (Adaptive and Legacy), chọn file ảnh nguồn, công cụ sẽ tự động tạo tất cả kích thước cần thiết và cập nhật manifest.

Chạy ứng dụng

Kết nối thiết bị, nhấn Run. Kiểm tra icon ứng dụng trên launcher thiết bị.