How to add sliding drawer and tab widget together? - android

I'm new to android. I want to add sliding drawer and tab widget together. Can Any one help me??
This is the app i want to create
I tried to combine following two components.
Sliding drawer=>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- The main content view -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/drawer_text" ></TextView>
</RelativeLayout>
<!-- The navigation drawer -->
<ListView
android:id="#+id/drawer"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#F3F3F4"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" ></ListView>
</android.support.v4.widget.DrawerLayout>
Tab Widget code=>
<?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:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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"/>
</LinearLayout>
</TabHost>
Can i combine this two?
This is the activity class
public class TestTab extends TabActivity implements OnTabChangeListener {
TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_drawer);
// Get TabHost Refference
tabHost = getTabHost();
// Set TabChangeListener called when tab changed
tabHost.setOnTabChangedListener(this);
TabHost.TabSpec spec;
Intent intent;
/************* TAB1 ************/
// Create Intents to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Favourite.class);
spec = tabHost.newTabSpec("First").setIndicator("")
.setContent(intent);
//Add intent to tab
tabHost.addTab(spec);
/************* TAB2 ************/
intent = new Intent().setClass(this, Offers.class);
spec = tabHost.newTabSpec("Second").setIndicator("")
.setContent(intent);
tabHost.addTab(spec);
/************* TAB3 ************/
intent = new Intent().setClass(this, NearBy.class);
spec = tabHost.newTabSpec("Third").setIndicator("")
.setContent(intent);
tabHost.addTab(spec);
// Set drawable images to tab
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.offertwo);
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.nearbytwo);
// Set Tab1 as Default tab and change image
tabHost.getTabWidget().setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.favoritetwo);
}
#Override
public void onTabChanged(String tabId) {
/************ Called when tab changed *************/
//********* Check current selected tab and change according images *******/
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
if(i==0)
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.favoritetwo);
else if(i==1)
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.offertwo);
else if(i==2)
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.nearbytwo);
}
Log.i("tabs", "CurrentTab: "+tabHost.getCurrentTab());
if(tabHost.getCurrentTab()==0)
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.favourite);
else if(tabHost.getCurrentTab()==1)
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.offers);
else if(tabHost.getCurrentTab()==2)
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.nearby);
}

You can put Tab host widget in content part of Drawer i.e. in relative view
try this xml :
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- The main content view -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<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">
<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"/>
</LinearLayout>
</TabHost>
</RelativeLayout>
<ListView
android:id="#+id/drawer"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#F3F3F4"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" ></ListView>
</android.support.v4.widget.DrawerLayout>

Try this. its working on my app perfectly. I m showing a listview inside the sliding drawyer.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relTapToCapture"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:visibility="visible" >
<SlidingDrawer
android:id="#+id/drawer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:content="#+id/content"
android:handle="#+id/handle" >
<ImageView
android:id="#+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sliding_handle" />
<RelativeLayout
android:id="#+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/listView_places"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/list_bg1"
android:divider="#drawable/border_left"
android:dividerHeight="1dp"
android:drawSelectorOnTop="true" >
</ListView>
</RelativeLayout>
</SlidingDrawer>
</FrameLayout>
</RelativeLayout>

Related

Something is wrong with xml Tabs

This is my Tabs in APP
First Tab
Second Tab
What am I doing wrong?
My activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:srcCompat="#drawable/header_logo" />
<TabHost
android:id="#android: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">
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
My first activity xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mapbox.mapboxsdk.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="59.908008"
mapbox:mapbox_cameraTargetLng="30.319100"
mapbox:mapbox_cameraZoom="8"/>
</FrameLayout>
And second activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:srcCompat="#drawable/mapbox_compass_icon" />
<TextView
android:id="#+id/idtest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Информация"></TextView>
</LinearLayout>
I need the design to look like this
How i see it in Android Studio
I don’t understand where the problem is, I created everything from the lesson in the Russian forum (I am Russian-speaking)
Your xml file is ready
next Put this code in MainActivity.java file for adding Tab in TabHost
//Assign id to Tabhost.
TabHostWindow = (TabHost) findViewById(android.R.id.tabhost);
//Creating tab menu.
TabSpec TabMenu1 = TabHostWindow.newTabSpec("First tab");
TabSpec TabMenu2 = TabHostWindow.newTabSpec("Second Tab");
//Setting up tab 1 name.
TabMenu1.setIndicator("Tab1");
//Set tab 1 activity to tab 1 menu.
TabMenu1.setContent(new Intent(this, TabActivity_1.class));
//Setting up tab 2 name.
TabMenu2.setIndicator("Tab2");
//Set tab 3 activity to tab 1 menu.
TabMenu2.setContent(new Intent(this, TabActivity_2.class));
TabHostWindow.addTab(TabMenu1);
TabHostWindow.addTab(TabMenu2);
for more info visit this
You just need to add a separate layout file which includes the tabs and your actionBar

How to add seperate layout and class to TabHost in android?

