3

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
    }

}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Arun Abraham
  • 4,011
  • 14
  • 54
  • 75
  • i'd suggest creating your own custom tabs. that way it will be guaranteed to look the same across all devices. the android tabs do not always look the same across different devices – james Apr 06 '11 at 14:03
  • Oh ... Is it ?? I was not aware of it...thanks... – Arun Abraham Apr 06 '11 at 14:09

2 Answers2

14

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..

Venky
  • 11,049
  • 5
  • 49
  • 66
  • Nice One... but if i want to Change the Color of the Ready made Tab Text Color then ???? –  Apr 21 '11 at 09:53
  • @Venky: Nice one. +1 from My side. But now i want to change the Color of the tabBar when it is click or selected then ? – Shreyash Mahajan Sep 13 '12 at 06:45
0

TabActivity is Deprecated from Android 4.2,API level 17. Use Fragments instead of TabActivity

Gowtham Kumar
  • 534
  • 8
  • 22