Snackbar android studio kotlin

How to use Snackbar in Android Kotlin?

This example demonstrates how to use Snackbar in Android Kotlin.

Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

Step 3 − Add the following code to src/MainActivity.kt

import android.graphics.Color import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button import android.widget.TextView import com.google.android.material.snackbar.Snackbar class MainActivity : AppCompatActivity() < lateinit var button: Button override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" button = findViewById(R.id.btnSnackBar) button.setOnClickListener < val snackBar = Snackbar.make( it, "Replace with your own action", Snackbar.LENGTH_LONG ).setAction("Action", null) snackBar.setActionTextColor(Color.BLUE) val snackBarView = snackBar.view snackBarView.setBackgroundColor(Color.CYAN) val textView = snackBarView.findViewById(com.google.android.material.R.id.snackbar_text) as TextView textView.setTextColor(Color.BLUE) snackBar.show() >> >

Step 4 − Add the following code to androidManifest.xml

Let’s try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project’s activity files and click the Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Читайте также:  Finding string in array java

Click here to download the project code.

Источник

Kotlin Android – Snackbar – Example

Android Snackbar is a material design component introduced with API 22.2.0. The functionality would resemble Android Toast, but unlike Toast, Snackbar could be dismissed by user or an action listener could be setup to respond to user actions.

Snackbar when shown, is displayed at the bottom of the screen, with default background color “#323232” and text color “#FFFFFF“. You may change the background and text color. We shall go through that in the tutorial – Snackbar – Change Background and Text Color.

The following screenshot is an example, of Snackbar sliding from from bottom of the screen, displaying a message.

Android Snackbar Example

Important Note : To use Snackbar in your android application, you need to include the ‘com.android.support.design’ package in your build.gradle (app) dependencies. There are different available versions of the package. You need to include the one with version matching the compileSdkVersion.

Once you modify the build.gradle (Module:app), Android Studio asks for Gradle Sync. Sync gradle and you are good to use Snackbar in your project.

Example – Kotlin Android Snackbar

Create an Android Application with Kotlin Support and find the code for activity_main.xml layout file and MainActivity.kt provided below.

In this Kotlin Android Snackbar Example, we shall display a button, and when the button is clicked, Snackbar is displayed at the bottom of the screen.

activity_main.xml

MainActivity.kt

package com.tutorialkart.snackbarexample import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.support.design.widget.Snackbar import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() < override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) btn.setOnClickListener < val snack = Snackbar.make(it,"This is a simple Snackbar",Snackbar.LENGTH_LONG) snack.show() >> >

Run this Android Application on a physical Android Device or Android Virtual Device, and you get an Activity with the button as shown in the following screenshot. When you click on the “Show Snackbar” button, a Snackbar appears at the bottom of the screen.

Android Snackbar Example

An action could be set to Snackbar using Snackbar.setAction() method.

Conclusion

In this Kotlin Android Tutorial, we have learnt to include support library for Snackbar in the dependencies with Example Android Application. In our next tutorial, we shall learn to set action listener to the Snackbar.

Источник

Android’s Snackbar in Kotlin

The Snackbar is a view that’s mostly used to give quick feedback to the user, like Toast. It appears at the bottom of the the screen and disappears after some timeout. Sometimes the user can “swipe” the Snackbar to make it disappear. But unlike Toast, you can put some actions in the Snackbar.

Figure 1. Snackbar example

Checklist before using Snackbar in your project

  1. Update the build.gradle file to include ‘com.android.support:design’ in the dependencies. Without this, you won’t be able to use the Snackbar control. In an Android Studio project, there are 2 gradle files. One is located on the project’s root folder and the other is inside the app folder. It’s the latter that we need to update
  2. Add an android:id attribute to the layout container.You will refer to it later when you use Snackbar. The layout container doesn’t have a View ID by default, so you need to put it in
  3. Import the Kotlin Android Extensions (KAE) in your MainActivity file. This is actually optional, but it would be nice if we can refer to View Ids without using findViewById() . The KAE synthesizes all the View Ids in the layout files and exposes them as extension properties of the class Listing 1. excerpt from app/build.gradle

Listing 2. excerpt from MainActivity.Kt

import kotlinx.android.synthetic.main_activity_main.* fun whenClicked(v:View)  snack=Snackbar.make(root_layout, "Hello", Snackbar.LENGTH_LONG) snack.show() > 

First line of Listing 2 imports the Kotlin Android Extension. This lets us refer to View elements using just their IDs.

Snackbar’s make() function takes 3 parameters, they are;

  1. the View ID of the layout container, this is why you need to put an android:id=»root_layout» in the container
  2. the String message you want to show, and
  3. the amount of time, in milliseconds, that the message will be visible

