5

I have the following method and I want to show a Snackbar. The problem is, I don't have my activity view and I don't know how to get it. In the code you'll see that Snackbar view is viewInflate_setup but it doesn't work. I know it doesn't work because this is the view from my alertdialog and not my current screen. I've tried getting the root view but then the snackbar is shown in the wrong place(Behind those three android buttons). How do I get my activty View?

public void confirmPassword(){
        final LayoutInflater inflateSetup = getLayoutInflater();
        final View viewInflate_setup = inflateSetup.inflate(R.layout.inflate_setup, null);

        AlertDialog.Builder alertSetup = new AlertDialog.Builder(this);
        alertSetup.setView(viewInflate_setup);
        final AlertDialog dialogSetup = alertSetup.create();
        dialogSetup.show();

        btnConfirmPassowod = (Button)viewInflate_setup.findViewById(R.id.btnConfirm);
        btnConfirmPassowod.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                System.out.println("User has clicked the [confirm] button");
                dialogSetup.setCancelable(false);
                edtPassword3 = (EditText)viewInflate_setup.findViewById(R.id.edtPassword3);
                SUpassword3 = edtPassword3.getText().toString();

                final ParseUser beforeConfirmPassword = ParseUser.getCurrentUser();
                ParseUser.logInInBackground(beforeConfirmPassword.getUsername(), SUpassword3, new LogInCallback() {
                    @Override
                    public void done(ParseUser parseUser, ParseException e) {
                        if(e==null){
                            //password confirmed, go to next setup
                            System.out.println("Password [" +SUpassword3+ "] and user [" +beforeConfirmPassword.getUsername()+ "] confirmed, go to setup");
                            dialogSetup.dismiss();
                        }else{
                            //passwords don't match
                            System.out.println("Password don't match: [" +SUpassword3 + "] USER [" +beforeConfirmPassword.getUsername()+ "]");
                            Snackbar.make(viewInflate_setup, "Wrong password. Try again.", Snackbar.LENGTH_LONG)
                                    .setAction("Action", null).show();
                            dialogSetup.dismiss();
                        }
                    }
                });
            }
        });
    }
madhan kumar
  • 1,560
  • 2
  • 26
  • 36
  • 1
    In what type of class is this method? – OneCricketeer Jul 29 '16 at 17:21
  • 1
    I am assuming this is an activity in which you have written the function. In that case you can add an `android:id` attribute to the root layout of your activity.xml file and then use `findViewById(R.id.root_layout_id)` to get the view. You can also get the root view of your activity via `findViewById(android.R.id.content)`. – wanpanman Jul 29 '16 at 17:25
  • That's what I did @wanpanman. Thanks. – Macaroni and not cheese Jul 29 '16 at 19:08

2 Answers2

8

well you can use findViewById(android.R.id.content) or getWindow().getDecorView() to get root view of your activity

i.e Snackbar.make(findViewById(android.R.id.content), "Wrong password. Try again.", Snackbar.LENGTH_LONG).show(); will do the job.

Krupal Shah
  • 8,949
  • 11
  • 57
  • 93
7

The only thing that you need is set an id of you root view and create an instance of it.

activity_main.xml

<LineaLayout
  ...
  android:id="my_root">
  ...

</LinearLayout>

MainActivity

private LinearLayout mRootView;
onCreate(...){
  ...
  mRootView = (LinearLayout) findViewById(R.id.my_root);
  ... 
}

And, in you alert dialog just create a snackbar like:

Snackbar.make(mRootView, "Wrong password. Try again.", Snackbar.LENGTH_LONG).setAction("Action", null).show();
Robert
  • 1,192
  • 1
  • 10
  • 20