Search code examples
androidlistviewandroid-linearlayoutandroid-xmlandroid-tabactivity

Listview is not viewing completly in tabview


I am trying to display list view in tab view but list view is not completely view able. List view is not getting scroll as well.I am not able to track the issue.Please see if you find issue.Any kind of help will be appreciable . !

Here is my code : Tabview XMl:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffffffff"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/light_gray"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/back_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:gravity="left"
                android:padding="2.0dip"
                 android:layout_gravity="center_vertical"
                android:textColor="@color/blue"
                android:background="@android:color/transparent"
                android:text="Back"
                android:textStyle="bold"
                android:typeface="normal" />

            <TextView
                android:id="@+id/clientusername"
                android:layout_width="match_parent"
                android:layout_height="45.0dip"
                android:layout_gravity="center_horizontal"
                android:gravity="center_horizontal"
                android:padding="2.0dip"
                android:text="UserName Here"
                android:textColor="@color/blue"
                android:textSize="27.0dip"
                android:textStyle="bold"
                android:typeface="monospace" />

        </LinearLayout>

        <TabHost
            android:id="@android:id/tabhost"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginTop="10.0dip" >

            <LinearLayout
                android:id="@+id/tabcontainer"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="250.0dip"
                    android:layout_height="40.0dip"
                    android:layout_gravity="center"
                    android:gravity="center" />

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_marginTop="15.0dip"
                    android:background="@android:color/white"
                    android:fadeScrollbars="false"
                    android:fadingEdge="none"
                    android:scrollbars="none" />
            </LinearLayout>
        </TabHost>
    </LinearLayout>

</ScrollView>

TabActivity:

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
import commonUtilities.VariableClass;

public class ContactsGroup extends TabActivity
{
  private static LayoutInflater inflater;
  private static Intent intent;
  private static final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0.5F);
  private static TabHost.TabSpec spec;
  private static TabHost tabHost;
  private String BLUE = "#296fa2";
  private String GRAY = "#929292";
  private String WHITE = "#FFFFFF";

  private TextView label1;
  private TextView label2;
  private View tab;
  TextView user_name;
  Button back_button;

  private void clearTabStyles()
  {
    for (int i = 0; ; i++)
    {
      if (i >= getTabWidget().getChildCount())
        return;
      this.tab = getTabWidget().getChildAt(i);
      this.tab.setBackgroundColor(Color.parseColor("#666666"));
    }
  }

  public void onBackPressed()
  {

    finish();
    super.onBackPressed();
  }

  public void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    setContentView(R.layout.clients_tab);
    try
    {
      this.user_name = ((TextView)findViewById(R.id.clientusername));
      this.user_name.setTypeface(MainActivity.tp_semi_bold);
      back_button=(Button)findViewById(R.id.back_button);
      try
      {

        this.user_name.setVisibility(View.INVISIBLE);
        inflater = (LayoutInflater)getSystemService("layout_inflater");
        tabHost = getTabHost();
        this.tab = inflater.inflate(R.layout.tab, getTabWidget(), false);
        this.tab.setLayoutParams(params);
        this.label1 = ((TextView)this.tab.findViewById(R.id.tabLabel1));
        this.label1.setTypeface(MainActivity.tp_normal);
        this.label1.setText(VariableClass.Messages.APPCONTACTS);
        intent = new Intent(this, AppContacts.class);

        spec = tabHost.newTabSpec("home").setIndicator(this.tab).setContent(intent);
        tabHost.addTab(spec);
        this.tab = inflater.inflate(R.layout.tab, getTabWidget(), false);
        this.tab.setLayoutParams(params);
        this.label2 = ((TextView)this.tab.findViewById(R.id.tabLabel2));
        this.label2.setTypeface(MainActivity.tp_normal);
        this.label2.setText(VariableClass.Messages.ALLCONTACTS);
        intent = new Intent(this, PhoneContacts.class);

        spec = tabHost.newTabSpec("users").setIndicator(this.tab).setContent(intent);
        tabHost.addTab(spec);
        tabHost.setCurrentTab(0);
        getTabWidget().getChildAt(0).setBackgroundColor(-1);
        this.label1.setTextColor(Color.parseColor(this.WHITE));
        this.label1.setBackgroundResource(R.drawable.bluetab_background);
        this.label2.setTextColor(Color.parseColor(this.GRAY));
        this.label2.setBackgroundResource(R.drawable.whitetab_background);
        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener()
        {
          public void onTabChanged(String paramAnonymousString)
          {
           clearTabStyles();
            View localView;
            if (paramAnonymousString.equals("home"))
            {
              localView =getTabWidget().getChildAt(0);
             label1.setTextColor(Color.parseColor(WHITE));
             label1.setBackgroundResource(R.drawable.bluetab_background);
             label2.setTextColor(Color.parseColor(GRAY));
             label2.setBackgroundResource(R.drawable.whitetab_background);
            }
            else if( paramAnonymousString.equals("users"))
            {
                localView =getTabWidget().getChildAt(1);
               label1.setTextColor(Color.parseColor(GRAY));
               label1.setBackgroundResource(R.drawable.whitetab_background);
               label2.setTextColor(Color.parseColor(WHITE));
               label2.setBackgroundResource(R.drawable.bluetab_background);
            }
          }
        });
      }
      catch (Exception localException2)
      {

          localException2.printStackTrace();
          finish();

      }
    }
    catch (Exception localException1)
    {
      localException1.printStackTrace();
    }
  }
  @Override
    protected void onResume() {
        // TODO Auto-generated method stub
      back_button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
    });
        super.onResume();
    }
  @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }
}