Learn Android Studio 3 with Kotlin

Learn Android Studio 3 with Kotlin

Learn more. See more code samples like this

Get the Newsletter

We can send tutorials straight to your inbox.

If you find any corrections in the page or article, the best place to log that is in the site’s GitHub repo workingdev.net/issues. To start a discussion with me, the best place to do that is via twitter @lovescaffeine

The Working Dev

Code samples, snippets and tutorials for the working programmer

Источник

Android Snackbar using Kotlin – Android Studio

We usually use Snackbar widget to show a small popup message or information. Snackbar in android is a widget introduced with the Material Design library. Android Snackbar will be displayed at the bottom of the mobile screen.

Snackbars can also offer the ability to perform an action, such as undoing an action that was just taken, or retrying an action that had failed.

In this tutorial you will learn the following:

Table of contents:

1. Create a New Project:

This is a beginner’s guide, so first create a new Android Studio Project. Click on the Android Studio Icon from the Computer and create your project with necessary information.

2. Add Dependencies:

Add below dependencies at the build.gradle file. Don’t forget to synchronized after adding the dependencies.

build.gradle(:app)

implementation 'com.google.android.material:material:1.1.0'

3. Add XML code:

Now we need some buttons to show Snackbar Message. Copy and paste below xml codes to the activity_main.xml file.

4. Create 2nd Activity:

Now create a new Activity from your project. We will add an action to the Snackbar message. When you will click on the Snackbar Action button the second activity will open.

5. Add Code:

Copy below codes and paste into your MainActivity.kt file. When button will be clicked the Snackbar message will be shown.

mainActivity.kt

package com.jonyapps.androidsnackbar import android.content.Intent import android.graphics.Color import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Toast import com.google.android.material.snackbar.Snackbar import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() < override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) button_1.setOnClickListener < Snackbar.make( linerLayout, "This is Snackbar Message", Snackbar.LENGTH_LONG ).show() >button_2.setOnClickListener < Snackbar.make( linerLayout, "Snackbar- showing a Toast Message", Snackbar.LENGTH_LONG ).setAction( "Show Now" )< Toast.makeText(applicationContext, "An Action Showing!", Toast.LENGTH_SHORT).show() linerLayout >.show() > button_3.setOnClickListener < val snackbar = Snackbar.make( linerLayout, "This is a snack bar.", Snackbar.LENGTH_INDEFINITE ) snackbar.setAction("Next") < val intent = Intent(this, Main2Activity::class.java) startActivity(intent) snackbar.dismiss() >snackbar.show() > > >

6. Run the Emulator:

So, at the last, run the Emulator and test you app with the Android Virtual Device. It is done and working. Through this, you can create Snackbar message inside the Android App.

Watch Youtube Tutorial:

If you have any comments, please post below.

Источник

Kotlin Android – Snackbar – Set Action – Example

An action could be set to Snackbar using Snackbar.setAction() method. setAction takes View.OnClickListener as second argument and you may write your set of instructions to perform the action.

In this tutorial, we will learn how to display Snackbar widget on even of an action.

The following screenshot is an example for Snackbar, with a message and an actionable item next to it.

Android Snackbar SetAction

Important Note : To use Snackbar in your android application, you need to include the ‘com.android.support.design’ package in your build.gradle (app) dependencies. There are different available versions of the package. You need to include the one with version matching the compileSdkVersion.

Following variants are available for setAction method.

1. setAction(int resId, View.OnClickListener listener)

Set the action to be displayed in this BaseTransientBottomBar.

2. setAction(CharSequence text, View.OnClickListener listener)

Set the action to be displayed in this BaseTransientBottomBar.

Example – Android Snackbar – Set Action

In this example, we shall set text “DISMISS” to appear in Snackbar and an action would be taken when clicked on it.

activity_main.xml

MainActivity.kt

package com.tutorialkart.snackbarexample import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.support.design.widget.Snackbar import android.view.View import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() < override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) btn.setOnClickListener < val snack = Snackbar.make(it,"This is a simple Snackbar",Snackbar.LENGTH_LONG) snack.setAction("DISMISS", View.OnClickListener < // executed when DISMISS is clicked System.out.println("Snackbar Set Action - OnClick.") >) snack.show() > > >

Run this Android application, and an Android screen will appear as shown below with a button. Click on the button “Show Snackbar”. Snackbar appears at the bottom of the screen.

Android Snackbar SetAction

Conclusion

In this Kotlin Android Tutorial, we have learnt to set an Action Listener to the Snackbar using setAction() method. In our next tutorial, we shall learn to change the text color and background of Snackbar and Action String in it.

Источник

Оцените статью