Create a gist now

Instantly share code, notes, and snippets.

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/arrow_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="@drawable/previous"
android:visibility="gone" />
<ImageView
android:id="@+id/arrow_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/next" />
<com.luksprog.droidtests.views.SpecialScroll
android:id="@+id/my_scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none"
android:layout_toRightOf="@id/arrow_left"
android:layout_toLeftOf="@id/arrow_right">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:tabStripEnabled="true" />
</com.luksprog.droidtests.views.SpecialScroll>
</RelativeLayout>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="65dip" >
<RelativeLayout
android:id="@+id/myTabContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dip" >
<TableLayout
android:id="@+id/tabSubs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginTop="20dip"
android:gravity="center_horizontal" >
<Button
android:id="@+id/btntestno1"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 1"
android:textColor="#000000" />
<Button
android:id="@+id/btntestno2"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 2"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:gravity="center_horizontal" >
<Button
android:id="@+id/btntestno3"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 3"
android:textColor="#000000" />
<Button
android:id="@+id/btntestno4"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 4"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:gravity="center_horizontal" >
<Button
android:id="@+id/btntestno5"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 5"
android:textColor="#000000" />
<Button
android:id="@+id/btntestno6"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 6"
android:textColor="#000000" />
</TableRow>
</TableLayout>
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
// Our special HorizontalScrollView
public class SpecialScroll extends HorizontalScrollView {
public interface PositionListener {
public void onLeftArrowRequired(boolean required);
public void onRightArrowRequired(boolean required);
public View implementScrolledView();
}
private PositionListener listener;
public void setPositionListener(PositionListener listener) {
this.listener = listener;
}
public SpecialScroll(Context context) {
super(context);
}
public SpecialScroll(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public SpecialScroll(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (l == 0) {
listener.onLeftArrowRequired(false);
} else {
listener.onLeftArrowRequired(true);
}
View v = listener.implementScrolledView();
Rect r = new Rect();
v.getDrawingRect(r);
if ((r.width() - l) == getWidth()) {
listener.onRightArrowRequired(false);
} else {
listener.onRightArrowRequired(true);
}
}
}
// The class that uses that implements the interface
public class TestTabWidget extends TabActivity implements TabContentFactory,
PositionListener {
private TabHost tabs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.views_testtabwidget);
SpecialScroll hsv = (SpecialScroll) findViewById(R.id.my_scrollView);
hsv.setPositionListener(this);
tabs = this.getTabHost();
tabs.addTab(tabs.newTabSpec("tag1")
.setIndicator("Really long indictor1").setContent(this));
tabs.addTab(tabs.newTabSpec("tag1")
.setIndicator("Really long indictor2").setContent(this));
tabs.addTab(tabs.newTabSpec("tag1")
.setIndicator("Really long indictor3").setContent(this));
tabs.addTab(tabs.newTabSpec("tag1")
.setIndicator("Really long indictor4").setContent(this));
tabs.addTab(tabs.newTabSpec("tag1")
.setIndicator("Really long indictor5").setContent(this));
tabs.addTab(tabs.newTabSpec("tag1")
.setIndicator("Really long indictor6").setContent(this));
tabs.addTab(tabs.newTabSpec("tag1")
.setIndicator("Really long indictor7").setContent(this));
tabs.addTab(tabs.newTabSpec("tag1")
.setIndicator("Really long indictor8").setContent(this));
}
@Override
public View createTabContent(String tag) {
Button b = new Button(this);
b.setText(tag);
return b;
}
@Override
public void onLeftArrowRequired(boolean required) {
if (required) {
((ImageView) findViewById(R.id.arrow_left))
.setVisibility(View.VISIBLE);
} else {
((ImageView) findViewById(R.id.arrow_left))
.setVisibility(View.GONE);
}
}
@Override
public void onRightArrowRequired(boolean required) {
if (required) {
((ImageView) findViewById(R.id.arrow_right))
.setVisibility(View.VISIBLE);
} else {
((ImageView) findViewById(R.id.arrow_right))
.setVisibility(View.GONE);
}
}
@Override
public View implementScrolledView() {
return findViewById(android.R.id.tabs);
}
}
@harshawardhan

Hey this is a great piece of code. I've the same scenario though there's a slight change. I'm setting a width for my tabs manually. All I'm doing is this:

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();

and then,

tabHost.getTabWidget().getChildAt(0).getLayoutParams().width = width/4;

for each child. Now I tried modifying SpecialView.onScrollChanged (Which I suppose should be adapted) but couldn't get it to work properly. Could you please help me with this?

@harshawardhan

I'm sorry about this. I just cleaned, built and ran again. And it worked!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment