com.eusecom.attendance.AbsenceActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.eusecom.attendance.AbsenceActivity.java

Source

/*
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.eusecom.attendance;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;

import com.eusecom.attendance.fragment.AbsTypesFragment;
import com.eusecom.attendance.fragment.ContactFragment;
import com.eusecom.attendance.fragment.LiveFragment;
import com.eusecom.attendance.fragment.MyAbsenceFragment;
import com.eusecom.attendance.fragment.MyAttendanceFragment;
import com.google.firebase.auth.FirebaseAuth;

public class AbsenceActivity extends BaseDatabaseActivity implements LiveFragment.OnFragmentInteractionListener {

    private FragmentPagerAdapter mPagerAdapter;
    private ViewPager mViewPager;
    private FloatingActionButton fab;
    int whatispage = 0;
    Toolbar mActionBarToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_absence);
        //showProgressDialog();

        mActionBarToolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(mActionBarToolbar);
        getSupportActionBar().setTitle(getString(R.string.absence));

        // Create the adapter that will return a fragment for each section
        mPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            private final Fragment[] mFragments = new Fragment[] { new MyAbsenceFragment(),
                    new MyAttendanceFragment(), new AbsTypesFragment() };
            private final String[] mFragmentNames = new String[] { "Absences", "Attendances", "AbsTypes" };

            @Override
            public Fragment getItem(int position) {
                return mFragments[position];
            }

            @Override
            public int getCount() {
                return mFragments.length;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mFragmentNames[position];
            }
        };
        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mPagerAdapter);

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            public void onPageScrollStateChanged(int state) {
            }

            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            public void onPageSelected(int position) {
                // Check if this is the page you want.
                if (position == 0) {
                    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_new_post);
                    fab.setVisibility(View.VISIBLE);
                    whatispage = 0;
                }
                if (position == 1) {
                    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_new_post);
                    fab.setVisibility(View.GONE);
                    whatispage = 1;
                }
                if (position == 2) {
                    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_new_post);
                    fab.setVisibility(View.GONE);
                    whatispage = 2;
                }
            }
        });

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

        // Button launches NewPostActivity
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_new_post);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent i = null;
                if (whatispage == 0) {
                    i = new Intent(AbsenceActivity.this, NewAbsenceActivity.class);
                }
                if (whatispage == 2) {
                    i = new Intent(AbsenceActivity.this, NewPostActivity.class);
                }
                Bundle extras = new Bundle();
                extras.putString("editx", "0");
                extras.putString("keyx", "0");

                i.putExtras(extras);
                startActivity(i);

            }
        });
        //hideProgressDialog();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_database, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_logout:
            FirebaseAuth.getInstance().signOut();
            startActivity(new Intent(this, EmailPasswordActivity.class));
            finish();
            return true;

        case R.id.action_settings:
            FirebaseAuth.getInstance().signOut();
            startActivity(new Intent(this, SettingsActivity.class));
            finish();
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onFragmentInteraction(Uri uri) {
        //only for LiveFragment
    }

}