Android how to update textview on UI - java

I have a textview in an activity. I am changing its visibility.
if(CheckBookmark(Constants.versusHeading.get(curVCount))) {
bookmark.setVisibility(View.VISIBLE);
bookmark.setText("Bookmarked");
}
else {
bookmark.setVisibility(View.GONE);
}
but UI is not updated. I have tried bookmark.invalidate() and bookmark.postinvalidate() but didn't work. If I change textview visibility how do I refresh UI? Activity is still alive and running.
Okay I think I need to add more details. My bad should have mentioned in the first place....Initially when activity is displayed bookmark is visible but have no text.
Then user creates a bookmark. Checkbookmark returns true if bookmark is successfully created. In that case I add bookmarked as text. I am setting it to visible as it wasn't working. If I quit activity and come back then bookmark text is displayed.

In general it should work the way you have it. The UI will update.
You need to make sure your if else is returning correctly.
Try setting the textview to VISIBILE intially to see if it appears and then try to toggle the TextView's visibility

As Pramod says, try
setVisibility(View.INVISIBLE)
instead of
setVisibility(View.GONE)

Related

Activity not paused quickly enough

In my AppCompatActivity, I am showing an AlertDialog at a specific event.
Once the AlertDialog is shown, the user should not be able to click on anything in my AppCompatActivity anymore.
I tried to disable the activity in onPause(): getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
And to check whether it is not paused: getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)
But it seems that onPause() is not called quickly enough for that.
I could set a static boolean to true whenever I pause the AppCompatActivity, set it on false in onResume() and check it in every single onClickListener/onTouchListener etc., but is there a better way to do it?
Every help is appreciated!
Edit:
Thanks for your comments! I found out, that when I am clicking on the screen while the UI thread is started (for opening the AlertDialog), the click is put in a queue. That's why the click event is executed while the AlertDialog is running. Do I need to save the AlertDialog and check if it is running or is there a better way?
I believe that you should let us know more about your code. However, I think that I can guess what you're saying. There is a setEnabled(false) method for every view component in android, and if you call it on a view component java object, it'll be disabled and no longer clickable. So you can simply disable your components whenever you want to show the AlertDialog and then enable them again by calling setEnabled(true) if you wish. I'm pretty sure pausing the activity is not what you should do. Because doing it manually, is not best practice anyway, at all.
I ended up saving an instance and writing a method to check if the Activity should not be active:
private void canRun() {
return (dialog == null || !dialog.isShowing()) && getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED);
}

OnCheckedChangeListener or OnClickListener with if statement for CheckBox's? What is the difference in functionality?

Hello I'm creating an android application that uses checkbox's, i'm wondering if it is better to use an OnCheckedChangeListener to do something when the state of the checkbox is changed or if it would be better to use an OnClickListener with an if statement inside it that is executed everytime the checkbox is checked or unchecked? Thanks
With OnCheckedChangeListener you receive an event whenever the checked status changes, even when done in code by using .setChecked().
Depending on what you are doing this can lead to unexpected behavior (for example when you have a checkbox in a listview, the view is recycled and the checkbox state is modified programmatically, it looks exactly the same way as if the user had clicked it).
Therefore, when you are writing code that is supposed to react to a user who clicked the checkbox you should use OnClickListener.

ScrollView contents update

I have a programmatically generated ScrollView with a TextView inside of it. I wish to update the text in the TextView from time to time and some have implemented a handler function to update the TextView GUI element. This seems to call/work correctly.
However, currently the only way I have found to actually get the TextView to show the appended information is to call:
consoleText.append("New text to add to TextView");
// then:
myScrollView.removeView(myTextView);
myScrollView.addView(myTextView);
This is not particularity optimal and was wondering how else I can refresh the contents of the ScrollView to show my newly added information...
Also:
invalidate();
postvalidate();
Do not seem to do anything - the TextView object has the new text in it (looking in debug) it's just not drawing it to the screen unless I call the add/remove function.
Thanks for any information/help you can give
FR
You should call invalidate() and requestLayout() on the TextView, and perhaps the ScrollView.

Value in edittext don't show until it get focus. Android

I'm building an app that get its layout based on external data. The layout is split into different blocks depending on the indata. These blocks are displayed with the help of a viewflipper. In each block there is, for the time being, one or more "textview" and "edittext". On the first page of the flipper all data is showing as it should in the textviews and edittexts. But on the other pages in the viewflipper the value in the edittexts is not showing until the edittext get focused. I have no idea why. So to be clear. The values for all edittexts is actually there, but doesn't show until the edittext get focused. The problem is the same on all devices I have runned the app on (emulator, HTC Desire, HTC Wildfire). Does anyone know how to fix this problem?
Here is the class that generate the layout:
public class ModuleLayout extends LinearLayout {
public ModuleLayout(Context context, ArrayList<BaseModuleItem> itemList) {
super(context);
TableLayout tempTable = new TableLayout(context);
for (int i = 0; i < itemList.size(); i++)
{
TableRow tempRow = new TableRow(context);
TextView tempTextView = new TextView(context);
tempTextView.setText(itemList.get(i).getName());
EditText tempEditText = new EditText(context);
tempEditText.setText(itemList.get(i).getItemValue());
tempRow.addView(tempTextView);
tempRow.addView(tempEditText);
tempTable.addView(tempRow);
}
tempTable.setColumnStretchable(1, true);
this.addView(tempTable);
}
}
Here is a picture of the problem in action.
The left picture displays all its values fine. The right picture is on the second position in the viewflipper and does not display the values in the edittexts, with the exception for the first that have focus. I should also mention that after an edittext has gotten focus it continues to display the value even if I fling to other views in the viewflipper and then back.
That Layout from how it appears here is not on the UI thread meaning no matter what you change you won't see anything until the UI thread (also known as the Main Thread) updates. The UI/Main thread is what all your activities run through so it's easy to think that stuff just updates automatically (since most of the previous work you have done is also somewhere within the Activity, I am assuming). The onFocusChanged event (clicking in the edittext) will update the UI thread by calling invalidate() on itself. You can force it to update by calling EditText.invalidate() if you need to, but it's usually best to have it update naturally after creation. Without seeing the rest of your code this is the best I can do for you, I hope this helps :)
Having not seeing your full code I'm making a bit of a guess-suggestion but have you tried textView.invalidate() (if calling from GUI thread) or textView.postInvalidate() (if calling from another thread)?

Android ListView - onListItemClick does not work properly

I created a ListView in Android, and a corresponding ListActivity. Each individual item in the ListView has just one TextView (I plan to add an image and a CheckBox later).The ListActivity overrides the onListItemClick to perform certain tasks on click of any item on the list.
Heres whats happening -
When I first tried clicking on any item, nothing happened.
I then tried setting the properties "Focusable" and "Focusable in Touch Mode" to false for the TextView, as mentioned here, here and here. The List items started recognizing clicks, but only when I clicked somewhere away from the TextView. Whenever I tried clicking on the TextView or anywhere near it, it did not work.
I also tried changing various attributes like Clickable, but nothing has worked so far.
Any idea what I could be doing wrong ?
Thanks
After playing around with virtually every attribute in my TextView, I finally found the reason why it was not working. It was because of the attribute android:inputType="text" in my TextView. I'm not sure why I added that piece of code (I probably copied the TextView from one of my other applications), but removing it solves my problem.
Class which will listen clicks on ListView should implement interface AdapterView.OnItemClickListener

Categories