How to show tab title fully in android - android

I am newbie in android. I developed an android application that shows 15 tabs. One problem is i can't see each tab's title fully. Is it tabs in the android is scrollable.
I use below code to create tabs. looking for better solution
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TabHost android:id="#android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent" android:id="#+id/linearLayout1" android:layout_height="match_parent" android:orientation="vertical">
<TabWidget android:layout_width="match_parent" android:layout_height="wrap_content" android:id="#android:id/tabs"></TabWidget>
<FrameLayout android:layout_width="match_parent" android:layout_height="500px" android:id="#android:id/tabcontent">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="#+id/tab1">
<TextView android:text="Tab1 Clicked" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="#+id/tab2">
<TextView android:text="Tab2 Clicked" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="#+id/tab3">
<TextView android:text="Tab3 Clicked" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="#+id/tab4">
<TextView android:text="Tab4 Clicked" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="#+id/tab5">
<TextView android:text="Tab5 Clicked" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="#+id/tab6">
<TextView android:text="Tab6 Clicked" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="#+id/tab7">
<TextView android:text="Tab7 Clicked" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="#+id/tab8">
<TextView android:text="Tab8 Clicked" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="#+id/tab9">
<TextView android:text="Tab9 Clicked" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="#+id/tab10">
<TextView android:text="Tab10 Clicked" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="#+id/tab11">
<TextView android:text="Tab11 Clicked" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="#+id/tab12">
<TextView android:text="Tab12 Clicked" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="#+id/tab13">
<TextView android:text="Tab13 Clicked" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="#+id/tab14">
<TextView android:text="Tab14 Clicked" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="#+id/tab15">
<TextView android:text="Tab15 Clicked" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
activity class
package mtx.samples;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class NewTabAppActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final AlertDialog.Builder ad = new AlertDialog.Builder(this);
TabHost tabHost = getTabHost();
// tab1
TabSpec ts1 = tabHost.newTabSpec("Tab1");
ts1.setIndicator("Tab 1", getResources().getDrawable(R.drawable.rss));
ts1.setContent(R.id.tab1);
tabHost.addTab(ts1);
// tab2
TabSpec ts2 = tabHost.newTabSpec("Tab2");
ts2.setIndicator("Tab 2");
ts2.setContent(R.id.tab2);
tabHost.addTab(ts2);
// tab3
TabSpec ts3 = tabHost.newTabSpec("Tab3");
ts3.setIndicator("Tab 3");
ts3.setContent(R.id.tab3);
tabHost.addTab(ts3);
// tab4
TabSpec ts4 = tabHost.newTabSpec("Tab4");
ts4.setIndicator("Tab 4");
ts4.setContent(R.id.tab4);
tabHost.addTab(ts4);
// tab5
TabSpec ts5 = tabHost.newTabSpec("Tab5");
ts5.setIndicator("Tab 5");
ts5.setContent(R.id.tab5);
tabHost.addTab(ts5);
// tab6
TabSpec ts6 = tabHost.newTabSpec("Tab6");
ts6.setIndicator("Tab 6");
ts6.setContent(R.id.tab6);
tabHost.addTab(ts6);
// tab7
TabSpec ts7 = tabHost.newTabSpec("Tab7");
ts7.setIndicator("Tab 7");
ts7.setContent(R.id.tab7);
tabHost.addTab(ts7);
// tab8
TabSpec ts8 = tabHost.newTabSpec("Tab8");
ts8.setIndicator("Tab 8");
ts8.setContent(R.id.tab8);
tabHost.addTab(ts8);
// tab9
TabSpec ts9 = tabHost.newTabSpec("Tab9");
ts9.setIndicator("Tab 9");
ts9.setContent(R.id.tab9);
tabHost.addTab(ts9);
// tab10
TabSpec ts10 = tabHost.newTabSpec("Tab10");
ts10.setIndicator("Tab 10");
ts10.setContent(R.id.tab10);
tabHost.addTab(ts10);
// tab11
TabSpec ts11 = tabHost.newTabSpec("Tab11");
ts11.setIndicator("Tab 11");
ts11.setContent(R.id.tab11);
tabHost.addTab(ts11);
// tab12
TabSpec ts12 = tabHost.newTabSpec("Tab12");
ts12.setIndicator("Tab 12");
ts12.setContent(R.id.tab12);
tabHost.addTab(ts12);
// tab13
TabSpec ts13 = tabHost.newTabSpec("Tab13");
ts13.setIndicator("Tab 13");
ts13.setContent(R.id.tab13);
tabHost.addTab(ts13);
// tab14
TabSpec ts14 = tabHost.newTabSpec("Tab14");
ts14.setIndicator("Tab 14");
ts14.setContent(R.id.tab14);
tabHost.addTab(ts14);
// tab15
TabSpec ts15 = tabHost.newTabSpec("Tab15");
ts15.setIndicator("Tab 15");
ts15.setContent(R.id.tab15);
tabHost.addTab(ts15);
ad.setTitle("NewTabApp");
ad.setNeutralButton("OK", null );
Button btnSendSettings = (Button)findViewById(R.id.btnSendSettings);
btnSendSettings.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
ad.setMessage("Send Settings button Clicked");
ad.show();
}
});
Button btnClose = (Button)findViewById(R.id.btnClose);
btnClose.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
System.exit(-1);
}
});
Button btnSave = (Button)findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ad.setMessage("Save button Clicked");
ad.show();
}
});
Button btnSend = (Button)findViewById(R.id.btnSend);
btnSend.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
ad.setMessage("Send button Clicked");
ad.show();
}
});
}
}

