0

I am try to to use the text entered into an EditText, which that EditText is on a PopupWindow. This is my code for the PopUpWindow.

public void popupInit() {
            View popupView = null;
            popupView = inflater.inflate(R.layout.poplayout, null);

            popUp = new PopupWindow(popupView,             LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
            popUp.setFocusable(true);
            popUp.setOutsideTouchable(isRestricted());
            popUp.setContentView(inflater.inflate(R.layout.poplayout, null, false));
            login = (Button) popupView.findViewById(R.id.loginButton);

        final EditText  username = (EditText) popupView.findViewById(R.id.username);
        final EditText  password = (EditText) popupView.findViewById(R.id.password);
            user_name =username.getText().toString();
            pass_word = password.getText().toString();


        }

when ran, the above code returns "" for both user_name and pass_word, which are field strings.

Thanks.

carvelle
  • 15
  • 1
  • 6
  • Please refer to this link: http://stackoverflow.com/questions/16531602/getting-edittext-input-from-popupwindow – user3487063 Aug 11 '14 at 14:51
  • I tried that link before asking the question, and I just tried it again, still getting the same empty string from EditText. Can you please point out any errors on the above code that I might have missed. – carvelle Aug 11 '14 at 15:17
  • just like a general question... user_name and pass_word are declared as string variables??? – geekCode Aug 11 '14 at 15:55
  • You need to do the setContentView and rest of the code when you close the popup – user3487063 Aug 11 '14 at 16:53

1 Answers1

1

Here is the piece of working code for you,change accordingly:

Brief Explanation:

when btnId is clicked a popup opens and when you enter username and password and click loginbutton, the popup dismisses and username password you entered will be printed in logs. check your LogCat to see what you have entered.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         final Button btn = (Button)findViewById(R.id.btnId);
         btn.setOnClickListener(new OnClickListener() {

             @Override
             public void onClick(View v) {

                 LayoutInflater layoutInflater 
                 = (LayoutInflater)getBaseContext()
                  .getSystemService(LAYOUT_INFLATER_SERVICE); 
                 View popupView = layoutInflater.inflate(R.layout.popup, null);  
               final View  popupView1 = layoutInflater.inflate(R.layout.popup, null);

                final PopupWindow popUp = new PopupWindow(popupView1,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
                 popUp.setFocusable(true);
                 popUp.setOutsideTouchable(isRestricted());
                 Button login = (Button) popupView1.findViewById(R.id.loginButton);
                 login.setOnClickListener(new Button.OnClickListener(){

         @Override
         public void onClick(View v) {
             popUp.setContentView(popupView1);


         final EditText  username = (EditText) popupView1.findViewById(R.id.username);
         final EditText  password = (EditText) popupView1.findViewById(R.id.password);
             String user_name =username.getText().toString();
             String pass_word = password.getText().toString();
         Log.i("info",(user_name+pass_word));
          popUp.dismiss();
         }});

                 popUp.showAsDropDown(btn, 50, -30);
                 }


         });

    }

To answer your question posted in comments, you should have something like below in your popupInit() method:

public void popupInit() {
         final LayoutInflater inflater 
         = (LayoutInflater)getBaseContext()
          .getSystemService(LAYOUT_INFLATER_SERVICE); 
       final View popupView = inflater.inflate(R.layout.popup, null);

       final PopupWindow popUp = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        popUp.setFocusable(true);
        popUp.setOutsideTouchable(isRestricted());

        Button login = (Button) popupView.findViewById(R.id.loginButton);
        login.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {

                 popUp.setContentView(inflater.inflate(R.layout.popup, null, false));
                 final EditText  username = (EditText) popupView.findViewById(R.id.username);
                    final EditText  password = (EditText) popupView.findViewById(R.id.password);
                        String user_name =username.getText().toString();
                        String pass_word = password.getText().toString();
                        Log.i("Username,password",(user_name+"-->"+pass_word));
                        popUp.dismiss();
            }

        });
         popUp.showAtLocation(popupView, Gravity.CENTER, 0, 0);
    }   
user3487063
  • 3,672
  • 1
  • 17
  • 24