कस्टम इंटरैक्टिविटी जोड़ना

इस गाइड में, कस्टम टैब में कस्टम इंटरैक्टिविटी जोड़ने का तरीका बताया गया है.

शेयर करने की डिफ़ॉल्ट कार्रवाई चालू करें

अगर आपने कस्टम शेयर ऐक्शन नहीं दिया है, तो ओवरफ़्लो मेन्यू में ब्राउज़र की डिफ़ॉल्ट शेयर करने की कार्रवाई चालू करें. इससे, उपयोगकर्ताओं को उनके देखे जा रहे कॉन्टेंट का लिंक आसानी से शेयर करने में मदद मिलती है:

    CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
    intentBuilder.setShareState(CustomTabsIntent.SHARE_STATE_ON);

कस्टम ऐक्शन बटन जोड़ें

कस्टम टैब टूलबार की मदद से, ज़रूरी कार्रवाइयों के लिए कस्टम ऐक्शन बटन जोड़ा जा सकता है. इस बटन में टेक्स्ट लेबल या कस्टम आइकॉन हो सकता है. आइकॉन की ऊंचाई 24dp और चौड़ाई 24-48 dp होनी चाहिए.

पसंद के मुताबिक शेयर करने की कार्रवाई वाला कस्टम टैब

उदाहरण के लिए, टूलबार में कस्टम शेयर ऐक्शन जोड़ा जा सकता है. ऐसा करने के लिए, एक BroadcastReceiver बनाएं. यह तब कॉल किया जाता है, जब उपयोगकर्ता कस्टम टैब में 'शेयर करें' कार्रवाई पर क्लिक करता है.

BroadCastReceiver को AndroidManifest.xml फ़ाइल में रजिस्टर करें:

<application …>
  <receiver android:name=".ShareBroadcastReceiver" />
</application>

इसके बाद, ShareBroadcastReceiver में एक नई क्लास जोड़ें. onReceive() तरीके में, इंटेंट से हाल ही में दिख रहे यूआरएल को एक्सट्रैक्ट करें और 'भेजने की इच्छा' को ट्रिगर करें.

public class ShareBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String url = intent.getDataString();
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, url);
        sendIntent.setType("text/plain");
        Intent shareIntent = Intent.createChooser(sendIntent, null);
        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(shareIntent);
    }
}

अब, ShareBroadcast के लिए PendingIntent बनाएं और उसे setActionButton() की मदद से रजिस्टर करें. लंबित इंटेंट को आइकॉन और ब्यौरे के साथ पास करें.

String shareDescription = getString(R.string.label_action_share);
Bitmap shareIcon = BitmapFactory.decodeResource(getResources(),
       android.R.drawable.ic_menu_share);

// Create a PendingIntent to your BroadCastReceiver implementation
Intent actionIntent = new Intent(
       this.getApplicationContext(), ShareBroadcastReceiver.class);
PendingIntent pendingIntent =
       PendingIntent.getBroadcast(getApplicationContext(), 0 /* request code */, actionIntent, PendingIntent.FLAG_MUTABLE);          

//Set the pendingIntent as the action to be performed when the button is clicked.
CustomTabsIntent intentBuilder = new CustomTabsIntent.Builder()
    …
    .setActionButton(shareIcon, shareDescription, pendingIntent)
    .build();

कस्टम मेन्यू आइटम जोड़ना

कस्टम टैब में, ब्राउज़र से मिलने वाली पांच डिफ़ॉल्ट कार्रवाइयां होती हैं: "फ़ॉरवर्ड करना", "पेज की जानकारी", "रीफ़्रेश करें", "पेज में ढूंढें" और "ब्राउज़र में खोलें" चुनें. इसके अतिरिक्त, आप सात और जोड़ सकते हैं. ये मेन्यू आइटम, आइकॉन पंक्ति और ब्राउज़र के दिए आइटम के बीच डाले जाएंगे. (नीचे दी गई इमेज देखें.) असल संख्या, ब्राउज़र को लागू करने के तरीके पर निर्भर करती है. (उदाहरण के लिए, Chrome के वर्शन 117 में, मेन्यू आइटम की संख्या पांच से बढ़ाकर सात कर दी गई है.) इसलिए, सबसे ज़रूरी आइटम को पहले जोड़ना बेहतर होता है.

सबसे ऊपर दाएं कोने में मौजूद तीन बिंदु वाले मेन्यू की मदद से, अपनी पसंद के मुताबिक कार्रवाइयां ऐक्सेस की जा सकती हैं:

पांच कस्टम मेन्यू आइटम वाला कस्टम टैब.

मेन्यू में कोई आइटम जोड़ने के लिए, टाइटल और PendingIntent वाले CustomTabsIntent.Builder.addMenuItem() पर कॉल करें. जब उपयोगकर्ता किसी मेन्यू आइटम पर टैप करेगा, तो ब्राउज़र PendingIntent को चालू कर देगा.

