How to hide ActionBar and NavigationBar after a certain delay time? - android

I would hide navigation bar and action bar of my app after some second that display don't get touched, and expand current view to fullscreen.
Then if user touch the screen (or better if he swipe down), make visible both again. How to?

You can use a Handler to delay certain actions.
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
// DO DELAYED STUFF
getActionBar().hide();
}
}, delaytime); // e.g. 3000 milliseconds
The actions you take inside the run() method will be executed after
the delay-time you set.

If you are using Eclipse, just create a new project and select "Fullscreen Activity": this is a good example on how to do what you want.

Related

Android Progress Bar style programmatically

I'm trying to instantiate a Progress Bar from the main Activity and add it to the view dynamically. When I do this, I get a bar:
ProgressBar bar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
bar.setLayoutParams(params);
bar.setId(mInt);
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
layout.addView(bar);
I started a new project that only includes the above code, and the progress bar displays correctly.
If I add the following lines to the above code, the screen is blank for a second and then shows a half full bar. I would expect to see an empty bar for a second and then it goes to 50%.
try {
Thread.sleep(2000);
} catch ...{
}
bar.setProgress(50);
If I add some code like this, however, the bar and changes display correctly.
Button b = new Button(this);
b.setText("change bar");
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
changeProg();
}
});
public void changeProg() {
switch (state) {
case 0: bar.setProgress(100); state = 1; break;
case 1: bar.setProgress(127); state = 2; break;
case 2: bar.setProgress(33); state = 0; break;
}
}
If I try to automate the process with a loop to call changeProg() every so often, the screen just stays blank - no widgets at all display, whether they are described by the XML or pro grammatically makes no difference.
I'm having trouble understanding why this behavior exists.
It seems that you call Thread.sleep(2000) from UI thread, which causes the problem, even create ANR. Try to create a new thread (separate thread) to change the progress value.
See the example below:
https://stackoverflow.com/a/17758416/3922207
ProgressBar pdialog = new ProgressBar(context,null, android.R.attr.yourcustomstyle);
and check your apptheme in style
SetProgress tip:
Set the current progress to the specified value. Does not do anything if the progress bar is in indeterminate mode.
So you have to call:
bar.setIndeterminate(false);
Have you tried looking at this site?
http://www.mkyong.com/android/android-progress-bar-example/
The correct way is use 1 thread to run your time consuming task and another thread to update the progress bar status
Hope this helps!

How to display 3 2 1 text in the center of the screen in transparency mode

First of all, I am new to android development and searched everywhere but didn't get any solution to my problem. So please don't rate this post negative.
I am making a small game application in which I want to display 3 2 1 at the center of the screen in bixtext, and it should come in transparency mode such that the activity below the
3 2 1 text should be visible and when it reaches 1 then the activity should start like in any game.
Add a TextView to your xml layout (textView123 here) and that wouldn't be so hard to go:
final Handler handler = new Handler();
final TextView textView = (TextView) findViewById(R.id.textView123);
final java.util.concurrent.atomic.AtomicInteger n = new AtomicInteger(3);
final Runnable counter = new Runnable() {
#Override
public void run() {
textView.setText(Integer.toString(n.get()));
if(n.getAndDecrement() >= 1 )
handler.postDelayed(this, 1000);
else {
textView.setVisibility(View.GONE);
// start the game
}
}
};
handler.postDelayed(counter, 1000);
As for making the Countdown activity transparent - getWindow().setBackgroundDrawable(null).
What you can do is when you launch the timer to countdown, when it finishes the 1 countdown, create a transition to the next activity with a fade-in transition.
Though i don't understand why you don't use a frameLayout and the countdown timer as just a view on the same activity. That would certainly be a better approach.

How to animate home button in action bar?

I'm using ActionBarSherlock and I would like to animate the home button to tell the user to click on it. I want to use three different images to show the animation.
How can I do this?
Get the action bar in you onCreate:
ActionBar actionBar = getSupportActionBar();
Create and load an animation drawable which you defined in XML:
// homeDrawable is a field on your activity
homeDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.my_thing);
Set the drawable as the icon on the action bar:
actionBar.setIcon(homeDrawable);
Post a Runnable to start the animation when the main thread is clear:
getWindow().getDecorView().post(new Runnable() {
#Override public void run() {
homeDrawable.start();
}
});
Don't forget to stop the animation at some point!

How can we display progress icon in button

I need to display progress icon in button so that user can interact other GUI elements while background task is processing.
I have searched in Android developer site and found that we can use animated drawables but don't know how to use them. Please advise on the same.
The very simple way to do this without using the animated drawable is to use "PregressBar" component in the design layout xml. When u need to show it, just set it's visibility property to visible and when you need to hide it, u can set it's visibility property to GONE. But remember this is UI task so when u need to do this with non-UI thread, u need to use Handler to set the status of "ProgressBar" component at runtime.
Below id the component in the layout file.
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ProgressBar>
Below is the code written in java file
ProgressBar prg;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
prg=(ProgressBar)findViewById(R.id.ProgressBar1);
prg.setVisibility(ProgressBar.GONE);
}
public void start_background_process()
{
// starting the process
prg.setVisibility(ProgressBar.VISIBLE);
new Thread(new Runnable()
{ public void run()
{
// Do your background stuff here which takes indefinite time
mHandlerUpdateProgress.post(mUpdateUpdateProgress);
}
} ).start();
}
final Handler mHandlerUpdateProgress= new Handler();
final Runnable mUpdateUpdateProgress = new Runnable() {
public void run() {
// ending the process
prg.setVisibility(ProgressBar.GONE);
}
};
If the default progress indicator is good enough for you (i.e. the spinning wheel), then you can just use ProgressBar. To change it from a normal progress bar to a spinning wheel, use progressBar.setIndeterminate(true).

how can move scroll bar automatically?

I am using grid view for display images, I want to scroll move automatically, whenever I add another images in gridview, is it possible?
I think you have to read a little bit more :
https://developer.android.com/reference/android/widget/AdapterView.html#setSelection(int)
ListView.setSelection(position);
https://developer.android.com/reference/android/widget/AbsListView.html#smoothScrollToPosition(int)
ListView.smoothScrollToPosition(position).
Either call setSelection() or use smoothScrollToPosition().
I got result using GridView.setSelection(GridView.getCount()),so scroll bar automatically move to last element of Grid View
Use TimerTask to do that since Adapter.notifyDataSetChanged() takes delay to update the grid or list.
Code is below:
new Timer().schedule(new TimerTask()
{
#Override
public void run() {
nameHandler.sendEmptyMessage(0);
}
},500);
//// And in handler::
Handler nameHandler = new Handler()
{
public void handleMessage(Message msg)
{
super.handleMessage(msg);
gridView.setSelection(selectorIndex);
// OR
gridView.smoothScrollToPosition(selectorIndex);
gridView.invalidate();
}
} ;

Categories

Resources