you can make scrolling tabs in android using HorizontalScrollView in your layout xml file .
<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="fill_parent"
android:layout_height="wrap_content"/>
</HorizontalScrollView>
For reference go through the link :
scrolling-tabs-android
Hope this will help you to solve your issue.

Related

Layout of fragment disintegrates when it dynamically replaces framelayout

I have an app with MainActivity that contains a FrameLayout which would be replaced by a fragment based on which item is clicked in the bottom layout. I have a fragment named "bookings" that must appear in the body area when "Bookings" option is clicked. The "bookings" fragment contains a spinner and a button that are placed at the center of screen inside of RelativeLayout. When the fragment's layout is placed directly inside of body of MainActivity, the spinner and button is centered, just the way I want it to be.
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottompanel"
android:orientation="vertical">
<!-- The FrameLayout goes here -->
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Spinner
android:id="#+id/city_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#+id/center_space"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:dropDownVerticalOffset="20dp"
android:dropDownWidth="match_parent"
android:popupBackground="#d5ddea"
android:spinnerMode="dropdown" />
<Space
android:id="#+id/center_space"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_centerInParent="true"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Done"
android:layout_centerHorizontal="true"
android:layout_below="#+id/center_space"
android:onClick="doneButton" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:weightSum="3"
android:layout_weight="0"
android:id="#+id/bottompanel">
<LinearLayout
android:id="#+id/profile_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:clickable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_profile"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/bookings_button"
android:clickable="true"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_bookings"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bookings"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/games_button"
android:clickable="true"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_tournaments"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Games"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
</RelativeLayout>
But when I replace the FrameLayout with the fragment, the spinner disappears and the button moves at the top.
Code in activity_main.xml:
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottompanel"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/fragment_frame"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:weightSum="3"
android:layout_weight="0"
android:id="#+id/bottompanel">
<LinearLayout
android:id="#+id/profile_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:clickable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_profile"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/bookings_button"
android:clickable="true"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_bookings"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bookings"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/games_button"
android:clickable="true"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_tournaments"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Games"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Code in fragment_bookings.xml:
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Spinner
android:id="#+id/city_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#+id/center_space"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:dropDownVerticalOffset="20dp"
android:dropDownWidth="match_parent"
android:popupBackground="#d5ddea"
android:spinnerMode="dropdown" />
<Space
android:id="#+id/center_space"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_centerInParent="true"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Done"
android:layout_centerHorizontal="true"
android:layout_below="#+id/center_space"
android:onClick="doneButton" />
</RelativeLayout>
Code of onCreateView of bookings.java:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_bookings, container, false);
Spinner loc = (Spinner)v.findViewById(R.id.city_spinner);
String[] cities = new String[]{
"Select a City",
"ABC",
"DEF",
"GHI",
"JKL",
"MNO"
};
// Initializing an ArrayAdapter
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this.getActivity(),android.R.layout.simple_spinner_item,cities
);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
loc.setAdapter(spinnerArrayAdapter);
loc.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
L1 = id;
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
return v;
}
Code in MainActivity.java:
package com.example.user.temporary;
import android.app.Activity;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity implements
profile.OnFragmentInteractionListener,
bookings.OnFragmentInteractionListener,
games.OnFragmentInteractionListener {
private Activity myActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.myActivity = this;
LinearLayout profilebtn = (LinearLayout)this.findViewById(R.id.profile_button);
LinearLayout bookingsbtn = (LinearLayout)this.findViewById(R.id.bookings_button);
LinearLayout gamesbtn = (LinearLayout)this.findViewById(R.id.games_button);
profilebtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
profile profileFragment = new profile();
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.fragment_frame, profileFragment);
trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
trans.commit();
}
});
bookingsbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
bookings bookings_frame = new bookings();
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.fragment_frame, bookings_frame);
trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
trans.commit();
}
});
gamesbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
games games_frame = new games();
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.fragment_frame, games_frame);
trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
trans.commit();
}
});
}
#Override
public void onFragmentInteraction(Uri uri){
}
}
Change height of FrameLayout to match_parent from wrap_content
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottompanel"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_frame"/>
</LinearLayout>
You have match_parent in the height for fragment layout. Try changing it to wrap_content and see if it works

