Example usage for android.text TextWatcher TextWatcher

List of usage examples for android.text TextWatcher TextWatcher

Introduction

In this page you can find the example usage for android.text TextWatcher TextWatcher.

Prototype

TextWatcher

Source Link

Usage

From source file:Main.java

public static void setEdittextListener(EditText et, final AlertDialog dialog) {
    et.addTextChangedListener(new TextWatcher() {
        @Override//from ww  w .  jav  a2  s.  c o  m
        public void afterTextChanged(Editable s) {
        }

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

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.toString().isEmpty()) {
                //disable "create" button if folder name is not specified
                dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
            } else {
                dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(true);
            }
        }
    });
}

From source file:Main.java

public static void uppercaseEditText(final EditText editText) {
    editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS
            | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
    editText.addTextChangedListener(new TextWatcher() {

        @Override/*  ww w . jav  a  2s . co m*/
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        @Override
        public void afterTextChanged(Editable arg0) {
            String s = arg0.toString();
            if (!s.equals(s.toUpperCase().trim())) {
                s = s.toUpperCase().trim();
                editText.setText(s);
                editText.setSelection(s.length());
            }
        }
    });
}

From source file:com.example.android.basicandroidkeystore.LogFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View result = inflater.inflate(R.layout.log_fragment, container, false);

    mLogView = (LogView) result.findViewById(R.id.sample_output);

    // Wire up so when the text changes, the view scrolls down.
    final ScrollView scrollView = ((ScrollView) result.findViewById(R.id.log_scroll));

    mLogView.addTextChangedListener(new TextWatcher() {
        @Override/* w ww  .j a  v a  2 s.  c  o  m*/
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
        }

        @Override
        public void afterTextChanged(Editable s) {
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
        }
    });

    return result;
}

From source file:Main.java

/**
 * Make a textview to a collapsible textview with the indicator on the right of the textview
 * //from  ww  w .j a  va  2 s .c o  m
 * @param tv , {@link TextView} to be converted
 * @param upDrawableResId , drawable resource id to be used as up indicator
 * @param downDrawableResId , drawable resource id to be used as down indicator
 * @param lineTreshold , no of line to be displayed for the collapsed state
 */
public static void makeCollapsible(final TextView tv, int upDrawableResId, int downDrawableResId,
        final int lineTreshold) {

    final Drawable[] drawables = tv.getCompoundDrawables();
    final Drawable up = tv.getContext().getResources().getDrawable(upDrawableResId);
    final Drawable down = tv.getContext().getResources().getDrawable(downDrawableResId);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], down, drawables[3]);
        tv.setEllipsize(TruncateAt.END);
        tv.setMaxLines(lineTreshold);
        tv.setTag(true);
        tv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (v instanceof TextView) {
                    TextView tv = (TextView) v;
                    boolean snippet = (Boolean) tv.getTag();
                    if (snippet) {
                        // show everything
                        snippet = false;
                        tv.setMaxLines(Integer.MAX_VALUE);
                        tv.setEllipsize(null);
                        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], up,
                                drawables[3]);
                    } else {
                        // show snippet
                        snippet = true;
                        tv.setMaxLines(lineTreshold);
                        tv.setEllipsize(TruncateAt.END);
                        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], down,
                                drawables[3]);
                    }
                    tv.setTag(snippet);
                }

            }
        });
    } else {
        tv.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {

                ViewTreeObserver vto = tv.getViewTreeObserver();
                vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

                    @SuppressWarnings("deprecation")
                    @SuppressLint("NewApi")
                    @Override
                    public void onGlobalLayout() {

                        tv.setEllipsize(TruncateAt.END);
                        int line = tv.getLineCount();
                        tv.setMaxLines(lineTreshold);
                        if (line <= lineTreshold) {
                            tv.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View arg0) {
                                    // empty listener
                                    // Log.d("line count", "count: "+
                                    // tv.getLineCount());
                                }
                            });
                            if (tv.getLayout() != null) {
                                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                                    tv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                                } else {
                                    tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                                }
                            }
                            return;
                        }

                        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], down,
                                drawables[3]);
                        tv.setTag(true);
                        tv.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                if (v instanceof TextView) {
                                    TextView tv = (TextView) v;
                                    boolean snippet = (Boolean) tv.getTag();
                                    if (snippet) {
                                        snippet = false;
                                        // show everything
                                        tv.setMaxLines(Integer.MAX_VALUE);
                                        tv.setEllipsize(null);
                                        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1],
                                                up, drawables[3]);
                                    } else {
                                        snippet = true;
                                        // show snippet
                                        tv.setMaxLines(lineTreshold);
                                        tv.setEllipsize(TruncateAt.END);
                                        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1],
                                                down, drawables[3]);
                                    }
                                    tv.setTag(snippet);
                                }

                            }
                        });

                        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                            tv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        } else {
                            tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        }
                    }

                });
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

        });
    }

}

