Giới thiệu
Bài viết hướng dẫn cách ẩn tiêu đề (title) của Action Bar nhưng vẫn giữ Action Bar hiển thị (để chứa menu, icon, v.v.) bằng setDisplayShowTitleEnabled(false).
Các bước thực hiện
Bước 1: Tạo project mới
Android Studio → File → New Project → Empty Activity.
Bước 2: Giao diện (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">
<TextView
android:id="@+id/tvStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Action Bar title disabled"
android:textSize="18sp" />
</LinearLayout>
Bước 3: Code (MainActivity.kt)
package com.example.hideactionbartitle
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Set title/subtitle (sẽ không hiển thị)
supportActionBar?.title = "Home"
supportActionBar?.subtitle = "Subtitle"
// Ẩn title hiển thị
supportActionBar?.setDisplayShowTitleEnabled(false)
tvStatus.text = "Action Bar title đã ẩn (nhưng Action Bar vẫn còn)"
}
}
Các cách ẩn liên quan
| Mục tiêu | Code | Kết quả |
|---|---|---|
| Ẩn title text | setDisplayShowTitleEnabled(false) |
Action Bar còn, menu/icon còn, title/subtitle ẩn |
| Ẩn toàn bộ Action Bar | supportActionBar?.hide() |
Action Bar biến mất hoàn toàn |
| Dùng Theme NoActionBar | Theme.MaterialComponents.DayNight.NoActionBar | Không tạo Action Bar từ đầu (tốt nhất) |
| Ẩn Home/Up icon | setDisplayShowHomeEnabled(false) |
Ẩn logo/icon/app icon |
Khi nào dùng cách nào?
- setDisplayShowTitleEnabled(false): Muốn custom title view (TextView riêng, font, màu, vị trí tùy chỉnh) nhưng vẫn dùng Action Bar cho menu.
- supportActionBar?.hide(): Tạm ẩn Action Bar (ví dụ: fullscreen video, đọc sách).
- Theme NoActionBar: Mặc định không có Action Bar, tự quản lý Toolbar trong layout (khuyên dùng cho app hiện đại).
Custom Title View (Thay thế title mặc định)
<!-- layout -->
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary">
<TextView
android:id="@+id/customTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Title"
android:textColor="@android:color/white"
android:textSize="20sp"
android:fontFamily="@font/custom_font" />
</androidx.appcompat.widget.Toolbar>
// Activity setSupportActionBar(toolbar) supportActionBar?.setDisplayShowTitleEnabled(false) // Ẩn title mặc định // customTitle đã trong layout, tự do style
Lưu ý
- Nếu dùng
supportActionBar?.hide()rồishow()sau, title sẽ xuất hiện lại theo giá trị đã set. - Với Toolbar tùy chỉnh, không cần ẩn title mặc định nếu không setSupportActionBar.