I have a Listview with EditTexts in every cell. Whenever I add text to the point that the EditText grows 1 line, the ListView scrolls to the top (and usually removes the EditText from view by doing so).
I found a few questions about this but they only seem to have "hacks" as answers and nothing that works for me.
ListView scrolls to top when typing in multi-line EditText
Listview with edittext - auto scroll on "next"
EDIT:
After a lot of research, I have found that it is because the editText is resizing, causing the ListView layout its subviews and then resetting to the top. I want to find a way for the EditText to grow while staying at the same place.
I tried this because of a suggestion I found online but, even though it keeps the ListView at the right height, it clears the focus from the EditText and I cannot seem to get it back with requestFocus.
private Stirng getTextWatcher(){
return new TextWatcher(){
#Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
#Override public void afterTextChanged(Editable s) {
listView.setSelection(index);
}
}
}
So what I would need is either:
Have a way for the ListView to not scroll automatically to the top when the subviews are being layed out again or
A way to focus my EditText after having called ListView.setSelected() or 3. Have another way to scroll the ListView that will not clear focus from the EditText.
For option
I have tried scrollTo() and smoothScrollToPosition(). scrollTo for some reason works but hides certain items from the ListView (they become invisible, not loaded). And smoothScrollToPosition() creates an animation where the ListView jumps to the top and then it brings the user back to the Item (and does it 2 or 3 times).
As par my view if you want to take input from ListView item by using EditText then you should use TextView in place EdiText and by click of TextView you should open and Input Dialog with EditText that is more simple and good solution. handling of EditText focus in ListView is vary difficult and you need to put extra Listeners for this.
OR
And if you have limited number of input box in your UI/UX then you can implement EditText with CustomListView by using ScrollViews.
ListView Scrolls to Top when EditText Grows
ListView has some known limitations related to child focus.
Solution
Why don't you try to use RecyclerView instead of ListView? Google introduced RecyclerView as the default recommended way of such UI. So custom ListView or hacks ore no more needed.
You can try adding this attribute to your ListView:
android:transcriptMode="normal"
This makes list automatically scroll to the bottom when a data set change notification is received and only if the last item is already visible on screen.
Read more here: attr_android:transcriptMode | Android Developers
Related
So I am trying to create a task app, in which, on the click of a button, I want my subtask custom view, which contains a text view, and 2 image views, inside a horizontal linear layout, to be displayed inside a vertical linear layout. The data required for the subtask is taken from a different activity, and then transferred using intents. Also, I need the id for every subtask that is added to be different. I have no idea how to do this. Please help. I couldn't code anything for this. I tried using layout inflater, searched up the net on what to do, but I still have no clue. Please help.
You can user dialog fragment for this and is look effective also
Let me give you link...
dialog fragment
I have done something similar in my project, adding horizontal linear layouts in a vertical linear layout programmatically.
Please check the below code:
View view = View.inflate(getActivity(), R.layout.item_horizontal_layout, null);
Button btnSave = view.findViewById(R.id.btnSaveModule);
layoutCount += 1; // A global value to store added layout counts
btnSave.setTag(layoutCount); // Set the tag on button so that we can use it later
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Access the layout position on button click
Log.d("Layout Position", ":" + (int)btnSave.getTag())
}
});
verticalLL.addView(view); // Add horizontal layout to vertical Linear Layout
I have a RecyclerView which is populated with items each containing a TextView. I call textView.setTextIsSelectable(true) on the TextView to make its text selectable, but I notice that when I tap on the TextView, or long press it to select text, the RecyclerView suddenly scrolls up so that the whole TextView is visible. I want to know how to disable this behaviour, so that touching or long pressing the TextView does not cause any scrolling.
hello I'm sorry for the ambiguous nature of my question but this is the easiest way I can explain the problem. Okay so I have an adapter which pulls parsed json data from mysql database and places each json object in a cardview and I have a recyclerview where these cardviews are displayed. Each cardview has a button with an onClick method to change color of button when it is clicked. This works fine except that the action performed on the first cardviews button is also performed on the seventh cardview, action of second cardview performed on the eighth cardview and that's how it continues. In a nutshell when button1 in cardview one is clicked, color of button1 in cardview 1,7,13,19,etc changes. That is looping through pairs of 6. Any idea how to resolve this please.
I solved it by implementing
#Override
public int getItemViewType(int position){
return position;
}
I'm trying to find the best approach to handle a user clicking a button, setting the text of that button to (in my attempt) a textview and then automatically move onto the the next textview to update on the next button press.
At the moment I can do this by manually specifying each element by ID to update but there must be a better way, or a different approach.
An example: imagine a table with 2 rows of each 4 columns (each contains a textview) and below that 6 buttons. When any button is pressed the first time the textview of the first column is set to the button text, the next press of any button sets the the textview of the second column is set to the button text, and so on. When a row is complete it will start back at column one of the next row.
at the moment I'm using a hard coded function triggered by the onclick event, I realized that I could implement some counter and user row[i]_col[j] (or such) but is there a better way to approach this (eg. some way similar to changing focus on edittext)?
public void setValue(View view) {
TextView textView = (TextView) findViewById(R.id.textView_row1_col1);
textView.setText(((Button) view).getText());
}
If i understood your problem correctly ,
I would suggest using an array of Textviews instead of using findviewbyid so much (very pricey memory wise )
TextView [][] txtArr = new TextView[XSize][Ysize];
Initial that in the start once and use it in the on click later.
Below is a picture of my app, I am currently using a modified version of this app to show my expandable content. Now my problem is with my ImageView, I currently have 3 layouts, one for the main dialog (with listview), one for the listview title & arrow and one for the item details (item row once expanded). I want to add animation to the ImageView so when you click the image or title textview, the list will expand and animate the change. How can I do this? I cannot get the onClick to work because it doesn't know there are 2 imageviews, it seens only one...
I will assume that your ImageView is this arrow inside your ListView element (because you didn't write it clearly enough).
You probably shouldnt try to attach onClickListener to your ImageView at all (nor to your ListView element title). The good way of implementing what you want you achieve would be to use ListView's onItemClickListener to detect clicks performed on specific items inside your listview, and then expanding appropriate expandable content.