Android: ayncTask tried to update the progress bar in orientation changed - android

I have two layouts for orientation: portrait layout and landscape layout. In the portrait layout, I used the progress bar and download the files. I used AsyncTask and updated the progress bar in the onPostExecute() method. When I change the landscape orientation, the system will replace with landscape layout. My problem is that when I change the landscape orientation during the downloading files, I got the NullPointerException for progress bar. Because AsyncTask tried to update the progress bar after downloading files. I have two questions for that.
I have no idea how to skip the updating this progress bar.
And when the orientation change back to portrait during downloading, I want to show the progress bar status.
EDIT
#Override
protected void onProgressUpdate(Integer... values) {
int count = values[0];
loadingBar.setProgress(count);
progressTxt.setText(count +"/" + listSize);
}
#Override
protected void onPostExecute(Integer result) {
loadingBar.setVisibility(View.INVISIBLE);
loadingBarLbl.setVisibility(View.INVISIBLE);
}
#Override
protected void onPreExecute() {
loadingBar.setVisibility(View.VISIBLE);
undefinedloadingBarLbl.setVisibility(View.VISIBLE);
}
Thanks.

Related

dynamically set size of indeterminate ProgressBar on ActionBar

I implemented Progress Bar on action bar during network call but displaying the progress Bar on Action Bar is too large. I want to set dynamic size of progressbar. I searched lot but not achieve my goal.so following is the code for ActionBar ProgressBar. Please anybody suggest me some answer and anybody having another way to do above task please tell me.
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); }
protected void onPreExecute() {
// Show IndeterminateProgressBar
setSupportProgressBarIndeterminateVisibility(true); }
protected void onPostExecute(Void result) {
setSupportProgressBarIndeterminateVisibility(false);}
Thanks In Advance
Set a Timer and use LayoutParams to change the size.
{ParentLayout}.LayoutParams params = new {ParentLayout}.LayoutParams(h,w);
progressBar.setLayoutParams(params);
And use the built-in java Timer to time it.

Make fragments full screen programmatically

I have a ViewPager and FragmentPagerAdapter set up in one activity with 3 fragments. All fragments are loaded simultaneously and are kept in memory using
mPager.setOffscreenPageLimit(2);
One of these fragments hosts a camera preview that I would like to make full screen, with no status bar or action bar. The other fragments require that the action bar be displayed.
How would I make only the camera preview fragment full screen while keeping the other two fragments normal? I have tried using themes, which means breaking the action bar.
Programmatically calling the following doesn't help, because it forces the whole activity to be full screen (and thus the other two fragments along with it):
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
I also tried implementing ViewPager.OnPageChangeListener:
public void onPageSelected(int position) {
if(position == 1)
getActionBar().hide();
else
getActionBar().show();
}
This does hide the action bar, but it doesn't hide the system status bar (I'm talking about where the notifications, status icons, and time are), and the action bar hide/show animation is also very choppy when swiping.
For an example of what I want to do exactly, see Snapchat - they manage to pull off the swiping between camera and other fragments perfectly.
Edit:
So I changed my onPageSelected to this:
public void onPageSelected(int position) {
if(position == 1) {
MainActivity.this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
MainActivity.this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
} else {
MainActivity.this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
MainActivity.this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
}
This enables me to change whether the status bar is displayed or not. However, it causes a large amount of jank. Same problem with the action bar.
I notice that in Snapchat the status bar slides up and down after the tab change is complete. I'm not sure how to implement this, so any advice regarding this aspect would be appreciated.
I figured out how to do this, though it's definitely a hack.
The trick is not to use the Android action bar, it's to make your own in your layout files.
I defined two more RelativeLayouts in my code:
<RelativeLayout
android:id="#+id/padding"
android:layout_width="fill_parent"
android:layout_height="25dip"
android:layout_alignParentTop="true"
android:background="#color/abs__background_holo_dark" />
<RelativeLayout
android:id="#+id/fake_action_bar"
android:layout_width="fill_parent"
android:layout_height="48.0dip"
android:layout_below="#id/padding" >
<!-- stuff here -->
</RelativeLayout>
The top RelativeLayout is padding for the Android status bar. I set it to 25dip, but it may vary - it would probably be best to set this value programmatically. Also you need to set it to a black color so that it matches the status bar color and makes the UI look more natural.
The bottom RelativeLayout is for your fake action bar. You need to stuff everything your action bar needs (title text, buttons, etc.) in this RelativeLayout. I set it to 48dip, but again, this will vary and you should probably set it programmatically.
In the Activity hosting your Fragments, ViewPager and FragmentPagerAdapter, you need to set the flags telling Android to make the status bar an overlay:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
Now we need to make the status bar hide and show when we switch fragments. Have your activity implement ViewPager.OnPageChangeListener and then use the following code:
#Override
public void onPageScrollStateChanged(int state) {
switch(state) {
case ViewPager.SCROLL_STATE_IDLE:
if(mPosition == 1) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
}
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
mPosition = position;
}
We keep track of the page number using onPageSelected and use it in onPageScrollStateChanged to dynamically hide/show the status bar depending on which Fragment is currently being displayed. You can't do this in onPageSelected because this will cause the status bar to redisplay halfway through a swipe.
You also need to make sure to add appropriate code for your fake action bar - for buttons, this would be OnClickListener.
First, in the activity/fragment you want to hide the status-bar:
Make a public static of the fragment.
public static Fragment QSFrag;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
QSFrag = this;
}
Then in your onPageSelected:
public void onPageSelected(int position) {
// TODO Auto-generated method stub
if(position == 1){
mActionBar.hide();
YourActivityWithNoStatusOrActionBar.QSFrag.getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}else{
mActionBar.show();
// mActionBar.setSelectedNavigationItem(position); I use this, not sure u need/want it.
}
}
I've just tested it, and it works. But it doesn't look awesome when you leave the fragment with no status-bar. :p
You can use this code in onCreate:
setTheme(android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}

Show white screen until layout is completely constructed

My problem is that I have complicated layout, and when my application starts, I see how every view is adding to layout. I see my custom window title bar construction too. I see empty custom window title, with gradient background on start, and then 1 - 2 sec later I see completed window title with my views.
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
Is there simple way to show white screen, until my activity and custom title bar will be completely constructed?
The main trouble is that I cant start application with no title, and then add custom window title.
Only solution I know is splash screen, but it is too complicated for just this small task.
Use an asynctask like this:
private class LoadingMyView extends AsyncTask<void, void, int> {
protected void onPreExecute ()
{
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
//show whiteView
}
protected int doInBackground() {
return 0;
}
protected void onPostExecute(int result) {
//Hide white View
}
}
Note: I didnt test the code but i think more or less is ok
AsyncTask

Scratch image in android?

I want to show image only when user scratch on that, image will be overlay with colors. So when user scratch on it they can see the image. I will finished that part by using this link.
But what i need is while user scratch on that image, progress bar want to show the current progress. if user scratches all the portion of image progress bar should finish 100%.
If you use Android-WScratchView library, you can register OnScratchCallback and assign the percentage of cleared part to your progressbar
scratchView.setOnScratchCallback(new WScratchView.OnScratchCallback() {
#Override
public void onScratch(float percentage) {
yourProgressBar.setProgress(percentage);//Assign progressbar value here
}
#Override
public void onDetach(boolean fingerDetach) {
if(mPercentage > 50){
scratchView.setScratchAll(true);
updatePercentage(100);
}
}
});
You can check full sample at here

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).

Categories

Resources