Edittext change Border Color always onclick - java

I have an edittext and want to change the border color after onclick.
After I click on it it shows me the red border color. But After I try it again nothing happens. It is still red.
first click red -> second click black -> third click red and so on
How can I fix it?
...
boolean focus = false
...
private void setOnFocusChangeListener(final EditText editText) {
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
editText.setBackgroundResource(R.drawable.black);
focus = false;
} else if (hasFocus) {
editText.setBackgroundResource(R.drawable.red);
focus = true;
} else if ((hasFocus) && focus) {
editText.setBackgroundResource(R.drawable.black);
focus = false;
}
}
});
}

In your case instead of onFocusChangeListener() you can use onClickListener() as you want to change on each click, you can do something like this :
int res = R.drawable.black; // Your default background
etEmailAdress.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (res == R.drawable.black) {
res = R.drawable.btn_green;
} else {
res = R.drawable.black;
}
etEmailAdress.setBackgroundResource(res);
}
});

Related

Remove space between characters

I am developing a simple calculator using Android Studio.
I have built many buttons that emulate the real keys.
I have set a number for each button.
When hitting the button, can get the text from the button pressed as well as print it to the display area where all numbers will appear.
Everything works well except for the fact that a space appears between numbers when being displayed as shown in the emulator.
What can I do ?
static void getSetText (Button button) {
String input;
button.setOnClickListener (new View.OnClickListener() {
#Override
public void onClick (View v) {
Button b = (Button) v;
buttText = b.getText().toString();
if (init) {
display.setText ("");
init = false;
}
display.setText (display.getText() + buttText);
}
});
}
EMULATOR:
A simple trimming would do if the cause of issue is an space,
static void getSetText (Button button) {
String input;
button.setOnClickListener (new View.OnClickListener() {
#Override
public void onClick (View v) {
Button b = (Button) v;
buttText = b.getText().toString();
if (init) {
display.setText ("");
init = false;
}
display.setText (display.getText().toString().trim() + buttText.trim());
}
});
}
But it could also be because of android:letterSpacing in your xml layout. Take a look here regarding this, https://developer.android.com/reference/android/widget/TextView#attr_android:letterSpacing
Remove the blankspace from the String.
static void getSetText (Button button) {
String input;
button.setOnClickListener (new View.OnClickListener() {
#Override
public void onClick (View v) {
Button b = (Button) v;
buttText = b.getText().toString();
if (init) {
display.setText ("");
init = false;
}
display.setText (display.getText().toString().replaceAll(" ","") + buttText.replaceAll(" ",""));
}
});
}

setPressed is not working

I have this in my code and the .setPressed doesn't work:
Button btndesligado = (Button) findViewById(R.id.button12);
btndesligado.setOnClickListener(new OnClickListener() { //Botão para pôr silêncio!
#Override
public void onClick(View v) {
btndesligado.setPressed(true);
som = false;
vibrar = false;
}
});
What is wrong? It doesn't give me any error, it just doesn't work when I open it and click it. It was suposed to be pressed after I press once.
Android is changing the setPressed both before and after your onClickEvent
so change your code this code
btndesligado.setOnClickListener(new OnClickListener() { //Botão para pôr silêncio!
#Override
public void onClick(View v) {
btndesligado.setPressed(true);
som = false;
vibrar = false;
}
});
to this
btndesligado.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// show interest in events resulting from ACTION_DOWN
if (event.getAction() == MotionEvent.ACTION_DOWN) return true;
// don't handle event unless its ACTION_UP so "doSomething()" only runs once.
if (event.getAction() != MotionEvent.ACTION_UP) return false;
btndesligado.setPressed(true);
som = false;
vibrar = false;
return true;
}
});
It seems nothing wrong with your codes, It's working as you did, It sets the View's internal state to "pressed", or false to reverts the View's internal state from a previously set "pressed" state. And you've put that inside onClick of button means it acts only after you press the button,
If you want it pressed or revert then do like this,
Button btndesligado = (Button) findViewById(R.id.button12);
btndesligado.setPressed(true);
btndesligado.setOnClickListener(new OnClickListener() { //Botão para pôr silêncio!
#Override
public void onClick(View v) {
som = false;
vibrar = false;
}
});
Try this way.
btndesligado.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
btndesligado.setPressed(true);
som = false;
vibrar = false;
}
});

Calling setStatusBarColor ANDROID

