androidandroid-layouttabactivity

Android UI TabActivity issue


I am trying to implement the following background for the application... enter image description here

For the background image(application background) ... i am setting the image in setContentView(layout)... by adding this line, i am getting a runtime exception...

if i set this background in the subactivities..i wont get the background to fill the full application background.. any idea whats the alternative?

public class HMITabActivity extends TabActivity{
    private TabHost tabHost = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.background);
        tabHost = getTabHost();
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                setTabHostColors();
            }
        });
        tabHost.addTab(tabHost.newTabSpec("Tasks")
                .setIndicator("Tasks", getResources().getDrawable(R.drawable.icon_task))
                .setContent(new Intent(this, Tasks.class)));
        tabHost.addTab(tabHost.newTabSpec("HMI")
                .setIndicator("HMI", getResources().getDrawable(R.drawable.icon_hmi))
                .setContent(new Intent(this, HMI.class)));
        tabHost.addTab(tabHost.newTabSpec("Diagnostics")
                .setIndicator("Diagnostics", getResources().getDrawable(R.drawable.icon_diagnostics))
                .setContent(new Intent(this, Diagnostics.class)));
        tabHost.addTab(tabHost.newTabSpec("About")
                .setIndicator("About", getResources().getDrawable(R.drawable.icon_info))
                .setContent(new Intent(this, Tasks.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

        Intent intent = new Intent(BackgroundService.class.getName());
        startService(intent); 
    }

    private void setTabHostColors() {
        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) {
            tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.rgb(1, 1, 1)); //unselected
        }
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.rgb(50, 120, 160)); // selected
    }

}

Solution

  • For this you must use Custom Tabs ,here is the Code try this :

      tabHost= getTabHost();
      tabHost.addTab(tabHost.newTabSpec("tab1").setContent(new Intent(this, Activity2.class)).setIndicator(prepareTabView("Names",R.drawable.icon)));
    

    where prepareTabView is method that Inflate View. Then Inflate a view like this :

        private View prepareTabView(String text, int resId) {
             View view = LayoutInflater.from(this).inflate(R.layout.tabs, null);
             ImageView iv = (ImageView) view.findViewById(R.id.TabImageView);
             TextView tv = (TextView) view.findViewById(R.id.TabTextView);
             iv.setImageResource(resId);
             tv.setText(text);
             return view;
        }
    

    Where tabs XML will look like this :

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:id="@+id/TabLayout" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:padding="5dip">
    
    <ImageView android:id="@+id/TabImageView" android:src="@drawable/icon"           
     android:layout_width="wrap_content" android:layout_height="wrap_content"/>
    
    <TextView android:id="@+id/TabTextView" android:text="Text" 
    android:paddingTop="5dip" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:textColor="@color/black" 
    android:textAppearance="@style/TabTextViewStyle" />
    
     </LinearLayout>
    

    Then now add your backgroung color as you like..