com.jimandreas.android.designlibdemo.SettingsActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.jimandreas.android.designlibdemo.SettingsActivity.java

Source

/*
 * Copyright (c) 2016 Ha Duy Trung
 * Copyright 2017 Jim Andreas
 *
 * 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.jimandreas.android.designlibdemo;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatDelegate;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SettingsActivity extends BottomNavViewBaseActivity implements SettingsFragment.RestartCallback {

    private static final int WHOAMI_NAVBAR_MENUID = R.id.action_settings;

    @BindView(R.id.collapsing_toolbar)
    CollapsingToolbarLayout ctl;
    @BindView(R.id.bottom_navigation)
    BottomNavigationView bottomNavigationView;
    @BindView(R.id.backdrop_settings_image)
    ImageView backdrop_image;

    Context context;

    @Override
    public BottomNavigationView getBottomNavigationView() {
        BottomNavigationView bnv = (BottomNavigationView) findViewById(R.id.bottom_navigation);
        return bnv;
    }

    @Override
    public int getActivityMenuID() {
        return WHOAMI_NAVBAR_MENUID;
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_settings;
    }

    public void doRestart() {
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                recreate();
            }
        }, 300);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_settings);
        ButterKnife.bind(this);

        context = this;

        ctl.setTitle(getResources().getString(R.string.text_settings));

        loadBackdrop();

        if (savedInstanceState == null) {
            Bundle args = new Bundle();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.settings_container,
                            Fragment.instantiate(this, SettingsFragment.class.getName(), args),
                            SettingsFragment.class.getName())
                    .commit();
        }
    }

    void setNightMode(@AppCompatDelegate.NightMode int nightMode) {
        AppCompatDelegate.setDefaultNightMode(nightMode);
    }

    private void loadBackdrop() {
        final ImageView imageView = (ImageView) findViewById(R.id.backdrop_settings_image);
        Glide.with(this).load(R.drawable.settings_backdrop).centerCrop().into(imageView);
    }
}