Pages

Subscribe:

Labels

Wednesday, 28 September 2011

Dynamically change the ListView height - Android

This post is used to change the ListView height. This is done depends on the childviews .
 This Utility.java class is like this


 import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.widget.ListAdapter;
import android.widget.ListView;

public class Utility {
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
}



In the main Acticity use this Utility class to change the listview height.

 phraseListView=(ListView)findViewById(R.id.phrase_listview);
 phraseAdapter=new PhraseListAdapter(this);
phraseListView.setAdapter(phraseAdapter);
Utility.setListViewHeightBasedOnChildren(phraseListView);
       

Android Custom Tabs.

In this post I customized the android TabWidget color,background,border and many of the TabWidget properties .To do is myself I used the this tutorial.Based on that tutorial I done the modifications in my project.

The xml layout file for the Tabs as like this

        <TabHost
                android:id="@android:id/tabhost"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:visibility="visible">
                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">
                    <TabWidget
                        android:id="@android:id/tabs"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:background="#000000"
                        android:padding="1dip"/>
                    <FrameLayout
                        android:id="@android:id/tabcontent"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content">
                        <ListView
                            android:id="@+id/email_listview"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:dividerHeight="2dip"
                            android:layout_marginTop="2dip"
                            android:layout_marginBottom="2dip"
                            android:background="@drawable/my_border"
                            android:scrollbars="vertical"
                            android:cacheColorHint="#bebebe"
                            android:scrollbarSize="35dip"
                            android:scrollingCache="true"/>
                          <ListView
                            android:id="@+id/domain_listview"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:dividerHeight="2dip"
                            android:layout_marginTop="2dip"
                            android:layout_marginBottom="2dip"
                            android:background="@drawable/my_border"
                            android:cacheColorHint="#bebebe"
                            android:scrollbars="vertical"
                            android:scrollbarSize="35dip"
                            android:scrollingCache="true"/>
                           <ListView
                            android:id="@+id/phrase_listview"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:dividerHeight="2dip"
                            android:layout_marginTop="2dip"
                            android:layout_marginBottom="2dip"
                            android:background="@drawable/my_border"
                            android:cacheColorHint="#bebebe"
                            android:scrollbars="vertical"
                            android:scrollbarSize="35dip"
                            android:scrollingCache="true"/>
                         
                    </FrameLayout>
                </LinearLayout>
            </TabHost>



