Error text on edittext box appears to be white - java

I have the following code:
EditText edit = (EditText) findViewById(R.id.bluetractor);
if (edit.getText().toString().equals("Blue Tractor"))
{
Toast.makeText(getApplicationContext(), "Correct", Toast.LENGTH_LONG).show();
}
else
{
edit.setError("Incorrect");
}
But when I click the button that executes the code, it all works well apart from the fact the writing (the "Incorrect") appears to be white and therefore it is not visible on the white background of the error popup. Can anyone help? Thanks

couldn't you either change the color in the XML for that view or
setError("error").setTextColor(bleh)

It can be done in xml by setting
<item name="android:textColorSecondaryInverse"></item>
Alternatively, you can use setError with a Spannable String

Related

Change background color of the button without changing its style

I am stucked in a problem developing an android App.
When the user clicks in a Button, I need to change the background colour of this button. But I need to do in a way that doesn't affects the style, mainly the shapes.
Okay as you have said without changing its style, i think here's what you want,
1.you might be interested in color filters like this
Button btn = (Button) findViewById(R.id.button1);
btn.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
You use different values according to your required colour.If you want to know the constant values of colours, you can refer the documents.
2.you can programmatically set the shade of the entire button using the PorterDuff multiply mode. This will change the button colour rather than just the tint.
For example for a red shaded button
btn.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
For a green shaded button
btn.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);
And so on.What it actually does is, it works by multiplying the current button colour value by your colour value.
3.You can also use an online tool like this Android Button Maker to customize your button and use android:background="#drawable/custom_btn" in your layout(inside the <Button> tag) to define the customized button.
Now there are many more ways too to achieve what you want but i think these are some easy and quick fixes you can use.Hope this helps.
Try:
Button myButton = (Button) findViewById(R.id.myButton1);
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
myButton.setBackgroundColor(color.Green);
}
});
Try this:
Button btn = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
btn.setBackgroundResource(color.lightblue);
}
});
The easiest way of changing a buttons background is using a state list drawable. This is a xml file where different drawables for the background of the button for different states are defined. An Example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true"
<item android:drawable="#drawabel/normal"/> // Drawable in normal state
<item android:drawabel="#drawable/pressed"/ android:state_pressed="true"> //Drawabel when pressed
</selector>
Just use android:background="#drawable/background.xml" in the buttons xml or setBackground(R.drawable.background). The framework will do the work for you.

How to hide keypad in android after pressing home button

I am showing a list with messages and each message's row has a comment button.When i click on comment button opens a comment box with edit text and button for submitting the comment.When comment box appears on screen keypad also appears for entering text.If i pressed home button before entering text then application goes background but keypad remains on screen.This is the thing irritating me.For custom list i am using a custom adapter and code for comment box is written in that adapter.I tried using
inputmgr.hideSoftInputFromWindow(txtComments.getWindowToken(), 0);
but it is not working. So how i can hide this keypad programmatically.
Try using the code in https://stackoverflow.com/a/1109108/1904479. Hope you are not testing it in Android version 4.1.
please use this method to hide soft keyboard.
public static void hideSoftKeyboard(Activity context) {
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputManager != null)
inputManager.hideSoftInputFromWindow(context.getWindow().getDecorView().getApplicationWindowToken(), 0);
context.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
KeyEvent.KEYCODE_HOME can NOT be intercepted. You can hide the keypad inputmgr.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0); in onStop() method of your activity.
It does not require token from focused view editText.

Change the default Button-Label on android Keyboard

Is it possible to change the label of the right-down button (OK-Button) in the default android-keyboard? I want to set my own text on it and I have tried the following code:
EditText et = (EditText) findviewById(...);
et.setImeOptions(EditorInfo.IME_ACTION_DONE);
et.setImeActionLabel("myLabel", EditorInfo.IME_ACTION_DONE);
However, the Label I see on the Button is the default from EditorInfo.IME_ACTION_DONE namely "OK". Should I adapt propely the above code, or is there another way to accomplish it?
You forgot
editText.setInputType(InputType.TYPE_CLASS_TEXT);
Otherwise the EditText defaults to TYPE_CLASS_TEXT | TYPE_TEXT_FLAG_MULTI_LINE, which always has the "enter as line break" key instead of IME_ACTION_DONE.

Dialog context issue - Android

I have created a dialog box like so using a custom layout:
dialog = new Dialog(FetchMenu.this, R.style.CustomDialogTheme);
dialog.setContentView(R.layout.custom_dialog_iab);
dialog.show();
I am now trying to edit a textbox within 'layout.custom_dialog_iab' for example:
TextView text = (TextView) findViewById(R.id.all_topics_unlock_button);
text.setText("Purchased");
My Question: How do I get the right context to be able to edit the textboxes?
P.S. I have tried 'dialog.getContext()' but still keep throwing null pointers?
You need to use:
TextView text = (TextView) dialog.findViewById(R.id.all_topics_unlock_button);
Note the dialog. in front of findViewById
regular findViewById() will search your activity layout rather than the dialog layout.

Can't get focus on editText when children are added in layout

i am adding a spinner, an image and an editText in a linearLayout everytime i click a button.
Now whenever i add this layout , editText shows blinking, means it has focus but the keyboard won't show up. Even i click on it, the keyboard won't show up. What most i can do is to click somewhere else and then back at the editText to make it show keyboard and proper focus.
I am using following code, how can i fix this bug.
viewHolder.title = (EditText) view.findViewById(R.id.AddNewDetail);
view.setTag(viewHolder);
layout.addView(view);
i think you should force the softkeyboard show.
((InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE))
.showSoftInput(editText,
InputMethodManager.SHOW_FORCED);
and close it
((InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(editText.getWindowToken(),
0);
and focus on edittext
editText.requestFocus();
May you have added the below line in for your activity in manifeast file
android:windowSoftInputMode="stateHidden"
try removing it or you can manaually use the requestfocus method to get focus. try the below method.
edittext.requestFocus();

Categories

Resources