Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Android में कस्टम नोटिफिकेशन लेआउट और टेक्स्ट कलर कैसे बनाएं?

Android में कस्टम नोटिफिकेशन लेआउट बनाने का पूरा गाइड

यह उदाहरण RemoteViews और NotificationCompat.Builder का उपयोग करके पूरी तरह से कस्टमाइज्ड नोटिफिकेशन (कस्टम लेआउट, इमेज, टेक्स्ट कलर, बटन) बनाने का तरीका दिखाता है।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में File → New Project चुनें।

चरण 2: मेन लेआउट (res/layout/notification_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">

    <Button
        android:id="@+id/btnNotification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Normal Notification"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="100dp" />

    <Button
        android:id="@+id/btnCustomNotification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Custom Notification"
        app:layout_constraintTop_toBottomOf="@id/btnNotification"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="20dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

चरण 3: कस्टम नोटिफिकेशन लेआउट (res/layout/custom_notification.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:background="#FFFFFF">

    <ImageView
        android:id="@+id/imgLeft"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/imgLeft"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:text="Custom Title"
        android:textColor="#212121"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/txtText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/imgLeft"
        android:layout_below="@id/txtTitle"
        android:layout_marginStart="16dp"
        android:text="Custom notification message"
        android:textColor="#757575"
        android:textSize="14sp" />

    <ImageView
        android:id="@+id/imgRight"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_notification_icon"
        android:padding="10dp" />

</RelativeLayout>

चरण 4: MainActivity.java

package com.app.sample;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notification_main);

        Button btnNormal = findViewById(R.id.btnNotification);
        Button btnCustom = findViewById(R.id.btnCustomNotification);

        btnNormal.setOnClickListener(v -> showNormalNotification());
        btnCustom.setOnClickListener(v -> showCustomNotification());
    }

    private void showNormalNotification() {
        Intent intent = new Intent(this, NotificationViewActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "default_channel")
            .setSmallIcon(R.drawable.ic_notification_small)
            .setContentTitle("सामान्य नोटिफिकेशन")
            .setContentText("यह एक साधारण नोटिफिकेशन है")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        createNotificationChannel(manager);
        manager.notify(1, builder.build());
    }

    private void showCustomNotification() {
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification);
        remoteViews.setTextViewText(R.id.txtTitle, "कस्टम टाइटल");
        remoteViews.setTextViewText(R.id.txtText, "कस्टम लेआउट के साथ नोटिफिकेशन");
        remoteViews.setTextColor(R.id.txtTitle, 0xFF1565C0); // नीला रंग
        remoteViews.setTextColor(R.id.txtText, 0xFF424242);  // ग्रे रंग
        remoteViews.setImageViewResource(R.id.imgLeft, R.mipmap.ic_launcher);
        remoteViews.setImageViewResource(R.id.imgRight, R.drawable.ic_notification_icon);

        Intent intent = new Intent(this, NotificationViewActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "custom_channel")
            .setSmallIcon(R.drawable.ic_notification_small)
            .setCustomContentView(remoteViews)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        createNotificationChannel(manager);
        manager.notify(2, builder.build());
    }

    private void createNotificationChannel(NotificationManager manager) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel defaultChannel = new NotificationChannel("default_channel",
                "सामान्य नोटिफिकेशन", NotificationManager.IMPORTANCE_DEFAULT);
            NotificationChannel customChannel = new NotificationChannel("custom_channel",
                "कस्टम नोटिफिकेशन", NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(defaultChannel);
            manager.createNotificationChannel(customChannel);
        }
    }
}

चरण 5: नोटिफिकेशन व्यू एक्टिविटी (NotificationViewActivity.java)

package com.app.sample;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class NotificationViewActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification_view);

        String title = getIntent().getStringExtra("title");
        String text = getIntent().getStringExtra("text");

        TextView txtTitle = findViewById(R.id.txtTitle);
        TextView txtText = findViewById(R.id.txtText);

        txtTitle.setText(title != null ? title : "कोई टाइटल नहीं");
        txtText.setText(text != null ? text : "कोई मैसेज नहीं");
    }
}

चरण 6: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.app.sample">

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

        <activity android:name=".NotificationViewActivity" />

    </application>
</manifest>

महत्वपूर्ण नोट्स

  • Android 8.0+ (API 26) में Notification Channel बनाना अनिवार्य है।
  • RemoteViews केवल सीमित व्यू और एट्रिब्यूट सपोर्ट करता है (सभी लेआउट/व्यू काम नहीं करते)।
  • एक्शन बटन जोड़ने के लिए .addAction() उपयोग करें।
  • बड़े कस्टम लेआउट के लिए setCustomBigContentView() उपयोग करें।
  1. Android में पेजिनेटिंग टेक्स्ट कैसे बनाएं? Android में पेजिनेटिंग टेक्स्ट कैसे बनाएं?

    यह उदाहरण दर्शाता है कि लंबे टेक्स्ट को पेज में बांटकर (पेजिनेशन) कैसे दिखाया जाए, जैसे ई-बुक रीडर ऐप्स में होता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: लेआउट XML res/layout/activity_main.xml - नेविगेशन बटन्स के साथ TextView: <?xml version=1.0 encoding=utf-

  1. एंड्रॉइड में कस्टम रेटिंग बार कैसे बनाएं? एंड्रॉइड में कस्टम रेटिंग बार कैसे बनाएं?

    एंड्रॉइड में कस्टम RatingBar बनाना — ड्रॉएबल, स्टाइल, और प्रोग्रामेटिक कंट्रोल एंड्रॉइड का डिफ़ॉल्ट RatingBar सितारे दिखाता है, लेकिन इसका लुक कस्टमाइज़ करना (रंग, आइकन, आकार, एनिमेशन) आम जरूरत है। यह ट्यूटोरियल **XML स्टाइलिंग** और **प्रोग्रामेटिक कंट्रोल** दोनों दिखाता है। चरण 1: नया प्रोजेक्ट बन

  1. एंड्रॉइड में टेक्स्ट फाइल कैसे बनाएं और उसमें डेटा डालें? एंड्रॉइड में टेक्स्ट फाइल कैसे बनाएं और उसमें डेटा डालें?

    इंटरनल स्टोरेज में फाइल लिखना और पढ़ना यह उदाहरण दर्शाता है कि FileOutputStream और OutputStreamWriter का उपयोग करके ऐप के इंटरनल स्टोरेज में टेक्स्ट फाइल कैसे बनाएं और लिखें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: लेआउट में EditText और बटन res/layout/activit