I have problem with the custom actionbar and actionbar tabs for android 4.0.when my application run in the 4.4(in nexus 7.0 tabs)it works fine,but the problem with 4.0 device.the custom actionbar and tabbar are combined and it shown in the whole actionbar. like this
Class
package com.android.timeline;
#SuppressLint({ "SimpleDateFormat", "NewApi" })
public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{
public int width;
private ActionBar actionBar;
private TextView actionBarTitle;
private TabPagerAdapter TabAdapter;
ArrayList<Fragment> fragmentlist = new ArrayList<Fragment>();
private ViewPager Tab;
private String[] tabs = { "About", "Watch Next", "Related" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentlist.add(new AboutDetail());
fragmentlist.add(new WatchNextDetail());
fragmentlist.add(new RelatedDetail());
currentAboutDetail = fragmentlist.get(0);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager) findViewById(R.id.pager);
Tab.setOffscreenPageLimit(2);
pagerchangeListener();
setupActionBar();
}
private void pagerchangeListener() {
Tab.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
Tab.setAdapter(TabAdapter);
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("mMyCurrentPosition", Tab.getCurrentItem());
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mMyCurrentPosition = savedInstanceState.getInt("mMyCurrentPosition");
// where mMyCurrentPosition should be a public value in your activity.
}
private void setupActionBar() {
actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
View cView = getLayoutInflater().inflate(R.layout.actionbartitle, null);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
actionBarTitle = (TextView) cView.findViewById(R.id.timeline);
getActionBar().setIcon(R.drawable.log);
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#f16c81")));
actionBar.setCustomView(cView);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
final ImageView actionBarDropDownImg = (ImageView) cView
.findViewById(R.id.pageback);
final ImageView share = (ImageView) cView.findViewById(R.id.setting);
final ImageView font = (ImageView) cView.findViewById(R.id.font);
OnClickListener neww = new OnClickListener() {
#Override
public void onClick(View v) {
if (v == actionBarDropDownImg) {
finish();
overridePendingTransition(R.anim.anim_left,R.anim.anim_right);
}
if (v == font) {
((AboutDetail) currentAboutDetail).fontIncreament();
}
}
};
actionBarDropDownImg.setOnClickListener(neww);
share.setOnClickListener(neww);
font.setOnClickListener(neww);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
}
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int i) {
Log.e("LOG", "Position || " + i);
return fragmentlist.get(i);
}
#Override
public int getCount() {
return fragmentlist.size(); // No of Tabs
}
#Override
public void destroyItem(View container, int position, Object object) {
}
}
#Override
public void onTabSelected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
Tab.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
please help me ,thanks in advance.
I had same issues with landscape mode and I found the solution here:
http://andreimihu.com/blog/2013/10/17/android-always-embed-tabs-in-actionbar/
Basically, the writer wants the tab inside actionbar while you and I wants it outside. So, just change the method call to false (code from the above link, but a bit modified):
// This is where the magic happens!
public void forceTabs() {
try {
final ActionBar actionBar = getActionBar();
final Method setHasEmbeddedTabsMethod = actionBar.getClass()
.getDeclaredMethod("setHasEmbeddedTabs", boolean.class);
setHasEmbeddedTabsMethod.setAccessible(true);
setHasEmbeddedTabsMethod.invoke(actionBar, false);
}
catch(final Exception e) {
// Handle issues as needed: log, warn user, fallback etc
// This error is safe to ignore, standard tabs will appear.
}
}
Related
I tried many times but it's not OK.
I try to change actionBar in MainActivity but the TabBar doesn't change, so I don't no how to change it.
This is my code :
MainActivivty
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private ActionBar actionBar;
private TabsPagerAdapter mAdapter;
private String[] tabs = { "Bài Hát", "Yêu Thích"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar ac = getActionBar();
ac.setBackgroundDrawable(new ColorDrawable(Color.rgb(72, 209, 204)));
//ac.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#E64260")));
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
if (position == 1) {
TabYeuThich frag = (TabYeuThich) mAdapter
.getFragmentTabYeuThich(1);
frag.resetPage();
} else {
TabBaiHat frag = (TabBaiHat) mAdapter
.getFragmentTabBaiHat(0);
frag.resetPage();
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
This is class : TabsPagerAdapter
public class TabsPagerAdapter extends FragmentPagerAdapter {
FragmentManager fm;
TabYeuThich fragYeuThich;
TabBaiHat fragBaiHat;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
this.fm = fm;
}
#Override
public Fragment getItem(int index) {
// TODO Auto-generated method stub
switch (index) {
case 0:
fragBaiHat = new TabBaiHat();
return fragBaiHat;
case 1:
fragYeuThich = new TabYeuThich();
return fragYeuThich;
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
public Fragment getFragmentTabYeuThich(int post) {
return fragYeuThich;
}
public Fragment getFragmentTabBaiHat(int post) {
return fragBaiHat;
}
Please help me ! Thanks all !
Useally I set up the action bar from themes.xml
I recommend you follow up the both tutorials
developer android
action bar explanation
NoteActivity Code:
public class NoteActivity extends FragmentActivity implements ActionBar.TabListener
{
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = {"Note", "Note Info"};
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.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
}
}
TabsPageAdapter.Java
public class TabsPagerAdapter extends FragmentPagerAdapter
{
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index)
{
case 0:
return new NoteFragment();
case 1:
return new NoteInfoFragment();
}
return null;
}
#Override
public int getCount()
{
// get item count - equal to number of tabs
return 2;
}
}
So basically, I used an example of Tabs View and got the Tabs working and it looks like this:
What would I have to do to make it just swipeable without the Tabs showing. An example is like Snapchat. It definitely uses the Swipe view control but the tabs are hidden. Can someone please show me how to get this done?
Since you already have a ViewPager in your code, all you have to do is remove the code that creates the ActionBar tabs (under the comment // Adding tabs), as well as the code that synchronizes the tab selection with the current page (start with the ViewPager.OnPageChangeListener and the ActionBar.TabListener callbacks and see if anything breaks).
Getting Null Pointer Exception near actionBar.setHomeButtonEnabled(false), I have also implemented View Pager and Tabs.
I have have not defined #android:style/Theme.Black.NoTitleBar in the manifest file.
When I don't implement TabView and View Pager and pass a normal activity then the app works but when I try to implement TabView and View Pager I getting Null Pointer Exception.
Can anyone point out the mistake which I would have done?
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Merchants", "Personal Payee" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen_tab_layout_bp);
//Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
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) {
}
});
}
#Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu); //inflate our menu
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.item_refresh) {
Intent i = new Intent(MainScreenTab.this,ListMerchantType.class);
startActivity(i);
return true;
}
else if (id == R.id.item_save) {
Intent i = new Intent(MainScreenTab.this,ListPayee.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}
ActionBarCompat contains one Activity class which all of your Activity
classes should extend: ActionBarActivity. This class itself extends
from FragmentActivity so you can continue to use Fragments in your
application. There is not a ActionBarCompat Fragment class that you
need to extend, so you should continue using
android.support.v4.Fragment as the base class for your Fragments.
You have the info here:
http://android-developers.blogspot.in/2013/08/actionbarcompat-and-io-2013-app-source.html
That's the reason why you are getting NPE on actionBar element. So you should have it this way:
public class YourActivity extends ActionBarActivity implements TabListener {
I want to change the tab colour in the ViewPager. I search but didn't find any good solution for it. I want to change the tab colour from default that is from black to white. Is their any way to do this programatically for i have to modify style.xml. I don't know how to do as i am new to android. plz somebody help
public class HomeActivity extends FragmentActivity implements ActionBar.TabListener
{
public ViewPager viewPager;
private PageAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Calculate EMI", "EMI Schedule"};
String TabFragmentSchedule;
public void setTabFragmentSchedule(String t){
TabFragmentSchedule = t;
}
public String getTabFragmentSchedule(){
return TabFragmentSchedule;
}
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#0F7BFF"));
actionBar.setBackgroundDrawable(colorDrawable);
mAdapter = new PageAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(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) {
}
});
}
//method that is called to swipe viewpage on button click from calculation fragment
public void switchToFragmentSchedule(){
// viewPager.setCurrentItem(1);
viewPager.setCurrentItem(1, false);
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
You should style your theme in your xml files. You can find all the information for this task here.
If you need to change it programmatically, you can use
mActionBar.setBackgroundDrawable(new ColorDrawable(0xffffffff));
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayShowTitleEnabled(true);
As a beginner, you might also find this styling tool very helpful.
I want to detach/attach my fragments, but how to set, that fragment not recreate, after I attach.
In fragment I have WebView; when I select and unselect tabs, webview load startpage.
There is my code:
public class MainActivity extends Activity implements OnClickListener, OnMenuItemClickListener {
ActionBar bar;
View v;
public static TextView tilt;
LayoutInflater inflater;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
onAddTab();
View v=getLayoutInflater().inflate(R.layout.action_bar, null);
ImageButton im = (ImageButton)v.findViewById(R.id.tab_toggle);
im.setOnClickListener(this);
getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_layout));
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActionBar().setCustomView(v);
onToggleTabs();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.tab_toggle:
onAddTab();
break;
}
}
public void onAddTab() {
final ActionBar bar = getActionBar();
View v=getLayoutInflater().inflate(R.layout.layout_tab, null);
tilt = (TextView)v.findViewById(R.id.tit_le);
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER;
v.setLayoutParams(lp);
closetab = (ImageButton)v.findViewById(R.id.close);
closetab.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
onRemoveTab();
}
});
bar.addTab(bar.newTab()
.setCustomView(v)
.setTabListener(new TabListener<Web>(this, "Tag A", Web.class)));
}
public void onRemoveTab() {
final ActionBar bar = getActionBar();
Tab tab = bar.getSelectedTab();
bar.removeTab(tab);
}
public void onToggleTabs() {
final ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
public void onRemoveAllTabs(View v) {
getActionBar().removeAllTabs();
}
public static class TabListener<T extends Fragment> implements ActionBar.TabListener{
private final Activity myActivity;
private final String myTag;
private final Class<T> myClass;
public TabListener(Activity activity, String tag, Class<T> cls) {
myActivity = activity;
myTag = tag;
myClass = cls;
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);
// Check if the fragment is already initialized
if (myFragment == null) {
// If not, instantiate and add it to the activity
myFragment = Fragment.instantiate(myActivity, myClass.getName());
ft.add(R.id.fragment0, myFragment, myTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(myFragment);
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);
if (myFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(myFragment);
}
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
There is Fragment:
public class Web extends Fragment implements OnLongClickListener, OnClickListener{
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
c=this.getActivity();
v = inflater.inflate(R.layout.activity_main, container, false);
return v;
}
#SuppressWarnings("deprecation")
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
wv = (WebView)v.findViewById(R.id.wv);
wv.setWebChromeClient(new WebChromeClient(){
#Override
public void onProgressChanged(WebView view, int progress) {
// TODO Auto-generated method stub
super.onProgressChanged(view, progress);
if(progress < 100 && pr.getVisibility() == ProgressBar.GONE){
pr.setVisibility(ProgressBar.VISIBLE);
}
pr.setProgress(progress);
if(progress == 100) {
pr.setVisibility(ProgressBar.GONE);
}
}
});
wv.setWebViewClient(new MyWebViewClient());
wv.loadUrl("http://www.google.com");
wv.setOnLongClickListener(this);}
Try setRetainInstance(boolean)
Also check this posts:
Android fragments setRetainInstance(true) not works (Android support library)
Fragment setRetainInstance not works (Android support lib)
Understanding Fragment's setRetainInstance(boolean)