How to access view inside tabhost? - android

Not able to access view inside tabhost.
I want to access myTitle layout as well as myTitleText and change their attributes programatically.
tried using findviewbyid and view.findviewbyid but gets null.
tried inflator but attributes remain unchanged.
Please help
<?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" >
<LinearLayout
android:id="#+id/layout_myTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="#+id/title_myTitleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TITLE" />
</LinearLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip" android:layout_weight="1.0"/>
</LinearLayout>
</TabHost>

For those who are searching for a answer on how to access a view inside TabHost do the following.
Let you have an XML like:
<LinearLayout ...
<TabHost
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Text 1 -->
<TextView
android:id="#+id/text1"
android:text="Some text 1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Text 2 -->
<TextView
android:id="#+id/text2"
android:text="Some text 2"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
If you want to access text2, you should first access to a container (tab2).
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
public void onTabChanged(String tabId) {
Log.i(TAG, "tabId = " + tabId);
if (tabId.equals("tab2")) {
LinearLayout layout = (LinearLayout) findViewById(R.id.tab2);
TextView text = (TextView) layout.findViewById(R.id.text2);
}
}
});

done using tabHost
TextView tv = (TextView) tabHost.findViewById(R.id.title_myTitleText);

Related

ListView Complications

Because the information in each tab may not display depending on screen size, i have added a scrollview. The list view displays the information from the database alright but then it does not spread across the screen. It takes only a small portion of the screen and I can only scroll in that small portion. If i have 2 sets on information I expect to see two rows but I see just one and I can scroll within that to see the second set of information. But when i remove the scroll view my listview displays like it is supposed to but then the other tabs suffer because i cannot scroll to see the other fields.
Below is my main activity:
<TabHost
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/tabHost">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"></TabWidget>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
android:fillViewport="true">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!--Overview Tab Starts-->
<LinearLayout
android:id="#+id/overviewTab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="#+id/displayListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<!--Overview Tab ends-->
<!--Base Data Tab starts-->
<LinearLayout
android:id="#+id/baseDataTab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="15dp">
//the needed views go here//
</LinearLayout>
<!--Base Data Tab ends-->
<!--Task Tab starts-->
<LinearLayout
android:id="#+id/taskTab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="15dp">
//the needed views go here//
</LinearLayout>
<!--Task Tab ends-->
<!--Survey Tab starts-->
<LinearLayout
android:id="#+id/surveyTab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="15dp">
//the needed views go here//
</LinearLayout>
<!--Survey Tab ends-->
</FrameLayout>
</ScrollView>
</LinearLayout>
</TabHost>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabHost"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- Overview Tab Starts -->
<LinearLayout
android:id="#+id/overviewTab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/displayListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<!-- Overview Tab ends -->
<!-- Base Data Tab starts -->
<LinearLayout
android:id="#+id/baseDataTab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="15dp" >
</LinearLayout>
<!-- Base Data Tab ends -->
<!-- Task Tab starts -->
<LinearLayout
android:id="#+id/taskTab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="15dp" >
</LinearLayout>
<!-- Task Tab ends -->
<!-- Survey Tab starts -->
<LinearLayout
android:id="#+id/surveyTab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="15dp" >
</LinearLayout>
<!-- Survey Tab ends -->
</FrameLayout>
</ScrollView>
</LinearLayout>
You had some problems with your layout_height and width attributes. I changed some of them, this should work ;-)

Problems with tabHost, buttons and vertical scrolls

