परिचय
एक्शन बार (टूलबार) में एक बैज आइकन दिखाना जो नोटिफिकेशन की संख्या प्रदर्शित करता है, एक सामान्य UI पैटर्न है। यह लेख एक कस्टम टूलबार लेआउट के साथ इसे लागू करने का तरीका दिखाता है।
कार्यान्वयन
चरण 1: लेआउट (activity_main.xml)
एक `Toolbar` जिसमें एक `Button` (आइकन के लिए) और उसके ऊपर एक `TextView` (बैज काउंट के लिए) शामिल है।
<Toolbar ...>
<RelativeLayout ...>
<TextView android:text="Notify Me" ... />
<RelativeLayout android:id="@+id/notificationBadge" ...>
<RelativeLayout android:id="@+id/badgeLayout" ...>
<Button android:background="@drawable/action_notification" android:layout_width="36dp" android:layout_height="36dp"/>
</RelativeLayout>
<TextView
android:id="@+id/tvBadgeNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/badgeLayout"
android:layout_alignEnd="@id/badgeLayout"
android:background="@drawable/item_count"
android:text="0"
android:textColor="#FFF"
android:textSize="16sp"
android:textStyle="bold"/>
</RelativeLayout>
</RelativeLayout>
</Toolbar>चरण 2: ड्रॉएबल (res/drawable/item_count.xml)
<shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="8dp"/> <solid android:color="#2196F3"/> <stroke android:width="1dip" android:color="#FFF"/> <padding android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp"/> </shape>
चरण 3: MainActivity.java
public class MainActivity extends AppCompatActivity {
static int notificationCount = 0;
TextView tvBadgeNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvBadgeNumber = findViewById(R.id.tvBadgeNumber);
}
public void createNotification(View view) {
// नोटिफिकेशन बनाने का कोड...
notificationCount++;
tvBadgeNumber.setText(String.valueOf(notificationCount));
}
}व्याख्या
- टूलबार में एक कस्टम लेआउट का उपयोग किया जाता है जिसमें आइकन बटन और बैज टेक्स्ट व्यू होता है।
- `item_count.xml` ड्रॉएबल बैज के लिए गोलाकार पृष्ठभूमि बनाता है।
- हर बार नोटिफिकेशन बनने पर काउंट बढ़ाया जाता है और बैज अपडेट किया जाता है।
- वास्तविक ऐप में, काउंट को SharedPreferences या डेटाबेस में स्थायी किया जाना चाहिए।