Hide fab on other tabs - android

How can I hide the fab on the other tabs and only show it on the first tab? I'm using the GET_ARGUMENTS to select between activities. Help please. Here is my code. Thanks.
public class OwnerTabs extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
SharedPreferences pref;
SharedPreferences.Editor editor;
private Button btnStart, btnStop;
private TextView tvCoordinates;
private BroadcastReceiver broadcastReceiver;
FloatingActionButton fab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_owner_tabs);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
pref = getSharedPreferences("Login.conf", Context.MODE_PRIVATE);
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOffscreenPageLimit(2);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent in = new Intent(OwnerTabs.this, InsertActivity.class);
startActivity(in);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.action_logout){
editor = pref.edit();
editor.clear();
editor.commit();
Intent in = new Intent (getApplicationContext(), MainActivity.class);
startActivity(in);
finish();
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (getArguments().getInt(ARG_SECTION_NUMBER) == 1){
View rootView = inflater.inflate(R.layout.content_ownerhome, container, false);
return rootView;
}
else if(getArguments().getInt(ARG_SECTION_NUMBER) == 2){
View rootView = inflater.inflate(R.layout.content_rented, container, false);
return rootView;
}
else if (getArguments().getInt(ARG_SECTION_NUMBER) == 3){
View rootView = inflater.inflate(R.layout.content_gps, container, false);
return rootView;
}
else {
View rootView = inflater.inflate(R.layout.fragment_owner_tabs, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Your Cars";
case 1:
return "Pending Cars";
case 2:
return "GPS";
}
return null;
}
}
Thanks for the help guys, hoping to get a kind response and not an arrogant one. :)

Add a on tab selected listener, and then put on the tab index you want, and call fab.hide()

Related

Launching activity from fragment tab layout

I have a Problem with launching an Activity from Fragment.
My Application is built like a tab layout with an Activity in the background and fragment with the tabs. In one of this tabs I want to set a Button, this Button has to launch the Activity. I tried everything but I'm wondering why it's not working.
The Logcat also shows nothing.
This is the Fragment with the Button
public class Freestyle extends Fragment {
public Freestyle() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_freestyle, container, false);
Button btnOpen;
btnOpen =(Button)view.findViewById(R.id.btnOpenPlan);
btnOpen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(getContext(),Plan.class);
startActivity(intent);
}
});
return view;
}
}
This is the Code from the Activity, which has to be launched
public class Plan extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_plan);
}
}
This is the Code from the Background Activity
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container2);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
//eine Serververbindung
rootView = inflater.inflate(R.layout.fragment_freestyle, container, false);
break;
case 2:
//eine weitere
rootView = inflater.inflate(R.layout.fragment_events, container, false);
break;
case 3:
//eine weitere
rootView = inflater.inflate(R.layout.fragment_map, container, false);
break;
case 4:
//eine weitere
rootView = inflater.inflate(R.layout.fragment_tipp, container, false);
break;
case 5:
//eine weitere
rootView = inflater.inflate(R.layout.fragment_person, container, false);
break;
}
return rootView;
}
}
public static class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
return 5;
}
}
Use this,
btnOpen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Plan.class);
getActivity().startActivity(intent);
}
});
Try this
Intent intent=new Intent(getActivity(),Plan.class);
getActivity().startActivity(intent);
Try using the getActivity(type casted to your Activity instance) to start new Activity from fragment
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(((MainActivity) getActivity()), Plan.class);
((MainActivity) getActivity()).startActivity(intent);
}
});
Hope this helps.
Try something like this
btnLaunchActivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Check if getActivity() is actually not null, could be null if fragment detatched, you could also check if its an instance you expect to be
if(getActivity() != null && getActivity() instanceof MyActivity) {
Intent intent = new Intent(getActivity(), TargetActivity.class);
//Launch the intent from the activity reference
getActivity().startActivity(intent);
}
}
});

activity is blank in tab activity

I am creating ListView in my app in tab Activity.but when app runs emulator shows blank activity. I don't know what is the issue with this. Please help. I am not getting list in the tab. I am getting list in simple activity without tab but I can't see list in tab activity. Let me know if you want other information.
This my ArtistTab.java activity
public class ArtistsTab extends Fragment {
ListView listView1;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.artist, container, false);
listView1 =(ListView)v.findViewById(R.id.listView);
return v;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// add your code here which executes after the execution of onCreateView() method.
ArrayList<String>my=new ArrayList<>();
my.add("devesh");
my.add("devesh");
my.add("devesh");
my.add("devesh");
ArrayAdapter arrayAdapter=new ArrayAdapter(getActivity(), layout.simple_list_item_1,my);
listView1.setAdapter(arrayAdapter);
}
}
This is my main Activity
public class MainActivity extends AppCompatActivity {
public ViewPagerAdapter pagerAdapter;
private ViewPager mViewPager;
private Toolbar toolbar;
private TabLayout tabLayout;
public static View rootView;
public static int tabNo;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
mViewPager.setAdapter(pagerAdapter);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition());//setting current selected item over viewpager
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// View rootView;
switch (getArguments().getInt(ARG_SECTION_NUMBER))
{
case 1: {
rootView = inflater.inflate(R.layout.songs, container, false);
break;
}
case 2: {
rootView = inflater.inflate(R.layout.album, container, false);
break;
}
case 3: {
rootView = inflater.inflate(R.layout.genres, container, false);
break;
}
case 4: {
rootView = inflater.inflate(R.layout.artist, container, false);
break;
}
}
return rootView;
}
}
}
artistTab xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView" />
</RelativeLayout>
Try like this to tabs with viewpager
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new ArtistsTab(), "ArtistsTab");
adapter.addFragment(new SongsTab(), "Songs");
//Do your stuff here
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}

Orientation with Fragments, text is not saved

When I change the orientation of my mobile phone, the text content is not saved and the default text is put in the textview, in my android manifest in my activity Cafeteria I have this
android:configChanges="keyboardHidden|orientation"
Cafeteria
public class Cafeteria extends BaseActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cafeteria);
/*Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);*/
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_cafeteria, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
Oferta oferta = new Oferta();
return oferta;
case 1:
MenuCafe men = new MenuCafe();
return men;
case 2:
Carta carta = new Carta();
return carta;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.ofertas);
case 1:
return getString(R.string.menu);
case 2:
return getString(R.string.carta);
}
return null;
}
}
MenuCafe
public class MenuCafe extends Fragment{
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
private AdaptadorMenu adapter;
private List<Menu> menu;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_menu, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view_menu);
menu = new ArrayList<>();
load_data_from_server(0);
linearLayoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(linearLayoutManager);
adapter = new AdaptadorMenu(getContext(),menu);
recyclerView.setAdapter(adapter);
return rootView;
}
}
Thank you for the help
Try giving this in Android manifest file
android:screenOrientation="portrait"
You must save text content , because when rotation screen ,your activity will reset life cycle of android, same as first open.
See how to save the status here: Losing data when rotate screen

Application crashes after 3rd fragment?

On tab, I have attached five fragments.Till 3rd fragment navigation is fine but when going to 4th and 5th fragment directly or by swiping, app is getting crashed.
This is my main activity.
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Videos"));
tabLayout.addTab(tabLayout.newTab().setText("Games"));
tabLayout.addTab(tabLayout.newTab().setText("Maps"));
tabLayout.addTab(tabLayout.newTab().setText("Quizze"));
tabLayout.addTab(tabLayout.newTab().setText("Discussion"));
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm, int tabCount) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Videos tab1 = new Videos();
return tab1;
case 1:
Notes tab2 = new Notes();
return tab2;
case 2:
MindMaps tab3 = new MindMaps();
return tab3;
case 3:
Quizze tab4 = new Quizze();
return tab4;
case 5:
Discussion tab5 = new Discussion();
return tab5;
}
return null;
}
#Override
public int getCount() {
return 5;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
case 3:
return "SECTION 4";
case 4:
return "SECTION 5";
}
return null;
}
}
}
This is my fragment class which is right now same for all the five fragments except the class name).
public class Videos extends android.support.v4.app.Fragment {
public static Videos newInstance() {
Videos fragment = new Videos();
return fragment;
}
public Videos() {
}
Button ClickMe;
TextView tv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.video, container, false);
ClickMe = (Button) rootView.findViewById(R.id.button);
tv = (TextView) rootView.findViewById(R.id.textView2);
ClickMe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(tv.getText().toString().contains("Hello")){
tv.setText("Hi molu");
}else tv.setText("Hello");
}
});
return rootView;
}
} // This is the end of our MyFragments Class
Case 4 is missing in your adapter. Add the Case 4 instead of 5 then it will work fine.

