I have an EditText inside a CardView.
When the CardView is selected, I request focus for the EditText, see below:
cardProblem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//request focus
etProblem.requestFocus();
//display keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(etProblem, InputMethodManager.SHOW_IMPLICIT);
}
});
The problem is that I don't want the EditText to be clickable (currently when I tap on the EditText it gets focus).
I understand that when I don't want it to be clickable, I can add:
android:clickable="false"
android:focusable="false"
But in this situation I can't set focusable to false because the EditText needs focus when I want to edit it.
Any ideas how I can resolve this?
I have resolve this, sort of...
Since I don't have time to waste, I went with the following:
I decided to set the visibility of EditText to invisible and when the CardView is selected I make the EditText visible then request focus.
Maybe it helps you:
create a fake EditText as a first child in your CardView and set the width and height to zero or set visibility to gone, so he will get the focus by default and the foucus in your main EditText will be removed and when you need to set focus to it just request the focus :
etProblem.requestFocus();
Related
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.
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.
In my app, I have a dialog with EditText. I have coded it in such a way that as soon as the dialog shows up,the soft keyboard shows up, the edit text will get focus , all the existing text in the editText is preselected. And user can start typing. This all works fine when I am holding my phone in portrait orientation. But the same thing does not work when I have phone in landscape orientation. Part of my code in my fragment is pasted below.
final EditText inputDeviceId = new EditText(getActivity());
inputDeviceId.setInputType(InputType.TYPE_CLASS_TEXT);
String savedDevId = getDeviceID();
inputDeviceId.setText(savedDevId);
inputDeviceId.setSelectAllOnFocus(true);
// some more code ... to create dialog , ok cancel buttons then,
final AlertDialog alertDialog = alertDialogBuilder.create();
inputDeviceId.setOnFocusChangeListener(new View.OnFocusChangeListener() {
if(hasFocus){
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
});
alertDialog.show();
Now this all works in portrait orientation and does not work in landscape.
In landscape mode the dialog with edittext shows up , it has focus and existing text is preselected but it does not show the keyboard. User has to click in the editText for keyboard to come up. Why? I am using nexus-5 to test.
I have set a content view in Android with:
setContentView(R.layout.activity_main);
Now after one of the buttons is clicked, the following code is executed to enable another button:
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
buttonPause.setEnabled(true);
...
This enables the button. BUT only after a minute or so.
Do I need to refresh the button or layout? Or is that bad practice? I am wondering what causes this delay. I have read about notifyDataSetChanged(), but I do not think that is the right method.
notifyDataSetChanged() has nothing to do with Buttons, but with Adapters.
Did you try to add a buttonPause.invalidate() right after enabling it ?
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();