I am trying to show logs on a overlay over all activities. I found this https://stackoverflow.com/a/19037235/3370924 answer which implements the overlay in the application class. I have added a TextView to the LinearLayout to show my logs.
At first it works but then I get the error android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
when I have opened a new Activity and I am trying to add a new string to the TextView.
Usually i use runOnUiThread() but this method is not available in Application class. Can anyone please help me?
If we pretend that your TextView is called tv, do:
tv.post(new Runnable() {
#Override
public void run() {
// put your update-the-TextView code here
}
});
Related
I have a button called panel_close in my java code. In the setOnclickListener method I update the visibility of some other views. However, it seems sometimes the code does not update the visibility of the panels properly. Here is the code
panel_close.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
View temp=findViewById(R.id.show_panel);
temp.startAnimation(window_close);
temp.setVisibility(View.GONE);
compass.setVisibility(View.VISIBLE); // compass is defined as a field. Sometimes the code does not make this view visible
stats.setVisibility(View.VISIBLE); // stats is defined as a field. Sometimes the code does not make this view visible
control.setVisibility(View.VISIBLE); // control is defined as a field. Sometimes the code does not make this view visible
loadAllList(); // this is an AsyncTask
loadAllpins(); // this is an AsyncTask
}
});
Any comment will be appreciated.
You should look at running these changes on the UI thread to make sure that the modification are visible to the UI:
Android - using runOnUiThread to do UI changes from a thread
You should give a detailed info about your variables first and this could be happening because you are making the layout or something like that invisible or gone, which the children are inside of. This will cause them to remain invisible.
How do you implement a custom view in Android so that it properly supports, or works with, the messaging queue ?
I'm trying to emulate the behavior of the built-in views so that I can properly / normally update a custom view with data within onCreate.
Currently, my custom view has ad-hoc set/update functions to put data in them. The problem with this is that my view's children views are not initialized until the first time onMeasure is called, which is after onCreate exits (which I think is how the built-ins do it).
Therefore, I want to know what the general strategy is (ie, what methods to override) to update a custom view from onCreate in such a way that the updates go into the message queue and reach the view after they are properly instantiated (just like the built-ins) ?
Thanks.
Look at View.post():
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView hello = ((TextView) findViewById(R.id.hello));
hello.post(new Runnable() {
#Override
public void run() {
hello.setText("Hello World!");
}
});
}
I am trying to create a simple game based on the 2D game framework as described in the 2011 Apress book, "Beginning Android Games" by Mario Zechner. Maybe that helps, maybe it doesn't, but either way:
I only have one activity for the whole game. Everything is rendered onto a bitmap, which is then sent to a SurfaceView, and what is sent to the SurfaceView will change based on what screen the game is on.
I want to implement a simple highscore system that lets users enter their name into an EditView on the main menu screen. I've been able to combine the SurfaceView and the EditView with a RelativeLayout, but the problem is getting rid of the EditView once the user moves past the main menu screen.
There is much more to this class, but here's a snipet of what I feel is important:
public abstract class AndroidGame extends Activity implements Game{
public EditText et;
public RelativeLayout rl;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
et = new EditText(this);
renderView = new AndroidFastRenderView(this, frameBuffer);
rl = new RelativeLayout(this);
rl.addView(renderView);
rl.addView(et);
setContentView(rl);
}
}
The above is the only activity for the game. The AndroidFastRenderView class extends SurfaceView. The problem I'm having is that while I need the SurfaceView to persist through the whole game, I need EditText to only persist for a short while, then disappear based on touch events.
I try to call RelativeLayout.removeView(et) from my class MainMenuScreen, which is responsible for constructing the bitmap sent to the SurfaceView as well as listening for touch events. But I get an uncaught exception that says I can't edit views that are defined in one thread from another.
I'm sorry for the novel, but I've been trying to solve this myself for hours with no luck. I feel like this is a very simple thing I'm just not getting. Any help is much appreciated!
Hm... I think only UI Thread can Add and delete views. Try to call the Thread like this:
runOnUiThread(new Runnable(){
public void run() {
/* STUFF To do in Thread */
//Delete the view...
rl.removeView(et);
}});
Hope it helps :)
I have a bit of a problem I cannot solve, since it might a bug or something like that. I would like to make a chart with androidplot, and it works really good, but of course it needs some time to draw it (using hundreds of data), so I use a progress dialog to indicate the loading. But what happens is really weird.
I can define the appearance of the activity when it's loading and when it's loaded. When its loading I define a textview in the background setting its text to "loading" and if it is loaded, that textview contains lots of datas, text etc.
onCreate
{
Thread thread = new Thread(new Runnable() {
public void run() {
-------what needs to be appeared after its loaded ----
Textview -> 12,3245,456,78,789
}
----what is on the screen while the progressbar is on---
TextView -> loading..
}
But most of the time after the progress dialog disappears, nothing happens, the textview still says "loading" and rarely it loads the datas and makes the data appear and changes the textview. What I noticed that the more data I would like to appear the rarelier it stays at the loading phase. Of course everytime the loading progessbar appeers then disappears.
Any Suggestion? It is really weird because I use it in a tablayout and other tabs never do this, always make the data appear.
Thanks in advance!
UP: Okay, its always the first tab, whatever it contains, so the first tab is somehow wrong...
The Andoid UI toolkit is not thread-safe. So, you must not manipulate your UI
from a worker thread—you must do all manipulation to your user interface from
the UI thread. Thus, there are simply two rules to Android's single thread model:
1. Do not block the UI thread
2. Do not access the Android UI toolkit from outside the UI thread
read this for more information on how to access UI elements from outside.
edit::
use AsyncTask ::
onCreate
{
new myLoading().execute();
}
class myLoading extends AsyncTask<Void, Void, Void>
{
protected Void doInBackground(Void ... ) {
.......... do all the loading here .........
}
protected void onPostExecute(Void ) {
Textview -> 12,3245,456,78,789
}
}
I figured out some workaround. I dont have any clue about the solution, my only guess is the loading screen somehow overtake the arrival of all the data and so the chart cannot be drawn. or whatever...
I put a thread.sleep(1000) and now it works.
I have a view (custom drawn) added with getWindowManager().addView() and later I'm modifiying the LayoutParameters of it (changing x & width) and call getWindowManager().updateViewLayout(). This works but I am getting two screen refreshes, first one only moves the whole thing according to the new x and later one scales it according to the new width. Any ideas about why is this happening even though I only call updateViewLayout just one time with the new layout parameters?
FYI: onDraw method of the custom drawn view mentioned here is also called only one time by the system during this process.
Try:
runOnUiThread(new Runnable() {
public void run() {
view.updateViewLayout();
}
});
http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)
If it doesn't work, check this:
How to move a view in Android?
Are you doing this?
try to do :
view.post(new Runnable() {
public void run() {
view.updateViewLayout();
}
});
updateViewLayout is an a method that can be overriden by your custom ViewGroup and in this overrided method you can implement all what your want to change.
Maybe you do something wrong in it?
Or also maybe you have to implement this code in UiThread like in other questions. - In this case when you change your parameters asynchronously with first call of drawing function by system you method maybe can change only one parameter and on second call the second parameter will be also changed.