Skip to content Skip to sidebar Skip to footer

Android Progress Dialog Within Listview

My app is designed as follows: Main Activity uses action bars First tab is a fragment that is split into 3 sections | Linear Layout containing List view | |Linear Layout containi

Solution 1:

I understand that I can put this spinner into the top right of the application. But can i place it either in the top right of the list views linear layout, or centred at the back of the linear layout?

If you can calculate the position of the view, you would probably be able to position the ProgressDialog where you want(for example see this question I answered Change position of progressbar in code). Also, keep in mind, that this could be very counter intuitive for the user who would see the screen for the dialog and the dialog placed at some weird position(he may not make the correlation between the position of the ProgressDialog and the view for which the data is loaded).

Another option could be to modify the current layout for the ListView part to add on top an initial gone FrameLayout(which will cover the entire ListView) with a background that simulates the background for a screen with a Dialog(this FrameLayout will contain a ProgressBar placed where you want). This FrameLayout would be made visible when the AsyncTask kicks in(it may be wise to block the touch events for the underlining ListView). This way the user can still do stuff in your app and it has a clear indication for the View that is loading its data. Of course this will work well if it's possible for the user to work with the other data independently from the loading ListView.

Solution 2:

The answer from Luksprog is so good that I thought I should list the code here: Absolutely perfect main_layout.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><ListViewandroid:id="@+id/first_list"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_above="@+id/anchor" ></ListView><Viewandroid:id="@id/anchor"android:layout_width="match_parent"android:layout_height="5dp"android:layout_centerVertical="true"android:background="#99cc00" /><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/anchor" ><ListViewandroid:id="@+id/second_list"android:layout_width="match_parent"android:layout_height="wrap_content" /><FrameLayoutandroid:id="@+id/overlay"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#33c1c1c1"android:visibility="gone" ><ProgressBarstyle="?android:attr/progressBarStyleSmall"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="right|top"android:indeterminate="true" /></FrameLayout></RelativeLayout>

MainActivity.java

publicclassMainActivityextendsActivity {

privateString[] mItems = { "Item no.1", "Item no.2", "Item no.3",
        "Item no.4", "Item no.5", "Item no.6", "Item no.7", "Item no.8",
        "Item no.9", "Item no.10", "Item no.11", };

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
    // setup the two ListViewsListView firstList = (ListView) findViewById(R.id.first_list);
    firstList.setAdapter(newArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, mItems));
    firstList.setOnItemClickListener(newOnItemClickListener() {

        @OverridepublicvoidonItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // just as an example, one you click an item in the first// ListView// a custom AsyncTask will kick in to load data in to the second// ListViewnewMyTask(MainActivity.this).execute((Void) null);
        }
    });
    ListView secondList = (ListView) findViewById(R.id.second_list);
    secondList.setAdapter(newArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, mItems));
    secondList.setOnItemClickListener(newOnItemClickListener() {

        @OverridepublicvoidonItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // just to test that you can't click the ListView if the data is// loadingToast.makeText(getApplicationContext(), "click",
                    Toast.LENGTH_SHORT).show();
        }
    });
}

privateclassMyTaskextendsAsyncTask<Void, Void, Void> {

    privateMainActivity mActivity;
    privateFrameLayout mFrameOverlay;

    publicMyTask(MainActivity activity) {
        mActivity = activity;
    }

    @OverrideprotectedvoidonPreExecute() {
        // the AsyncTask it's about to start so show the overlay
        mFrameOverlay = (FrameLayout) mActivity.findViewById(R.id.overlay);
        // set a touch listener and consume the event so the ListView// doesn't get clicked
        mFrameOverlay.setOnTouchListener(newOnTouchListener() {

            @OverridepublicbooleanonTouch(View v, MotionEvent event) {

                returntrue;
            }
        });
        mFrameOverlay.setVisibility(View.VISIBLE);
    }

    @OverrideprotectedVoiddoInBackground(Void... params) {
        // do heavy worktry {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        returnnull;
    }

    @OverrideprotectedvoidonPostExecute(Void result) {
        //remove the overlay
        mFrameOverlay.setVisibility(View.GONE);
        // setup the ListView with the new obtained dataString[] obtainedData = { "D1", "D2", "D3" };
        ListView theList = (ListView) mActivity
                .findViewById(R.id.second_list);
        theList.setAdapter(newArrayAdapter<String>(mActivity,
                android.R.layout.simple_list_item_1, obtainedData));
    }

}
}

Luksprog, hope you don't mind me posting your code, just didn't want it to vanish from git and this answer be lost to others.

Post a Comment for "Android Progress Dialog Within Listview"