I have a dialog with dynamic content, so I've created a listview inside this dialog and then an Adapter which decides what layout each item has,
Adapter chooses from 3 layouts,
one contains checkbox - works fine
second contains spinner - works fine
third contains edittext - SW keyboard will never display
Here is the edittext item layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="?android:listPreferredItemHeight"
android:paddingStart="24dp"
android:paddingEnd="24dp"
>
<TextView
android:id="#+id/name"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Test"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
/>
<EditText
android:id="#+id/value"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:focusable="true"
/>
</LinearLayout>
and the adapter choosing a layout:
class FieldAdapter extends ArrayAdapter<ProfileField> {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ProfileField field = getItem(position);
switch (field.type) {
case ProfileFieldType.CUSTOM_TEXT:
convertView = inflater.inflate(R.layout.scanprofile_dialog_edit_item, parent, false);
break;
case ProfileFieldType.CUSTOM_CHECK:
convertView = inflater.inflate(R.layout.scanprofile_dialog_checkbox_item, parent, false);
break;
default:
ProfileField.ListProfileField listField = (ProfileField.ListProfileField) field;
convertView = inflater.inflate(R.layout.scanprofile_dialog_item, parent, false);
break;
}
return convertView;
}
...
}
I've tried adding this to onCreateDialog
final AlertDialog alertDialog = builder.create();
final Window dialogWindow = alertDialog.getWindow();
dialogWindow.clearFlags(
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
return alertDialog;
But it didn't help at all.
Any ideas why the SW keyboard is not shown?
EditText View inside ListView is not supported in android as both need focus to work properly.
In Your case all the event are consume by ListView that's why keyboard is not opening when you are trying to clicking on EditText view.
You can give focus to EditTextView but it will create problem in ListView functionality.
You can either put EditText in header/footer of ListView or if you have
fix number of view like only 3 then directly plot those view on dialog view instead of inflating them inside ListView.
Refer the here if you still want to use EditText inside ListView.
add in xml
<EditText...>
<requestFocus />
add in code
boolean checkFocus=youredittext.requestFocus();
Log.i("CheckFocus", ""+checkFocus);
if(checkFocus==true)
{
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(youredittext, InputMethodManager.SHOW_IMPLICIT);
}
you can try this code (help you proactively do it) any time you want)
//edittext demo code
final EditText myEditText = (EditText)findViewById(R.id.editText);
myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
myEditText.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(myEditText, 0);
}
}, 50); //require system show soft keyboard after 50 miliseconds
}
});
Related
I am making a window to open a dialog window and write a comment.
When you open the dialog window, there is a layout where you can enter your first comment.
Comment can only be written in one line and the following comment layout is created by pressing EnterKey in the input field.
I originally implemented this as a RecyclerView, but now I am trying other methods as well. (using addView())
But when I used addView() I got the following error.
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
The solution to this is shown in the here. (And I would appreciate it if you could explain why this error occurs.)
There is no error when using this.
However, no more items are added.
There is only the first item that existed when the dialog box was opened, and there is no response when pressing the Enter key.
I want to add items dynamically in real time.
What should I do?
fragment_wc_dialog.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".fragment.WritingCommentDialogFragment">
</LinearLayout>
wc_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragment.WritingCommentDialogFragment">
<EditText
android:id="#+id/comment_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:gravity="center_vertical"
android:drawableLeft="#drawable/ic_bullet_point"
android:drawablePadding="5dp"
android:layout_marginHorizontal="10dp"
android:background="#null"
android:textSize="15sp"
android:inputType="text"
android:maxLines="1"
android:maxLength="22"
android:imeOptions="actionNone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
WCDialogFragment.java
public class WritingCommentDialogFragment extends DialogFragment {
LinearLayout mContainer; // input layout
View inputView;
EditText editText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_writing_comment_dialog, container, false);
initViews(view);
mContainer.addView(inputView); // The first item to be present when the dialog is opened
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN) {
mContainer.addView(inputView);
}
return true;
}
});
return view;
}
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
#Override
public void onResume() {
super.onResume();
Toast.makeText(getContext(), "onResume()", Toast.LENGTH_SHORT).show();
setDialogSize();
}
private void initViews(View view) {
mContainer = view.findViewById(R.id.container);
inputView = LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item, null);
editText = inputView.findViewById(R.id.comment_edit);
}
}
ADDED
Your problem and the linked issue are different, as far as I can tell. The link has two views that they want to swap back and forth between, so the answer is to remove one view before adding the second. You want to build up a list of views, so you must use different instances rather than adding the same view over and over.
If you want the newly-added view to also respond to the Enter key, you can set the same listener to the new view too:
if(event.getAction() == KeyEvent.ACTION_DOWN) {
EditText addedView = (EditText) LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item, mContainer, false);
addedView.setOnEditorActionListener(this);
mContainer.addView(addedView);
}
I have a RelativeLayout nested inside another RelativeLayout. By default, it is hidden. When user clicks a button, the nested RelativeLayout becomes visible. This layout contains an EditText. When user is finished typing, I want to hide the keyboard:
<RelativeLayout
...
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cameraEmoji"
android:contentDescription="#string/camera_emoji"
android:src="#mipmap/ic_image_black_24dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/cameraText"
android:layout_marginStart="57dp" />
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/emojiTextView"
android:background="#80444444">
<EditText
android:id="#+id/actionEmojiText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="#FFF"
android:imeActionId="#+id/finishText"
android:imeActionLabel="#string/action_complete_emoji_text"
android:imeOptions="actionDone"
android:maxLines="1"
android:singleLine="true"/>
</RelativeLayout>
...
</RelativeLayout>
The activity:
cameraText = (ImageButton)findViewById(R.id.cameraText);
cameraText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
emojiTextView = (RelativeLayout) findViewById(R.id.emojiTextView);
emojiTextView.setVisibility(View.VISIBLE);
final EditText actionEmojiText = (EditText)findViewById(R.id.actionEmojiText);
actionEmojiText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
if (id == EditorInfo.IME_ACTION_UNSPECIFIED) {
emojiTextView.setVisibility(View.INVISIBLE);
View view = MainActivity.this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
dropTextIn(actionEmojiText);
}
return false;
}
});
}
});
getCurrentFocus returns null but the keyboard remains visible. How can I hide the keyboard after typing in the EditText field?
editText.setFocusable(false);
editText.setFocusableInTouchMode(true);
try to add these two properties to EditText, It will hide keyboard whenever you touch any other view after typing to Edittext
I discovered what appears to be the issue. getCurrentFocus() was returning null because the item I had focused I had set to View.INVISIBLE before invoking getCurrentFocus(). Consequently, the view was no longer visible and thus no longer focused.
I have EditText and input type is textNoSuggestions.
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="5dp">
<EditText
android:id="#+id/firstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Name"
android:inputType="textNoSuggestions" />
</android.support.design.widget.TextInputLayout>
Also i have one button and in button onclick method i try to change keyboard's input type.This is a source
final Button changeKeyboard = (Button) dialog.findViewById(R.id.change_keyboard);
changeKeyboard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
firsName.setRawInputType(InputType.TYPE_CLASS_NUMBER);
}
});
Is it a possible to change keyboards' input type in button click when keyboard is showing?
How i can solve my problem thanks everyone
set is setTransformationMethod(), not setInputType(). So something like:
firstName.setTransformationMethod(numberTransformationMethod.getInstance());
On your code, change:
firsName.setRawInputType(InputType.TYPE_CLASS_NUMBER);
to this (firstName with "t", the name on your xml):
firstName.setRawInputType(InputType.TYPE_CLASS_NUMBER);
Also, you positively can change your keyboard calling setInputType, like this: firstName.setInputType(x), where x is an int and can be 1 (alfanumeric); 2 (numeric) or 3 (phone like).
EDIT:
You can hide your keyboard calling this on your activity:
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
firsName.setInputType(InputType.TYPE_CLASS_NUMBER);
I am working on android application in which i am using ViewGroup to show my footer in my list view. Now i want to make an listener event for that view group, so user can press that footer. My code to make that footer and view group is given below along with the xml.
ViewGroup footer = (ViewGroup) getLayoutInflater().inflate(R.layout.footer_view, mListView, false);
View header = getLayoutInflater().inflate(R.layout.footer_view, null);
Button headerButton = (Button)header.findViewById(R.id.footerRefreshBtn);
mListView.addFooterView(footer);
headerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "I am clicked", Toast.LENGTH_LONG).show();
}
});
// It is not showing toast message on click.
XML for footer:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical" >
<Button
android:id="#+id/footerRefreshBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_gravity="center_horizontal"
android:text="Refresh Button"
android:textColor="#color/white"
android:background="#color/gray" />
</LinearLayout>
I couldn't find out how to setOnClickListener to a footer, but I found a different solution. I think you could use this, just find the footerRefreshBtn and set Button's OnClickListener only before or after you add footer view. Both ways are working.
LayoutInflater inflater = getLayoutInflater();
ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer, mListView, false);
mListView.addFooterView(footer, null, false);
mListView.setAdapter(mAdapter);
Button footerRefreshBtn = (Button) footer.findViewById(R.id.footerRefreshBtn);
footerRefreshBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "I am clicked", Toast.LENGTH_LONG).show();
}
});
This way, you are only assigning the Button's onClick event.
Nevertheless if you still want to set an onClickListener to a whole footer, you can get footer's layout and set an onClickListener to this layout using the method I mentioned above.
Background
I have a form-like activity, which has some views that can be created dynamically upon pressing.
I'm using an xml for each field that is inflated upon clicking on a button.
What I need is that upon choosing to add a new item, it will get focus, scroll if needed, and show the keyboard so that the user can type things into it. The keyboard may be shown upon adding the field or when clicking on the EditText.
The problem
For some reason, on some devices (and I don't think it's even an android version issue) when inflating the new view, the editText within it can get focus but it doesn't show the keyboard, even if it has focus and the user clicks on it.
In such a case, the only way to show the keyboard is to click on another EditText.
I've tested it, and noticed that it doesn't occur when I don't use xml at all, meaning when i create the views only in code.
The question
Why does it occur? What can I do in order to fix it?
I've already tried so many possible solutions, but none work on all devices.
Sample Code
the next code has the described issue on xperia j (android 4.0.4) an galaxy mini (android 2.3.6) .
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewGroup container = (ViewGroup) findViewById(R.id.LinearLayout1);
final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
final View view = inflater.inflate(R.layout.field, null);
// using the next code works when using it instead of inflating a layout:
// final EditText editText = new EditText(MainActivity.this);
// editText.setText("item " + container.getChildCount());
container.addView(view);
}
});
}
the field layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFffffff"
android:gravity="center_vertical" >
<Spinner
android:id="#+id/typeSpinner"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:background="#null"
android:padding="5dp" />
<include
android:id="#+id/removeItemView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
layout="#layout/remove_item_view" />
<EditText
android:id="#+id/fieldEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/removeItemView"
android:layout_toRightOf="#+id/typeSpinner"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="field"
android:imeOptions="actionDone"
android:inputType="phone"
android:minWidth="200dp"
android:padding="5dp"
android:singleLine="true"
tools:ignore="HardcodedText" >
<requestFocus />
</EditText>
</RelativeLayout>
the main layout file:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
tools:ignore="HardcodedText" />
</LinearLayout>
</ScrollView>
EDIT: found a partial solution which won't show the keyboard right away, but at least it will be shown when pressing the editText:
public static void forceFocusOnView(final View view) {
if (view == null)
return;
view.post(new Runnable() {
#Override
public void run() {
view.clearFocus();
view.post(new Runnable() {
#Override
public void run() {
view.requestFocus();
}
});
}
});
}
I'm not sure in a reason of that problem. Could be some missmatches when Android sdk were rewritten in order to match some devices.
So I think that the best solution to you is to show your keyboard manually and do not try to move a train, just take the path of the least resistance:
EditText et=(EditText)this.findViewById(R.id.edit_text);
et.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(et, 0);<----------------
}
});
Here you go , man. Wish you luck.
Oh i see...
private EditText et;
private Runnable setFocus;
......
protected void onCreate(...){
.
.
.
setFocus=new Runnable(){
#Override
public void run() {
et.requestFocus();
}
};
}
....
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
final View view = inflater.inflate(R.layout.field, null);
//try this:
et=(EditText)view.findViewById(r.id.fieldEditText);
et.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(et, 0);<----------------
}
});
container.addView(view);
container.requestLayout();<--------------
et.postDelayed(setFocus,300);
}
});