Java tutorial
/* * Copyright 2016 Carlo Eduardo Rodrguez Espino. * * 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 io.github.carlorodriguez.morningritual; import android.app.Activity; import android.support.v4.content.ContextCompat; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.viewpagerindicator.CirclePageIndicator; public class InfoActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_info); ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { actionBar.setDisplayHomeAsUpEnabled(true); } setTitle(getString(R.string.how_to_use)); SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); ViewPager mViewPager = (ViewPager) findViewById(R.id.container); mViewPager.setAdapter(mSectionsPagerAdapter); CirclePageIndicator titleIndicator = (CirclePageIndicator) findViewById(R.id.titles); titleIndicator.setViewPager(mViewPager); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); return true; default: return super.onOptionsItemSelected(item); } } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { /** * The fragment argument representing the section number for this * fragment. */ private static final String ARG_SECTION_NUMBER = "section_number"; public PlaceholderFragment() { } /** * Returns a new instance of this fragment for the given 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; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_info, container, false); TextView textView = (TextView) rootView.findViewById(R.id.section_label); TextView contentTextView = (TextView) rootView.findViewById(R.id.section_content); ImageView contentImageView = (ImageView) rootView.findViewById(R.id.section_image); switch (getArguments().getInt(ARG_SECTION_NUMBER)) { case 2: setSectionView(getActivity(), contentImageView, R.drawable.ic_time, textView, R.string.morning_ritual, contentTextView, R.string.morning_ritual_section_two); break; case 3: setSectionView(getActivity(), contentImageView, R.drawable.ic_gratitude, textView, R.string.gratitude_title, contentTextView, R.string.gratitude_section); break; case 4: setSectionView(getActivity(), contentImageView, R.drawable.ic_health, textView, R.string.health_title, contentTextView, R.string.health_section); break; case 5: setSectionView(getActivity(), contentImageView, R.drawable.ic_visualization, textView, R.string.visualization_title, contentTextView, R.string.visualization_section); break; default: setSectionView(getActivity(), contentImageView, R.drawable.ic_morning_ritual, textView, R.string.morning_ritual, contentTextView, R.string.morning_ritual_section); break; } return rootView; } } public static void setSectionView(Activity activity, ImageView contentImageView, int drawableId, TextView titleTextView, int titleResId, TextView contentTextView, int contentResId) { contentImageView.setImageDrawable(ContextCompat.getDrawable(activity, drawableId)); titleTextView.setText(activity.getString(titleResId)); contentTextView.setText(activity.getString(contentResId)); } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */ 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 5 total pages. return 5; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return getString(R.string.morning_ritual); case 1: return getString(R.string.morning_ritual); case 2: return getString(R.string.gratitude_title); case 3: return getString(R.string.health_title); case 4: return getString(R.string.visualization_title); } return null; } } }