This is my xml file:
<ProgressBar
android:id="#+id/progress_index"
style="#android:style/Widget.ProgressBar.Inverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"/>
<ListView
android:id="#+id/listview_index"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="outsideInset">
</ListView>
I try to mProgress.setVisibility(View.GONE) after mListView.setAdapter() ,but when I check the "Show layout bounds" in the "Developer options".The layout not actually gone.As the picture shows,the ProgressBar is still in the center of the FrameLayout.Why?
Java code:
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mProgress = (ProgressBar) view.findViewById(R.id.progress_index);
mProgress.setVisibility(View.VISIBLE);
mAdapter = new CustomAdapter(getActivity());
new MyTask().execute();
lv.setAdapter(mAdapter);
}
public class MyTask extends AsyncTask<Integer, Void, ArrayList<NewsItem>> {
protected ArrayList<NewsItem> doInBackground(Integer... params) {
// some execution....
}
protected void onPostExecute(ArrayList<NewsItem> result) {
mAdapter.notifyDataSetChanged();
mProgress.setVisibility(View.GONE);
mListView.setVisibility(View.VISIBLE);
}
}
can you try this:
ProgressDialog mProgress = new ProgressDialog(activity.this);
...........
mProgress.dismiss();
Related
I have 3 menu items in action bar when I click any one of them data retrieved from webservice which is working fine. but while retrieving I need to show Progress dialog in the screen .I have tried different ways like starting in new thread, AsyncTask. But finally stuck. Please can anyone help me out. Here is my complete code.
import hello.aws.graph.BatteryGraph;
public class BatteryGraph extends AppCompatActivity {
static ProgressDialog dialog = null;
static String value;
public static Menu menu;
static ArrayList<TrackingBean> Battery;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_line_chart);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent newIntent;
switch (item.getItemId()) {
case R.id.bb_menu_favorites:
value="favorites";
getData(value);
getSupportFragmentManager().beginTransaction().replace(R.id.container, new PlaceholderFragment()).commit();
break;
case R.id.bb_menu_mylist:
value="mylist";
getData(value);
getSupportFragmentManager().beginTransaction().replace(R.id.container, new PlaceholderFragment()).commit();
break;
case R.id.bb_menu_recents:
value="myrecents";
getData(value);
getSupportFragmentManager().beginTransaction().replace(R.id.container, new PlaceholderFragment()).commit();
break;
}
return true;
}
public void getData(final String value)
{
AsyncCallWS task = new AsyncCallWS();
try {
task.execute().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
/**
* A fragment containing a line chart.
*/
public static class PlaceholderFragment extends Fragment {
private LineChartView chart;
private LineChartData data;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
View rootView = inflater.inflate(R.layout.fragment_line_chart, container, false);
chart = (LineChartView) rootView.findViewById(R.id.chart);
generateData();
return rootView;
}
private void generateData() {
/****This method used to Plot points on graph ****/
}
}
private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(BatteryGraph.this, R.style.MyTheme);
dialog.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
dialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.cp_bar));
dialog.setCancelable(false);
dialog.show();
getSupportFragmentManager().beginTransaction().replace(R.id.container, new PlaceholderFragment()).commit();
}
#Override
protected Void doInBackground(Void... params) {
Battery=WebService.getDataFromWS(value);
return null;
}
#Override
protected void onPostExecute(Void result) {
if(dialog!=null||dialog.isShowing()) {
dialog.dismiss();
}
}
}
}
activity_line_chart.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="hello.aws.graph.BatteryGraph"
tools:ignore="MergeRootFrame" />
fragment_line_chart.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="hello.aws.graph.BatteryGraph$PlaceholderFragment" >
<LinearLayout
android:id="#+id/l1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin1top"
android:orientation="vertical">
<lecho.lib.hellocharts.view.LineChartView
android:id="#+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</lecho.lib.hellocharts.view.LineChartView>
</LinearLayout>
</RelativeLayout>
i hope it will help u...
private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
pDialog = new ProgressDialog(getActivity());//here activity name or if u are using fragment put getActivity()
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.setCanceledOnTouchOutside(false);
pDialog.show();
getSupportFragmentManager().beginTransaction().replace(R.id.container, new PlaceholderFragment()).commit();
}
#Override
protected Void doInBackground(Void... params) {
Battery=WebService.getDataFromWS(value);
return null;
}
#Override
protected void onPostExecute(Void result) {
if(dialog!=null||dialog.isShowing()) {
pDialog.dismiss();
}
}
}
My problem is that when i click button "Click" to call my API it should show my progressBar(spinner) while I'm calling API. Instead my application freeze for less than a second and then when it's done calling it shows my loading spinner for a brief time (it just flash)
Here is my code
private ProgressBar spinner;
public View onCreateView(...)
{
spinner = (ProgressBar) view.findViewById(R.id.progressBar1);
spinner.setVisibility(View.GONE);
...
}
private class MyTask extends AsyncTask<String, Void, Void> {
String pageContent = "";
DataOutputStream wr;
#Override
protected void onPreExecute() {
spinner.setVisibility(View.VISIBLE);
}
#Override
public Void doInBackground(String... params) {
requestResult.setSuccess(false);
HttpURLConnection connection;
try {
String url = "myURL";
//I only call my Api here. I delete rest so this won't bother you
requestResult.setSuccess(true);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
JsonParser parser = new JsonParser();
//Same here i delete nonimportant
MyResponse = new Gson().fromJson(jsonContent, MyResponse.class);
done();
spinner.setVisibility(View.GONE);
}
}
I've tried hounder different things it always freeze for a second and then my loading spinner flash and my content from API is shown.
Can you help me with that one, please?
My xml file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll_view_send"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/buttonSend"
android:visibility="gone"/>
</RelativeLayout>
</ScrollView>
Try the below snippet. It does not require spinner to be declared in layout file.
AsyncTask<String, String, String> asyncObject =
new AsyncTask<String, String, String>() {
ProgressDialog progDailog;
#Override
protected void onPreExecute() {
super.onPreExecute();
progDailog =
new ProgressDialog(Activity.this);
progDailog.setMessage("Loading");
progDailog.setCancelable(false);
progDailog.show();
}
#Override
protected String doInBackground(String... urls) {
//Do background stuff here
return null;
}
#Override
protected void onPostExecute(String result) {
progDailog.cancel();
//Do post background stuff here.
}
};
asyncObject.execute(null, null, null);
Please try this working sample, pass the spinner to your MyTask like this:
private class MyTask extends AsyncTask<String, Void, Void> {
String pageContent = "";
DataOutputStream wr;
private final ProgressBar progress;
public MyTask(final ProgressBar progress) {
this.progress = progress;
}
#Override
protected void onPreExecute() {
progress.setVisibility(View.VISIBLE);
}
...
#Override
protected void onPostExecute(Void result) {
JsonParser parser = new JsonParser();
//Same here i delete nonimportant
MyResponse = new Gson().fromJson(jsonContent, MyResponse.class);
done();
progress.setVisibility(View.GONE);
}
}
Then call new MyTask(progress).execute();
EDIT: In your question you refer that your phone is freezing while calling the MyTask...
Please check this step to avoid freezing your UI in AsyncTask:
Do not call MyTask using new MyTask().get()
Try moving to doInBackground this part MyResponse = new Gson().fromJson(jsonContent, MyResponse.class); done(); this could be expensive.
Hope its helps!!
I have trouble displaying the layout of my main activity :
I create an ActivityA with an ImageView.
In onCreate(), I launch an AsyncTask, which retrieves content from Internet, and opens an ActivityB.
When I launch my application, it displays ActivityB right away.
public class ActivityA extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyTask mytask = new MyTask();
mytask.execute();
}
}
My MainActivity xml file is the following :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/loadingPanel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/home_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_home_page" />
<ProgressBar
android:id="#+id/marker_progress"
style="?android:attr/progressBarStyle"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_vertical|center_horizontal"
android:indeterminate="true" />
</RelativeLayout>
The ProgressBar is used to show the loading process of the AsyncTask.
thanks for helping
public class ActivityA extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyTask mytask = new MyTask();
mytask.execute();
}
}
private class MyTask extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... params) {
// code for retrieve contents from Internet
}
#Override
protected void onPostExecute(String result) {
// cancel ProgressBar and start activity B here like
Intent i = new Intent(A.this, B.class);
startActivity(i);
}
#Override
protected void onPreExecute() {
// showing ProgressBar
}
}
don't forgot to mention A activity as a launcher.
I hope it will help you to solve this problem.
When I start activity with ListView it start to load some information from the Internet and after apply adapter, but during loading ListVIew doesn't show me a ProgressBar ring. When I trying to use SherlockListActivity instead ListView I have same problem.
<?xml version="1.0" encoding="utf-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/comments"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
public class CommentsActivity extends SherlockActivity{
#InjectView(R.id.comments) ListView mCommentsListView;
private String mPostId;
private CommentsAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.comment_activivty);
ButterKnife.inject(this);
Intent intent = getIntent();
mPostId = intent.getStringExtra("POST_ID");
LC lc = new LC();
lc.execute(mPostId);
}
public void setupAdapter(){
mAdapter = new CommentsAdapter(this);
mCommentsListView.setAdapter(mAdapter);
}
private class LC extends LoadComments{
#Override
protected void onPostExecute(Void result) {
//setup adapter after loading comments
setupAdapter();
}
}
}
Add ProgressBar to your layout file:
<ProgressBar
android:id="#+id/progress_bar"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Inject the ProgressBar in your activity:
#InjectView(R.id.progress_bar) ProgressBar mProgressBar;
Now you have to hide it when the adapter is set:
public void setupAdapter(){
mAdapter = new CommentsAdapter(this);
mCommentsListView.setAdapter(mAdapter);
mProgressBar.setVisibility(View.GONE);
}
Do it like this,
#Override
protected Void OnPreExecute(){
ProgressDialog prgDialog = new ProgressDialog(getContext());
prgDialog.setTitle("Loading");
prgDialog.setMessage("please wait")
prgDialog.setCancelable(false);
prgDialog.show();
}
on post Execute
private class LC extends LoadComments{
#Override
protected void onPostExecute(Void result) {
//setup adapter after loading comments
prgDialog.dismiss();
setupAdapter();
}
}
Hi I have a AysncTask that i'm wanting to inflate a loading view whilst that runs when it's finished hide it. does anyone know how to do this? at moment its not showing the loading view.
this is what i have tried
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
loadingView = LayoutInflater.from(getBaseContext()).inflate(R.layout.loadingcell,
null);
//Check Preferences which sets UI
checkPreferences();
PostTask posttask;
posttask = new PostTask();
posttask.execute();
}
#Override
protected Boolean doInBackground(Void... params) {
boolean result = false;
loadFixtures();
publishProgress("progress");
loadResultsFeed();
publishProgress("progress");
loadNewsFeed();
publishProgress("progress");
return result;
}
protected void onProgressUpdate(String... progress) {
StringBuilder str = new StringBuilder();
for (int i = 1; i < progress.length; i++) {
str.append(progress[i] + " ");
}
}
#Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
Log.v("BGThread", "begin fillin data");
FillData();
loadingView.setVisibility(View.GONE);
}
}
loadingcell.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:id="#+id/loadingcell">
<include
layout="#layout/header" />
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="87dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="115dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/progressBar1"
android:gravity="center"
android:text="Loading..."
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/blue" />
</RelativeLayout>
You are not using an AsyncTask in you code. Use something like this:
private class doSomething extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
//Inflate you view or do anything here before loading stuff
}
protected Void doInBackground(Void... urls) {
//loading stuff
}
protected void onProgressUpdate(String... progress) {
//change your UI - like a progressbar
}
#Override
protected void onPostExecute(String unused) {
//Hide the progressbar
}
You need to have a content for the Activity - which you can set using setContentView