CustomTabsIntent intent = new CustomTabsIntent.Builder()
        ...
        .addMenuItem("Menu item 1", pendingIntent)
        .addMenuItem("Menu item 2", pendingIntent)
        .addMenuItem("Menu item 3", pendingIntent)
        .addMenuItem("Menu item 4", pendingIntent)
        .addMenuItem("Menu item 5", pendingIntent)
        .build();

'बंद करें' बटन को पसंद के मुताबिक बनाएं

ऐप्लिकेशन के काम करने के दौरान, कस्टम टैब को बेहतर तरीके से फ़िट करने के लिए, 'बंद करें' बटन को पसंद के मुताबिक बनाएं. अगर आपको उपयोगकर्ताओं को यह दिखाना है कि कस्टम टैब एक मॉडल डायलॉग है, तो डिफ़ॉल्ट “X” बटन का इस्तेमाल करें. अगर आपको उपयोगकर्ता को यह महसूस कराना है कि कस्टम टैब ऐप्लिकेशन फ़्लो का हिस्सा है, तो बैक ऐरो का इस्तेमाल करें.

CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
intentBuilder.setCloseButtonIcon(BitmapFactory.decodeResource(
    getResources(), R.drawable.ic_arrow_back));

सबसे नीचे मौजूद टूलबार जोड़ें

नीचे मौजूद टूलबार, कस्टम टैब में ज़्यादा सुविधाएं जोड़ने का बेहद आसान तरीका है.

सबसे नीचे मौजूद टूलबार वाला कस्टम टैब

RemoteViews ऑब्जेक्ट को CustomTabIntent.Builder.setSecondaryToolbarViews() में पास करने पर, नीचे मौजूद टूलबार को पूरी तरह से पसंद के मुताबिक बनाया जा सकता है. साथ ही, डाइनैमिक तौर पर अपडेट किया जा सकता है.

सबसे पहले, एक नई लेआउट फ़ाइल बनाकर, टूलबार के लेआउट का एलान करें, res/layout/custom_tab_toolbar.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <Button xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ct_toolbar_next"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_margin="8dp"
        android:padding="8dp"
        android:paddingStart="16dp"
        android:paddingEnd="16dp"
        android:text="Next" />

    <Button xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ct_toolbar_previous"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_margin="8dp"
        android:padding="8dp"
        android:text="Previous" />
</LinearLayout>
अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है

अगला चरण BroadcastReceiver को रजिस्टर करना है, जो AndroidManifest.xml फ़ाइल में टूलबार के इंटरैक्शन मैनेज करता है:

<application …>
  <receiver android:name=".CustomTabBottomToolbarBroadcastReceiver" />
</application>

इसके बाद, BroadcastReceiver लागू करें. यह सबसे नीचे मौजूद टूलबार के सभी इंटरैक्शन को हैंडल करेगा:

public class BottomToolbarBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String url = intent.getDataString();
        int remoteViewId = intent.getIntExtra(EXTRA_REMOTEVIEWS_CLICKED_ID, -1);
        if (remoteViewId == R.id.ct_toolbar_previous) {
          // handle previous
        } else if (remoteViewId == R.id.ct_toolbar_next) {
          // handle next
        }
    }
}

आखिर में, टूलबार रजिस्टर करें:

// Create the pending intent
Intent actionIntent = new Intent(
       this.getApplicationContext(), BottomToolbarBroadcastReceiver.class);

PendingIntent pendingIntent =
       PendingIntent.getBroadcast(getApplicationContext(), 0 /* request code */, actionIntent, PendingIntent.FLAG_MUTABLE);          

// Pass the toolbar layout to the RemoteViews instance
RemoteViews secondaryToolbarViews = new RemoteViews(getPackageName(), R.layout.custom_tab_toolbar);
// All toolbar buttons
int[] clickableIds = {R.id.ct_toolbar_next, R.id.ct_toolbar_previous};

// Register the bottom toolbar when creating a new custom tab intent
CustomTabsIntent intent = new CustomTabsIntent.Builder()
    .setSecondaryToolbarViews(secondaryToolbarViews, clickableIds, toolbarPendingIntent)
    .build();

बुकमार्क और डाउनलोड करने के बटन

तीन बिंदु वाले मेन्यू में, बुकमार्क और डाउनलोड बटन डिफ़ॉल्ट रूप से चालू होते हैं. इन्हें बंद करने के लिए, इनका इस्तेमाल करें CustomTabsIntent.Builder में कोड:

CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
    .setBookmarksButtonEnabled(false)
    .setDownloadButtonEnabled(false)
    .build();

अगला लेख: कस्टम टैब में वेब कॉन्टेंट को तेज़ी से लोड करने का तरीका जानें.