I have a TabView in one of my classes. Now I want to place a custom actionbar above the tabview. I'm using ActionBarSherlock for this. So now my problem is that I should extend SherlockActivity in order to use Actionbar. But I need to extend TabActivity for achieving Tabs. How can I fix this problem? Somebody please help.
This is my class.
public class ActivityRestaurantMenuListTab extends TabActivity {
TabHost tabHost;
HorizontalScrollView mHorizontalScrollView;
int tabnum;
String RestaurantName="Restaurant 1";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.restaurant_menu_list_tab);
ActionBar actionBar = getSupportActionBar();
View actionBarView = getLayoutInflater().inflate(
R.layout.custom_actionbar_restaurant, null);
ImageView back = (ImageView) actionBarView.findViewById(R.id.btn_back);
ImageView table = (ImageView) findViewById(R.id.btn_table);
ImageView basket = (ImageView) findViewById(R.id.btn_basket);
TextView tableno = (TextView) findViewById(R.id.txt_table_no);
TextView basketitems = (TextView) findViewById(R.id.txt_basket_items);
TextView actionBarTitle = (TextView) actionBarView
.findViewById(R.id.txt_title);
actionBarTitle.setText(RestaurantName);
basket.setVisibility(View.VISIBLE);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
actionBar.setCustomView(actionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
Intent intent = getIntent();
tabnum = intent.getIntExtra("category", 0);
Toast.makeText(getApplicationContext(), "Selected tab is " + tabnum,
Toast.LENGTH_SHORT).show();
tabHost = (TabHost) findViewById(android.R.id.tabhost);
mHorizontalScrollView = (HorizontalScrollView) findViewById(R.id.hs);
TabWidget widget = tabHost.getTabWidget();
TabSpec tab1 = tabHost.newTabSpec("Item 1");
TabSpec tab2 = tabHost.newTabSpec("Item 2");
TabSpec tab3 = tabHost.newTabSpec("Item 3");
TabSpec tab4 = tabHost.newTabSpec("Item 4");
tab1.setIndicator("Item 1");
tab1.setContent(new Intent(this, Activity.class));
tab2.setIndicator("Item 2");
tab2.setContent(new Intent(this, Activity.class));
tab3.setIndicator("Item 3");
tab3.setContent(new Intent(this, Activity.class));
tab4.setIndicator("Item 4");
tab4.setContent(new Intent(this, Activity.class));
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
tabHost.addTab(tab4);
float scale = getResources().getDisplayMetrics().density;
final double tabWidth = (int) (150 * scale + 0.5f);
for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
tabHost.getTabWidget().getChildTabViewAt(i).getLayoutParams().width = (int) tabWidth;
}
final double screenWidth = getWindowManager().getDefaultDisplay()
.getWidth();
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
int nrOfShownCompleteTabs = ((int) (Math.floor(screenWidth
/ tabWidth) - 1) / 2) * 2;
int remainingSpace = (int) ((screenWidth - tabWidth - (tabWidth * nrOfShownCompleteTabs)) / 2);
int a = (int) (tabHost.getCurrentTab() * tabWidth);
int b = (int) ((int) (nrOfShownCompleteTabs / 2) * tabWidth);
int offset = (a - b) - remainingSpace;
mHorizontalScrollView.scrollTo(offset, 0);
}
});
for (int i = 0; i < widget.getChildCount(); i++) {
View v = widget.getChildAt(i);
TextView tv = (TextView) v.findViewById(android.R.id.title);
tv.setAllCaps(false);
tv.setTextSize(15);
if (tv == null) {
continue;
}
tabHost.setCurrentTab(tabnum);
v.setBackgroundResource(R.drawable.tabselector);
tabHost.getTabWidget().setDividerDrawable(null);
}
}
}
try to add a Relativelayout or linear layout with width match parent and height wrap content just above the tabview in the xml file we can give backgroundcolor,we can place icons inside this top dummyactionbar layout and can avoid extending Sherlock activity
sample xml file can be
<RelativeLayout
android:layout_height="@dimen/titlebarsize"
android:layout_width="match_parent"
android:background="@color/red"
android:layout_alignParentTop="true"
android:id="@+id/topbar"
>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/title"
android:text="@string/steptwotitle"
style="@style/actionbarphonetextviewstyle"
/>
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/back"
android:id="@+id/back"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_centerVertical="true"
android:background="@null"
android:visibility="gone"
/>
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/sidemenutest"
android:id="@+id/sidemenu"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:layout_centerVertical="true"
android:background="@null"
/>
</RelativeLayout>
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="@+id/topbar"
android:layout_centerHorizontal="true"
>
class code
protected void onCreate(Bundle savedInstanceState) {
back=(ImageButton)findViewById(R.id.back);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent in=new Intent(CommentsPage.this,Playmyfile.class);
startActivity(in);
}
});
}
in manifest inside your activity tag add
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"