Search in sources :

Example 31 with NavigationView

use of android.support.design.widget.NavigationView in project LeMondeRssReader by MBach.

the class MainActivity method setupDrawerLayout.

private void setupDrawerLayout() {
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(this);
    navigationView.getMenu().getItem(1).setChecked(true);
}
Also used : NavigationView(android.support.design.widget.NavigationView)

Example 32 with NavigationView

use of android.support.design.widget.NavigationView in project twicalico by moko256.

the class MainActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    subscription = new CompositeSubscription();
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.getChildAt(0).setOnClickListener(v -> {
        Fragment fragment = getMainFragment();
        if (fragment instanceof MovableTopInterface) {
            ((MovableTopInterface) fragment).moveToTop();
        }
    });
    drawer = findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();
    drawer.addDrawerListener(new DrawerLayout.DrawerListener() {

        @Override
        public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
        }

        @Override
        public void onDrawerOpened(@NonNull View drawerView) {
        }

        @Override
        public void onDrawerClosed(@NonNull View drawerView) {
            if (isDrawerAccountsSelection) {
                changeIsDrawerAccountsSelection();
            }
        }

        @Override
        public void onDrawerStateChanged(int newState) {
        }
    });
    navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(item -> {
        int id = item.getItemId();
        if (!item.isChecked()) {
            switch(id) {
                case R.id.nav_timeline:
                    replaceFragment(new HomeTimeLineFragment());
                    break;
                case R.id.nav_mentions:
                    replaceFragment(new MentionsFragment());
                    break;
                case R.id.nav_account:
                    startMyUserActivity();
                    break;
                case R.id.nav_follow_and_follower:
                    replaceFragment(new MyFollowFollowerFragment());
                    break;
                case R.id.nav_like:
                    replaceFragment(UserLikeFragment.newInstance(GlobalApplication.userId));
                    break;
                case R.id.nav_settings:
                    startActivity(new Intent(this, SettingsActivity.class));
                    break;
            }
        }
        drawer.closeDrawer(GravityCompat.START);
        return (id != R.id.nav_settings) && (id != R.id.nav_account);
    });
    headerView = navigationView.inflateHeaderView(R.layout.nav_header_main);
    userNameText = headerView.findViewById(R.id.user_name);
    userIdText = headerView.findViewById(R.id.user_id);
    userImage = headerView.findViewById(R.id.user_image);
    userBackgroundImage = headerView.findViewById(R.id.user_bg_image);
    userBackgroundImage.setOnClickListener(v -> changeIsDrawerAccountsSelection());
    updateDrawerImage();
    accountListView = new RecyclerView(this);
    accountListView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    accountListView.setLayoutManager(new LinearLayoutManager(this));
    accountListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
    accountListView.setVisibility(View.GONE);
    navigationView.addHeaderView(accountListView);
    SelectAccountsAdapter adapter = new SelectAccountsAdapter(this);
    adapter.setOnImageButtonClickListener(accessToken -> {
        drawer.closeDrawer(GravityCompat.START);
        if (accessToken.getUserId() != GlobalApplication.userId) {
            PreferenceManager.getDefaultSharedPreferences(this).edit().putString("AccountKey", accessToken.getKeyString()).apply();
            ((GlobalApplication) getApplication()).initTwitter(accessToken);
            updateDrawerImage();
            clearAndPrepareFragment();
        }
    });
    adapter.setOnAddButtonClickListener(v -> {
        PreferenceManager.getDefaultSharedPreferences(this).edit().putString("AccountKey", "-1").apply();
        GlobalApplication.twitter = null;
        startActivity(new Intent(this, OAuthActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
    });
    adapter.setOnRemoveButtonClickListener(v -> new AlertDialog.Builder(this).setMessage(R.string.confirm_logout).setCancelable(true).setPositiveButton(R.string.do_logout, (dialog, i) -> {
        SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        TokenSQLiteOpenHelper helper = new TokenSQLiteOpenHelper(this);
        helper.deleteAccessToken(helper.getAccessToken(defaultSharedPreferences.getString("AccountKey", "-1")));
        int point = helper.getSize() - 1;
        if (point != -1) {
            AccessToken accessToken = helper.getAccessTokens()[point];
            defaultSharedPreferences.edit().putString("AccountKey", accessToken.getKeyString()).apply();
            ((GlobalApplication) getApplication()).initTwitter(accessToken);
            updateDrawerImage();
            clearAndPrepareFragment();
        } else {
            adapter.getOnAddButtonClickListener().onClick(v);
        }
        helper.close();
    }).setNeutralButton(R.string.back, (dialog, i) -> dialog.cancel()).show());
    accountListView.setAdapter(adapter);
    subscription.add(Single.create(singleSubscriber -> {
        TokenSQLiteOpenHelper helper = new TokenSQLiteOpenHelper(this);
        AccessToken[] accessTokens = helper.getAccessTokens();
        helper.close();
        ArrayList<Pair<User, AccessToken>> r = new ArrayList<>(accessTokens.length);
        for (AccessToken accessToken : accessTokens) {
            long id = accessToken.getUserId();
            CachedUsersSQLiteOpenHelper userHelper = new CachedUsersSQLiteOpenHelper(this, id, accessToken.getType() == Type.TWITTER);
            User user = userHelper.getCachedUser(id);
            if (user == null) {
                try {
                    user = ((GlobalApplication) getApplication()).getTwitterInstance(accessToken).verifyCredentials();
                    userHelper.addCachedUser(user);
                } catch (TwitterException e) {
                    singleSubscriber.onError(e);
                    return;
                } finally {
                    userHelper.close();
                }
            }
            r.add(new Pair<>(user, accessToken));
        }
        singleSubscriber.onSuccess(r);
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(o -> {
        ArrayList<Pair<User, AccessToken>> pairs = (ArrayList<Pair<User, AccessToken>>) o;
        adapter.getImagesList().addAll(pairs);
        adapter.notifyDataSetChanged();
    }, Throwable::printStackTrace));
    findViewById(R.id.fab).setOnClickListener(v -> startActivity(new Intent(this, PostActivity.class)));
    tabLayout = findViewById(R.id.toolbar_tab);
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            Fragment fragment = ((FragmentPagerAdapter) ((UseTabsInterface) getMainFragment()).getTabsViewPager().getAdapter()).getFragment(tab.getPosition());
            if (fragment instanceof MovableTopInterface) {
                ((MovableTopInterface) fragment).moveToTop();
            }
        }
    });
    tweetListViewPool = new RecyclerView.RecycledViewPool();
    userListViewPool = new RecyclerView.RecycledViewPool();
    getSupportFragmentManager().addOnBackStackChangedListener(() -> attachFragment(getMainFragment()));
    if (savedInstanceState == null) {
        prepareFragment();
    }
}
Also used : NavigationView(android.support.design.widget.NavigationView) LinearLayout(android.widget.LinearLayout) Type(com.github.moko256.twicalico.entity.Type) Bundle(android.os.Bundle) ImageView(android.widget.ImageView) CachedUsersSQLiteOpenHelper(com.github.moko256.twicalico.database.CachedUsersSQLiteOpenHelper) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) Intent(android.content.Intent) NonNull(android.support.annotation.NonNull) TwitterStringUtils(com.github.moko256.twicalico.text.TwitterStringUtils) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) TabLayout(android.support.design.widget.TabLayout) GravityCompat(android.support.v4.view.GravityCompat) Single(rx.Single) Menu(android.view.Menu) Schedulers(rx.schedulers.Schedulers) User(twitter4j.User) View(android.view.View) TwitterException(twitter4j.TwitterException) PreferenceManager(android.preference.PreferenceManager) DrawerLayout(android.support.v4.widget.DrawerLayout) AccessToken(com.github.moko256.twicalico.entity.AccessToken) Fragment(android.support.v4.app.Fragment) ActivityOptionsCompat(android.support.v4.app.ActivityOptionsCompat) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) AppCompatActivity(android.support.v7.app.AppCompatActivity) ViewGroup(android.view.ViewGroup) CompositeSubscription(rx.subscriptions.CompositeSubscription) RecyclerView(android.support.v7.widget.RecyclerView) FragmentManager(android.support.v4.app.FragmentManager) AlertDialog(android.support.v7.app.AlertDialog) TextView(android.widget.TextView) SharedPreferences(android.content.SharedPreferences) Toolbar(android.support.v7.widget.Toolbar) Pair(android.support.v4.util.Pair) TokenSQLiteOpenHelper(com.github.moko256.twicalico.database.TokenSQLiteOpenHelper) FragmentPagerAdapter(com.github.moko256.twicalico.widget.FragmentPagerAdapter) ActionBarDrawerToggle(android.support.v7.app.ActionBarDrawerToggle) User(twitter4j.User) ActionBarDrawerToggle(android.support.v7.app.ActionBarDrawerToggle) ArrayList(java.util.ArrayList) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) Fragment(android.support.v4.app.Fragment) AccessToken(com.github.moko256.twicalico.entity.AccessToken) TabLayout(android.support.design.widget.TabLayout) DrawerLayout(android.support.v4.widget.DrawerLayout) TwitterException(twitter4j.TwitterException) TokenSQLiteOpenHelper(com.github.moko256.twicalico.database.TokenSQLiteOpenHelper) Pair(android.support.v4.util.Pair) SharedPreferences(android.content.SharedPreferences) CachedUsersSQLiteOpenHelper(com.github.moko256.twicalico.database.CachedUsersSQLiteOpenHelper) Intent(android.content.Intent) NavigationView(android.support.design.widget.NavigationView) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) CompositeSubscription(rx.subscriptions.CompositeSubscription) RecyclerView(android.support.v7.widget.RecyclerView) LinearLayout(android.widget.LinearLayout)