How to set the text inside my tab widget

I want to change text in my tab Bar to get text in singleLine and remove the bold style but how to do it ? Please show me the good way
My below code don't change anything...
Here my 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabHost
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabHost"
android:layout_gravity="right">
<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/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:id="#+id/text1"
android:text="#string/text_v1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:id="#+id/text2"
android:text="#string/text_v2"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:id="#+id/text3"
android:text="#string/text_v3"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:id="#+id/text4"
android:text="#string/text_v4"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:id="#+id/text5"
android:text="#string/text_v5"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Here the java code:
package com.atonfoaziz.tabs5;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TabHost tabHost;
TabHost.TabSpec spec;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (TabHost)findViewById(R.id.tabHost);
if (tabHost != null){
tabHost.setup();
spec = tabHost.newTabSpec("Tab I").setContent(R.id.linearLayout1).setIndicator("Contacts");
tabHost.addTab(spec);
spec = tabHost.newTabSpec("Tab II").setContent(R.id.linearLayout2).setIndicator("Calls");
tabHost.addTab(spec);
spec = tabHost.newTabSpec("Tab III").setContent(R.id.linearLayout3).setIndicator("Discussions");
tabHost.addTab(spec);
spec = tabHost.newTabSpec("Tab IV").setContent(R.id.linearLayout4).setIndicator("Research");
tabHost.addTab(spec);
spec = tabHost.newTabSpec("Tab V").setContent(R.id.linearLayout5).setIndicator("Settings");
tabHost.addTab(spec);
TabWidget widget = tabHost.getTabWidget();
int tabCount = widget.getTabCount();
for (int i = 0; i < tabCount; i++){
View view = widget.getChildTabViewAt(i);
if (view != null){
TextView textView = (TextView)findViewById(R.id.title);
if (textView != null){
textView.setAllCaps(false);
textView.setSingleLine(true);
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
if (Build.VERSION.SDK_INT < 23){
textView.setTextAppearance(this, R.style.tab_appereance);
} else {
textView.setTextAppearance(R.style.tab_appereance);
}
textView.setTextSize(16);
}
}
}
}
}
}

Put a tabhost inside a fragment?