My base layout like follow
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="lk.abanservice.shankan.abansserviceapp.CheckStatus">
<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:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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">
<LinearLayout
android:id="#+id/checkSerach"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="#+id/checkDisplayAll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
so i need to add following separate file and class to this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title_request"
android:id="#+id/textViewTitleRequestt"
android:textStyle="bold" />
i tried several ways to do this it can be do using TabActivity but that was removed from android 11. anyone has solution for that?
I want to develop following interfaces
This is Tab Host Example.. Change variables and class name according to your requirements...
MainActivity.java
public class MainActivity extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tab = getTabHost();
TabSpec news = tab.newTabSpec("New");
news.setIndicator("Check New Job");
Intent intent = new Intent(MainActivity.this,First.class);
news.setContent(intent);
TabSpec old = tab.newTabSpec("Old");
old.setIndicator("Check Old Job");
Intent intent_old = new Intent(MainActivity.this,Second.class);
old.setContent(intent_old);
tab.addTab(news);
tab.addTab(old);
}
}
activity_main.java
<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" >
<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" >
</FrameLayout>
</LinearLayout>
</TabHost>

ActionBar not Displaying

I had created a project with three tabs in android using TabHost and TabSpec.I extend my
main activity with TabActivity The problem is ActionBar didn't display on the app.I used the Holo.Light.DarkActionBar theme.
MainActivity.java
public class MainActivity extends TabActivity {
TabHost tabhost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabhost=getTabHost();
TabHost.TabSpec homespec=tabhost.newTabSpec("Home");
homespec.setIndicator("Home",null);
Intent homeintent=new Intent(this,HomeActivity.class);
homespec.setContent(homeintent);
TabHost.TabSpec eventspec=tabhost.newTabSpec("Event");
eventspec.setIndicator("Event");
Intent eventintent=new Intent(this,EventActivity.class);
eventspec.setContent(eventintent);
TabHost.TabSpec profilespec=tabhost.newTabSpec("Profile");
profilespec.setIndicator("Profile");
Intent profileintent=new Intent(this,ProfileActivity.class);
profilespec.setContent(profileintent);
tabhost.addTab(homespec);
tabhost.addTab(eventspec);
tabhost.addTab(profilespec);
}
}
activity_main.xml
`
<TabHost
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#android:id/tabhost"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_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">
<LinearLayout
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
/>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"/>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"/>
</FrameLayout>
</LinearLayout>
</TabHost>
Is there any other way to achieve this Suggest me also?
Set theme for this activity in AndroidManifest.xml:
<activity android:name=".MainActivity"
android:theme="#android:style/Theme.Holo.Light"/>
Go to activity_main and klick to Design -> Theme (on top bar, next to Device name and land-scape) -> Manifest Themes -> and choose your theme.
edit:
add this of course (If you are not intentionally forgotten )
<?xml version="1.0" encoding="utf-8"?>
change your layout like this:
<?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="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_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" >
</FrameLayout>
</LinearLayout>
</TabHost>

TabHost add tabs dynamically

I have a TabHost and I want to create tabs dynamically. I have looked at many examples but they are not working.
TabHost tabHost;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();
TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");
tab1.setIndicator("First Tab");
tab1.setContent(new Intent(this,MainActivity2.class));
tabHost.addTab(tab1);
This is the last thing that I have tried but it does not work. Here is some links that I have considered. I can't find what I am doing wrong.
Example1
Example2
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.LH.tabletres.MainActivity">
<LinearLayout
android:id="#+id/LinearLayout01"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
I think you forget to set android:id="#android:id/tabhost" to your tabhost. like below
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >........
.....
</TabHost>
I think you have to write
<TabHost
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
........
</TabHost>
so change #android:id to #+id
I hope that helps.
Use this code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabHost
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
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="match_parent"
android:layout_height="wrap_content"
android:tabStripEnabled="false">
</TabWidget>
</HorizontalScrollView>
<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"
android:background="#FFFFFF"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
final TabHost tabs = (TabHost) findViewById(R.id.tabhost);
tabs.setup();
tabs.setCurrentTab(0);
for (int i = 0; i < 5; i++) {
TabHost.TabSpec spec1 = tabs.newTabSpec("tag1");
spec1.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
return (new AnalogClock(MainActivity.this));
}
});
spec1.setIndicator("Clock");
tabs.addTab(spec1);
}

Why can't my TabHost child activity find the parent TabHost ID?

I have a TabHost activity that are switching activities, so my main looks like this:
<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" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-4dp"
android:layout_weight="0" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp" />
</LinearLayout>
</TabHost>
And one of my child activities XML is a ListView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
And in this child activity, (which extends ListActivity) I'm trying to find the parent TabHost:
TabHost tabHost = (TabHost) getParent().findViewById(R.id.tabhost);
tabHost.setCurrentTab(0);
But, the R.id.tabhost is not found (tabhost id cannot be resolved).
What am I doing wrong?
Make it android.R.id.tabhost
It will work.

Categories

Resources