Example 33 with NavigationView

use of android.support.design.widget.NavigationView in project IITB-App by wncc.

the class MainActivity method updateNavigationView.

private void updateNavigationView() {
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    View header = navigationView.getHeaderView(0);
    header.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Bundle bundle = new Bundle();
            bundle.putString(Constants.USER_ID, currentUser.getUserID());
            ProfileFragment profileFragment = new ProfileFragment();
            profileFragment.setArguments(bundle);
            updateFragment(profileFragment);
            DrawerLayout drawer = findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
        }
    });
    TextView nameTextView = header.findViewById(R.id.user_name_nav_header);
    TextView rollNoTextView = header.findViewById(R.id.user_rollno_nav_header);
    ImageView profilePictureImageView = header.findViewById(R.id.user_profile_picture_nav_header);
    nameTextView.setText(currentUser.getUserName());
    rollNoTextView.setText(currentUser.getUserRollNumber());
    Picasso.with(this).load(currentUser.getUserProfilePictureUrl()).into(profilePictureImageView);
}
Also used : NavigationView(android.support.design.widget.NavigationView) Bundle(android.os.Bundle) TextView(android.widget.TextView) ProfileFragment(in.ac.iitb.gymkhana.iitbapp.fragment.ProfileFragment) ImageView(android.widget.ImageView) DrawerLayout(android.support.v4.widget.DrawerLayout) NavigationView(android.support.design.widget.NavigationView) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView)

