Android table-layout not rendering real-time - android

I'm a newbie to android facing a UI update problem.
I have some TextViews and EditText widgets in rows of a table-layout.
When the user clicks on an item on a list, I want to populate the details of that item into this table.
I believe my code for updating the edittext widgets is working find and I can see that it has been updated.
However,I observe the table-row becomes blank on an update and when I click on the edittext, only then can I see the updated text (I guess I can see updated text only on onFocus() or something).
Could someone please tell me why does it behave this way?
Thanks

When you update the view use another thread and call invalidate();
yourEditText.post(new Runnable() {
#Override
public void run() {
yourEditText.setText("your text");
yourEditText.invalidate();
}
});

Related

Long pressed broken for EditText (Or android.support.v7.widget.AppCompatEditText) after the view is recycled in RecyclerView

Bug summary
After EditText is being recycled in RecyclerView, its long press behavior which used to select all text, and show context menu "Cut/Copy/Paste", no longer work as expected.
This problem occurs from Android 15 till Android 28.
I tried both EditText and android.support.v7.widget.AppCompatEditText. Both yields same problem.
I can confirm this problem occurs after View is being recycled. If I apply setIsRecyclable(false); in ViewHolder, the problem will not occur.
Steps to reproduce
Long press on 1st EditText. We can confirm all text in EditText will be selected. Context menu will be shown.
Scroll RecyclerView till the end of list.
Scroll RecyclerView till the start of list.
Long press on 1st EditText. All text in EditText will NOT be selected. Context menu will NOT be shown.
Expected behavior
After the view has been recycled, we expect step 4 will still behave exactly same as step 1.
Source code
https://github.com/yccheok/edittext_bug_in_android9
Issue tracker
https://issuetracker.google.com/issues/125425940
I was wondering, has anyone encounter same problem as I do? Do you have any good workaround on this? Note, I need my RecyclerView item being recycle-able. Hence, using setIsRecyclable(false); is not an option for me.
In short, the problem happen because EditText doesn't perform Editor#prepareCursorControllers during attachToWindow.
Since Editor#prepareCursorControllers is not a public accessible function, we can invoke it indirectly using setCursorVisible.
edtImgDesc.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
#Override
public void onViewAttachedToWindow(View v) {
edtImgDesc.setCursorVisible(false);
edtImgDesc.setCursorVisible(true);
}
#Override
public void onViewDetachedFromWindow(View v) {
}
});
Reference source: https://www.jianshu.com/p/e334134a4ef7 (The blog is in Chinese)

How to change number of column of a gridview at same visible item. Android

I am writing an android app where I am using a grid view to display some items. I want to give users a configurable view where they can change the no of columns on the activity by clicking floating action button. I am changing the column no using
gridView.setNumColumns(selectedColumnNo);
This is working fine But the problem is if a user changes no of column after some scrolling the First Visible Position is set to the first item of the array list, so the user has to scroll the view again. Can someone please tell me where I am doing wrong. Or Is this the proper way to do this or should I use a different approach.
A code snippets will be helpful
Thanks.
Update::
currently I am using the bellow snippets
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int firstPosition = gv.getFirstVisiblePosition();
if(gv.getNumColumns()==2)
{
gv.setNumColumns(1);
gv.setSelection(firstPosition);
}
else {
gv.setNumColumns(2);
gv.setSelection(firstPosition);
}
}
});
Now the problem is on every 4th switch grid view is showing the first element of the arraylist
Right before you call setNumColumns(), save the GridView's first visible position:
int firstPosition = gridView.getFirstVisiblePosition();
Then, after you change the number of columns, pass that integer to setSelection():
gridView.setSelection(firstPosition);
"Selection", counter-intuitively, is not the same thing as "activation". It will ensure that the view is on-screen, but not visibly affect it in any other way.

Android - AutoCompleteTextView not showing after setText is called

