android anko alert custom layout currentFocus - android

I have a alert view with custom layout which contains EditText
activity.alert {
var quantityEt: EditText? = null
customView {
linearLayout {
orientation = LinearLayout.VERTICAL
quantityEt = editText {}
quantityEt!!.requestFocus()
Log.e("LOL", ac.window.currentFocus.toString())
}
}
}.show()
then I request focus to this edit text. However, activity.window.currentFocus outputs that the currentFocus android.support.v7.widget.RecyclerView.
I need the currentFocus to be EditText. I also tried to tap it to request focus, but still got RecyclerView.
Is there a way to achieve that?

Related

Get text from EditText within for loop in Kotlin

I'm developing an app in Android in Kotlin, and in one activity I have 10 editText. How i could get the text of all editTexts within a for loop? EditTexts are into Constraint Layouts, which are into a Linear Layout.enter image description here
You can iterate over the child views of the parent view group and just gather the texts, something like this:
val parentView: ViewGroup = findViewById(R.id.parent)
for (i in 0 until parentView.childCount) {
val view: View = parentView.getChildAt(i)
if (view is EditText) {
Log.d("text", view.text.toString())
}
}
Change the R.id.parent to the correct id of course - the id of your constraint layout.

Disable positive button in alert dialog in kotlin

I am trying to disable okButton till the edit text is empty.I couldn't find a solution to it. I tried adding text watcher but I don't know how to disable positive button in that.
val inputTxt = EditText(this)
alert("Enter your mobile number") {
customView = inputTxt
inputTxt.setInputType(InputType.TYPE_CLASS_NUMBER)
inputTxt.setFilters(arrayOf<InputFilter>(InputFilter.LengthFilter(10)))
inputTxt.setRawInputType(Configuration.KEYBOARD_12KEY);
okButton {
startActivity(intentFor<NewActivity>()
}
isCancelable = false
cancelButton { finish() }
}.show()
This is not possible (in a clean way) with the AlertDialog API itself, but you could have your own "OK" button in the custom view that you've added. The main risk is that it is not garanteed to look exactly (same margins and paddings) as the regular "OK" button.

Xamarin.Forms View does not show keboard keyboard when we focus the EditText in Android

In Forms inherited from View and I have added EditText in Android project for that forms view by using custom render. And I have manually focused the view in button click dynamically. And now EditText has been focused but keyboard does not show.
Please find the Sample here for your reference
So to resolve the above issue by push to show the keyboard in EditText OnFocusChanged() by using the following code snippet. But when I have added the view inside of TableView in pcl, keyboard gitches when we tap the on that view.
InputMethodManager inputManager = (InputMethodManager)this.Context.GetSystemService(Context.InputMethodService);
if (gainFocus)
{
if (inputManager != null)
{
inputManager.ShowSoftInput(this, ShowFlags.Forced);
}
}
else
{
if (inputManager != null)
{
inputManager.HideSoftInputFromWindow(this.WindowToken, 0);
}
}

Anko ListItem setOnClickListener

I'm trying to play around with some Kotlin and Anko (more familiar with iOS) and taking from their example, there is this code:
internal open class TextListWithCheckboxItem(val text: String = "") : ListItem {
protected inline fun createTextView(ui: AnkoContext<ListItemAdapter>, init: TextView.() -> Unit) = ui.apply {
textView {
id = android.R.id.text1
text = "Text list item" // default text (for the preview)
isClickable = true
setOnClickListener {
Log.d("test", "message")
}
init()
}
checkBox {
id = View.generateViewId()
setOnClickListener {
Log.d("hi", "bye")
}
init()
}
}.view
My row appears how I want with a checkbox and textview. But I want to bind an action to the row selection not the checkbox selection. Putting a log message in both, I see that I get a log message when the row is selected which flips the checkbox. It does not, however, log my "test:message" from the textView click handler. Is there a way to get around this?
Apparently your issue has been addressed here. As the checkbox is consuming all the focus of ListItem you should set the CheckBox's focusable flag to false:
checkBox {
focusable = View.NOT_FOCUSABLE
}
Unfortunately setFocusable call requires at least API 26, but you could define view .xml and inflate the view manually as described here:
<CheckBox
...
android:focusable="false" />
Alternatively you could try setting a onTouchListener returning false which means the touch event will be passed to underlying views.
Let me know if it works ;)

Keyboard placement obscures view below EditText would like to keep visible

I have an activity that is basically a long form of entry fields.
On each row, I want to show a TextView to serve as hint text just below each EditText and I want the TextView to remain visible at all times when the user is entering data. Unfortunately, the soft keyboard obscures the hint text and always positions itself immediately below the EditText. Is there any technique that will allow the TextView below the EditText to also be visible when the soft keyboard appears and the contents are adjusted (via windowSoftInputMode=adjustResize|adjustPan), without having the user scroll ?
Vishavjeet got me on the right track in suggesting I scrolldown to reveal the view that may be overlapped by the keyboard. Below is a function similar to what I used to solve the problem. It can be called when the EditText above the TextView receives focus:
// View targetView; // View that may be hidden by keyboard
// ScrollView scrollContainerView; // Scrollview containing hiddenView
//
void assureViewVisible (View targetView, ScrollView, scrollContainerView) {
Window rootWindow = activity.getWindow();
Rect rMyView = new Rect();
View rootview = rootWindow.getDecorView();
rootview.getWindowVisibleDisplayFrame(rMyView); // Area not taken up by keyboard
int subTextPos[] = new int[2];
targetView.getLocationInWindow(subTextPos); // Get position of targetView
int subTextHt = targetView.getHeight(); // Get bottom of target view
if ((subTextPos[1]+subTextHt) > rMyView.bottom) { // Is targetView at all obscured?
int scrollBy = (subTextPos[1]+subTextHt) - rMyView.bottom + 10; // add a small bottom margin
mMeasurementViewScrollView.smoothScrollBy(0, scrollBy); // Scroll to subtext
}
}
EDIT:
By understanding the problem more deeply, I think that you should add scroll programatically when user clicks on the Edittext. Here is the code to do that:
private final void focusOnView()
{
new Handler().post(new Runnable()
{
#Override
public void run()
{
your_scrollview.scrollTo(0, your_EditBox.getBottom());
}});
}
From my personal experience I think there is not such way to do that. The thing you can do is place the hint textview toRightOf the editext. Or Use modern Approach by using a Hint Placeholder on Edittext:
In XML, it's simply android:hint="someText"
Programatically you can use edittext.setHint(int);
pass R.string.somestring in above method.

Categories

Resources