I am doing an aplication and I need a tabhost with vertical scrollview in all the tabs, and one of them also need to have a button which bring us to another layout.
The thing is that all the tabwidgets are displayed properly but it doesn't work the scrolling in any of the tabs. And the button doesn´t work at all... I tried to debug and I checked that even the buttons's methon on onclick is never called when I press the button
I am desperate, I need a solution, I've already lost a lot of time here.
This is the most important part of the xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabHost
android:id="#+id/TabHost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/tab5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="30dp"
android:text="Logic expression entered:"/>
<TextView
android:id="#+id/textViewE2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:layout_marginTop="10dp"
android:layout_marginLeft="30dp" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/tab4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="30dp"
android:text="Logic expression reduced:"/>
<TextView
android:id="#+id/textViewD2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:layout_marginTop="10dp"
android:layout_marginLeft="30dp" />
<Button
android:id="#+id/buttonD1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="See the esquematic"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
And this is the important code of the Java class
public class Traduccion extends Activity{
String estado_expresionlogica ="A*B*C+A*¬B*C";
TextView tvD2,tvE2;
String sfuncionreducida;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.traduccion);
Resources res =getResources();
TabHost tabs = (TabHost)findViewById(R.id.TabHost);
//Tab 5
tabs.setup();
TabHost.TabSpec spec5 =tabs.newTabSpec("Tab 5");
spec5.setContent(R.id.tab5);
spec5.setIndicator("", res.getDrawable(android.R.drawable.ic_lock_idle_alarm));
//charging the textview needed
this.tvE2=(TextView) findViewById(R.id.textViewE2);
tvE2.setText(estado_expresionlogica);
//Tab 4
tabs.setup();
TabHost.TabSpec spec4 =tabs.newTabSpec("Tab 4");
spec4.setContent(R.id.tab4);
spec4.setIndicator("", res.getDrawable(android.R.drawable.ic_lock_idle_alarm));
//Cargar los textviews que sean necesarios para la pestaña
this.tvD2=(TextView) findViewById(R.id.textViewD2);
////////Here is a code to copy the answer on sfuncionreducida
tvD2.setText(sfuncionreducida);
tabs.addTab(spec5);
tabs.addTab(spec4);
}
public void Esquematico(View v){
Intent i = new Intent (Traduccion.this, ReducidaEsquematico.class);
startActivity(i);
}
}
I wish you can solve at least one of the two problems.. The one with the scrollview or the one with the button!
Thanks a lot.

how to set scroll bar for tab host

i want to set scroll bar in tab host.in tab there is five tab but i want to display only three
tab at a time and how to make scrollbar for that so tab can be scroll for more tab...please provide me some solution.......
this is m xml file.....
<RelativeLayout
android:id="#+id/rl3"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TabHost
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#drawable/gray" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/layTab"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true"
android:gravity="center" >
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
</HorizontalScrollView>
</RelativeLayout>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" >
<fragment
android:id="#+id/fragment1"
android:name="info.tech.slidermenu.Home_feed"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="50dp" />
<fragment
android:id="#+id/fragment2"
android:name="info.tech.slidermenu.Upcoming_tennise_match"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="50dp" />
<fragment
android:id="#+id/fragment3"
android:name="info.tech.slidermenu.Upcoming_tennise_match"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="50dp" />
<fragment
android:id="#+id/fragment4"
android:name="info.tech.slidermenu.Upcoming_tennise_match"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="50dp" />
<fragment
android:id="#+id/fragment5"
android:name="info.tech.slidermenu.Upcoming_tennise_match"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="50dp" />
</FrameLayout>
</RelativeLayout>
</TabHost>
</RelativeLayout>
this is class file....
public void tabselect() {
TabHost tab = (TabHost) findViewById(R.id.tabhost);
tab.setup();
tab.setHorizontalScrollBarEnabled(true);
TabHost.TabSpec spec = tab.newTabSpec("tab1");
spec.setContent(R.id.fragment1);
spec.setIndicator("Feed");
tab.addTab(spec);
spec = tab.newTabSpec("tab2");
spec.setContent(R.id.fragment2);
spec.setIndicator("tennis");
tab.addTab(spec);
spec = tab.newTabSpec("tab3");
spec.setContent(R.id.fragment3);
spec.setIndicator("boxing");
tab.addTab(spec);
spec = tab.newTabSpec("tab3");
spec.setContent(R.id.fragment4);
spec.setIndicator("cricket");
tab.addTab(spec);
spec = tab.newTabSpec("tab3");
spec.setContent(R.id.fragment5);
spec.setIndicator("base ball");
tab.addTab(spec);
tab.setCurrentTab(0);
}
screen shot....
You're already using a HorizontalScrollView for the TabWidget, so this is the correct way. If you're not able to scrool despite doing is this way, probably you're having a layout problem.
You didn't detail if you're using a custom layout for your TabWidget, but if not, this would be a good approach to achieve what you want, since there's not a method to say "I just want to show 3 tabs at a time".
Simply define a new layout for your tabs so the three together fit the screen's width, the rest will be reachable by scrolling.
---- EDIT ----
I see pretty much LinearLayouts in your code. Try something like this:
<LinearLayout
android:id="#+id/TabContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="99"
android:orientation="vertical">
<TabHost
android:id="#+android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/TabLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<TabWidget
android:id="#+android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TabWidget>
</HorizontalScrollView>
<FrameLayout
android:id="#+android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp" />
</LinearLayout>
</TabHost>
</LinearLayout>