Example 34 with NavigationView

use of android.support.design.widget.NavigationView in project todo-mvp-rxjava by albertizzy.

the class NavigationViewActions method navigateTo.

/**
 * Returns a {@link ViewAction} that navigates to a menu item in {@link NavigationView} using a
 * menu item resource id.
 *
 * <p>
 * View constraints:
 * <ul>
 * <li>View must be a child of a {@link DrawerLayout}
 * <li>View must be of type {@link NavigationView}
 * <li>View must be visible on screen
 * <li>View must be displayed on screen
 * <ul>
 *
 * @param menuItemId the resource id of the menu item
 * @return a {@link ViewAction} that navigates on a menu item
 */
public static ViewAction navigateTo(final int menuItemId) {
    return new ViewAction() {

        @Override
        public void perform(UiController uiController, View view) {
            NavigationView navigationView = (NavigationView) view;
            Menu menu = navigationView.getMenu();
            if (null == menu.findItem(menuItemId)) {
                throw new PerformException.Builder().withActionDescription(this.getDescription()).withViewDescription(HumanReadables.describe(view)).withCause(new RuntimeException(getErrorMessage(menu, view))).build();
            }
            menu.performIdentifierAction(menuItemId, 0);
            uiController.loopMainThreadUntilIdle();
        }

        private String getErrorMessage(Menu menu, View view) {
            String NEW_LINE = System.getProperty("line.separator");
            StringBuilder errorMessage = new StringBuilder("Menu item was not found, " + "available menu items:").append(NEW_LINE);
            for (int position = 0; position < menu.size(); position++) {
                errorMessage.append("[MenuItem] position=").append(position);
                MenuItem menuItem = menu.getItem(position);
                if (menuItem != null) {
                    CharSequence itemTitle = menuItem.getTitle();
                    if (itemTitle != null) {
                        errorMessage.append(", title=").append(itemTitle);
                    }
                    if (view.getResources() != null) {
                        int itemId = menuItem.getItemId();
                        try {
                            errorMessage.append(", id=");
                            String menuItemResourceName = view.getResources().getResourceName(itemId);
                            errorMessage.append(menuItemResourceName);
                        } catch (NotFoundException nfe) {
                            errorMessage.append("not found");
                        }
                    }
                    errorMessage.append(NEW_LINE);
                }
            }
            return errorMessage.toString();
        }

        @Override
        public String getDescription() {
            return "click on menu item with id";
        }

        @Override
        public Matcher<View> getConstraints() {
            return allOf(isAssignableFrom(NavigationView.class), withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE), isDisplayingAtLeast(90));
        }
    };
}
Also used : NavigationView(android.support.design.widget.NavigationView) UiController(android.support.test.espresso.UiController) NotFoundException(android.content.res.Resources.NotFoundException) MenuItem(android.view.MenuItem) NavigationView(android.support.design.widget.NavigationView) View(android.view.View) ViewAction(android.support.test.espresso.ViewAction) Menu(android.view.Menu)

