Set different views in one Activity - android

I've created a button, if I click it the following will start:
public void onClick(View v) {
// TODO Auto-generated method stub
Crunch();
tts.speak("Kurze Pause", TextToSpeech.QUEUE_ADD, null);
Unterarm();
SitUp();
}
By calling Crunch(), I want to setContentView(R.layout.1), when it is finished I want to change the contentView to another layout, by calling Unterarm(). Then to another one by calling SitUp().
How can I handle it in the best way? I've created all of the layouts and methods, but it isn't working.

It's not working because it will execute all methods without any delay between them so you will get the feeling that only the last one is working and if the last one is restoring the initial state, you'll feel like nothing is happening.
I suggest you wrap the method inside an asyncTask and set some delay between the method calls. It should be straight forward enough.

You have to use animations, this is from the offical documentation
http://developer.android.com/training/animation/crossfade.html
hope it helps :)

Related

Launching custom activity onBackpressed launches activity twice

I have this really weird problem I can't quite wrap my head around. I have been pondering on this for hours and I have absolutely no clue why my code is behaving like this.
I have a Viewpager from which images can be deleted, in order to give the illusion of real time updating I open the same activity every time a photo is deleted so the viewpager's content will change immediately and accordingly.
For this reason I have overridden onBackPressed() to make sure that when this method is initiated the user returns to the real past activity and not the activity with the original images. I've read posts about this and tried to do things such as remove the super method and add the finish() method but nothing seems to work.
#Override
public void onBackPressed() {
//creates two instances of AccountApartementActivity
overridePendingTransition(0, 0);
add_button_view.setVisibility(View.INVISIBLE);
delete_button_view.setVisibility(View.INVISIBLE);
Intent i = new Intent(ApartementEditActivity.this, AccountAndApartementActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
i.putExtra("fromActivity", "2");
startActivity(i);
finish();
overridePendingTransition(0, 0);
}
In neither of the two activities' lifecycle methods I have overridden have I written code that tells the application to create the same activity twice, or I'm somehow overlooking it.
Weirdest thing is that this worked fine just a day ago but today I changed the data type of one of my variables inside my PagerAdapter method and ever since the onBackPressed() method has been broken.
Please let me know if I am missing something obvious, it happens a lot.
I think it is not the best practice to manipulate the activities stack this way. You could eventually run out of memory.
I don’t know how your photos are organized, maybe with a container layout like a gridview or a recyclerview?
Then you should update views with the adapter and the notifyDataSetChanged method.
Good luck !

get notified when screen becomes visible in libgdx

The answer I expected at first was the show method. Unfortunately if show has a lot of work to do, there is a delay between method call and screen appearance. So how does one get notified when screen appears?
Using show() should be fine, the screen will be shown straight after so just put stuff you want done at the end where the delay should be negligible.
If the work that show() does is in a super class, then just override it, call super.show() and then do your stuff after, like so...
#Override
public void show () {
super.show();
doAnyOtherSlowStuffThatMightNeedDoing();
doTimeCriticalStuff();
}
I should probably add that a better solution would be to not do slow stuff in the show() method to start with, but that's a whole other debate.

How to uncall a method in android?

I am not sure if this is possible or not but what I am after is that, I have a method which I called in my onCreate and the method runs when the app is starts, basically this method does bunch of things like put numbers on Textview, change colour of text etc (this method does around 12 things atm). I have a button, what I want to do is, when the button is pressed I want to stop using the method that was called on start. For example;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpMethod();
removeMethod();
}
public void removeMethod(){
Code for button and listener
.......
.......
.......
.......
{
you can do it with putting it in a thread, you can pause/stop as you wish.
here is simple tutorial on thread in java, you can put your in a thread which you want to do in setUpMethod().
http://www.tutorialspoint.com/java/java_multithreading.htm
and use Thread control methods to control the execution of code.
http://www.tutorialspoint.com/java/java_thread_control.htm
You just need to read through setUpMethod(), work out what the reverse of each action of it is, and put all those undo steps in removeMethod(). Post the code of setUpMethod() if you need more help.
If you do those things on main thread, you can't stop, because everything you do is on one threat. 12 things should finish and after this removeMethod() will be called.
Simply revert all those 12 changes to initial state in removeMethod method -set text, textColor and other things to initial values
To "stop the method being called again when the button is pressed", create a boolean somewhere, and add the line
if (wasRunAlready) return;
to the start and
wasRunAlready = true;
to the end of setUpMethod(). It will then be impossible for the code inside to run twice.

How to call a method with View param

I have a source code of an app for android which get location from network or gps. There is the next code:
// Callback method for the "both providers" button.
public void useCoarseFineProviders(View v) {
mUseFine = false;
mUseBoth = true;
setup();
}
There is a button, and on the onClick event call to "useCoarseFineProviders", my question is that I want to delete this button and call this method from the onCreate method, but I don't know how to do this.
I need to learn so much things. Thanks for your help.
Your code isn't actually doing anything, or using the view parameter passed in. so if you want to move it to onCreate, just do it- take the body of the function, paste it into the bottom of onCreate, then delete this function and the code (probably in your xml) telling it to call this function in onClick.

Which method gets called the moment the activity is fully laid out and ready for user interaction?

I need a way to run some code at the exact moment in which the activity is fully loaded, laid out, drawn and ready for the user's touch controls. Which method/listener does that?
Commonsware is right, without explaining what your are trying to do and why, it's not possible to answer your question and I suspect, with detail, you are probably thinking about it the wrong way.
However, I do have some code where I needed to do some very funky layout stuff after everything had been measured.
I could have extended each of the view classes in the layout and overriden onMeasure() but that would have been a lot of work. So, I ended up doing this. Not great, but it works.
mainMenuLayout is the layout I needed to get funky with. The onGlobalLayout callback is called when the layout has completed drawing. Utils.setTitleText() is where the funkiness takes place and as I pass mainMenuLayout to it, it has access to the position and size of all of the child views.
mainMenuLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// only want to do this once
mainMenuLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
// set the menu title, the empty string check prevents sub-classes
// from blanking out the title - which they shouldn't but belt and braces!
if (!titleText.equals("")){
Utils.setTitleText(_context,mainMenuLayout,titleText);
}
}
});
I've found that if I post a Runnable to the message queue, it will run after the content for the activity has been drawn. For example, if I want the width and height of a View, I would do this:
view.post( new Runnable() {
#Override
public void run() {
int width = view.getWidth(); // will be non-zero
int height = view.getHeight(); // will be non-zero
}
} );
I've found success with this anytime after I call setContentView().
onRestoreInstanceState method is the one called to restore UI state which is called after onResume .I think you can use this onRestoreInstanceState method.. and put your code after restoring UI state from the savedInstanceState...
Try onPostResume() called after onResume() at this moment the Activity instance should be visible and all underlying Views are rendered. In many situations this is true when onResume() is called as well.
Maybe it little helps:
#Override
public void onAttachedToWindow(){}

Categories

Resources