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

एंड्रॉइड नोटिफिकेशन में टेक्स्ट को मार्की (स्क्रॉल) कैसे करें?

कस्टम लेआउट से मार्की/स्क्रॉलिंग टेक्स्ट नोटिफिकेशन

स्टैंडर्ड नोटिफिकेशन में टेक्स्ट मार्की नहीं होता। इसके लिए RemoteViews से कस्टम लेआउट बनाएं जिसमें TextView पर ellipsize="marquee" और singleLine="true" सेट हो।

चरण 1: प्रोजेक्ट और मेन लेआउट

सामान्य बटन वाला लेआउट।

चरण 2: कस्टम नोटिफिकेशन लेआउट

res/layout/custom_notification_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:padding="10dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_marginEnd="10dp"
        android:contentDescription="@string/app_name"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/image"
        android:text="Testing"
        android:textColor="#000"
        android:textSize="13sp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_toEndOf="@id/image"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s..."
        android:textColor="#000"
        android:textSize="13sp" />
</RelativeLayout>

चरण 3: MainActivity — RemoteViews उपयोग करें

package app.tutorialspoint.com.notifyme;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RemoteViews;

public class MainActivity extends AppCompatActivity {
    public static final String NOTIFICATION_CHANNEL_ID = "10001";
    private final static String default_notification_channel_id = "default";

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

    public void createNotification(View view) {
        // कस्टम लेआउट से RemoteViews बनाएं
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this, default_notification_channel_id);

        mBuilder.setContent(contentView); // कस्टम व्यू सेट करें
        mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground); // फिर भी जरूरी
        mBuilder.setAutoCancel(true);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
            mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
            assert mNotificationManager != null;
            mNotificationManager.createNotificationChannel(notificationChannel);
        }

        assert mNotificationManager != null;
        mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
    }
}

चरण 4: मैनिफेस्ट

मानक।

मार्की काम करने की शर्तें

  • android:ellipsize="marquee"
  • android:singleLine="true"
  • android:layout_width="wrap_content" या निश्चित चौड़ाई
  • व्यू को फोकस मिलना चाहिए — नोटिफिकेशन में RemoteViews ऑटोमैटिकली फोकस नहीं पाता

⚠️ सीमाएं

  • नोटिफिकेशन में RemoteViews मार्की हमेशा काम नहीं करता (सिस्टम फोकस नहीं देता)
  • बेहतर विकल्प: BigTextStyle — लंबा टेक्स्ट एक्सपैंड करके पढ़ने दें
  • या setStyle(new BigTextStyle().bigText(longText)) उपयोग करें

एंड्रॉइड 12+ (API 31) में बदलाव

कस्टम RemoteViews नोटिफिकेशन पर प्रतिबंध बढ़ गए हैं। मीडिया प्लेबैक, कॉलिंग जैसे खास केस के अलावा कस्टम लेआउट डिप्रिकेटेड माने जाते हैं। स्टैंडर्ड स्टाइल (BigText, Inbox, BigPicture) उपयोग करें।

  1. Android ऐप में मार्की (Marquee) टेक्स्ट कैसे उपयोग करें? Android ऐप में मार्की (Marquee) टेक्स्ट कैसे उपयोग करें?

    Android में मार्की (स्क्रॉलिंग) टेक्स्ट बनाने का गाइड यह ट्यूटोरियल दिखाता है कि कैसे TextView में लंबे टेक्स्ट को क्षैतिज रूप से स्क्रॉल (मार्की प्रभाव) कराया जाए। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं। चरण 2: लेआउट फ़ाइल में कोड जोड़ें res/lay

  1. Android में व्यू की स्थिति को डायनामिकली कैसे सेट करें? Android में व्यू की स्थिति को डायनामिकली कैसे सेट करें?

    Android में व्यू की स्थिति प्रोग्रामेटिकली बदलने का गाइड यह ट्यूटोरियल दिखाता है कि कैसे RelativeLayout.LayoutParams और addRule() का उपयोग करके रनटाइम पर एक व्यू (TextView) की स्थिति को दूसरे व्यू (Button) के सापेक्ष बदला जाए। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनकर न

  1. Android ऐप में होम बटन कैसे ओवरराइड करें? Android ऐप में होम बटन कैसे ओवरराइड करें?

    होम बटन दबाने का पता लगाने के लिए onUserLeaveHint() का उपयोग - लेकिन होम बटन को पूरी तरह ओवरराइड/ब्लॉक नहीं किया जा सकता। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: लेआउट res/layout/activity_main.xml: <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns: