How to continue typing in edittext in android after poping popupwindow

261 views Asked by At
//imports


public class MainActivity extends Activity implements  OnCheckedChangeListener
 {
    int count=0,ints......;
    TextView textviews...;
    int itemCode,month;
    ArrayList<String> Name = new ArrayList<String>();
    AlertDialog alertDialog ;
    SharedPreferences sp;
    EditText EtItemCode;
    Editor editor ;
    RadioButton RbForItemCode,RbForItemName;
    RadioGroup RgForItemVsName;
    PopupWindow popupWindowItems;
    String popUpItems[];
    ProgressDialog pDialog;



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);
        init();


      //shared prefs
        sp = this.getSharedPreferences("MySharedPrefsFile", Context.MODE_PRIVATE); 
        editor = sp.edit();
        intForShardPref= sp.getInt("NEW_INSTALLATION",1); // getting Integer(1 is for no)

        if(intForShardPref==1){

             Intent intent = new Intent(this,Verify.class);
            startActivityForResult(intent, 1);

        }
        else{
            //do nothing

        }




        EtItemCode.addTextChangedListener(new TextWatcher() {

              public void afterTextChanged(Editable s) {

                  ToCheckItemCodeOfEdittext = EtItemCode.getEditableText().toString();


                  DatabaseHandler db=new DatabaseHandler(MainActivity.this);                    
                    List<Item> Items = db.getAllItemWithSubString(ToCheckItemCodeOfEdittext,itemNumberOrNameSelectedInRadioButtonOptions);
                 if(EtItemCode.length()>2){



                     if(EtItemCode.length()>3||flagForPopUpWindow>EtItemCode.length())
                     popupWindowItems.dismiss();

                Name.clear();

                for (Item cn : Items) {
                    if(RbForItemCode.isChecked()==true){
                    Name.add(Integer.toString(cn.getItemNumber()));
                    }
                    else{
                        Name.add(cn.getName());
                    }
                }

                  popUpItems = new String[Name.size()];
                    Name.toArray(popUpItems);
                    popupWindowItems = popupWindowItems();
                    ***popupWindowItems.setFocusable(false);***

                    popupWindowItems.showAsDropDown(findViewById(R.id.et_item_code), -5, 0);


                 }
                 else{
                     if(flagForPopUpWindow==3&&EtItemCode.length()==2)
                         popupWindowItems.dismiss();


                 }

                 flagForPopUpWindow=EtItemCode.length();

              }

              public void onTextChanged(CharSequence s, int start, int before, int count) {
              }

              public void beforeTextChanged(CharSequence s, int start, int count, int after) {

              }
            });

    }




    private void init() {
        // TODO Auto-generated method stub

        //some code
    }


    public PopupWindow popupWindowItems() {

        // initialize a pop up window type
        PopupWindow popupWindow = new PopupWindow(this);

        // the drop down list is a list view
        ListView listViewItems = new ListView(this);

        // set our adapter and pass our pop up window contents
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(
                this,                                        //context for activity
                android.R.layout.simple_list_item_1,   //layout used
                Name){                  //Items to be displayed

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {

                        // setting the ID and text for every items in the list
                        String item = getItem(position);

                        String text = item.toString();

                        // visual settings for the list item
                        TextView listItem = new TextView(MainActivity.this);

                        listItem.setText(text);
                        //listItem.setTag(id);
                        listItem.setTextSize(22);
                        listItem.setPadding(10, 10, 10, 10);
                        listItem.setTextColor(Color.WHITE);

                        return listItem;
                    }
        };                                     


        listViewItems.setAdapter(adapter);

        // set the item click listener
        listViewItems.setOnItemClickListener(new ItemsDropdownOnItemClickListener());

        // some other visual settings
        //popupWindow.setFocusable(true);

        popupWindow.setWidth(EtItemCode.getWidth());
        //popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        Rect r = new Rect();
        View rootview = this.getWindow().getDecorView(); // this = activity
        rootview.getWindowVisibleDisplayFrame(r);

        popupWindow.setHeight(r.height()-3*EtItemCode.getHeight());

        // set the list view as pop up window content
        popupWindow.setContentView(listViewItems);

        return popupWindow;
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater blowUp = getMenuInflater();
        blowUp.inflate(R.menu.cool_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.phone_number:
            Intent p = new Intent(MainActivity.this,PhoneNumber.class);
            startActivity(p);
            break;
        case R.id.exit:
            finish();

            break;
        }
        return false;
     }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        //some code
    }



    class  LoadDataBase extends AsyncTask<String, String, String>{


        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pDialog.setMessage("DataBase Loading..");
            pDialog.setIndeterminate(true);
            pDialog.setCancelable(false);
            pDialog.show();

        }
        @Override
        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub
            DatabaseHandler db = new DatabaseHandler(MainActivity.this);
             Log.d("Reading: ", "Reading all Items..");
                List<Item> Items = db.getAllItem();       
         //some code

                //DatabaseHandler db1 = new DatabaseHandler(this);              
                count= db.getItemCount();
            return null;
        }


        protected void onPostExecute(String file_url) {

            pDialog.dismiss();

            }
    }



    public class ItemsDropdownOnItemClickListener implements OnItemClickListener {


        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
            Toast.makeText(MainActivity.this, "Item is: ", Toast.LENGTH_SHORT).show();
            InputMethodManager imm = (InputMethodManager)getSystemService(
                      Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(EtItemCode.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

            // dismiss the pop up
            popupWindowItems.dismiss();

            // get the text and set it as the button text
            String selectedItemText = ((TextView) v).getText().toString();
            Toast.makeText(MainActivity.this, "Item is: " + selectedItemText, Toast.LENGTH_SHORT).show();

            DatabaseHandler db =new DatabaseHandler(MainActivity.this);
            Item item= new Item();
            if(RbForItemCode.isChecked()==true){
                item = db.getItem(Integer.valueOf(selectedItemText));
            }
            else if(RbForItemName.isChecked()==true){
                item = db.getItem(selectedItemText);
            }
            itemCode=item.getItemNumber();



        }

    }



    @Override
    public void onCheckedChanged(RadioGroup arg0, int arg1) {
        // TODO Auto-generated method stub
        if(RbForItemCode.isChecked()==true){
            itemNumberOrNameSelectedInRadioButtonOptions="id";
            //some code


            }
        }
        else if(RbForItemName.isChecked()==true){
            //some code
        }
    }

}

This code is working fine on my phone galaxy note 2(custom rom of note 4.. Android 4.4.4).By fine I mean I can type in edittext and popupwidow shows up after 3rd text(because of condition I have put) and I can select an option from the popupwindow. When I try to run the code on any other phone like galaxy grand then callback to ItemsDropdownOnItemClickListener is not registered and I can only scroll the popupwindow and keep on typing in edittext but cannot select any of the options in the popupwindow. If I set popupWindowItems.setFocusable(false) to true i.e popupWindowItems.setFocusable(true), and then as soon as I type 3rd letter in edittext,edittext looses focus and popwindow gains it. Now I can select anything.. But now I cannot continue to type in edittext.I have to manually select edittext and type.

Now I want that after tying 3rd word in edittext a popupwindow should appear(which currently is appearing) and edittext doesn't loose focus(so that i can continue typing).. Further if I select anything from popupwindow ,ItemsDropdownOnItemClickListener should be called which is currently being not called in other phones when popupWindowItems.setFocusable(false);

I am doing this to load data from sqlite database and show in popupwindow once the user types 3rd word. Any suggestion is recommended

Edit: Problem solved. Now using AutoComplete TextView

0

There are 0 answers