Android UI TabActivity issue
Asked Answered
A

2

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
    }

}
Angi answered 6/4, 2011 at 13:55 Comment(2)
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 devicesGobble
Oh ... Is it ?? I was not aware of it...thanks...Angi
S
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..

Sst answered 6/4, 2011 at 14:16 Comment(2)
Nice One... but if i want to Change the Color of the Ready made Tab Text Color then ????Histiocyte
@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 ?Imputable
R
0

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

Runthrough answered 28/8, 2013 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.