DEV
DEV

Reputation: 41

Android snackbar

in my app i'm using a download DB sql lite and i have set this code to return a message for terminated download

protected String doInBackground(String... params) {


        if(first) {
            File dbPath = getDatabasePath(DatabaseHelper.DB_NAME);
            if (dbPath.exists()) {
                DBProvider provider = new DBProvider(MainActivity.this);
                database_comune_dao commune_dao = new database_comune_dao(provider.getDb());
                FirstProjectApplication.allComunes.clear();
                FirstProjectApplication.allComunes = commune_dao.getAllComune();


            }


            return "SUCCESS";

        }

i would like to change the return message with the new Snackbar of Material Design and this is the code

Snackbar.with(getApplicationContext()) // context
                .text("SUCCESS") // text to display
                .show(this);

i have try to change return whit this code but i have error. Any idea how to set this snackbar for this?

Thanks

Upvotes: 0

Views: 2614

Answers (2)

Ravi
Ravi

Reputation: 35539

you need to use SnackBar with following this syntax :

Snackbar.make(view, message, duration)
        .setAction(action message, click listener)
        .show();

you can avoid setAction() but make() and show() must be there, for more information please refer this

Upvotes: 1

Daniel Orozco
Daniel Orozco

Reputation: 185

You must use:

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private CoordinatorLayout coordinatorLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
        Snackbar.make(coordinatorLayout,"Your text",Snackbar.LENGTH_SHORT).show();
    }

}

mainactivity.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

</android.support.design.widget.CoordinatorLayout>

build.gradle

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:design:23.1.0'
}

Result:

Snackbar

Upvotes: 2

Related Questions