So I have an EditText with fixed size that is most likely to contain a long text with multiple lines.
When the view doesn't have focus I want to scroll to the top of it. Managed to do that using view.scrollBy(0,0) inside it's setOnFocusChangeListener when hasFocus is false.
But I also want to scroll down to the last line of the EditText when hasFocus is true. Is there a way to get some valid int values to pass in the view.scrollBy(x,y) based on the last text line inside the view?
If your EditText has focus then you could go with
editText.setSelection(editText.text.length) //place the cursor at the end of the text
which should automatically scroll down to the end of the editText
Check it out if it will work
EditText editText = (EditText) findViewById(R.id.edtText);
editText.setSelection(editText.getText().length());
Related
I tried to remove focus from empty editText but it isn't working correctly.
I called clearFocus() on edittext ,and then I placed break point at my onFocusChanged() function call.
Here is what happened:
onFocusChanged() called 4 times with the focused parameters values false,true,false,true.
What I thought was that onFocusChanged() must be called only once (with focused = false)
Sorry for my bad English. Any help would be appreciated.
Thanks
In xml, make parent layout
android:focusable="true"
android:focusableInTouchMode="true"
and then call clearFocus on edit text and then call parent request focus
mFragmentBase.editText.clearFocus();
mFragmentBase.parentLayout.requestFocus();
This is happening because your EditText is the first focusable view.
From the docs,
Note: When a View clears focus the framework is trying to give focus
to the first focusable View from the top. Hence, if this View is the
first from the top that can take focus, then all callbacks related to
clearing focus will be invoked after which the framework will give
focus to this view.
You can try setting a dummy focusable view above the EditText to clear the focus from it.
Instead of clearing the focus from EditText create an empty view at the top of the layout
<LinearLayout
android:id="#+id/focusableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:focusable="true"
android:focusableInTouchMode="true"/>
and declare a utility function to hide the keyboard like this
fun hideKeyboard(activity: Activity) {
val inputMethodManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(activity.currentFocus?.windowToken, 0)
}
Finally when you need to clear focus just call
focusableLayout.requestFocus()
hideKeyboard(currentActivity)
I am trying to hide unwanted TextView and EditText fields dynamically using my program. The code is working, but the hidden fields is being replaced with blank space. I wish the other contents below the hidden fields must be automatically get loaded on the hidden field(so that unnecessary scrolls and plain white field can be avoided)
EDIT 1
Please find the sample
http://i.stack.imgur.com/nnXrV.png
http://i.stack.imgur.com/BDQFm.png
text3.setVisibility(View.GONE);
edit3.setVisibility(View.GONE);
Put visibility of TextView and EditText to View.GONE not to View.INVISIBLE
TextView tv;
EditText et;
tv.setVisibility(View.GONE);
et.setVisibility(View.GONE);
Use as above when you want to hide the view.
this will hide your textview and edittext
TextView tv = (TextView)findviewbyId(R.id.textview1);
EditText et = (EditText)findviewbyId(R.id.edittext1);
tv.setVisibility(View.Gone);
et.setVisibility(View.Gone);
I have a ListView and in that ListView I have a Row with some TextViews and EditTexts in it.
When I press anywhere on the row I want the EditText to take focus so text can be entered.
The problem is that I cannot get this to work. When i place the EditText in the ListView the OnItemClick will not respond when I click on the ListView.
I tried using focusable="false" on the EditText and it allowed me to click on the ListView again but I could not get the EditText to get focus even after setting the focusable to true.
Next I tried using the android:descendantFocusability="beforeDescendants" in the ListView but it did not seem to make any change, it still wouldn't work.
Does anyone have an idea on what I can do to make it work?
Try this, it should let you click on a line and focus the EditText (modified from this SO Answer):
public void onItemSelected(AdapterView<?> listView, View view, int position, long id)
{
EditText yourEditText = (EditText) view.findViewById(R.id.youredittextid);
listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
yourEditText.requestFocus();
}
public void onNothingSelected(AdapterView<?> listView)
{
// onNothingSelected happens when you start scrolling, so we need to prevent it from staying
// in the afterDescendants mode if the EditText was focused
listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
}
do like this
ListView listView = (ListView) findViewById(R.id.yourListViewId);
listView.setItemsCanFocus(true);
listView.setClickable(false);
ie,
after getting the listview object from xml, disable the click on this listview and pass the focus to the child views
you can add a property as enabled="false" in the xml row layout, and on the onItemClickListener you can setEnabled(true) and then setFocusable(true) , this should solve it.
I have a textview that I have linked to an edittext box so that when it is edited, it changes my textview. I have a radiobutton used in the interpretation of the textview but it has to be selected before i edit the edittext box in question.
What I would like is for the textview to be changed when i change the radiobutton AS WELL AS when i change the edittext boxes. An example is:
I have 3 Edittext boxes and 1 RadioGroup (consisting of 2 RadioButtons [but one of the buttons is selected by default]). I have 1 Textview.
I would like it so that when all edittext boxes are filled in, the textview is changed. I also want it so that when i change the radiobutton, it will change the textview again. And then i can go back and edit any of the 3 edittext boxes and it will change the textview. At the moment, i only have 1 addTextChangedListener in use at the moment, which is on the edditext box at the bottom of the screen (in the hope the user fills all the top fields in first). But the problem is that if the user changes the topmost edittext box, it doesnt change the textview because the listener is on the bottom box.
Hope that is clear.
Add a text changed listener to all the text boxes.
This listener should be the same for all of them. It simply needs to check if the other boxes have been filled.
if(
textBox1.getText().toString() != "" &&
textBox2.getText().toString() != "" &&
textBox3.getText().toString() != "") {
//All boxes have a value, so change the TextView
....
}
i'm new to android. So can any one help me to get the following, when i enter the number in a EditText, the entered no.of EdidText should be appear dynamically. For example if i entered 3 in a EditText then, 3 EditText(no.of) have to be shown on the screen. How can i show this?
You need to add a TextWatcher to your EditText by calling EditText.addTextChangedListener(). Then in your TextWatcher wait for calls to afterTextChanged and count the characters and update your title. This will be easier if you make your Activity implement the the TextWatcher interface itself.
Add layout to your xml where you want to display the Edittext.
Then create the Layout variable e.g
LinearLayout ll = (LinearLayout)findViewById(R.id...);
and create new textboxes and add to the linearlayout e.g
EditText[] edit = new EditText[]();
EditText[] edit= new EditText[entered number];
initialize the variables in the loop by new Edittext(context);
and add each one to the Layout
`ll.addView(edit[iterator]);`