Example 35 with NavigationView

use of android.support.design.widget.NavigationView in project Varis-Android by dkhmelenko.

the class MainActivity method setupDrawerLayout.

/**
 * Sets up navigation drawer layout
 */
private void setupDrawerLayout() {
    mDrawerLayout = findViewById(R.id.drawer_layout);
    mDrawerLayout.addDrawerListener(mDrawerToggle);
    final NavigationView view = findViewById(R.id.navigation_view);
    view.setNavigationItemSelectedListener(menuItem -> {
        switch(menuItem.getItemId()) {
            case R.id.drawer_login:
                Intent loginIntent = new Intent(MainActivity.this, AuthActivity.class);
                startActivityForResult(loginIntent, AUTH_ACTIVITY_CODE);
                break;
            case R.id.drawer_logout:
                getPresenter().userLogout();
                finish();
                startActivity(getIntent());
                break;
            case R.id.drawer_licenses:
                LicensesDialogFragment dialog = LicensesDialogFragment.newInstance();
                dialog.show(getSupportFragmentManager(), "LicensesDialog");
                break;
            case R.id.drawer_about:
                Intent aboutIntent = new Intent(MainActivity.this, AboutActivity.class);
                startActivity(aboutIntent);
                break;
        }
        menuItem.setChecked(false);
        mDrawerLayout.closeDrawers();
        return true;
    });
}
Also used : LicensesDialogFragment(com.khmelenko.lab.varis.about.LicensesDialogFragment) NavigationView(android.support.design.widget.NavigationView) Intent(android.content.Intent)

Aggregations

NavigationView (android.support.design.widget.NavigationView)70 View (android.view.View)42 Toolbar (android.support.v7.widget.Toolbar)25 ActionBarDrawerToggle (android.support.v7.app.ActionBarDrawerToggle)24 TextView (android.widget.TextView)21 DrawerLayout (android.support.v4.widget.DrawerLayout)17 ImageView (android.widget.ImageView)16 UiController (android.support.test.espresso.UiController)13 ViewAction (android.support.test.espresso.ViewAction)13 Menu (android.view.Menu)10 ActionBar (android.support.v7.app.ActionBar)8 Intent (android.content.Intent)7 FloatingActionButton (android.support.design.widget.FloatingActionButton)7 MenuItem (android.view.MenuItem)7 Handler (android.os.Handler)6 SearchView (android.support.v7.widget.SearchView)6 BindView (butterknife.BindView)6 FlexboxLayout (com.google.android.flexbox.FlexboxLayout)6 TabLayout (android.support.design.widget.TabLayout)5 FlakyTest (android.support.test.filters.FlakyTest)5