Hướng dẫn này minh họa cách ẩn (invisible) hoặc xóa (gone) Button trong Android sử dụng Kotlin.
Bước 1: Tạo dự án mới
Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.
Bước 2: Thiết kế giao diện (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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@android:color/white"
android:padding="4dp"
android:text="Click to hide me"
android:textColor="@android:color/holo_red_dark"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
Bước 3: Viết code MainActivity.kt
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val button: Button = findViewById(R.id.button)
button.setOnClickListener {
button.visibility = View.INVISIBLE
Toast.makeText(baseContext, "Button is hidden", Toast.LENGTH_SHORT).show()
}
}
}
Bước 4: Cấu hình AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.q11">
<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>
Chạy ứng dụng
Kết nối thiết bị, nhấn Run. Nhấn button "Click to hide me" → Button biến mất (INVISIBLE), Toast hiển thị "Button is hidden".
Sự khác biệt: INVISIBLE vs GONE
| Thuộc tính | Hiển thị | Chiếm không gian layout | Nhận touch event |
|---|---|---|---|
View.VISIBLE | Hiện | Có | Có |
View.INVISIBLE | Ẩn | Vẫn chiếm | Không |
View.GONE | Ẩn | Không chiếm | Không |
Ví dụ sử dụng GONE (xóa hoàn toàn khỏi layout)
button.setOnClickListener {
button.visibility = View.GONE // Button biến mất, các view khác dãn ra lấp đầy
Toast.makeText(this, "Button removed from layout", Toast.LENGTH_SHORT).show()
}
Xóa Button hoàn toàn khỏi ViewGroup (parent)
val parent = button.parent as? ViewGroup parent?.removeView(button) // Button bị destroy, không thể hiện lại được
Animation khi ẩn/hiện (Bonus)
// Fade out
button.animate().alpha(0f).setDuration(300).withEndAction {
button.visibility = View.GONE
}.start()
// Fade in
button.visibility = View.VISIBLE
button.alpha = 0f
button.animate().alpha(1f).setDuration(300).start()