I try to desin a screen , which is a layout of a fragment like this
https://dl.dropboxusercontent.com/u/37599516/Untitled.png
Or like this in instagram
https://dl.dropboxusercontent.com/u/37599516/Screenshot%202014-01-09%2010.27.19.png
i try this code :
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayout1"
android:layout_marginRight="5dip"
android:background="#drawable/image_bg"
android:padding="3dip" >
<ImageView
android:id="#+id/userAvatar"
android:layout_width="wrap_content"
android:layout_height="89dp"
android:src="#drawable/test_avatar" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/thumbnail"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/thumbnail"
android:layout_below="#+id/relativeLayout1"
android:layout_toRightOf="#+id/thumbnail"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="soloco bolo"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:src="#drawable/description_section_icon" />
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="123456"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:src="#drawable/ic_menu_add" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="123dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/thumbnail" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:text="Đà Nẵng"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="68dp"
android:gravity="center_horizontal|center_vertical"
android:text="Activate"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:text="6/1/2013-7/1/2013"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/containerTabhost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/relativeLayout2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/linearLayout1"
android:background="#drawable/backgroundactionbar"
android:orientation="vertical" >
<TabHost
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.30" >
<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="92dp" >
</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"
android:orientation="vertical" >
<ListView
android:id="#+id/tab1list_place_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/tab2list_place_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/tab3list_place_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
and using this code to work with tab host in my fragment class
public class HostFragment extends Fragment {
private FragmentTabHost mTabHost;
public HostFragment() {
// Empty constructor required for fragment subclasses
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_host_layout,
container, false);
setHasOptionsMenu(true);
TabHost mTabHost = (TabHost) rootView.findViewById(R.id.tabhost);
mTabHost.setup( );
TabSpec photospec = mTabHost.newTabSpec("Search");
photospec.setIndicator("Search",
getResources().getDrawable(R.drawable.action_search));
Intent searchIntent = new Intent(getActivity(),
HostViewIntroduce.class);
photospec.setContent(searchIntent);
TabSpec songspec = mTabHost.newTabSpec("Favorites");
songspec.setIndicator("Favorites",
getResources().getDrawable(R.drawable.action_search));
Intent favoriteIntent = new Intent(getActivity(),
HostViewTips.class);
songspec.setContent(favoriteIntent);
mTabHost.addTab(songspec);
mTabHost.addTab(photospec);
return rootView;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
}
}
and it throw nullpointer exception
01-09 13:40:31.781: E/AndroidRuntime(5871): FATAL EXCEPTION: main
01-09 13:40:31.781: E/AndroidRuntime(5871): java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
You need to use FragmentTabHost's custom addTab() which will also take your Fragment class as parameter.
Replace
TabSpec photospec = mTabHost.newTabSpec("Search");
photospec.setIndicator("Search",
getResources().getDrawable(R.drawable.action_search));
Intent searchIntent = new Intent(getActivity(),
HostViewIntroduce.class);
photospec.setContent(searchIntent);
TabSpec songspec = mTabHost.newTabSpec("Favorites");
songspec.setIndicator("Favorites",
getResources().getDrawable(R.drawable.action_search));
Intent favoriteIntent = new Intent(getActivity(),
HostViewTips.class);
songspec.setContent(favoriteIntent);
with
TabHost.TabSpec photospec = mTabHost.newTabSpec("Search");
photospec.setIndicator("Search",
getResources().getDrawable(R.drawable.action_search));
photospec.setIndicator(rootView);
mTabHost.addTab(photospec, HostViewIntroduce.class, null);
TabSpec songspec = mTabHost.newTabSpec("Favorites");
songspec.setIndicator("Favorites",
getResources().getDrawable(R.drawable.action_search));
songspec.setIndicator(rootView);
mTabHost.addTab(songspec, HostViewTips.class, null);

android tab: writing text on top

I am implementing tab layout in android. I am able to implement tabs however my implementation gives following output:
In this image, text "Tab 1" "Tab 2" "Tab 3" all are written at the bottom in tab. I want it to write on the top. I have already used android:gravity="top" as well as android:layout_gravity="top" but there did nothing for me. please help. i am using following layout:
<?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:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
android:gravity="top"
android:layout_gravity="top"
>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:layout_gravity="top"
/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
please help.
We can achieve this by setting TabSpec content with customized views.Here is the sample.
in main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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">
<View android:layout_width="fill_parent" android:layout_height="0.5dip"
android:background="#000" />
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginLeft="0dip" android:layout_marginRight="0dip" />
<View android:layout_width="fill_parent" android:layout_height="2dip"
android:background="#696969" />
<View android:layout_width="fill_parent" android:layout_height="2dip"
android:background="#000" />
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="fill_parent" />
</LinearLayout>
</TabHost>
</LinearLayout>
in tabs_bg.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabsLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#drawable/tab_bg_selector"
android:padding="10dip" android:gravity="center" android:orientation="vertical">
<TextView android:id="#+id/tabsText" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Title"
android:textSize="15dip" android:textColor="#drawable/tab_text_selector" />
</LinearLayout>
i use some custom selector for view, you can create on your own.
In activity Main.class,
private TabHost mTabHost;
private void setupTabHost() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// construct the tabhost
setContentView(R.layout.main);
setupTabHost();
mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
setupTab(new TextView(this), "Tab 1");
setupTab(new TextView(this), "Tab 2");
setupTab(new TextView(this), "Tab 3");
}
private void setupTab(final View view, final String tag) {
View tabview = createTabView(mTabHost.getContext(), tag);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
public View createTabContent(String tag) {return view;}
});
mTabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
which provide output like this...

