Reputation: 2246
I am developing Chat Application in Android and I want to add dynamic Chat Tabs dependent on current matched Users like attached in screenshot below :
In Screen shot Chat Tabs are at Top but I want Chat Tabs at bottom
. Now I want to develop logic in onCreate method
so that
If there are three matched users then create 3 tabs,
If there are four matched users then create 4 tabs,likewise..
I searched a lot for chat Tabs and found ways to create Chat Tabs by using TabHost
..But also found that it is deprecated,not sure.. Another way is to setup Chat Tabs in Action Bar
..Somewhere found that use ActionBarSherlock
. I am very confused about chat tabs that what to use ?
Any help will be appreciated.
Upvotes: 4
Views: 12515
Reputation: 3489
I have been using ActionBarSherlock for a while now, but I have now migrated to the new Android SDK 18 Action Bar Compat. Your project looks like a straightforward implementation of action bar tabs and I can see no reason why you should start using ActionBarSherlock (or tabHost) at this point.
Action Bar Compat is very similar to Action Bar Sherlock and has the advantage that it is an integral component of the Android V4 support libraries (compatable back to SDK 7). See the "New v7 appcompat library" section in http://developer.android.com/tools/support-library/index.html .
It also has the advantage that it is clearly documented. This guide details how to set it up: http://developer.android.com/tools/support-library/setup.html
(pay particular attention to the section "Adding libraries with resources")
Having done that, you follow this guide to set up the support Action Bar: http://developer.android.com/guide/topics/ui/actionbar.html
The section "Adding Navigation Tabs" gives a clear example of a tabListener and how to add the tabs. You will need to make some minor adjustments to this code (for loop / if statements) to decide how many tabs to add. I have done this before and it is straightforward programming.
Upvotes: 1
Reputation: 4217
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
public static TabHost tabHost;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.tab_main);
// call addtab() how many times you need and pass tag and image resource id
}
private void addTab(String tag, int drawableId) {
tabHost = getTabHost();
TabHost.TabSpec spec = tabHost.newTabSpec(tag);
// tab_indicator layout contains only imageview. this is for fix image size, position
View tabIndicator = LayoutInflater.from(this).inflate(
R.layout.tab_indicator, getTabWidget(), false);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
tabHost.addTab(spec);
}
}
Upvotes: 1
Reputation: 126
Now in latest version of android included ActionBarSherlock library. So you can directly add tab using that library in android.
Sample code:
try
{
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// For each of the sections in the app, add a tab to the action bar.
actionBar.addTab(actionBar.newTab().setText(R.string.firsttab).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.second).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.third).setTabListener(this));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
}
catch(Exception e)
{
e.printStackTrace();
}
Upvotes: 1