Reputation: 949
Asking the Simplest Question, I have searched a lot related to my problem but didn't find any solution. I Have a layout in an Android application in which I'm using a listview
with progressbar
. All the task in doing in AsyncTask class. I want to Show the progress bar in centre of the activity before loading a list. Can anyone tell me where i'm going wrong or what changes are required for this solution. Thanks in Advance.
How I tried
activity_main:
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
android:visibility="invisible" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ListView>
MainActivity:
public class GettingAlerts extends ListActivity {
// Initialization
IPAddress ipAddressObject = new IPAddress();
public static String GETTING_ALERTS_URL = "http://"
+ IPAddress.IP_Address.toString()
+ "/MyServices/Alerts/AlertService.svc/gettingAllAlerts";
// TAGS
public static String TAG_NAME = "GetAllAlertsResult";
public static String TAG_ALERT_TITLE = "alertTitle";
public static String TAG_ALERT_DESCRIPTION = "alertDescription";
public static String TAG_ALERT_OWNER = "alertOwner";
public static String TAG_ALERT_DEADLINE = "alertDeadline";
static String Serv_Response = "";
ArrayList<Alerts> alertsList;
ProgressBar pg;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.alert_activity_list);
pg = (ProgressBar) findViewById(R.id.progressBar1);
// requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
// setProgressBarIndeterminateVisibility(true);
alertsList = new ArrayList<Alerts>();
ListView lv = getListView();
// setProgressBarIndeterminateVisibility(false);
new GettingAlertsList().execute();
}
private class GettingAlertsList extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
// LinearLayout linlaHeaderProgress = (LinearLayout)
// findViewById(R.id.linlaHeaderProgress);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(GETTING_ALERTS_URL);
HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpGet);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpEntity httpEntity = httpResponse.getEntity();
try {
Serv_Response = EntityUtils.toString(httpEntity);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (Serv_Response != null) {
JSONObject jsonObj = new JSONObject(Serv_Response);
JSONArray quiz = jsonObj.getJSONArray(TAG_NAME);
for (int i = 0; i < quiz.length(); i++) {
JSONObject c = quiz.getJSONObject(i);
String alert_title = c.getString(TAG_ALERT_TITLE);
String alert_owner = c.getString(TAG_ALERT_OWNER);
String alert_desc = c.getString(TAG_ALERT_DESCRIPTION);
String alert_dealdine = c.getString(TAG_ALERT_DEADLINE);
Alerts alertObject = new Alerts();
alertObject.setAlertTitle(alert_title);
alertObject.setAlertDescription(alert_desc);
alertObject.setAlertDeadline(alert_dealdine);
alertObject.setAlertOwner(alert_owner);
alertsList.add(alertObject);
}
}
} catch (JSONException e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
CustomAlertListAdapter adapter = new CustomAlertListAdapter(
GettingAlerts.this, alertsList);
setListAdapter(adapter);
pg.setVisibility(View.GONE);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pg.setVisibility(View.VISIBLE);
}
}
}
Image
As you could see the progressbar is at the top of the activity. I want that progressbar in center
of the activity before loading the List.
Upvotes: 0
Views: 1358
Reputation: 24853
Try this..
Remove android:layout_alignParentTop="true"
and android:layout_gravity="center_horizontal"
and add android:layout_centerInParent="true"
<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" >
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="invisible" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ListView>
</RelativeLayout>
Upvotes: 2
Reputation: 128428
I assume currently you have used LinearLayout
but instead get RelativeLayout
in XML and change order of widgets as below and remove android:layout_alignParentTop="true"
from ProgressBar.
<RelativeLayout
...
...>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ListView>
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
Upvotes: 0
Reputation: 70406
Change visibility to display/hide progress bar. You might need to set to change some attributes like style:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="@+id/progress_bar_wrapper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@null"
android:visibility="gone">
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="100dp"
android:layout_height="30dp"
android:progress="0"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
Upvotes: 0