I am trying to add tabs to the action bar in my android application. Using FragmentPagerAdapter seems a nice idea but when I try to set the adapter to the ViewPager object, it returns a nullpointerexception. Cannot figure out th problem, please help... Here is the partial code segment:
public class Aps_MainActivity extends FragmentActivity implements ActionBar.TabListener {
ViewPager vPager;
PackagePagerAdapter pPAdapter;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aps__main);
setTabs();
}
void setTabs() {
// Creating the adapter that will return a fragment for each sections
// of the app.
pPAdapter = new PackagePagerAdapter(getSupportFragmentManager());
actionBar = getActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
vPager = (ViewPager) findViewById(R.id.pager);
try{
vPager.setAdapter(pPAdapter);
}
catch(Exception e){
Log.e("ADAPTER", e.toString());
}
vPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < pPAdapter.getCount(); i++) {
actionBar.addTab(
actionBar.newTab()
.setText(pPAdapter.getPageTitle(i))
.setTabListener(this));
}
}
Related
I have my popup windows with "extends activity". I searched and I knew that I cannot extent twice.
So, How can I put the content in a pop up with the FragmentActivity?
Edit:
this is the main activity
public class Kulintang extends AppCompatActivity implements OnMenuItemClickListener {
SoundPool mySound;
int aID, bID, cID, dID, eID, fID, gID, hID;
private Button brecord;
private String FILE;//File path
private MediaRecorder record;
......
this is the class i want to pop up when i tap the button
public class play extends FragmentActivity implements ActionBar.TabListener {
ActionBar actionBar;
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1=actionBar.newTab();
tab1.setText("Recorded");
tab1.setTabListener(this);
ActionBar.Tab tab2=actionBar.newTab();
tab2.setText("Songs");
tab2.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
}
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();
}
I have 3 different Tabs, where each one has text. Now I want to find out how to Add drawable images on top of the text of the tab. I know that this was possible using tabhosts but that has been deprecated. My class is the following :
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 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));
}
/**
* 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) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
See this link: http://developer.android.com/reference/android/app/ActionBar.Tab.html#setIcon%28android.graphics.drawable.Drawable%29
Sample code:
actionBar.addTab(actionBar.newTab().setText(tab_name).setIcon(R.drawable.tab_icon).setTabListener(this));
But please consider:
Action bar navigation modes are deprecated and not supported by inline toolbar action bars. Consider using other common navigation patterns instead. (http://developer.android.com/design/patterns/navigation.html)
Edit:
For your Code you could do something like this:
Create the icons as you do for the titles:
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };
//make sure this has the same length as tabs-array!
private int[] icons = {R.drawable.tab_rated, R.drawable.tab_games, R.drawable.tab_movies}
And add them in a loop:
// Adding Tabs
for (int i = 0; i < tabs.length; i++) {
actionBar.addTab(actionBar.newTab().setText(tabs[i]).setIcon(icons[i]).setTabListener(this));
}
I am building an app that uses three tab fragments and I want to create a function in one place that I can call from all three tabs. I would assume that I need to create the new function in the activity that contains the tabs, but I'm not sure how to declare it or how to call it from one of the tab fragments.
This is how i create my tabs in the main activity:
public class MainActivity extends FragmentActivity implements TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Search", "History", "Saved" };
#Override
protected void onCreate(Bundle savedInstanceState) {
//Initialise the Database connection
DBAdapter.init(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Setup tab bar
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//Add the tabs to the action bar
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
//Tab Swipe change listener
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) {
}
});
}
}
Create the function you need to call in the parent Activity and it must be a public method
and you can call that function like this:
((YourActivityClassName)getActivity()).yourPublicMethod();
I have an action bar activity with 3 tabs.
I have a main activity, where I post an xml to a webservice and get back an xml in an AsyncTaskj. I'm initializing a static variable of the main activity in AsncTask. From main activity another activity is called, where Action Bar is initialized. Each action bar has a ListFragment, where I use the static variable. I have no issue accessing a Static variable from 2 List activities but in first ListFragment m not able to access the initialized static variable.
Edit 1
My Main Activity
submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
GenerateXml gx=new GenerateXml();
String requestXml=gx.generateXml(tv1.getText().toString(),tv2.getText().toString(),tv3.getText().toString(),tv4.getText().toString());
myNewTask = new MyTask(requestXml);
myNewTask.execute();
Intent intent=new Intent(getApplicationContext(),TabActivity.class);
startActivity(intent);
}
});
In my AsyncTask doInBackground
MainActivity.responseXml=responseXML;
The Activity where tabs are created
public class TabActivity extends Activity {
MyTask myNewTask;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(true);
/** Creating All Tab */
Tab tab = actionBar.newTab()
.setText("All")
.setTabListener(new CustomTabListener<AllMsgFragment>(this, "All", AllMsgFragment.class));
//.setIcon(R.drawable.android);
actionBar.addTab(tab);
/** Creating Success Tab */
tab = actionBar.newTab()
.setText("Success")
.setTabListener(new CustomTabListener<SuccessMsgFragment>(this, "Success", SuccessMsgFragment.class));
//.setIcon(R.drawable.apple);
actionBar.addTab(tab);
/** Creating Error Tab */
tab = actionBar.newTab()
.setText("Error")
.setTabListener(new CustomTabListener<ErrorMsgFragment>(this, "error", ErrorMsgFragment.class));
//.setIcon(R.drawable.apple);
actionBar.addTab(tab);
}
}
My first ListFragment:
public class AllMsgFragment extends ListFragment {
public static String response;
public ArrayList<HashMap<String, String>> msgDetails;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
response=MainActivity.responseXml;
XmlToArrayList xmlArray=new XmlToArrayList();
try {
msgDetails=xmlArray.arrayListXml(response);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(getActivity(), msgDetails,
R.layout.msg_preview,
new String[] { "ObSystem", "ObName", "Msgstate"}, new int[] {
R.id.bs, R.id.si, R.id.msgStatus});
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onStart() {
super.onStart();
Log.e("first","1");
/** Setting the multiselect choice mode for the listview */
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}