Change Title Text Case to lowercase in Sherlock ActionBar Tab Views [duplicate] - android

This question already has answers here:
how to show text in lower case in android?
(6 answers)
Closed 3 months ago.
I created a tab application using Sherlock ActionBar Tab Views. I like to change the Tab title to lowercase. see screen shot
I want to change TAB1 to tab1
Any solution is greatly appreciated
my code as follow
public class MainActivity extends SherlockFragmentActivity {
// Declare Variables
ActionBar mActionBar;
ViewPager mPager;
Tab tab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
// Activate Navigation Mode Tabs
mActionBar = getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Locate ViewPager in activity_main.xml
mPager = (ViewPager) findViewById(R.id.pager);
// Activate Fragment Manager
FragmentManager fm = getSupportFragmentManager();
// Capture ViewPager page swipes
ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
// Find the ViewPager Position
mActionBar.setSelectedNavigationItem(position);
}
};
mPager.setOnPageChangeListener(ViewPagerListener);
// Locate the adapter class called ViewPagerAdapter.java
ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(fm);
// Set the View Pager Adapter into ViewPager
mPager.setAdapter(viewpageradapter);
// Capture tab button clicks
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Pass the position on tab click to ViewPager
mPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
};
// Create first Tab
tab = mActionBar.newTab().setText("Tab1").setTabListener(tabListener);
mActionBar.addTab(tab);
// Create second Tab
tab = mActionBar.newTab().setText("Tab2").setTabListener(tabListener);
mActionBar.addTab(tab);
// Create third Tab
tab = mActionBar.newTab().setText("Tab3").setTabListener(tabListener);
mActionBar.addTab(tab);
}
thanks

Please add this in TabLayout tag of xml file
app:tabTextAppearance="#android:style/TextAppearance.Widget.TabWidget"
this will help you :)

try this cod
text-transform: capitalize;

Try This :-
tab = mActionBar.newTab().setText("Tab1").setTabListener(tabListener);
mActionBar.addTab(tab);
TextView x1 = (TextView) mTabHost.getTabWidget().getChildAt(0)
.findViewById(android.R.id.title);
x1.setAllCaps(false);
// Create second Tab
tab = mActionBar.newTab().setText("Tab2").setTabListener(tabListener);
mActionBar.addTab(tab);
TextView x2 = (TextView) mTabHost.getTabWidget().getChildAt(0)
.findViewById(android.R.id.title);
x2.setAllCaps(false);
// Create third Tab
tab = mActionBar.newTab().setText("Tab3").setTabListener(tabListener);
mActionBar.addTab(tab);
TextView x3 = (TextView) mTabHost.getTabWidget().getChildAt(0)
.findViewById(android.R.id.title);
x3.setAllCaps(false);

Related

android make changes in another fragment

I have the following scenario:
In my main activity, I'm using sliding tabs using viewpager and fragmentpageradapter.
Suppose I have 2 tabs.
In Tab 2, on an event(say button press), I start a new activity using startActivityForResult.
Now, when I get back to Tab 2 from the new activity (in onActionResult), I want Tab 1 to get refreshed.
I'm currently doing it using getActivity.recreate()
Is this good way to do considering performance or is there any other way?
My main activity onCreate code is:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
// setup action bar for tabs
final ActionBar actionBar = getActionBar();
TabsPagerAdapter mAdapter = new TabsPagerAdapter(getFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(true);
// Create a tab listener that is called when the user changes tabs.
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
};
ActionBar.Tab tab = actionBar.newTab()
.setText(R.string.tab1)
.setTabListener(tabListener);
actionBar.addTab(tab, 0, false);
tab = actionBar.newTab()
.setText(R.string.tab2)
.setTabListener(tabListener);
actionBar.addTab(tab, 1, true);
// on swiping the viewpager make respective tab selected
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
getActionBar().setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}

onTabSelected change Fragment