Activity1 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent_clients_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffffff"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/con_search_button"
        android:layout_width="fill_parent"
        android:layout_height="40.0dip"
        android:layout_margin="5.0dip"
        android:background="@drawable/edbackground"
        android:drawableLeft="@drawable/search"
        android:drawablePadding="10.0dip"
        android:hint="search clients"
        android:textColor="@color/gray"
        android:textSize="15.0dip" />

    <ListView
        android:id="@+id/con_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="5.0dip"
        android:layout_weight="0.1"
        android:background="@android:color/white" />

</LinearLayout>

Activity1 code :

import java.util.ArrayList;
import pjsua2Utilies.AppData;
import commonUtilities.CommonUtility;
import commonUtilities.VariableClass;
import dto.Contacts;
import Adapter.AppContactListAdapter;
import Adapter.ContactListAdapter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager.BadTokenException;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class AppContacts extends Activity {
    ListView contactslistview;
    EditText search_bar;
    ArrayList<Contacts>contactslist; 
    AppContactListAdapter adapter;
    Context c ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contacts_list);
        initialise();
    }
    public void initialise()
    {
        contactslistview = (ListView)findViewById(R.id.con_list);
        search_bar = (EditText)findViewById(R.id.con_search_button);
        c = AppContacts.this;
    }
    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        new getContacts().execute(null,null,null);
        super.onStart();
    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub

        contactslistview.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                if(AppData.reg_status){
                    Contacts dto = (Contacts)arg0.getItemAtPosition(arg2);
                    Intent i = new Intent(c, CallingScreen.class);
                    i.putExtra(VariableClass.Vari.NAMETOCALL,dto.getName());
                    i.putExtra(VariableClass.Vari.NUMBERTOCALL,dto.getNumber());
                    dto=null;
                    startActivity(i);
                }

            }
        });
        search_bar.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                String startwith=search_bar.getText().toString();
                ArrayList<Contacts>filterList=new ArrayList<Contacts>();
                int contactsCount=contactslist.size();
                for(int i=0;i<contactsCount;i++)
                {
                    if(contactslist.get(i).getName().startsWith(startwith) ||contactslist.get(i).getName().toLowerCase().startsWith(startwith) || contactslist.get(i).getName().toUpperCase().startsWith(startwith))
                    {
                        filterList.add(contactslist.get(i));

                    }
                }
                adapter=new AppContactListAdapter(c, filterList);
                contactslistview.setAdapter(adapter);
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub
            }
            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
            }
        });
        super.onResume();
    }
    class getContacts extends AsyncTask<Void,Void, Void>{
        String response=null;
        @Override
        protected void onPostExecute(Void result) {

            try {
                if(!((Activity)(c)).isFinishing())

                if(response.equals("1"))
                {

                }
                else if(response.equals("0")){}

                else{
                    if(contactslist==null&&contactslist.size()==0)
                        Toast.makeText(c, "No Contacts Found",2000).show();
                    else{
                        adapter=new AppContactListAdapter(c,contactslist);
                        contactslistview.setAdapter(adapter);
                    }
                }
            } catch (BadTokenException e) {
                // TODO: handle exception
            }
            catch (Exception e) {
                // TODO: handle exception
            }


            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub

            contactslist=new ArrayList<Contacts>();
            try {
                new CommonUtility().show_PDialog(c, "Fetching Contacts..");

            } catch (BadTokenException e) {
                // TODO: handle exception
            }
            catch (Exception e) {
                // TODO: handle exception
            }

            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            response=Apis.getApisInstance(c).getContactsList("","");
            if(response.contains("{"))
            {
                contactslist=new JsonParser().getAllPhone91Contacts(response);
            }
            return null;
        }

    }
}

Solution

  • ListView inside a ScrollView is your exact problem.

    The parent ScrollView will not accept the ListView as its child.You only have to handle the ListView child for your layout xml

    Refer this : How can I put a ListView into a ScrollView without it collapsing? and Android list view inside a scroll view