Define the my_border.xml drawable file under the drawable folder. This is used to define the custom drawables (here is for custom border for listview.


<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="2dp" android:color="#FFFF00" />
    <padding android:left="2dp" android:top="0dp"
            android:right="2dp" android:bottom="0dp" />
    <solid android:color="#bebebe" />
    <corners android:radius="4dp" />
</shape>



In the main Activity write java code like this.



private TabHost tabhost;
display = getWindowManager().getDefaultDisplay();
width = display.getWidth()/5+2;
height = display.getHeight();
tabhost=(TabHost)findViewById(android.R.id.tabhost);
tabhost.setup();

//This is the devider drawable between to widgets.
tabhost.getTabWidget().setDividerDrawable(R.drawable.devider);

TabHost.TabSpec spec;
       
        View view1 = getLayoutInflater().inflate(R.layout.tabs_bg,null);
        TextView tv1 = (TextView) view1.findViewById(R.id.tabsText);
        tv1.setText("Email");
        spec = tabhost.newTabSpec("email").setIndicator(tv1);
        spec.setContent(R.id.email_listview);
        tabhost.addTab(spec);
       
        View view = getLayoutInflater().inflate(R.layout.tabs_bg,null);
        TextView tv = (TextView) view.findViewById(R.id.tabsText);
        tv.setText("Domain");
        spec = tabhost.newTabSpec("domain").setIndicator(tv);
        spec.setContent(R.id.domain_listview);
        tabhost.addTab(spec);
       
        View view2 = getLayoutInflater().inflate(R.layout.tabs_bg,null);
        TextView tv2 = (TextView) view2.findViewById(R.id.tabsText);
        tv2.setText("Phrase");
        spec = tabhost.newTabSpec("phrase").setIndicator(tv2);
        spec.setContent(R.id.phrase_listview);
        tabhost.addTab(spec);
       
        tabhost.setCurrentTab(0);
        tabhost.getTabWidget().getChildAt(0).getLayoutParams().height = 60;
        tabhost.getTabWidget().getChildAt(1).getLayoutParams().height = 60;
        tabhost.getTabWidget().getChildAt(2).getLayoutParams().height = 60;
       

The devider.xml file is as follows res/drawable.devider.xml



<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#000000" android:centerColor="#000000"
        android:endColor="#000000"    android:angle="-90" />
    <size android:width="1dip" />
</shape>



tabs_bg.xml layout file defines the layout for the tabwidget. res/layout/tabs_bg.xml



<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabsText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Title"
    android:background="@drawable/tab_bg_selector"
    android:textSize="15dip"
    android:textColor="@drawable/tab_text_selector"
    android:layout_margin="100dip"/>

   
The background and the textcolor selector files are defined like this under the res/drawable folder.





tab_bg_selector.xml


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!--  Active tab -->
    <item android:state_selected="true" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/tab_bg_selected" />
    <!--  Inactive tab -->
    <item android:state_selected="false" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/tab_bg_unselected" />
    <!--  Pressed tab -->
    <item android:state_pressed="true" android:drawable="@drawable/tab_bg_press" />
    <!--  Selected tab (using d-pad) -->
    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@color/transparent" />
</selector>







tab_text_selector.xml


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@android:color/black" />
    <item android:state_focused="true" android:color="@android:color/white" />
    <item android:state_pressed="true" android:color="@android:color/white" />
    <item android:color="#f8f8f8" />
</selector>



tab_bg_seleced.xml under res/drawable folder


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#ffffff" android:centerColor="#f2f2f2"
        android:endColor="#d8d8d8" android:angle="-90" />
    <stroke android:width="0dip" android:color="#000000" />
</shape>





tab_bg_unseleted.xml under res/drawable folder


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#5C5C5C" android:centerColor="#424242"
        android:endColor="#222222"    android:angle="-90" />
    <stroke android:width="0dip" android:color="#000000" />
</shape>



tab_bg_press.xml under the res/drawable folder



<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" android:color="#D9D900">
    <gradient android:startColor="#D9D900" android:centerColor="#D9D900"
        android:endColor="#D9D900" android:angle="-90" />
    <stroke android:width="1dp" android:color="#000000" />
</shape>





colors.xml under the res/values folder is like this ..



<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="transparent">#045645</color>
</resources>

             

Get Files from SDcard

To do this we need to get the root directory of the external storage directory, i.e SDcard.

File f = new File("/sdcard");
private List<String> item = new ArrayList<String>();
private List<String> path = new ArrayList<String>();
getFiles(f);
The getFiles(..) method retrieves the all files from the sdcard and stores
    

private void getFiles(File f)
    {
        File[] files=f.listFiles();
        for (int i = 0; i < files.length; i++)
        {
            try{
                if (files[i].isDirectory()) {
                    getFiles(files[i]);
                }
                else
                {
                    if (files[i].isFile() ) {
                        if(files[i].getParent().equals("/sdcard/DCIM/.thumbnails"))
                            continue;
                        String test=files[i].getName().toLowerCase();
                        if(test.endsWith(".mp3")||test.endsWith(".mp4")||test.endsWith(".3gp")||test.endsWith(".wav")||test.endsWith("flac")||test.endsWith("xmf")||test.endsWith("ota"))
                        {
                            path.add(files[i].getPath());
                            item.add(files[i].getName());
                        }
                    }
                }
            }catch(Exception e){
                 e.getMessage();
            }
        }   
    }//get files