How to get access to view that is located outside of FrameLayout of TabHost?

When an Activity X is loaded into FrameLayout when tab is changed I want that X to be able access to tmp LinearLayout that is located outside of TabHost.
Assume that TmpActivity is loaded into FrameLayout when some tab was clicked, so in that activity I want do something like this
public void onCreate(Bundle savedInstanceState) {
LinearLayout ll = (LinearLayout)findViewById(R.id.tmp);
}
How I can do this?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/tmp"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="50px"
android:background="#00FF00">
</LinearLayout>
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="500px"
android:padding="5dp"
android:background="#FF0000"/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FF00FF" />
</LinearLayout>
</TabHost>
</LinearLayout>
You can get your parent activity from your activity loaded in your tab host frame layout. From there, you can access your linear layout throught a getter or a member variable located in your TabHostActivity.

Why is this TextView showing up on both of my TabPages? It should only be on the first

I have a TextView that is only supposed to show up on the first TabPage (which contains the ScrollView), but for some reason it shows up on both of my TabPages, whereas the TableLayout only shows up in the first TabPage like it should. I have no idea why...here is my XML file: The TextView in question is the 'primaryInfo' in the ScrollView.
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabHost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TabWidget
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs">
</TabWidget>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#android:id/tabcontent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Primary Info"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:gravity="center_horizontal"
android:id="#+id/piTextView"
android:background="#595959"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/specsTab"
android:layout_centerHorizontal="true"/>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/photosTab">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/thumbnailGallery"
android:id="#+id/selectedPhoto"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/imageProgressLayout"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#android:style/Widget.ProgressBar.Small"
android:indeterminate="true"
android:layout_marginTop="5dp"
android_layout_marginLeft="20dp"
android_layout_marginBottom="20dp"
android:id="#+id/galleryProgress"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/loadingTextView"
android:text="Loading images..."
android:layout_marginLeft="5dp"
android:layout_centerVertical="true"
android:textSize="13sp"/>
</LinearLayout>
<Gallery
android:id="#+id/thumbnailGallery"
android:layout_above="#+id/galleryProgress"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content"/>
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
edit
Tab setup code:
public void tabSetup()
{
TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
tabHost.setup();
TextView tv1 = new TextView(this);
tv1.setText("Specs");
tv1.setHeight(50);
TextView tv2 = new TextView(this);
tv2.setText("Photos");
tv2.setHeight(50);
TabSpec spec1=tabHost.newTabSpec("Specs");
spec1.setContent(R.id.specsTab);
spec1.setIndicator("Specs");
TabSpec spec2=tabHost.newTabSpec("Photos");
spec2.setIndicator("Photos");
spec2.setContent(R.id.photosTab);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
}
Well, the problem was that when I changed the layout, I forgot to change the View that I passed into the setContent method since the top level view for that tab page changed. Here is what it was before I changed the layout (and it worked originally):
TabSpec spec1=tabHost.newTabSpec("Specs");
spec1.setContent(R.id.specsTab);
spec1.setIndicator("Specs");
and this is what it needed to be with the current layout that I posted above:
TabSpec spec1=tabHost.newTabSpec("Specs");
spec1.setContent(R.id.scrollView);
spec1.setIndicator("Specs");
Problem solved. Credit to Jack for getting me to post the code since that is when I finally noticed it ;)

Categories

Resources