Hossein Jafari Hossein Jafari - 2 months ago 14
Android Question

Error In Snack Bar Android with this,getActivity,etc

i try to show snack bar when activity started but don't work.

final Snackbar snackbar = Snackbar.make(???, "test", Snackbar.LENGTH_INDEFINITE);
snackbar.setAction("dismiss", new View.OnClickListener() {
@Override
public void onClick(View v) {
snackbar.dismiss();
}
});

snackbar.show();

Answer

Full Working Demo

MainActivity

package pk.sohail.gallerytest.activity;

import android.content.Context;
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.ImageView;

import pk.sohail.gallerytest.R;

public class MainActivity extends AppCompatActivity {

    Context context;
    private CoordinatorLayout coordinatorLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;

        coordinatorLayout = (CoordinatorLayout) findViewById(R.id
                .coordinatorLayout);

        ImageView image = (ImageView) findViewById(R.id.myimage);
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {

                Snackbar snackbar = Snackbar
                        .make(coordinatorLayout, "Message is deleted", Snackbar.LENGTH_LONG)
                        .setAction("UNDO", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Snackbar snackbar1 = Snackbar.make(coordinatorLayout, "Message is restored!", Snackbar.LENGTH_SHORT);
                                snackbar1.show();
                            }
                        });

                snackbar.show();
            }
        });
    }
}

activity_main.xml

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

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/white">


        <ImageView
            android:id="@+id/myimage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@mipmap/ic_launcher" />
    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

Style: parent="Theme.AppCompat.Light"

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <!--<item name="colorPrimary">@color/colorPrimary</item>-->
        <!--<item name="colorPrimaryDark">@color/colorPrimaryDark</item>-->
        <!--<item name="colorAccent">@color/colorAccent</item>-->
    </style>

Manifest: set theme to @style/AppTheme in application tag.

<application
    ...
    android:theme="@style/AppTheme"
    ...
    >
</application>