com.limakilogram.www.bawang.ui.main.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.limakilogram.www.bawang.ui.main.MainActivity.java

Source

/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * 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.limakilogram.www.bawang.ui.main;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import com.facebook.appevents.AppEventsLogger;
import com.limakilogram.www.bawang.R;
import com.limakilogram.www.bawang.ui.confirmorder.ConfirmOrderActivity;
import com.limakilogram.www.bawang.ui.detailorder.OrderListActivity;
import com.limakilogram.www.bawang.ui.main.grosirfragment.GrosirFragment;
import com.limakilogram.www.bawang.ui.main.limakilofragment.LimakiloFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 */
public class MainActivity extends AppCompatActivity {
    //    implements } ContactsListFragment.OnContactsInteractionListener{

    private DrawerLayout mDrawerLayout;

    // True if this activity instance is a search result view (used on pre-HC devices that load
    // search results in a separate instance of the activity rather than loading results in-line
    // as the query is typed.
    private boolean isSearchResultView = false;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final ActionBar ab = getSupportActionBar();
        ab.setHomeAsUpIndicator(R.drawable.ic_menu);
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setDisplayShowTitleEnabled(false);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        if (navigationView != null) {
            setupDrawerContent(navigationView);
        }

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        if (viewPager != null) {
            setupViewPager(viewPager);
        }

        //        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        //        fab.setOnClickListener(new View.OnClickListener() {
        //            @Override
        //            public void onClick(View view) {
        //                Snackbar.make(view, "Here's a Snackbar", Snackbar.LENGTH_LONG)
        //                        .setAction("Action", null).show();
        //            }
        //        });

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

        // Check if this activity instance has been triggered as a result of a search query. This
        // will only happen on pre-HC OS versions as from HC onward search is carried out using
        // an ActionBar SearchView which carries out the search in-line without loading a new
        // Activity.
        //        if (Intent.ACTION_SEARCH.equals(getIntent().getAction())) {
        //
        //            // Fetch query from intent and notify the fragment that it should display search
        //            // results instead of all contacts.
        //            String searchQuery = getIntent().getStringExtra(SearchManager.QUERY);
        //            ContactsListFragment mContactsListFragment = (ContactsListFragment)
        //                    getSupportFragmentManager().findFragmentById(R.id.contact_list);
        //
        //            // This flag notes that the Activity is doing a search, and so the result will be
        //            // search results rather than all contacts. This prevents the Activity and Fragment
        //            // from trying to a search on search results.
        //            isSearchResultView = true;
        //            mContactsListFragment.setSearchQuery(searchQuery);
        //
        //            // Set special title for search results
        //            String title = getString(R.string.contacts_list_search_results_title, searchQuery);
        //            setTitle(title);
        //        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            mDrawerLayout.openDrawer(GravityCompat.START);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        AppEventsLogger.activateApp(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        AppEventsLogger.deactivateApp(this);
    }

    private void setupViewPager(ViewPager viewPager) {
        Adapter adapter = new Adapter(getSupportFragmentManager());
        adapter.addFragment(new LimakiloFragment(), "Paket");
        adapter.addFragment(new GrosirFragment(), "Grosir");
        viewPager.setAdapter(adapter);
    }

    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                menuItem.setChecked(true);
                mDrawerLayout.closeDrawers();

                Context context = getApplicationContext();

                switch (menuItem.getItemId()) {
                case R.id.nav_home:
                    Intent intent1 = new Intent(context, OrderListActivity.class);
                    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent1.putExtra(OrderListActivity.EXTRA_HISTORY, "order");

                    context.startActivity(intent1);
                    break;
                case R.id.nav_messages:

                    Intent intent2 = new Intent(context, OrderListActivity.class);
                    intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent2.putExtra(OrderListActivity.EXTRA_HISTORY, "bid");
                    context.startActivity(intent2);
                    break;

                }
                return true;
            }
        });
    }

    //    @Override
    //    public void onContactSelected(Uri contactUri) {
    //        Toast.makeText(this, "oncontactseleceted", Toast.LENGTH_SHORT).show();
    //    }
    //
    //    @Override
    //    public void onSelectionCleared() {
    //        Toast.makeText(this, "onselecetioncleared", Toast.LENGTH_SHORT).show();
    //    }

    static class Adapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();
        private final List<String> mFragmentTitles = new ArrayList<>();

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles.get(position);
        }
    }

    @Override
    public boolean onSearchRequested() {
        // Don't allow another search if this activity instance is already showing
        // search results. Only used pre-HC.
        return !isSearchResultView && super.onSearchRequested();
    }

}