How can I call setStatusBarColor on a buttonclick? I have the event listener code but I'm unsure how to call this method. I'm trying to change the status bar color on button click.
Here's my code:
public static void setStatusBarColor(Activity activity, int statusBarColor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// If both system bars are black, we can remove these from our layout,
// removing or shrinking the SurfaceFlinger overlay required for our views.
Window window = activity.getWindow();
if (statusBarColor == Color.BLACK && window.getNavigationBarColor() == Color.BLACK) {
window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
} else {
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
}
window.setStatusBarColor(Color.parseColor("#4CAF50"));
}
}
Here is my button listener
public void addButtonListener() {
Button = (Button) findViewById(R.id.Button);
Button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setStatusBarColor();
}
});
}
Change your method call to this
In Activity
public void onClick(View view) {
setStatusBarColor(this, Color.parseColor("#4CAF50"));
}
In Fragment:
public void onClick(View view) {
setStatusBarColor(getActivity() , Color.parseColor("#4CAF50"));
}
Or remove the parameter from method
public void onClick(View view) {
setStatusBarColor();
}
public static void setStatusBarColor() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// If both system bars are black, we can remove these from our layout,
// removing or shrinking the SurfaceFlinger overlay required for our views.
//change here
Window window = activity.getWindow();
// By -->>>>> Window window = getWindow();
//or by this if call in Fragment
// -->>>>> Window window = getActivity().getWindow();
int statusBarColor = Color.parseColor("#4CAF50");
if (statusBarColor == Color.BLACK && window.getNavigationBarColor() == Color.BLACK) {
window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
} else {
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
}
window.setStatusBarColor(statusBarColor);
}
}

Android - Ghost Views after Animations

In my SMS app, I have a dialog that opens when the user clicks on an attached image to expand it.
Everything worked fine until I decided to add animations. This dialog has 3 buttons (one close ImageButton in the top right corner and two buttons in a bottom bar of the dialog). When the user clicks on the image, the buttons anime out/in. This all works fine except the buttons are still clickable even after they're hidden.
Before Animations:
After Animations:
My code for the dialog below:
// Declare these here to increase scope. (Listeners)
private static boolean ContainerChanged;
private static boolean ActionsHidden;
private static boolean AnimStarted;
private static boolean closeAnimFinished;
private static boolean barAnimFinished;
private void showExpandedImageDialog(Uri imgUri)
{
// Initialize Dialog
final Dialog dialog = new Dialog(ActivitySend.this, R.style.FullHeightDialog);
dialog.setContentView(R.layout.dialog_attachment_image_send);
// Initialize Views
final RelativeLayout Container = (RelativeLayout) dialog.findViewById(R.id.Container);
final LinearLayout actions = (LinearLayout) dialog.findViewById(R.id.Actions);
final ImageButton btnClose = (ImageButton) dialog.findViewById(R.id.btnClose);
Button btnReplace = (Button) dialog.findViewById(R.id.btnReplace);
Button btnRemove = (Button) dialog.findViewById(R.id.btnRemove);
ImageView image = (ImageView) dialog.findViewById(R.id.Image);
// Load Image & Make Zoomable
PhotoViewAttacher mAttacher = new PhotoViewAttacher(image);
image.setImageURI(imgUri);
mAttacher.update();
// Get animations ready
final Animation fiCloseAnim = AnimationUtils.loadAnimation(ActivitySend.this, R.anim.fade_in);
fiCloseAnim.setFillEnabled(true);
fiCloseAnim.setFillAfter(true);
final Animation foCloseAnim = AnimationUtils.loadAnimation(ActivitySend.this, R.anim.fade_out);
foCloseAnim.setFillEnabled(true);
foCloseAnim.setFillAfter(true);
final Animation dBarAnim = AnimationUtils.loadAnimation(ActivitySend.this, R.anim.slide_down);
dBarAnim.setFillEnabled(true);
dBarAnim.setFillAfter(true);
final Animation uBarAnim = AnimationUtils.loadAnimation(ActivitySend.this, R.anim.slide_up);
uBarAnim.setFillEnabled(true);
uBarAnim.setFillAfter(true);
// Reset static variables
ActionsHidden = false;
AnimStarted = false;
closeAnimFinished = false;
barAnimFinished = false;
// Initialize listeners
AnimationListener closeAnimListener = new AnimationListener()
{
#Override
public void onAnimationEnd(Animation animation)
{
closeAnimFinished = true;
if (closeAnimFinished && barAnimFinished)
{
AnimStarted = false;
closeAnimFinished = false;
barAnimFinished = false;
if (ActionsHidden)
{
actions.setVisibility(View.VISIBLE);
btnClose.setVisibility(View.VISIBLE);
}
else
{
actions.setVisibility(View.GONE);
btnClose.setVisibility(View.GONE);
}
ActionsHidden = !ActionsHidden;
}
}
#Override
public void onAnimationRepeat(Animation animation)
{
// Nothing
}
#Override
public void onAnimationStart(Animation animation)
{
AnimStarted = true;
}
};
AnimationListener barAnimListener = new AnimationListener()
{
#Override
public void onAnimationEnd(Animation animation)
{
barAnimFinished = true;
if (closeAnimFinished && barAnimFinished)
{
AnimStarted = false;
closeAnimFinished = false;
barAnimFinished = false;
if (ActionsHidden)
{
actions.setVisibility(View.VISIBLE);
btnClose.setVisibility(View.VISIBLE);
}
else
{
actions.setVisibility(View.GONE);
btnClose.setVisibility(View.GONE);
}
ActionsHidden = !ActionsHidden;
}
}
#Override
public void onAnimationRepeat(Animation animation)
{
// Nothing
}
#Override
public void onAnimationStart(Animation animation)
{
AnimStarted = true;
}
};
// Set listeners
fiCloseAnim.setAnimationListener(closeAnimListener);
foCloseAnim.setAnimationListener(closeAnimListener);
dBarAnim.setAnimationListener(barAnimListener);
uBarAnim.setAnimationListener(barAnimListener);
// Actions Appear/Disappear onTap (Animate)
mAttacher.setOnPhotoTapListener(new OnPhotoTapListener()
{
#Override
public void onPhotoTap(View view, float x, float y)
{
if (!AnimStarted && ActionsHidden)
{
actions.startAnimation(uBarAnim);
btnClose.startAnimation(fiCloseAnim);
}
else if (!AnimStarted)
{
actions.startAnimation(dBarAnim);
btnClose.startAnimation(foCloseAnim);
}
}
});
// Make dialog square
ContainerChanged = false;
ViewTreeObserver vto = Container.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()
{
public boolean onPreDraw()
{
// Set boolean to save processing power
if (!ContainerChanged)
{
int width = Container.getMeasuredWidth();
Container.getLayoutParams().height = width;
ContainerChanged = true;
}
return true;
}
});
// Set button listeners
btnClose.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
dialog.dismiss();
}
});
btnReplace.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
}
});
btnRemove.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
}
});
// Show dialog
dialog.show();
}
Everything works perfectly if I remove the animations and only use view.setVisibility(View.GONE) but I really like the animations...
Any ideas on what I could do to fix this?
I could probably use view.setClickable(false); but I'm sure there's a better solution than that.
You can do it like this:
Let your activity implement AnimationListener. Then there will be an overridden method public void onAnimationEnd(Animation arg0), write setClickable(false) or setEnabled(false) in this method for all three of your buttons, enable them again when you are starting your animation....hope you got me..

