show and hide password using drawable icons in android - java

I have designed a screen with password field using EditText, in the EditText I am using drawableRightIcon which has to show the password visible when we click the drawable button and also replace that drawable icon with another icon? can anyone help please?

Following is the code that I'm using currently in my apps for this purpose. We basically put a touch listener to our EditText and identify if the click occurred on the drawable and act accordingly (also switching icons):
getPasswordEditText().setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_RIGHT = 2; // index
if (event.getAction() == MotionEvent.ACTION_UP) {
if (event.getRawX() >= (getPasswordEditText().getRight() - getPasswordEditText().getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
if (passwordShown) {
passwordShown = false;
// 129 is obtained by bitwise ORing InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
getPasswordEditText().setInputType(129);
// Need to call following as the font is changed to mono-space by default for password fields
getPasswordEditText().setTypeface(Typeface.SANS_SERIF);
getPasswordEditText().setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.locked_icon, 0);
} else {
passwordShown = true;
getPasswordEditText().setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
getPasswordEditText().setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.unlocked_icon, 0);
}
return true;
}
}
return false;
}
});

Execute the following statements when your image button is clicked
String password= your_editText.getText().toString().trim();
your_editText.setText(password);
your_imgview.setImageResource(R.drawable.new_image)

Use the following code to show and hide the password. You can use an ImageView beside the check box.
Used a checkbox (you can use a image button also instead of it using the same concept except that you have to use an onCLickListener instead).
loginShowHidePassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// EditText loses the cursor position when you change the InputType
int curPos = mPasswordView.getSelectionStart();
if (isChecked) {
mPasswordView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
imageview.setImageResource(R.drawable.newImage);
} else {
mPasswordView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
loginShowHidePassword.setImageResource(R.drawable.newImage);
}
mPasswordView.setSelection(curPos);
}
});
You need to save a new a new image named newImage in drawable.
mPasswordView is the password EditText

Related

Show button only after an event is done in android SDK relative view

I have a relative view (Scrollable) in which I have a textview followed by a edittext.
What I am trying to do is when I press enter/done on virtual keyboard for taking input from edittext then only show up the button (saying click to continue) to proceed. If I put the button in xml it is visible before hand so I tried creating the button dynamically in .java file but I am getting an error.
Code given below.
pwd = (TextView) findViewById(R.id.pwd);
pwd.setText(System.getProperty("user.dir").toString()+" $ ");
command_input = (EditText) findViewById(R.id.command);
command_input.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) ||
(keyCode == KeyEvent.KEYCODE_ENTER)){
command=command_input.getText().toString();
System.setProperty("user.dir",command);
command_output = (TextView) findViewById(R.id.command_output);
command_output.setText("running!");
Button next = new Button(this); **//error in "this"**
next.setText("Click To Continue");
return true;
}
return false;
}
});
Views visibility is what you're looking for.
There are 3 visibility modes :
visible : by default, the view is shown
invisible : the view is not shown, but takes space (its size) on the layout
gone : the view is not shown, and doesn't take space
So, to answer your question, On your layout :
<RelativeLayout
.....
<Button
android:id="#+id/continueButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Continue"
android:visibility="gone" />
</RelativeLayout>
On your activity :
final Button continueButton = (Button)findViewById (R.id.continueButton);
command_input.setOnKeyListener (new View.OnKeyListener () {
public boolean onKey (View v, int keyCode, KeyEvent event) {
if (event.getAction () == KeyEvent.ACTION_DOWN || keyCode == KeyEvent.KEYCODE_ENTER){
continueButton.setVisibility (View.VISIBLE);
return true;
}
return false;
}
});
You are passing incorrect parameter (context) to the create button. You Should use Button next = new Button(YourActivity.this); inside any anonymous class.
"this" always refers to the current instance. You are passing "this" inside anonymous class "new View.OnKeyListener", so here "this" is referring to "View.OnKeyListener" instance and not to Activity that extends Context so you are getting error.

ImageView on again click

I want my ImageView to change it's drawable resource as it is pressed. The problem occurs when ImageView is pressed for the second time.
Let me explain, if ImageView is pressed first time, I want it to change from drawable A to drawable B. If ImageView is pressed again I want it to change from drawable B to drawable A.
That pressed again part is not working..
Here's my code:
public void imageViewBiljeskeNaListiCheckMarkMetoda(View view){
imageViewBiljeskeNaListiCheckMark = (ImageView) findViewById(R.id.imageViewBiljeskeNaListiCheckMark);
if (view == imageViewBiljeskeNaListiCheckMark){
imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_obavljeno);
} else {
imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_nije_obavljeno);
}
}
Remove this from the method....
You need to init the object once in the onCreate...
imageViewBiljeskeNaListiCheckMark = (ImageView) findViewById(R.id.imageViewBiljeskeNaListiCheckMark);
Then add a boolean variable to control the state of the view. .
public void imageViewBiljeskeNaListiCheckMarkMetoda(View view){
flag =!flag;
if (view == imageViewBiljeskeNaListiCheckMark){
if (flag) {imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_obavljeno);
} else {
imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_nije_obavljeno);
}
}
I sugggest to use the "tag" of the view, and keep in the tag the information you need (e.g. if the view is pressed or not)
http://developer.android.com/intl/es/reference/android/view/View.html#setTag(java.lang.Object)
Can't you just use a toggle method like this?
private void toggleDrawableOnClick(){
/* now you can check to see if the set drawable is A using its id */
if(visible drawable is A){
imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_nije_obavljeno);
}else{
imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_obavljeno);
}
}
This should be easier I believe!!

