Switching back and forth between 2 layouts - android

I'm trying to make an application thats has blinking effect,
e.g. switching back and forth between 2 layouts, one is red and the other is blue for example.
(any layout has diffrent image in it)
When trying to switch fast between 2 activities or 2 fragments the application is crashing.
How can I programmatically change activity layout in a better way?

My personal reservations against blinking aside, you could change just the background color of your root layout with a timer.

You should just change the background color or the layout that is being displayed by the Activity.
Think about efficiency:
If you change layouts android will have to inflate the XML and its widgets and you will have to get handles to all these by querying and layout (ie findViewById). You can think of the first problem of this as refreshing a web page to change the color of an element instead of just rendering dynamically. You can think of the second part of this as not caching DOM handles and having to requery-ing the DOM every time you want to provide and action in JavaScript. Both are bad practice.
Or, you could just change the background of the current layout every X seconds, minutes or whatever you are trying to do. There are many ways to do this - the AlarmManager, or start a Thread with a timeout - or better yea, start a new Thread that will post a runnable back to the main thread to change the background color - then sleep the auxiliary thread for X seconds and repeat the loop.
The second idea is not only good practice - but you are using the SDK framework correctly.
Good Luck!

Related

Unity making a timer

So im making a android app in Unity an im trying to make a specific timer so the timer starts as soon as you go the the scene and also I want the numbers to scroll down then stop for a second then scroll again it would look something like this
5
6
00:14:57
8
9
and it would scroll down to the next number ten stop then the next number then stop
how to make a timer in unity with this behavior and look ?
There are plenity of ways to make a timer, I'm going to assume that that's not the trouble, what you are asking for is an animation that looks nice and works with the data of your timer.
You can have a FSM in the animator to make an animation between three text components so each second them will scroll up, once the upper text is outside the view, ui mask, you put them at the end and then change his text value to the next second and so on in a loop.
Another way to do this is the same I just wrote but using scripting and coroutines instead of the animator, I prefer this approaching since I have full control of what are appearing to the user and what are occurring behind in the code.

Overriding Android Chronometer class

I want to override the android chronometer class. I want it to inflate a custom XML layout and to also have a quicker update interval.
I've found one that modifies the update interval from the default 1s to 0.1s on github.
But I want to do more and have imageViews or Buttons display the time instead of the default textView it uses. I haven't really done this before except for a recycle view, so some explanation would be nice.
My end goal is to have an efficient stopwatch with custom digits and display, like the one found on HTC one M7 in the default clock app or any Samsung phone. I also need it to run in its own Fragment and be able to handle orientation changes and the activities onPause() or onStop() without losing any time.
Would it be better to use Intent Service and a Results Reciever? if so how would i accomplish this?
Chronometer is good for certain extent, but instead of customizing it you can create your own custom layout and display the time based on your requirements using a handler which would be the best solution for your problem.Find the link below for example
http://www.shawnbe.com/index.php/tutorial/tutorial-3-a-simple-stopwatch-lets-add-the-code/

How to reduce time between activities in android studio

I have an app with 3 activities. I noticed there is a slight delay when going from 1 activity to the next (~0.3sec). My Java code is pretty simple, my XML are a little less simple. My XMLs on average contain: 4 TextView, 1 list view and 4 buttons. I removed the animation effect in the manifests xml. But still i notice this 0.3 sec delay between activities. Is there a way to reduce that delay between activities Or my xmls are simply too heavy?
You can try to use the Fragments instead of Activities. You can dynamically show them, without delay, because they will be always loaded in a memory. The delay between Activities start is because the system must at first check intent, for other application that can make this action, and after that create new Activity.
Written a code in Run time for 3 activities and try it.

How to show different layouts (Activity, Fragment)?

I've a single activity that changes its appearance (color and text) depending on an alarm. This activity 'dissapears' when the user presses a button and then I perform some background work. Until this point it is working fine but the problem appears when two alarms are scheduled for the same time. Then, only the first layout is shown. What I expect to happen is that both colored screens are shown. There are 10 possible colors and I don't wanna implement a different activity nor fragment for each one.
Can anyone suggest a better approach?
Thanks in advance.
You can set first layout as ContentView,then wait some times and then set another layout as ContentView of your Activity.You can use AsyncTask to use UI Thread after waiting some times in another thread.

Newbie in Android development. Implement several different processes in an activity

I'm begining to learn android development, and I'm trying to make an app just to learn the language and philosophy.
This app, has to show an image in the middle of the screen, a button below, and a chronometer in the right side. When the app starts, the chronometer has to begin a countdown. When the user press the button, a blur effect has to be applied to the image, and the seconds left to finish the countdown increase by 10.
I almost know how to program the blur efect to the image, the button press, and the countdown and increase by 10 whenever the button is pressed. But I'm not sure about putting all together.
As far as I know, it should be done by designing an activity, and putting inside the activity the image, the button, and another image or a set of changing images or text for the countdown clock. But as I advance in my studied, today I have read that in order to manage different actions in an activity it is neccesary to do it by using fragments. And I have found much complex programming fragments than activities.
So the question is: can I make what I'm trying to do by a simple activity and defining classes and methods for the image effect and the countdown clock or have I to make it with fragments?
Thank you very much.
today I have read that in order to manage different actions in an activity it is neccesary to do it by using fragments
To be blunt, either you either misunderstood what you read, or you are reading the wrong material.
can I make what I'm trying to do by a simple activity and defining classes and methods for the image effect and the countdown clock
Yes.
have I to make it with fragments?
No. It is possible that the whole UI might be a fragment, particularly if it might be shown alongside something else in some cases (e.g., a tablet) and not in others (e.g., a phone). And there is nothing stopping you from making that UI using several fragments, though that would be rather unusual.
As others have already conveyed, no need to go with fragments.. Activity wud suffice.. As far as putting it together is considered, I guess you need to learn more about layouts.. Layouts are the files which basically help you put things on UI as you want it to look like.. There are plenty of material available online for understanding layouts.. Happy learning.. :)

Categories

Resources