एंड्रॉइड में एक्शन बार का सबटाइटल प्राप्त करना
यह सरल ट्यूटोरियल दिखाता है कि getSupportActionBar().getSubtitle() का उपयोग करके एक्शन बार का वर्तमान सबटाइटल कैसे प्राप्त करें।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: लेआउट (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:textSize="30sp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
चरण 3: MainActivity कोड (java/MainActivity.java)
package com.example.myapplication;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
if (getSupportActionBar() != null) {
// सबटाइटल सेट करें
getSupportActionBar().setSubtitle("सैराम");
// सबटाइटल प्राप्त करें
CharSequence subtitle = getSupportActionBar().getSubtitle();
textView.setText("सबटाइटल: " + subtitle);
} else {
textView.setText("एक्शन बार उपलब्ध नहीं");
}
}
}
सबटाइटल उपयोग केस
- यूजर नाम/ईमेल दिखाना
- वर्तमान स्क्रीन संदर्भ (जैसे "सेटिंग्स > प्रोफाइल")
- कनेक्शन स्टेटस (ऑनलाइन/ऑफलाइन)
- ऐप वर्जन/बिल्ड जानकारी
- चयनित आइटम काउंट (मल्टी-सेलेक्ट मोड)
सबटाइटल स्टाइलिंग
<!-- styles.xml में -->
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="actionBarStyle">@style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="Widget.MaterialComponents.ActionBar.Primary">
<item name="subtitleTextAppearance">@style/MySubtitleText</item>
</style>
<style name="MySubtitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Subtitle">
<item name="android:textColor">@android:color/white</item>
<item name="android:textSize">14sp</item>
<item name="android:fontFamily">sans-serif-medium</item>
</style>
टूलबार में सबटाइटल
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setSubtitle("कस्टम सबटाइटल");
// या लेआउट में
<androidx.appcompat.widget.Toolbar
android:subtitle="सबटाइटल"
android:subtitleTextColor="@android:color/white"
android:subtitleTextAppearance="@style/MySubtitle" />
ऐप टेस्ट करें
चलाएं - "सबटाइटल: सैराम" दिखेगा।