How to create tabs in an Android application and dynamically add tabs (depending on user matching)

I am developing a chat application on Android, and I want to add dynamic chat tabs depending on the currently agreed users, as shown in the screenshot below:

enter image description here

In the Screenshots section, the Chat tabs are on top, but I want Chat Tabs at bottom . Now I want to develop the logic in the onCreate method so that

If there are three consistent users, create 3 tabs,

If there are four consistent users, then create 4 tabs, too ..

I searched chat tabs many times and found ways to create chat tabs using TabHost .. But also found that it was outdated, but not sure. Another way is to set up the chat tabs in the Action Bar .. Somewhere they found that use ActionBarSherlock . I am very confused about chat tabs, what to use?

Any help would be appreciated.

+4
source share
3 answers
 @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); } } 
+1
source

Now, in the latest version of android, the ActionBarSherlock library is included. This way you can directly add a tab using this library in android.

Code example:

  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(); } 
+1
source

I have been using ActionBarSherlock for a while, but now I have switched to the new Android Action SDK 18. Your project looks like a simple implementation of the action bar tabs, and I see no reason why you should start using ActionBarSherlock (or tabHost) at this stage.

The Action Bar screen is very similar to the Action Bar Sherlock and has the advantage of being an integral component of the Android V4 support libraries (compatible with SDK 7). See the “New v7 appcompat application library” section at http://developer.android.com/tools/support-library/index.html .

This also has the advantage of being clearly documented. This guide details how to set it up: http://developer.android.com/tools/support-library/setup.html

(pay special attention to the section "Adding libraries with resources")

Having done this, you follow this guide to set up the support action bar: http://developer.android.com/guide/topics/ui/actionbar.html

The “Adding Navigation Tabs” section gives a clear example of tabListener and how to add tabs. You will need to make some minor adjustments to this code (for / if statements) to determine how many tabs you need to add. I have done this before and this is simple programming.

+1
source

Source: https://habr.com/ru/post/1492073/


All Articles