Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set input type as password to EditText programmatically [duplicate]

I am using alert dialog with EditText. I want to set input type as password to that edit text programmatically. I found these two methods:

final EditText input = new EditText(getActivity()); 

input.setTransformationMethod(PasswordTransformationMethod.getInstance());

input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 

However, these don't work for me. It shows the text, but I want EditText.

This is the code of dialog with EditText:

public void showDialog(){
        
           /* Alert Dialog Code Start*/     
                AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
//              alert.setTitle("JPOP"); //Set Alert dialog title here
                alert.setMessage("              Please enter password"); //Message here
                
                Log.e("dialog in password ","passworddddddddddddddddd");

                // Set an EditText view to get user input 
                final EditText input = new EditText(getActivity());
//              input.setInputType(InputType.TYPE_CLASS_TEXT);
//              input.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
//              input.setTransformationMethod(PasswordTransformationMethod.getInstance());
                
//              final EditText input = new EditText(getActivity()); 
                input.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
                input.setTransformationMethod(new PasswordTransformationMethod());
                
                
                input.setHint("Password");
                input.setSingleLine();
                input.setTextSize(14);
                alert.setView(input);

                alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    strPassword = input.getEditableText().toString().trim();    
                    
                    if(strPassword.length()!=0){
                 
                 String prestatus =DataUrls.preferences.getString("Password", "");
                 if(prestatus.equals(strPassword)){
                    
                     if(price_reports_check){
                         
                         price_reports_check=false;
                         
                            ReportsFragment reportfragment = new ReportsFragment();
                            FragmentManager fragmentManager = getFragmentManager();
                            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                            fragmentTransaction.replace(R.id.details, reportfragment);
                            fragmentTransaction.commit();   
                     }else{
                        PriceListFragment pricelistfragment = new PriceListFragment();
                        FragmentManager fragmentManager = getFragmentManager();
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.details, pricelistfragment);
                        fragmentTransaction.commit();
                     }
                     
                 }else
                 {
                     showDialog();
                     Toast.makeText(getActivity(), "The password you entered is wrong", Toast.LENGTH_SHORT).show();
                 }
                 
                    }
                    else
                    {
                        showDialog();
                        Toast.makeText(getActivity(), "Please Enter Password", Toast.LENGTH_SHORT).show();
                        
                    }
                 
                } // End of onClick(DialogInterface dialog, int whichButton)
            }); //End of ok....
                
                alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int whichButton) {
                    // Canceled.
                      dialog.cancel();
                  }
            }); //End of alert.setNegativeButton
                
                
                AlertDialog alertDialog = alert.create();
                
                   TextView title = new TextView(getActivity());
                  // You Can Customise your Title here 
                    title.setText("JPOP");
//                  title.setBackgroundColor(Color.DKGRAY);
                    title.setPadding(10, 10, 10, 10);
                    title.setGravity(Gravity.CENTER);
//                  title.setTextColor(Color.WHITE);
                    title.setTextSize(20);
                    alert.setCustomTitle(title);
                    alert.setCancelable(false);

                    alert.show();
           
                
         }

What did I do wrong?

like image 542
rams Avatar asked Jul 16 '13 10:07

rams


2 Answers

You are getting this problem because you are using alert.setCustomTitle(title);

after

input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        input.setTransformationMethod(PasswordTransformationMethod.getInstance());

Which making it again to normal type

either change : alert.setCustomTitle(title); to alert.setTitle("your title here");

or if you want to use customeTitle

Than use following code

input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
input.setTransformationMethod(PasswordTransformationMethod.getInstance());
alert.setView(input);

after

alert.setCustomTitle(title);
like image 171
Tarsem Singh Avatar answered Oct 11 '22 01:10

Tarsem Singh


only try thisinput.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

like image 24
rahultheOne Avatar answered Oct 11 '22 02:10

rahultheOne