From source file:com.google.codelab.smartlock.SignInFragment.java

@Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
    View view = layoutInflater.inflate(R.layout.fragment_sign_in, container, false);
    mUsernameTextInputLayout = (TextInputLayout) view.findViewById(R.id.usernameTextInputLayout);
    mPasswordTextInputLayout = (TextInputLayout) view.findViewById(R.id.passwordTextInputLayout);

    mUsernameEditText = (EditText) view.findViewById(R.id.usernameEditText);
    mUsernameEditText.addTextChangedListener(new TextWatcher() {
        @Override/*  w  w  w .  j  a v a2 s  .c  o m*/
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            validateUsernameLayouts(charSequence);
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    });
    mPasswordEditText = (EditText) view.findViewById(R.id.passwordEditText);
    mPasswordEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            validatePasswordLayouts(charSequence);
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    });

    mSignInButton = (Button) view.findViewById(R.id.signInButton);
    mSignInButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View view) {
            setSignEnabled(false);
            String username = mUsernameTextInputLayout.getEditText().getText().toString();
            String password = mPasswordTextInputLayout.getEditText().getText().toString();

            if (CodelabUtil.isValidCredential(username, password)) {
                ((MainActivity) getActivity()).goToContent();
            } else {
                Log.d(TAG, "Credentials are invalid. Username or password are incorrect.");
                setSignEnabled(true);
            }
        }
    });

    Button clearButton = (Button) view.findViewById(R.id.clearButton);
    clearButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mUsernameTextInputLayout.getEditText().setText("");
            mPasswordTextInputLayout.getEditText().setText("");
        }
    });

    mSignInProgressBar = (ProgressBar) view.findViewById(R.id.signInProgress);
    mSignInProgressBar.setVisibility(ProgressBar.INVISIBLE);

    return view;
}

From source file:com.example.hbranciforte.trafficclient.DataTraffic.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_data_traffic);
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }/* ww  w.jav  a2  s. c  o m*/
    try {
        zone_info = new JSONObject((String) getIntent().getSerializableExtra("zone_info"));
    } catch (JSONException e) {
        e.printStackTrace();
    }
    EditText ed_fichas = (EditText) findViewById(R.id.fichas);
    ed_fichas.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
            //float price = (float) 0.0;
            Integer time = 0;
            TextView txtvalor = (TextView) findViewById(R.id.valor);
            TextView txttiempo = (TextView) findViewById(R.id.txtTime);
            if (s.toString() != "") {
                try {
                    price = (float) zone_info.getDouble("unit_price") * Float.valueOf(s.toString());
                    time = zone_info.getInt("unit_time") * Integer.parseInt(s.toString());
                    txtvalor.setText("$ ".concat(Float.toString(price)));
                    txttiempo.setText(time.toString().concat(" min"));
                } catch (Exception e) {
                    Log.e("getting data0:", e.getMessage());
                    txttiempo.setText("0 min");
                    txtvalor.setText("$ 0.0");

                }
            }
        }
    });
}

From source file:com.autoparts.buyers.activity.UserInfoEditActivity.java