I'm having a weird issue with AutoCompleteTextView.
I have a AutoCompleteTextView that shows suggestions of cities when typing in it.
The list of cities is retrieved from a remote server via JSON. When I use the soft keyboard or the Mic Button on the soft keyboard, the suggestions work fine. AutoCompleteTextView does show the suggested cities.
But, I have a problem when I try to set the text using myAutoCompleteTextView.setText("Chi") , the auto complete does not show..
I have also tried myAutoCompleteTextView.append("Chi") but still no luck..
The adapter is there, its just that the suggestions don't show.
Any tips?
Thanks.
Yes you are right there is a bug in AutocompleteTextview to show default suggestion using setText(""); method.
But you can achieve this by adding some more lines of code as below.
autoText.postDelayed(new Runnable() {
#Override
public void run() {
autoText.showDropDown();
}
},500);
autoText.setText("chi");
autoText.setSelection(autoText.getText().length());
It is due to filtering,
No Need to any extra code for manage it, I found it in very easy and working way.
Google Dev. Reference link
autoText.setText("Default Value here",false)
autoText.setSelection(autoText.text.count()) // kotlin
as per documentation second parameter you can pass for filtering.
boolean: If false, no filtering will be performed as a result of this call.
Biraj Zalavadia's answer work, but you must write to "settext" in Runnable.
Like this:
mACTextViewEmail.postDelayed(new Runnable() {
#Override
public void run() {
mACTextViewEmail.showDropDown();
mACTextViewEmail.setText("My text is here");
mACTextViewEmail.setSelection(mACTextViewEmail.getText().length());
}
},500);
I searched for it and just found this solution that worked so well
Look at this issue
fun AutoCompleteTextView.showDropdown(adapter: ArrayAdapter<String>?) {
if(!TextUtils.isEmpty(this.text.toString())){
adapter?.filter?.filter(null)
}
}
In kotlin language, you can use this extension function.

Hide/show AChartEngine series using checkboxes

Basically I have a chart with four different series, that by default shows all four of them at the same time. I want to allow the user to hide and/or show them again as he pleases by marking checkboxes.
I found a similar question here and followed both the advices given, but it didn't work. Here is what one of my checkboxes looks like now:
checkBox1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(((CheckBox)v).isChecked()){
multiRenderer.addSeriesRenderer(distanceRenderer);
// distanceRenderer.setColor(Color.parseColor("#990000")); //(making transparent method)
}
else{
//distanceRenderer.setColor(Color.TRANSPARENT); //(making transparent method)
multiRenderer.removeSeriesRenderer(distanceRenderer);
}
}
});
The idea is to when I uncheck the checkbox, I will hide that one renderer, when I check it again, it will be added again.
In the suggestions of the thread I linked to, they said to remove all renderers and then reenter the ones that aren't supposed to be hidden. I also tried this, but to no avail. The third option was simply to change the color to transparent whenever I wanted it to disappear, this also didn't work.
I debugged it, and it is getting in the conditions correctly, but nothing happens. Is there something I need to call that I've missed?
Thanks!
Edit: I don't know if this somehow helps, but I also tried to call the XYMultipleSeriesDataset's function repaint(). This caused an IllegalStateException, soooo.... Still no good...
Try this for hide purpose :
r.setColor(Color.TRANSPARENT);
mChartView.repaint();
where r is XYSeriesRenderer .

Displaying an array of objects, one at a time through a single dialog... instead of several dialogs

In my application I have a list of questions stored in an ArrayList, and I want to display a dialog that shows one question, and then continues to the next one after the question is answered. The way that I'm currently doing it (iterating through a loop) hasn't been working because it just layers all of the dialogs on top of one another all at once which causes a host of other issues. What I'm looking for is a way to still iterate through the questions, but just change the layout of the dialog each time until it has finished each question in the list. Can anyone give me a good pointer for how to get this going?
You can make a function that takes title and message as parameters and shows a dialog.
showDialog(String title, String message){ // Show dialog code here}
Within that dialog's answer button's listener call another function (showQuestion(currentQuestion)) that iterates the arrayList till it is over
int currentQuestion=0;
ArrayList<QuestionObject> questionList;
showQuestion(int i){
if(i<questionList.size()){
showDialog(questionList.get(i).getTitle,questionList.get(i).getMessage);
currentQuestion++;
}else{
//quiz is over
}
}
I assume you mean that you just want to change 1 single layout(created within XML i.e main.xml). In order to do this, make sure that the class your working on is pointing to that layout. From there (assuming your using an Event listener for when the user submits an answer) you can change do as you want by the following:
TextView txt = (TextView) findViewById(R.id.textView); // references the txt XML element
and in your Event listener, if the answer is correct then change(Have i be a global variable thats initially set to 0).
if(i<arrayList.size()){
txt.setText(arrayList.get(++i));
}else{
txt.setText("You Finished");
}
From there, in the else statement, you can change arrayLists and reset i to 0;
If you are trying to use the positive, neutral, and negative buttons; then you may have problems with multiple dialogs. Try defining a customized layout with your own TextViews, ListViews, and Buttons. You can implement listeners and everything else like a regular layout. Then just pass your customized layout to the dialog through AlertDialog.Builder.setView().
PS If you include code examples of what you are currently doing we can provided answers that are less vague.

Categories

Resources