I want to create Tabview like below in Image. I tried it but can't get proper view.
Here is My Code.. It's not give exact view that I want. So, How can I customized it.
public class BandInfo extends TabActivity implements OnTabChangeListener {
TabHost tabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.band_info);
tabHost = getTabHost();
tabHost.getTabWidget().setBackgroundResource(R.drawable.footerbar);
// Tab for Bio
TabSpec bioSpec = tabHost.newTabSpec("Bio");
// Setting Title for Tab
bioSpec.setIndicator("Bio");
Intent bioIntent = new Intent(this,Bio.class);
bioSpec.setContent(bioIntent);
// Tab for Upcoing Shows
TabSpec upcomingShowSpec = tabHost.newTabSpec("Upcoming Shows");
upcomingShowSpec.setIndicator("Upcoming Shows");
Intent upcomingShowIntent = new Intent(this, UpcomingShow.class);
upcomingShowSpec.setContent(upcomingShowIntent);
// Tab for Band Members
TabSpec bandMemberSpec = tabHost.newTabSpec("Band Members");
bandMemberSpec.setIndicator("Band Members");
Intent bandMemberIntent = new Intent(this, BandMembers.class);
bandMemberSpec.setContent(bandMemberIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(bioSpec); // Adding Bio Tab
tabHost.addTab(upcomingShowSpec); // Adding Upcoming Show Tab
tabHost.addTab(bandMemberSpec); // Adding Band Members Tab
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.footerbar);
}
tabHost.getTabWidget().setDividerDrawable(R.drawable.footer_saprater);
}
#Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.footerbar);
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(R.drawable.bandmember_active);
}
Please Help me with some code...
Thanks in Advance..
This is an output Screen I get.
Heyy i found this. Try this. you'll surely make it.
http://code.google.com/p/android-custom-tabs/
For setting width,
tabHost.getTabWidget().getChildAt(0).getLayoutParams().width =(int) 30;
Also check this link: http://adanware.blogspot.in/2012/04/android-custom-tab-layouts-just-using.html and http://www.speakingcode.com/2011/12/01/creating-custom-views-for-tabs-in-an-android-activity-using-tabhost-and-tabwidget/
Related
I have these 4 tabs as below. The problem is whenever I increase the tabs the tab size gets smaller. Is there any solution when they can simply be scrolled horizontally and also have the same size.
public class MainActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Tab for Maths
TabSpec mathspec = tabHost.newTabSpec("Maths");
mathspec.setIndicator("Maths");
Intent mathIntent = new Intent(this, MathActivity.class);
mathspec.setContent(mathIntent);
// Tab for Units
TabSpec unitsspec = tabHost.newTabSpec("Units");
// setting Title and Icon for the Tab
unitsspec.setIndicator("Units");
Intent unitsIntent = new Intent(this, UnitsActivity.class);
unitsspec.setContent(unitsIntent);
// Tab for Physics
TabSpec physicsspec = tabHost.newTabSpec("Physics");
// setting Title and Icon for the Tab
physicsspec.setIndicator("Physics");
Intent physicsIntent = new Intent(this, PhysicsActivity.class);
physicsspec.setContent(physicsIntent);
// Tab for Chemistry
TabSpec chemistryspec = tabHost.newTabSpec("Chemistry");
// setting Title and Icon for the Tab
chemistryspec.setIndicator("Chemistry");
Intent chemistryIntent = new Intent(this, ChemistryActivity.class);
chemistryspec.setContent(chemistryIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(mathspec); // Adding maths tab
tabHost.addTab(unitsspec); // Adding units tab
tabHost.addTab(physicsspec); // Adding physics tab
tabHost.addTab(chemistryspec); // Adding chemistry tab
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextSize(10);
}
}
}
Switch over to Fragments. You will never face such an issue.
See this tutorial.
http://wptrafficanalyzer.in/blog/implement-swiping-between-tabs-with-viewpager-in-action-bar-using-sherlock-library/
Moreover tab activity gets deprecated.
I need to implement tabs in my project.i have a layout, in which i have two tabs and a button. For two tabs,I have two activities and button calling different activity. the thing is I am showing result of button on first tab. i.e tab0 is active on tab0 event and on button click event. And am able to change the tab events using tabHost.setOnTabChangedListener, but now what i further want is, say suppose i click on button so now button view is displaying but again if i click on tab0, tab0 activity should be displayed.
i used onClick but after using this my TabChangeListner not works.
would you suggest solution for my problem.
Thanks.
Below is my code that is working fine :
public class TabLayoutUsingTabChangeEventActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
final TabHost.TabSpec sp1 = tabHost.newTabSpec("TAB1");
TabHost.TabSpec sp2 = tabHost.newTabSpec("TAB2");
//Creating First Tab
Intent intent1 = new Intent(this, Tab1Activity.class);
sp1.setIndicator("TAB1").setContent(intent1);
tabHost.addTab(sp1);
//Creating Second Tab
Intent intent2 = new Intent(this, Tab2Activity.class);
sp2.setIndicator("TAB2").setContent(intent2);
tabHost.addTab(sp2);
//Tab Changed Event
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
Log.i("TabId :", tabId);
if(tabId.equals("TAB2")){
Log.i("TAB1", "TAB1 Changed");
Intent intent1 = new Intent().setClass(getApplicationContext(), Tab1Activity.class);
sp1.setIndicator("TAB1").setContent(intent1);
tabHost.setCurrentTab(0);
}
}
});
Button addNewButton = (Button)findViewById(R.id.add_new_ticket_btn);
addNewButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent in = new Intent().setClass(getApplicationContext(), AddNewTicketActivity.class);
sp1.setContent(in);
tabHost.setCurrentTab(0);
//startActivity(in);
}
});
}
}
You need to use AvtivityGroup to open new activities in the same tab.
Here is the complete example.
http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html
I have TabActivity with tabs on bottom which is composed of several different activities like this:
public class HomeScreen extends TabActivity {
private TabHost mTabHost;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
try{
setContentView(R.layout.hometabs);
Resources res = getResources(); // Resource object to get Drawables
mTabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
String strName;
// Create an Intent to launch an Activity for the tab (to be reused)
strName=Central.Res.getString(R.string.tab_Books);
intent = new Intent().setClass(this, BookList.class);
spec = mTabHost.newTabSpec(strName).setIndicator("",
res.getDrawable(R.drawable.ic_menu_database))
.setContent(intent);
mTabHost.addTab(spec);
// Create an Intent to launch an Activity for the tab (to be reused)
strName=Central.Res.getString(R.string.tab_Questions);
intent = new Intent().setClass(this, QuestionsList.class);
intent.putExtra("NoteType", UserDataSQLHelper.NOTE_TYPE_QUEST );
spec = mTabHost.newTabSpec(strName).setIndicator("",
res.getDrawable(R.drawable.ic_menu_dialog))
.setContent(intent);
mTabHost.addTab(spec);
My problem is that I have the same window title for every tab. This window title is set to application name.
I need to be able to change this title to the name of the tab.
I am trying to do it by calling setTitle("MyTitle"); in onCreate() of corresponding activity and by overriding TabActivity onChildTitleChanged:
public void onChildTitleChanged(Activity childActivity, CharSequence title)
{
View tabView = mTabHost.getCurrentTabView();
int idTitle = android.R.id.title;
TextView tvTitle = (TextView) tabView.findViewById(idTitle);
tvTitle.setText(title);
}
But the result I get isn't what I need - it sets text to tab under the tab icon.
Could you please help me?
try with following code,
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
Log.i("Tab id", ""+tabId);
setTitle(tabId);
}
});
setIndicator (CharSequence label, Drawable icon).
Use this function and the label will be shown as a title.
i have Tab host which is shown below,
private TabHost myTabHost;
......
setContentView(R.layout.vidtab);
Intent intent=getIntent();
intent = new Intent().setClass(this, RecordActivityGroup.class);
myTabHost = (TabHost)this.findViewById(android.R.id.tabhost);
myTabHost.setup();
TabSpec rectab = myTabHost.newTabSpec("Record");
rectab.setIndicator("Record",getResources().getDrawable(R.drawable.irecord));
rectab.setContent(intent);
myTabHost.addTab(rectab);
intent = new Intent().setClass(this, sharingProject.class);
TabSpec setting = myTabHost.newTabSpec("Hint");
setting.setIndicator("Hint",getResources().getDrawable(R.drawable.isettings));
setting.setContent(intent);
myTabHost.addTab(setting);
in that record Activity group i have following code,
RecordActivityGroup extends TabGroupActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startChildActivity("RecordingActivity", new Intent(this,Record.class));
}
and Record.class is an activity which contain one button, i need to switch to second tab while clicking button in Record class, how to do that? i any one know that please help me out.
You can use an method in your main class like following
public void switchToTab(int tabid){
myTabHost.setCurrentTab(tabid);
}
And now in Record class on your Button click call this
YourStartCalss parentActivity;
parentActivity= (YourStartCalss) this.getParent();
parentActivity.switchToTab(yourtabid);
How could I few layout and require to create in one Tabactivity.
I have try the code below but no luck got error.
tabHost.addTab(tabHost.newTabSpec("Sales Order").setIndicator("Sales Order").setContent(R.layout.frm_txn_so_item_list));
OK let me explain clearly.
I have a code below, as you can see I have 4 tab layout page. Each of it has it own activity class. I have a button which is belong to cls_so_item_list.class, whenever I am try to call it in my cls_so it always return me null value.
So I have come out an idea, to remove all the tab page(item,product,summary,Report) activity classes then create it one standalone class which is cls_so.
My question is how do I put the layout page inside the tabHost.addTab? Thanks
public class cls_so extends TabActivity implements OnClickListener {
protected TabHost tabHost;
int intSalesOrderId;
src_txn_so.cls_so_obj objSalesOrder;
static final String LIST_ID = "list_id";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tabHost = getTabHost();
newTabIntent("Item", null, cls_so_item_list.class);
newTabIntent("Product",
getResources().getDrawable(R.drawable.so_product),
cls_so_prd_list.class);
newTabIntent("Summary",
getResources().getDrawable(R.drawable.so_summary),
cls_so_summary.class);
newTabIntent("Report",
getResources().getDrawable(R.drawable.so_report),
cls_so_summary.class);
Button btnSOLineDiscount = (Button) findViewById(R.id.txn_so_btn_line_discount);
btnSOLineDiscount.setOnClickListener(this);
tabHost.setCurrentTab(0);
}
protected void newTabIntent(String label, Drawable icon, Class<?> pageClass) {
TabSpec tabSpec = tabHost.newTabSpec(label);
tabSpec.setIndicator(label, icon);
Intent SOIntent = new Intent().setClass(this,pageClass);
SOIntent.putExtra(LIST_ID, -1);
tabSpec.setContent(new Intent(this, pageClass));
tabHost.addTab(tabSpec);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
It doesn't work like that. In your main layout, add in the FrameLayout with the id of tabcontent an element <include> that points on that layout.
In your code you'll have to change it to.
tabHost.addTab(tabHost.newTabSpec("Sales Order").setIndicator("Sales Order").setContent(R.id.my_included_layout));
if you're <include> has the id "#+id/my_included_layout"