private void init() {
    context = this;
    title = getIntent().getStringExtra("title");
    setTitle(title);/*from  w w w.j  av a 2  s  .c  o  m*/

    mEditText = (EditText) findViewById(R.id.mEditText);
    String content = getIntent().getStringExtra("content");
    position = getIntent().getIntExtra("position", 0);

    if (position == 0) {
        hint = "??";
    } else {
        hint = "?";
    }
    mEditText.setHint(hint);
    mEditText.setText(content);
    mEditText.requestFocus();
    CommonData.setEditCursor(mEditText);
    // ?
    mEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            setRightView(getString(R.string.confirm_ok), -1);
        }

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

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}

From source file:com.springsource.greenhouse.twitter.PostTweetActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.post_tweet);

    textViewCount = (TextView) this.findViewById(R.id.post_tweet_count);
    textWatcher = new TextWatcher() {
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }/* w  w  w .  ja  v a  2  s. com*/

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            textViewCount.setText(String.valueOf(MAX_TWEET_LENGTH - s.length()));
        }

        public void afterTextChanged(Editable s) {
        }
    };

    final EditText editText = (EditText) findViewById(R.id.post_tweet_text);
    editText.addTextChangedListener(textWatcher);

    final Button button = (Button) findViewById(R.id.post_tweet_button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // hide the soft keypad
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(
                    Context.INPUT_METHOD_SERVICE);
            EditText editText = (EditText) findViewById(R.id.post_tweet_text);
            inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), 0);
            new PostTweetTask().execute();
        }
    });
}

From source file:com.lithidsw.wallbox.app.colorwall.ColorWallFrag.java

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    fa = super.getActivity();
    prefs = fa.getSharedPreferences(C.PREF, Context.MODE_PRIVATE);
    ll = (LinearLayout) inflater.inflate(R.layout.colorwall_frag, container, false);
    mColorCode = (EditText) ll.findViewById(R.id.color_code);
    mColorCode.addTextChangedListener(new TextWatcher() {
        @Override//w w  w  . j a  v a  2  s.c  o m
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() == 7) {
                if (s.toString().startsWith("#")) {
                    try {
                        mColor = Color.parseColor(s.toString());
                        mPicker.setImageDrawable(new ColorDrawable(mColor));
                    } catch (NumberFormatException e) {
                        mColorCode.setText("");
                        Toast.makeText(fa, "Invalid color format: " + s.toString()
                                + "\nPlease use a valid 6 digit color code", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    mColorCode.setText("");
                    Toast.makeText(fa, "Needs to start with # symbol before color code", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    wm = WallpaperManager.getInstance(fa);
    ll.findViewById(R.id.color_black).setOnClickListener(this);
    ll.findViewById(R.id.color_white).setOnClickListener(this);
    ll.findViewById(R.id.color_blue).setOnClickListener(this);
    ll.findViewById(R.id.color_green).setOnClickListener(this);
    ll.findViewById(R.id.color_purple).setOnClickListener(this);
    ll.findViewById(R.id.color_yellow).setOnClickListener(this);
    ll.findViewById(R.id.color_red).setOnClickListener(this);

    mPicker = (ImageView) ll.findViewById(R.id.picker);

    if (!prefs.getBoolean(C.PREF_COLORWALL_FIRST_RUN_MAIN, false)) {
        prefs.edit().putBoolean(C.PREF_COLORWALL_FIRST_RUN_MAIN, true).commit();
        String title = getResources().getString(R.string.main_title_colorwall);
        String message = getResources().getString(R.string.colorwall_description);
        new CustomDialogs().openFirstDialog(fa, title, message);
    }
    setHasOptionsMenu(true);
    return ll;
}

From source file:com.autoparts.sellers.activity.UserInfoEditActivity.java

private void init() {
    context = this;
    title = getIntent().getStringExtra("title");
    setTitle(title);/*from   ww w .  j a  v a  2  s .  co m*/
    mEditText = (EditText) findViewById(R.id.mEditText);
    String content = getIntent().getStringExtra("content");
    position = getIntent().getIntExtra("position", 0);
    if (position == 0) {
        hint = "??";
    } else {
        hint = "?";
    }
    mEditText.setHint(hint);

    mEditText.setText(content);
    mEditText.requestFocus();
    CommonData.setEditCursor(mEditText);
    // ?
    mEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            setRightView(getString(R.string.confirm_ok), -1);
        }

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

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}