Textview onclick open keyboard and set the input as Textview's text - android

I am trying to click on textview and open keyboard (i did that) and then what the user inputs i want to set that text in particular text view. I am using Fragments.
Getting keyboard by:
TextView test = (TextView) rootView.findViewById(R.id.txtSent);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}// end onClick
});

set android:editable="true". Although you're better off with an EditText.

Related

How to display input method picker list title and summary in textview android?

i am sing keyboard view to select. After selecting the keyboard, how to display the selected keyboard text in textview android?
i can display only language but how to display the Title and summary for change keyboard?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public void onClick(View view) {
InputMethodManager imm = (InputMethodManager) MainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showInputMethodPicker();
InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();
String localeString = ims.getLocale();
Locale locale = new Locale(localeString);
String currentLanguage = locale.getDisplayLanguage();
Toast.makeText(MainActivity.this,currentLanguage,Toast.LENGTH_SHORT)
.show();
//Title and summary for change keyboard need to display
}
});
}
}
there are two ways to handle this, the simplest way is use a non-size-edittext
don't hide the text view, if it hide then edtTxt.getText().toString() gets empty always
<EditText
android:id="#+id/edtTxt"
android:layout_width="0px"
android:layout_height="0px" />
So that user can't see that. and on click of button
edtTxt.requestFocus();
edtTxt.setText("");
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(edtTxt.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED,
0);
Now edtTxt.getText().toString() giving text.
Without an EditText you're going to have a hard time.
An InputMethod needs to be connected to a view. Whatever view you use, you need to override onCreateInputConnection to return a custom InputConnection object that at a minimum implements commitText (for word input), deleteSurroundingText (for deletes), and sendKeyEvent (for keyboards that assume you're in dumb mode), and all of the completion functions. Input connections are complicated things and you'll screw up 3rd party keyboards like Swiftkey and Swype if you don't get it right. I really don't suggest doing this.
If you want to do this your best chance of getting it right is to claim your window is a TYPE_NULL input type. Most keyboards will dumb themselves down and assume you only accept the simplest commands in that mode. But you can't count on it.
I'd look at the InputConnection returned by the EditText class and copy as much of it as possible.

changing the button text from the edittext state

I have one button and 1 edittext. The button is set to "change", and EditText is enable-false. When you click on a button, its text is changed to "save", and edit text is available for input. After a second press, the text changes to "change" again, and the button becomes Enable-false.
how to implement it?
Use this onClickListener on your button -
Button yourButton = (Button) view.findViewById(R.id.button)
EditText yourEditText = (EditText) view.findViewById(R.id.edittext)
yourButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (!yourEdittext.isEnabled()){
yourButton.setText("Save");
yourEditText.setEnabled(true);
counter++;
}else if (yourEdittext.isEnabled()){
yourButton.setText("Change again");
yourButton.setEnabled(false);
}
}
});
Make sure to set the initial button's text and the edit text disabled for the starting point

edittext not getting editable from read only mode dynamically

I am trying to make edittext editable after a button click.
Below is my code snippet.
Before click event:
name_value = (EditText) findViewById(R.id.name_value);
name_value.setText(DashBoardDisplay_l.getName());
name_value.setFocusable(false);
likes_value = (EditText) findViewById(R.id.likes_value);
likes_value.setText(DashBoardDisplay_l.getLikes());
likes_value.setFocusable(false);
calls_value = (EditText) findViewById(R.id.Calls_value);
calls_value.setText(DashBoardDisplay_l.getCalls());
calls_value.setFocusable(false);
After click event:
android.view.View.OnClickListener enable_listeners = new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Zumbare","in enable listners");
Button b = (Button) v;
if(b.getText().toString().equalsIgnoreCase("edit")){
Log.d("Zumbare","in edit condition");
desc.setOnClickListener(show_buttons);
icon.setOnClickListener(slct_image);
name_value.setFocusable(true);
likes_value.setFocusable(true);
calls_value.setFocusable(true);
edit.setText("Save");
name_value.requestFocus();
}
}
};
But after button click event , no focus is coming on to name_value edittext and also not editable.
so what is wrong i am doing here or anything more to do?
You would need to use setEnabled() for that.
setFocusable() will not make edittext editable. It will just set focus on particular edittext.
// make editable
name_value.setEnabled(true);
likes_value.setEnabled(true);
calls_value.setEnabled(true);
// set focusable
name_value.setFocusable(true);
likes_value.setFocusable(true);
calls_value.setFocusable(true);
//set focus to particular edittext
name_value.requestFocus();
should work in onClick()'s code.

How to go to Edittext when button clicked in android

I have a layout which contains button at top and some textviews and then at bottom I have edittext.When I click button, it should point to edittext. How to proceed? Thanks in advance.
To show keyboard when EditText is in focus mode.
myEditText.requestFocus();
if(myEditText.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
If you want to show some message
myEditText.setError("Please Enter Valid Value!");
You can remove message focus by Using:
myEditText.clearFocus();
In onClick method of a button write this edittext.requestFocus();
onButtonClick write below code
myedittext.requestFocus();
Try this,
OnClickListener buttonListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText edit = (EditText)v.findViewById(your edittxt id);
edit.requestFocus();
}
}
Yes go with requestFocus().
Inside your onClick() method write,
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editText.requestFocus();
}
private fun getKeaboardButtonListener(view: LinearLayout): View.OnClickListener {
return View.OnClickListener {
val editText :EditText = view.findViewById(R.id.editText)
editText.requestFocus()
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
}
}

Android: softkeyboard not showing up

I have 2 EditTexts in the MainActivity Layout. If i run the application normally the 1st EditText gets focused but the softkeyboard is not openned.
but when i used this:
public class TestingActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText et1 = (EditText) findViewById(R.id.editText1);
EditText et2 = (EditText) findViewById(R.id.editText2);
et2.requestFocus();
InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mInputMethodManager.showSoftInput(et2, InputMethodManager.SHOW_IMPLICIT);
}
}
expecting the 2nd EditText will get focus and softkeyboard will be openned.
I only get focus, but the softkeyboard is openned only when i click on the EditText.
Thank You
Try specifying the android:windowSoftInputMode attribute in your AndroidManifest.xml file for your activity.
For example:
<activity android:name=".TestingActivity" android:windowSoftInputMode="stateVisible|adjustResize" />
You probably don't need any of the code that uses InputMethodManager in your Activity.
I notice that one reason for the keyboard not showing up is selecting an inputtype not supported by the specific Android device. For instance InputType.TYPE_NUMBER_VARIATION_NORMAL will not work on my Asus Transformer (no keyboard shows up), while InputType.TYPE_CLASS_NUMBER will work just fine.
et2.clearFocus();
et2.requestFocus();
InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mInputMethodManager.showSoftInput(et2, InputMethodManager.SHOW_IMPLICIT);
I meet the problem on Android N platform and resolve it by refocusing the editview.
I don`t know the real reason why the editview should be cleared first,but it works fine for me.
Sometimes you will need to post-delay showing keyboard command, so in my case, i did the following
editText.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
}, 300);
For getting the focus to particular edittext just add the tag inside your edit text.
<EditText
android:id="#+id/etBox"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="enter into editbox"
>
<requestFocus/>
</EditText>

Categories

Resources