can anyone give sample code for TabHost in Android?

I need sample code to create TabHost in android.
can anyone help me.
The Android Developer site has an excellent fully worked code sample for creating tabs in Android using the TabWidget and TabHost.
Check out Hello, TabWidget.
I had done tabhost related code with respect to displaying scores
TabHost host = getTabHost();
host.setup ();
TabSpec allScoresTab = host.newTabSpec("allTab");
allScoresTab.setIndicator(getResources().getString(R.string.all_scores), getResources().getDrawable(android.R.drawable.star_on));
allScoresTab.setContent(R.id.ScrollViewAllScores);
host.addTab(allScoresTab);
TabSpec friendScoresTab = host.newTabSpec("friendsTab");
friendScoresTab.setIndicator(getResources().getString(R.string.friends_scores), getResources().getDrawable(android.R.drawable.star_on));
friendScoresTab.setContent(R.id.ScrollViewFriendScores);
host.addTab(friendScoresTab);
host.setCurrentTabByTag("allTab");
My xml contains :
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bkgrnd">
<RelativeLayout
android:id="#+id/RelativeLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ImageView_Header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/quizicon"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</ImageView>
<TextView
android:id="#+id/TextView01"
android:layout_height="wrap_content"
android:text="#string/scores"
android:textSize="#dimen/screen_title_size"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:layout_width="wrap_content"
android:layout_gravity="fill_horizontal|center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:shadowColor="#android:color/white"
android:textColor="#color/title_color">
</TextView>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ImageView_Header2"
android:layout_height="wrap_content"
android:src="#drawable/quizicon"
android:layout_gravity="right|center_vertical"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true">
</ImageView>
</RelativeLayout>
<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">
<ScrollView android:id="#+id/ScrollViewAllScores" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical">
<TableLayout android:id="#+id/TableLayout_AllScores" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="*" />
</ScrollView>
- <ScrollView android:id="#+id/ScrollViewFriendScores" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical">
<TableLayout android:id="#+id/TableLayout_FriendScores" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="*" />
</ScrollView>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
activity_main.xml contains
<?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="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/scroll" />
<HorizontalScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:layout_alignParentBottom="true" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/tabs_bg" />
</HorizontalScrollView>
</RelativeLayout>
</TabHost>
and java code is
TabHost tabHost = getTabHost();
TabSpec spec;
Intent intent;
//Home Tab
View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.home, null);
intent = new Intent(MainActivity.this, Firstclass.class);
spec = tabHost.newTabSpec("HOME").setIndicator(view1)
.setContent(intent);
tabHost.addTab(spec);
//Calendar Tab
View view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.calendar_tab, null);
intent = new Intent(MainActivity.this, Calendar.class);
spec = tabHost.newTabSpec("CALENDAR").setIndicator(view2)
.setContent(intent);
tabHost.addTab(spec);
res/layout/activity_main.xml
<LinearLayout 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=".MainActivity">
<TabHost
android:id="#+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<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"
android:background="#ffc916"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is tab 1" />
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#da8200"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is tab 2" />
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5b89ff"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is tab 3" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
src/MainActivity.java
public class MainActivity extends AppCompatActivity {
TabHost tabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost host = (TabHost)findViewById(R.id.tabHost);
host.setup();
//Tab 1
TabHost.TabSpec spec = host.newTabSpec("Tab One");
spec.setContent(R.id.tab1);
spec.setIndicator("Tab One");
host.addTab(spec);
//Tab 2
spec = host.newTabSpec("Tab Two");
spec.setContent(R.id.tab2);
spec.setIndicator("Tab Two");
host.addTab(spec);
//Tab 3
spec = host.newTabSpec("Tab Three");
spec.setContent(R.id.tab3);
spec.setIndicator("Tab Three");
host.addTab(spec);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
You can Follow these tutorials for help
Creating a tabbed UI with TabHost
Android Tab Layout Tutorial

Categories

Resources