I am trying to change the Fragment onTabSelected.
Because at the moment, if you swipe the transitions work fine. But if you click on the Tab, the Fragments won't switch. The Tab gets highlighted, but the content remains the same.
The issue is with my onTabSelected method. I need a suggestion on how to switch to Fragments onTabSelected.
Unfortunately I cannot extend my MainActivity to FragmentActivity, since I won't be able to use ActionBar in that case (I am required to extend ActionBarActivity).
onTabSelected snippet:
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
int position = tab.getPosition();
switch (position) {
case 0:
break;
case 1:
break;
case 2:
break;
}
}
Part of MainActivity that matters here:
public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Novice", "Članki", "O Tribuni" };
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getSupportActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(android.app.ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
Any help / suggestion appreciated.
OK, I figured it out by myself guys.
The answer is to notify the viewPager about the TabSelected.
Use this line of code in your onTabSelected method:
mViewPager.setCurrentItem(tab.getPosition());
Hope it helps someone else too!
Instead of using an OnTabSelectedListener you can implement a FragmentPagerAdapter and forward the ViewPager events to the TabLayout:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(...);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new TabPagerAdapter(getSupportFragmentManager()));
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
This worked for me, so should work for you:
#Override
public void onTabSelected(android.support.v7.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition(),true);
drawerLayout.closeDrawers();
}

in swipe tabs how to keep the tab no reopen when u swipe more then one tab

i have swipe tabs with 3 tab i first tab will have web view with Facebook other got text only , now if i swipe from first tab to the second then come back to the first its good but if i swipe to the 3rd tab when i go back to the first tab the web view will reload again , how can i stop that
this is my code
home.java
public class MainActivity extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position); }
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
#Override
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}};
//Add New Tab
actionBar.addTab(actionBar.newTab().setText("Android").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("iOS").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Windows").setTabListener(tabListener));
}
}
ViewPager has a method setOffscreenPageLimit() that determines how many pages to retain when they go off screen.
try increasing the page limit. For example:
Tab.setOffscreenPageLimit(3);

Adding tabs in top and bottom of the activity simoultaneously in android

I am having a SherlockActionbar with Tab Using view-Pager
Current Output:: (Clearly you can see three tabs on top)
My Code::
MainActivity.java
public class MainActivity extends SherlockFragmentActivity {
// Declare Variables
ActionBar mActionBar;
ViewPager mPager;
Tab tab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
// Activate Navigation Mode Tabs
mActionBar = getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Locate ViewPager in activity_main.xml
mPager = (ViewPager) findViewById(R.id.pager);
// Activate Fragment Manager
FragmentManager fm = getSupportFragmentManager();
// Capture ViewPager page swipes
ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
// Find the ViewPager Position
mActionBar.setSelectedNavigationItem(position);
}
};
mPager.setOnPageChangeListener(ViewPagerListener);
// Locate the adapter class called ViewPagerAdapter.java
ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(fm);
// Set the View Pager Adapter into ViewPager
mPager.setAdapter(viewpageradapter);
// Capture tab button clicks
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Pass the position on tab click to ViewPager
mPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
};
// Create first Tab
tab = mActionBar.newTab().setText("Tab1").setTabListener(tabListener);
mActionBar.addTab(tab);
// Create second Tab
tab = mActionBar.newTab().setText("Tab2").setTabListener(tabListener);
mActionBar.addTab(tab);
// Create third Tab
tab = mActionBar.newTab().setText("Tab3").setTabListener(tabListener);
mActionBar.addTab(tab);
}
}
activity_main.xml
<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.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
ViewPagerAdapter.java
public class ViewPagerAdapter extends FragmentPagerAdapter {
// Declare the number of ViewPager pages
final int PAGE_COUNT = 3;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int arg0) {
switch (arg0) {
// Open FragmentTab1.java
case 0:
FragmentTab1 fragmenttab1 = new FragmentTab1();
return fragmenttab1;
// Open FragmentTab2.java
case 1:
FragmentTab2 fragmenttab2 = new FragmentTab2();
return fragmenttab2;
// Open FragmentTab3.java
case 2:
FragmentTab3 fragmenttab3 = new FragmentTab3();
return fragmenttab3;
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return PAGE_COUNT;
}
}
How can i achieve this Or is this possible::(I need three tabs at top and three tabs at bottom)
If this is possible what changes should i need to make for my code ?
If I'm getting this right you want to have the same tabs in top and bottom simultaneously both tied to the contents in between?
If so, I would suggest extending tabhost activity from sherlock library and change the layout so that the tabs are in both in top and bottom using the same variable.
Hope this helps.