Simple edit text?

I am getting the users information from an edit text. I do have a listener that gets their entered information after clicking submit, but I want to also get the entered info after clicking back or clicking somewhere else:
For example, if the users clicks on the black space, I want to get the text they entered. If they type "hello", and click back rather than "enter", I still want to get the text hello. If, however, they don't type anything, I don't care about their input. How can I achieve this?
Thanks,
Ruchir
First add these as a class variables
private String inputText;
private EditText yourEditText;
Get the instance of your EditText View
yourEditText = (EditText)findViewById(R.id.your_editText);
When a button is clicked, you can get the content of the EditText field like this
Button mButton = (Button)findViewById(R.id.m_button);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
inputText = yourEditText.getText().toString();
}
});
If a user press the Back button, you can get the input if any like this
#Override
public void onBackPressed() {
inputText = yourEditText.getText().toString();
super.onBackPressed();
}
Then check if there is any value assigned to your String variable
if(inputText.equals("") || inputText == null){
// there is no value
}else{
// there is value entered.
}
To extend my solution for clicking some where else
add a class variable
private boolean isEditTextHasFocus;
then create a focus listener which will check if the Edittext has focus
private View.OnFocusChangeListener focusListener = new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
isEditTextHasFocus = true;
} else {
isEditTextHasFocus = false;
}
}
}
Add this line in onCreate(Bundle savedInstanceState) method
yourEditText.setOnFocusChangeListener(focusListener);
Then override to onTouchEvent(MotionEvent event) listener and access the Edittext input when the key up action is called
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if(!isEditTextHasFocus){
inputText = yourEditText.getText().toString();
}
}
return true;
}
I hope this will give you a further idea to find your unique solution.
Overriding what happens when the back button is pressed is bad practice and is unnecessary for what you want to do.
You need to use a special listener called onFocusChangedListener. This function is called anytime an element gains or loses focus. In this case for your editText it will be called whenever someone clicks on it or away. Pressing the back button or leaving the editText in any way will call this function. In the following code I check if
if(!username.hasFocus())
which makes it so the value is only saved when focus from the editText is lost rather than everytime focus is changed.
You haven't added any of your own code so I am just going to use obvious placeholder variables in my code example.
Edittext username = (EditText findViewById(R.id.YOUR_EDITTEXTS_ID);
String previousValue = ""; // to keep track of value change
String usernameValue = "";
username.setOnFocusChangeListener(new View.OnFocusChangeListener(
{
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (username.hasFocus()){
//take note of value for comparison when clicking away
previousValue = username.getText().toString();
} else if (!username.hasFocus()){
// check if value has changed
if (!previousValue.equals(username.getText().toString()){
usernameValue = username.getText().toString();
}
}
}
});

How to toggle picture src of ImageButton

I have an image button and want that when the user clicks on it-it changes its src to a different drawable-but i want the background color i defined in xml to remain the same. Here is the code that i have done so far, but doesnt work, because im changing background and not the source-but general concept i will use:
public void onClick(View view) {
if (bgenabled == true) {
holder.ib.setBackground(res.getDrawable(R.drawable.location_deactive));
bgenabled = false;
} else { holder.ib.setBackground(res.getDrawable(R.drawable.location_active));
bgenabled = false;}
Just call the setImageDrawable to replace the current image you are using
with your ImageButton.
ImageButton button;
...
button.setImageDrawable(getResources().getDrawable(R.drawable.new_image));

Updating TextView on keyDown?

I am trying to update a TextView which will show the system volume. I have managed to capture the current system volume and display it, but it doesn't update when the volume is turned up/down (obviously).
I know there is an easy solution somewhere I just cant think! onKeyListeners?
I am now using this but it doesnt work:
TextView sysVol = (TextView)findViewById(R.id.systemVolume);
sysVol.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
//system volume
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int i = curVolume * 4;
String aString = Integer.toString(i);
TextView sysVol = (TextView)findViewById(R.id.systemVolume);
sysVol.setText(aString);
return true;
}
return false;
}
});
Well, the pseudo approach would be the following scenario:
1) Attach a listener to the view
2) Filter the different events in the callback
3) Update the TextView upon the (event==volume_up OR event==volume_down)
Basically, you'd want to implement the View.onKey()-method for this to be a minor bump in the road, instead of the current roadblock-state.
Summary: Add the following snippet to your code (some modification needed)
public YourClassHere extends SomethingCool implements OnKeyListener /*<-- important part*/
{
public boolean onKey(View v) {
// do something when the selected button is pushed
return true;
}
}
I think the problem is that the textview is not refreshing after you have changed the text. Try putting sysVol.requestLayout() after you changed the text to refresh the textview. Also, I don't think you are suppose to put return false after the closing bracket of the onKey() method.

Categories