Start and stop recording with button click

I'm making a simple android app for recording sound. I have a startRecording() and stopRecording() methods. Now I implemented a ToggleButton named "Touch to record" so as you can already imagine, when the button is checked, you have to hold the record button to record sound and when the button is on "off" you have to click to start and then click to stop.
This is the current code:
touchToRecord.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked)
{
recBtn.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn_pressed);
chTimer.start();
chTimer.setTextColor(Color.GREEN);
startRecording();
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn);
chTimer.stop();
stopRecording();
nameAlert();
}
return true;
}
});
}
else
{
//onClickListener
}
}
});
Now I'm not sure how to make the onClickListener. If I try to do it like this:
recBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
It won't work because it underlines setOnClickListener and says:
The method setOnClickListener (View.OnClickListener) in the type View is not applicable for the arguments (new DialogInterface.OnClickListener(){})
Also, one more thing after I get this working; how do I check if the method is already running with if statement? I want to do something like this:
if (startRecording == isRunning)
{
stopRecording();
}
You could try android toggle button. Please see more information on:
http://developer.android.com/guide/topics/ui/controls/togglebutton.html
Solved the problem!
I avoided using onClickListener by setting onClick in XML and then just creating the method. Here's the code:
public void recordBtnClick(View v){
final ToggleButton touchToRecord = (ToggleButton)findViewById(R.id.tBtn1);
final ImageButton recBtn = (ImageButton) findViewById(com.whizzappseasyvoicenotepad.R.id.recButton);
if (touchToRecord.isChecked() == false)
{
if (recorder == null)
{
recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn_pressed);
chTimer.start();
chTimer.setTextColor(Color.GREEN);
startRecording();
}
else if (recorder != null)
{
recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn);
chTimer.stop();
stopRecording();
nameAlert();
}
}
else
{
//DO NOTHING
}
}

Categories

Resources