public
Created

  • Download Gist
R.layout.views_testtabwidget
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
<?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>
SpecialScroll
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
// 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);
}
 
}
 
}
TestTabWidget.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
// 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);
}
 
}

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?

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.