Problem with TabActivity for Android UI

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

For a background image (application background) ... I set the image to setContentView (layout) ... adding this line, I get an exception at runtime ...

if I set this background in subactivities .. I don’t get a background to fill in the full background of the application .. any idea what is 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 } } 
+3
source share
2 answers

For this you should use custom tabs, here is a sample code:

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

where prepareTabView is the method that inflates the view. Then overlay the view as follows:

  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; } 

If the XML tabs 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 add your back color as you like.

+14
source

TabActivity deprecated with Android 4.2, API level 17. Use Fragments instead of TabActivity

0
source

Source: https://habr.com/ru/post/915968/


All Articles