Button inside fragment

I'm hoping someone can help me with this extremely annoying problem. I'm new to working with fragments. I've have spend two days trying to get buttons to work inside my fragment. My app is a tab activity with sliding fragments auto created by android studio. I can change fragments by sliding and using the tabs. But I cannot get the buttons to respond. My app does not crash and my Log.e doesn't get registered in Logcat. I have copied lots of examples from the internet, but nothing seems to work.
I have tried implementing View.OnClickListener and not implementing it but nothing works. I'll post two examples that should work, but they don't.
FRAGMENT without implementing View.OnClickListener
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_summary_loggs, container, false);
Button test = (Button) rootView.findViewById(R.id.testButton);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e("DEBUGG", "BUTTON PRESSED");
}
});
return rootView;
FRAGMENT with implementing View.OnClickListener
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_summary_loggs, container, false);
Button test = (Button) rootView.findViewById(R.id.testButton);
test.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.testButton:
Log.e("DEBUGG", "BUTTON PRESSED");
break;
}
}
This is only two examples of many that I have tried and it is driving me crazy. On all the examples on internet they all get them to work. My buttons simply won't respond when I press them. I will be extremely thankful if you could help me with this.
Layoutfile
<FrameLayout 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" tools:context="xxxxxx.SummaryLoggs">
<!-- TODO: Update blank fragment layout -->
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:text="#string/hello_blank_fragment3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:id="#+id/testButton"
android:layout_gravity="center" />
</FrameLayout>
ACTIVITY where i swipe fragments view
import...
public class AmLogger extends ActionBarActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
Handler customHandler = new Handler();
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_am_logger);
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_am_logger, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
private int mPosition;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPosition = getArguments().getInt(ARG_SECTION_NUMBER);
Log.e("DEBUGG", "mPosition: " + mPosition);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_summary_loggs, container, false);
switch (mPosition) {
case 1:
rootView = inflater.inflate(R.layout.fragment_add_time, container, false);
break;
case 2:
rootView = inflater.inflate(R.layout.fragment_item_list, container, false);
break;
}
return rootView;
}
}
}
I think you're not creating 3 fragments. You should have 3 fragment classes in your activity one for each tab and also depending upon design for each fragment, you need to have 3 layout files. Then declare the button in your appropriate layout file and use it in your Fragment class as shown below.
public class AmLogger extends ActionBarActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
Handler customHandler = new Handler();
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_am_logger);
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_am_logger, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
// AddTime fragment
return AddTime.newInstance(position + 1);
case 1:
// MyLogs fragment
return MyLogs.newInstance(position + 1);
case 2:
// SummaryLogs fragment
return SummaryLogs.newInstance(position + 1);
}
return null;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
//AddTime fragment
public static class AddTime extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static AddTime newInstance(int sectionNumber) {
AddTime fragment = new AddTime();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public AddTime() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_addtime, container, false);
return rootView;
}
}
//MyLogs Fragment
public static class MyLogs extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static MyLogs newInstance(int sectionNumber) {
MyLogs fragment = new MyLogs();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public MyLogs() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_mylogs, container, false);
return rootView;
}
}
//SummaryLogs fragment
public static class SummaryLogs extends Fragment implements View.OnClickListener{
private static final String ARG_SECTION_NUMBER = "section_number";
public static SummaryLogs newInstance(int sectionNumber) {
SummaryLogs fragment = new SummaryLogs();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public SummaryLogs() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_summarylogs, container, false);
//Code to get the button from layout file
Button btn = (Button) rootView.findViewById(R.id.testButton);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Implement the code to run on button click here
}
});
return rootView;
}
}
}

Categories

Resources