I need to show splash layout file until operation is completed and than change to main layout.
Here is an example .
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
//Here app is copying files or something other it doesn't mater.
RelativeLayout panel = new RelativeLayout(this);
//Here is creating of main layout
setContentView(panel);
I need to show splash screen while operations are performing (between two methods setContentView) And than I need to set main layout,
I have tried to do in this way but it shows only black screen and only than shows main layout (panel).
Where is the problem ?
Thx for help in advance.
You can do something like this, if you're not doing heavy stuff precisely on setContentView(R.layout.main)
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
handler = new Handler();
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
//Do some heavy stuff
return null;
}
#Override
public void onPostExecute(Void result){
handler.post(new Runnable(){
#Override
public void run(){
setContentView(R.layout.main);
}
});
}
}.execute();
}
EDIT You can check this True SplashScreen
Here is a sample taken from my project:
Layout page, you can customize the progress bar as you want:
`
<ProgressBar
android:id="#+id/inquiry_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:visibility="gone"
android:indeterminateDrawable="#drawable/loading_progress"></ProgressBar>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal|center_vertical"
android:id="#+id/panel_inquiry"
>
.....
</FrameLayout>
`
Java source code:
private ProgressBar progressBar;
private FrameLayout inquiryFrame;
inquiryFrame=(FrameLayout)findViewById(R.id.panel_inquiry);
progressBar=(ProgressBar)findViewById(R.id.inquiry_progress);
In order to show the splash screen/do work in background
private void callService() {
progressBar.setVisibility(View.VISIBLE);
inquiryFrame.setVisibility(View.GONE);
new ServerReqAsync().execute(this
, ServiceConstants.getRequestList());
}
When background job completed, just view the activity default layout
progressBar.setVisibility(View.GONE);
inquiryFrame.setVisibility(View.VISIBLE);
Thanks
Related
I have a dummy RelativelayoutView with progress bar.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/progressbar">
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/progressBar" />
</RelativeLayout>
When my app starts I need this RelativelayoutView to appear for 5 seconds and should diappear with or without animation.
I'm not sure I understood your question, but I'd suggest you to write something like this in your activity:
private Handler mHandler;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
mHandler = new Handler(Looper.getMainLooper());
mHandler.postDelayed(new Runnable() {
public void run() {
// hide your progress bar
findViewById(R.id.progressBar).setVisibility(View.GONE);
// or finish this activity and start a new one
startActivity(new Intent(MyActivity.this, MySecondActivity.class));
}
}, 5000);
}
#Override
public void onDestroy() {
super.onDestroy();
mHandler.removeCallbacks(null);
}
And I think you should add the android:indeterminate="true" attribute to your progress bar.
Hope it helps. Cheers.
I use android.support.v4.widget.SwipeRefreshLayout in my Android app. It wraps a ListView. The content of the list view is downloaded from a server.
A progress view is shown when user swipes down in order to reload data from server. The progress view looks like a piece of circle that grows and shrinks during the animation. It seems that the style of this progress view cannot be customized much. However, I am fine with its built-in style.
I also show the same progress animation during initial data loading. This can be achieved by calling mySwipeRefreshLayout.setRefreshing(true). That's perfectly OK too.
However, I would like to show the same progress indication through the whole app. Consider e.g. another activity that looks like a form with submit button. There is neither a ListView nor a SwipeRefreshLayout in this form activity. Some progress indication should be displayed while the submitted data are transferred to the server. I want to show a progress bar with the same animation like in SwipeRefreshLayout.
Is there a simple and clean way to have the same progress indicator for both a SwipeRefreshLayout and a form activity that does not contain any list view and refresh layout and does not support any swipe gesture?
Thanks.
However, I would like to show the same progress indication through the
whole app. Consider e.g. another activity that looks like a form with
submit button. There is neither a ListView nor a SwipeRefreshLayout in
this form activity. Some progress indication should be displayed while
the submitted data are transferred to the server. I want to show a
progress bar with the same animation like in SwipeRefreshLayout.
yes you can use SwipeRefreshLayout to show like ProgressDialog when button click. check below.
i have added SwipeRefreshLayout inside my layout file still there is not ListView or ScrollView. just like below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="niu.com.djandroid.jdroid.androidstack.MainActivity"
tools:showIn="#layout/activity_main">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/activity_main_swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<SeekBar
android:id="#+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="54dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
now in my activity i used AsyncTask to show SwipeRefreshLayout. also use mSwipeRefreshLayout.setOnRefreshListener(null) to stop swipe gesture.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);
mSwipeRefreshLayout.setOnRefreshListener(null);
((Button) findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// mSwipeRefreshLayout.setRefreshing(true);
// call AsyncTask
new LongOperation().execute("");
}
});
}
private class LongOperation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return "Executed";
}
#Override
protected void onPostExecute(String result) {
// stop mSwipeRefreshLayout
mSwipeRefreshLayout.setRefreshing(false);
}
#Override
protected void onPreExecute() {
// start mSwipeRefreshLayout
mSwipeRefreshLayout.setRefreshing(true);
}
#Override
protected void onProgressUpdate(Void... values) {
}
}
EDITED:15/02/20
After 4 year, Here is great explanation on android developer site
mSwipeRefreshLayout.setOnRefreshListener(null)
didn't work for me, so I made this method to enable and disable refreshing.
private void setToRefresh(boolean refresh) {
if (refresh) {
mSwipeRefreshLayout.setEnabled(true);
mSwipeRefreshLayout.setRefreshing(true);
} else {
mSwipeRefreshLayout.setRefreshing(false);
mSwipeRefreshLayout.setEnabled(false);
}
}
I am working on splash screen for my android app. The screen starts from my MainActivity.java, in which onCreate() method contains the following code. Also the same activity class is loading maps in its onResume() method and in it i m loading a new layout which is activity_main.xml. That xml has black screen and no background screen.
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.loading_screen);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
textView = (TextView) findViewById(R.id.textView1);
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText(progressStatus + "/"
+ progressBar.getMax());
}
});
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
compass = new CompassSensor(this, compassHandler);
}
my layout for loading_screen.xml is as follow.
loading_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/splash_screen_loading" xmlns:android="http://schemas.android.com/apk/res/android">
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/progressBar1"
android:layout_marginLeft="15dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
The problem i m facing is that only black screen shows during app loading. I tried to debug the problem but all my efforts were useless. Plz help.
You should not attempt this in onCreate, if you expect visual changes. Nothing is shown to user during onCreate. I am aware of Thread.sleep(200).
You may want to try doing it in your onResume, just after calling super.onResume. The way you described it, R.layout.loading_screen is not even supposed to be shown.
Also, if you're using AsyncTask to load maps, Developers lets you know how to publish progress. Take a look at this: AsyncTask. Maybe you could add this prior work (with progress on R.layout.loading_screen) to asynchronous task, too.
I have been trying for a while now, Have looked at a few SO posts, and got to this stage. I am sure I am very close to getting it working, but am missing something very basic.
Code Snippets of the activity below
public class SearchResultsMap extends MapActivity {
....
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.searchresultsmap);
...
busy = (LinearLayout) findViewById(R.id.busy);
busy.bringToFront();
progressBar = (ProgressBar) findViewById(R.id.progressBar) ;
progressBar.bringToFront() ;
searchResponse = (HashMap<Integer, CustomJourneyUserInformation>) this.getIntent()
.getSerializableExtra("searchResponse");
new GetProviderAndUserDetails(this).execute(null);
>>edit1 progressBar.setMax(searchResponse.size());
>>edit1 progressBar.setProgress(0) ;
...
}
//Inner Class, type AsyncTask
public class GetProviderAndUserDetails extends AsyncTask<String, Integer, Object> {
public GetProviderAndUserDetails(Activity mapActivity) {
this.mapActivity = mapActivity;
}
protected void onPreExecute() {
busy.setVisibility(View.VISIBLE);
setProgressBarVisibility(true) ;
setProgressBarIndeterminate(true) ;
setProgressBarIndeterminateVisibility(true) ;
}
#Override
protected void onProgressUpdate(Integer... progress) {
if (searchActivity != null) {
searchActivity.setProgress(progress[0]);
>>edit1 progressBar.setProgress(progress[0]) ;
}
}
#Override
protected Object doInBackground(String... args) {
...
//for loop, i
publishProgress(i) ;
//end for loop
...
}
protected void onPostExecute(Object result) {
super.onPostExecute(result);
busy.setVisibility(View.GONE);
setProgressBarVisibility(false) ;
setProgressBarIndeterminate(false) ;
setProgressBarIndeterminateVisibility(false) ;
...
}
}
Layout XML File
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/busy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:orientation="vertical"
android:visibility="visible" >
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:indeterminateOnly="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp" />
</LinearLayout>
.....
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/searchMap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/tip"
android:layout_alignParentTop="true"
android:apiKey="mykey"
android:clickable="true" />
....
</RelativeLayout>
Device Results : Unable to upload to stackoverflow. Some SO issue.
searchresults image
You're calling setProgressBarIndeterminate() and related functions, which act on your activity's title, not on the progress bar in your layout.
Use progressBar.setIndeterminate() and friends instead.
EDIT: And if you want a progress bar instead of the indeterminate progress indicator, use setIndeterminate(false).
Setting the style to progressBarStyleHorizontal in the xml worked finally. Seems like progressBarStyleLarge cant show progress.
Also the indeterminate thing helped.
I use useful guide written by Cyril Mottier there empty state of ListView inflate in ViewStub with id #android:id/empty. In this method all good, but if i have ListActivity without attached Adapter via setListAdapter or attached empty Adapter then i see empty view. It's all clear but if i want realize logic like this:
Open ListActivity
Attach adapter
Execute AcyncTask and get data for Adapter, show progress dialog
Call notifyDataSetChanged
dismiss dialog
Then from 1 to 3 steps i look empty view with error (e.g. "not found"). But I want show error only after execute AsyncTask. While during executing AsyncTask i want show only process dialog. I try play around use visibility option of #android:id/empty, try use include in xml. But can't realize this logic. Any suggestion would appreciate.
UPD: More information
I have activity.xml:
<FrameLayout
android:id="#id/gd_action_bar_content_view"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ListView
android:id="#android:id/list"
style="#style/listViewStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="false" />
<ViewStub
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
layout="#layout/error" />
</FrameLayout>
error.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="20dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:src="#drawable/ic_cheese" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:textStyle="bold"
android:textSize="16sp"
android:text="#string/no_cheese" />
</LinearLayout>
ListActivity Class:
public class MyListActivity extends ListActivity implements OnScrollListener, OnItemClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity); //inflate activity with ViewStub
mAdapter = new MyAdapter(this, Collections.EMPTY_LIST);
setListAdapter(mAdapter);
new SearchAndFill().execute("");
}
}
And Async class:
private class SearchAndFill extends AsyncTask<String, Void, List<Object>>{
LoadingDialog dialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
if (dialog==null){
dialog = new LoadingDialog(MyListActivity.this);
dialog.setCancelable(false);
}
dialog.show();
}
#Override
protected List<Object> doInBackground(String... str) {
return search(str[0]); //this is HTTP GET request
}
#Override
protected void onPostExecute(List<Object> result) {
super.onPostExecute(result);
dialog.dismiss();
showResult(result); //this is fill adapter data and call notifyDataSetChanged
}
}
When activity create and inflate activity.xml it's show empty with and then execute AsyncTask, while AsyncTask executing i see empty view. When call onPostExecute method of AsyncTask i see result of HTTP GET request. When activity creating and inflated activity.xml it's show empty with and then execute AsyncTask, while AsyncTask executing i see empty view. When call onPostExecute method of AsyncTask i see result of HTTP GET request. I think it's wrong. I want show blank ListView before AsyncTask executed, and after if result equals Collections.EMPTY_LIST i want show text with image (e.g nothing found)
As much as I understood this is what you want to achieve...
public class MyListActivity extends ListActivity implements OnScrollListener, OnItemClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity); //inflate activity with ViewStub
((ViewStub) findViewById(R.id.YOUR_VIEW_STUB_ID)).setVisibility(View.GONE);
mAdapter = new MyAdapter(this, Collections.EMPTY_LIST);
setListAdapter(mAdapter);
new SearchAndFill().execute("");
}
}
... ... ....
private class SearchAndFill extends AsyncTask<String, Void, List<Object>>{
LoadingDialog dialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
if (dialog==null){
dialog = new LoadingDialog(MyListActivity.this);
dialog.setCancelable(false);
}
dialog.show();
}
#Override
protected List<Object> doInBackground(String... str) {
return search(str[0]); //this is HTTP GET request
}
#Override
protected void onPostExecute(List<Object> result) {
super.onPostExecute(result);
if (result == null || result.size() == 0){
mAdapter.notifyDataSetInvalidated();
((ViewStub) findViewById(R.id.YOUR_VIEW_STUB_ID)).setVisibility(View.VISIBLE);
} else {
// add stuff to mAdapter here...
mAdapter.notifyDataSetChanged();
}
dialog.dismiss();
}
}
I suggest you to try setting text value of your empty view to empty string ("") then change the text value to error message when AsyncTask is complete.
Hope it solved your problem.