How to switch between Action bar tabs smoothly without reloading fragments every time

Is my following Action bar Tab implementation method is efficient or not? Because whenever i switch between tabs the Tab content fragments are replaced and load again in frame layout.
I want to add fragments at first time itself and show the fragments smoothly(without reloading) when switch between tabs.
Note: i tried to add all fragments on onCreate method.But the fragments are overlapped with each other and display all fragments in frame layout at same time.
My Code:
public class ManageActivity extends SherlockFragmentActivity implements ActionBar.TabListener {
private Activity mActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = getSupportActionBar().newTab();
tab1.setText("Tab1");
tab1.setTabListener(this);
getSupportActionBar().addTab(tab1);
ActionBar.Tab tab2 = getSupportActionBar().newTab();
tab2.setText("Tab2");
tab2.setTabListener(this);
getSupportActionBar().addTab(tab2);
ActionBar.Tab tab3 = getSupportActionBar().newTab();
tab3.setText("Tab3");
tab3.setTabListener(this);
getSupportActionBar().addTab(tab3);
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
String selectedtab = tab.getText().toString();
if(selectedtab.equalsIgnoreCase("Tab1"))
{
getSupportFragmentManager().beginTransaction().replace(R.id.tabfragment_container, new Tab1Fragment()).commit();
}
else if (selectedtab.equalsIgnoreCase("Tab2")) {
getSupportFragmentManager().beginTransaction().replace(R.id.tabfragment_container, new Tab2Fragment()).commit();
}
else if (selectedtab.equalsIgnoreCase("Tab3")){
getSupportFragmentManager().beginTransaction().replace(R.id.tabfragment_container, new Tab3Fragment()).commit();
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
activity_manage.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/tabfragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ManageActivity" >
</FrameLayout>
Note: My Tab contents are Listfragments which query data from Sqlite using loader manager.
I use a ViewPager that contains all my fragments. Every time a tab is selected I only switch the position in the Viewpager.
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
int position = adapter.findItemPosition(this.id);
ViewPager viewPager = (YourActivity)getActivity().getViewPager(); // Convenience method
if (viewPager != null) {
viewPager.setCurrentItem(position, this.shouldScroll);
} else {
Log.d(getClass().getSimpleName(), "No pager available");
}
}
To make this code working you need a Viewpager and have to add all tabs into the ViewPager through an instance of PagerAdapter. I use a FragmentPagerAdapter as there are not that many tabs most of the time.
This also enables your user to swipe through the tabs. If the user changes the selected tab through swiping you need to update the selected tab. I use ActionBarSherlock in all of my Apps. Therefore the next Snippet uses a supportActionBar. This code example is a simple listener that can be set on the ViewPager to update tab changes through swiping.
private final class TabPageChangedListener extends ViewPager.SimpleOnPageChangeListener {
#Override
public void onPageSelected(int position) {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setSelectedNavigationItem(position);
} else {
Log.e(getClass().getSimpleName(),
"No actionbar available to change selected tab.");
}
}
}
There is also the possibility to disable swiping by overwriting the ViewPager and intercepting the swiping touch motions if you do not want the tabs to be swipeable.

Categories

Resources