Abhilasha
Abhilasha

Reputation: 949

listview scrolls to bottom of screen

I have an activity with the following layout.

enter image description here

So in this screen, initally the ListViewis empty. I type some text in EditTextand click on the search icon next to it, the app fetched the items from server and populates the ListView. Now the problem is that the ListView scrolls down to the last item.

Code in my onClick() of search Button

                    String str_searchTxt = mEt_search.getText().toString().trim();
            if (str_searchTxt.length() == 0) {
                Toast.makeText(getApplicationContext(),
                        getResources().getString(R.string.ENTER_MANDATORY),
                        Toast.LENGTH_LONG).show();
                return;
            }
            // get items List in araryList
            itemList = getItemList(messageHandler,str_searchTxt);

Code in my message handler:

            public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
        case 2:
            String txt = msg.getData().getString("text");

            Log.i("Result", txt);
            progressDialog.dismiss();
            if (txt.equalsIgnoreCase("success")) {
                lview3 = (ListView) findViewById(R.id.listView1);
                adapter = new ListViewCustomAdapter(
                        SearchAssetActivity.this, itemList);
                lview3.setAdapter(adapter);
                lview3.setOnItemClickListener(SearchAssetActivity.this);
                lview3.requestFocus();
                lview3.setSelection(0);             

            }
        }

Upvotes: 0

Views: 3998

Answers (2)

Lalit Poptani
Lalit Poptani

Reputation: 67286

You can use setSelection(position) of ListView,

mListView.setSelection(first_position);

Else you can try,

mListView.setSelectionAfterHeaderView();

Upvotes: 3

Sarim Sidd
Sarim Sidd

Reputation: 2176

Try this:

yourscrollview.fullScroll(ScrollView.FOCUS_UP);

Upvotes: 0

Related Questions