স্ন্যাকবার অ্যান্ড্রয়েডের টোস্টের মতোই কিন্তু এটি অ্যাকশনের সাথে ইন্টারঅ্যাক্ট করতে যাচ্ছে। এটি অন্যান্য ভিউয়ের সাথে কোনো ইন্টারঅ্যাকশন ছাড়াই স্ক্রিনের নীচে বার্তাটি দেখাবে এবং টাইম-আউটের পরে স্বয়ংক্রিয়ভাবে বন্ধ হয়ে যাবে৷
এই উদাহরণটি দেখায় কিভাবে Android Snackbar সংহত করতে হয়।
ধাপ 1 − অ্যান্ড্রয়েড স্টুডিওতে একটি নতুন প্রকল্প তৈরি করুন, ফাইল ⇒ নতুন প্রকল্পে যান এবং একটি নতুন প্রকল্প তৈরি করতে সমস্ত প্রয়োজনীয় বিবরণ পূরণ করুন৷
ধাপ 2 - build.gradle খুলুন এবং ডিজাইন সমর্থন লাইব্রেরি নির্ভরতা যোগ করুন।
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.andy.myapplication"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
} ধাপ 3 − res/layout/activity_main.xml-এ নিম্নলিখিত কোড যোগ করুন।
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:id="@+id/layout" android:layout_height="match_parent"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="Click here" /> </android.support.design.widget.CoordinatorLayout>
উপরের কোডে, আমরা লেআউট আইডি ঘোষণা করেছি কারণ স্ন্যাকব্যাকের জন্য প্যারেন্ট ভিউ প্রয়োজন। ব্যবহারকারী বোতামে ক্লিক করলে এটি নীচে স্ন্যাকবার খুলবে।
পদক্ষেপ 4৷ − src/MainActivity.java
-এ নিম্নলিখিত কোড যোগ করুনimport android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
CoordinatorLayout coordinatorLayout;
@TargetApi(Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
coordinatorLayout=findViewById(R.id.layout);
Button button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Welcome to tutorialspoint.com", Snackbar.LENGTH_LONG)
.setAction("Click", new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = "http://www.tutorialspoint.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
// Changing message text color
snackbar.setActionTextColor(Color.RED);
// Changing action button text color
View sbView = snackbar.getView();
TextView textView = (TextView) sbView.findViewById(
android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.YELLOW);
snackbar.show();
}
});
}
} উপরোক্ত ক্ষেত্রে ব্যবহারকারী যখন বোতামে ক্লিক করেন, তখন নিচের মতন স্ন্যাকবার দেখাবে -
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Welcome to tutorialspoint.com", Snackbar.LENGTH_LONG)
.setAction("Click", new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = "http://www.tutorialspoint.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
// Changing message text color
snackbar.setActionTextColor(Color.RED);
// Changing action button text color
View sbView = snackbar.getView();
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.YELLOW);
snackbar.show(); উপরের কোডে, আমরা tutorialspoint.com-এ স্বাগতম হিসাবে বার্তা সহ স্ন্যাকবার ঘোষণা করেছি এবং "ক্লিক করুন" হিসাবে একটি বোতাম। ব্যবহারকারী ক্লিক বোতামে ক্লিক করলে এটি ডিফল্ট ব্রাউজারে একটি টিউটোরিয়াল পয়েন্ট ওয়েব সাইট খুলবে।
ধাপ 5 - manifest.xml
-এ নিম্নলিখিত কোড যোগ করুন<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.andy.myapplication"> <uses-permission android:name="android.permission.INTERNET" /> <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> </application> </manifest>
উপরের কোডে, আমরা ওয়েব সাইট দেখানোর জন্য ইন্টারনেট অনুমতি ঘোষণা করেছি।
আপনার অ্যাপ্লিকেশন চালানোর চেষ্টা করা যাক. আমি ধরে নিচ্ছি আপনি আপনার কম্পিউটারের সাথে আপনার আসল অ্যান্ড্রয়েড মোবাইল ডিভাইসটি সংযুক্ত করেছেন৷ একটি অ্যান্ড্রয়েড স্টুডিও থেকে অ্যাপটি চালানোর জন্য, আপনার প্রকল্পের কার্যকলাপ ফাইলগুলির একটি খুলুন এবং টুলবার থেকে রান আইকনে ক্লিক করুন। একটি বিকল্প হিসাবে আপনার মোবাইল ডিভাইস নির্বাচন করুন এবং তারপরে আপনার মোবাইল ডিভাইসটি পরীক্ষা করুন যা আপনার ডিফল্ট স্ক্রীন প্রদর্শন করবে -

আপনি যখন একটি বোতামে ক্লিক করবেন, এটি উপরে দেখানো হিসাবে ক্লিক বোতাম সহ স্ন্যাকবার বার্তা দেখাবে।

আপনি "ক্লিক" বোতামে ক্লিক করলে এটি tutorialspoint.com ওয়েবসাইটের সাথে ডিফল্ট ব্রাউজার খুলবে।