Java tutorial
package com.bamalearn.bamalearnburmese; import android.content.res.Configuration; import android.database.MatrixCursor; import android.graphics.Color; import android.graphics.Typeface; import android.provider.BaseColumns; import android.support.v4.app.FragmentManager; import android.support.v4.view.GravityCompat; import android.support.v4.view.MenuItemCompat; import android.support.v4.widget.CursorAdapter; import android.support.v4.widget.DrawerLayout; import android.os.Bundle; import android.support.v4.widget.SimpleCursorAdapter; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.SearchView; import android.support.v7.widget.Toolbar; import android.view.Gravity; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.LinearLayout; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; import com.afollestad.materialdialogs.MaterialDialog; import DAL.DAL_Settings; import adapter.DrawerList_Adapter; import baseclass.BaseActivity; import butterknife.ButterKnife; import butterknife.InjectView; import common.BaseFactory; import common.Common_helper; import config.Constant_variables; import db_helper.Db_Help; import fragments.Fragment_About; import fragments.Fragment_Diy_container; import fragments.Fragment_Licenses; import fragments.Fragment_Settings; import fragments.Fragment_howtouse; import fragments.Fragment_vocablist; import model.Word; import uk.co.chrisjenx.calligraphy.CalligraphyConfig; public class DrawerMainActivity extends BaseActivity { @InjectView(R.id.drawer_layout) DrawerLayout mDrawerLayout; Toolbar toolbar; @InjectView(R.id.left_drawer) LinearLayout mDrawerListLayout; @InjectView(R.id.left_drawer_lv) ListView mDrawerList; @InjectView(R.id.title_action2) TextView mTitle; TextView app_version; ActionBarDrawerToggle mDrawerToggle; DAL_Settings mDalSettings; String[] DrawerMenuList; int[] DrawerIcons; BaseFactory dalfactory = new BaseFactory(); Common_helper common_helper; private static final String[] SUGGESTIONS = { "Bauru", "Sao Paulo", "Rio de Janeiro", "Bahia", "Mato Grosso", "Minas Gerais", "Tocantins", "Rio Grande do Sul" }; private SimpleCursorAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); common_helper = new Common_helper(this); CalligraphyConfig.initDefault(new CalligraphyConfig.Builder().setDefaultFontPath("fonts/Roboto-Regular.ttf") .setFontAttrId(R.attr.fontPath).build()); final String[] from = new String[] { "cityName" }; final int[] to = new int[] { android.R.id.text1 }; mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); setContentView(R.layout.activity_drawer_main); ButterKnife.inject(this); toolbar = (Toolbar) findViewById(R.id.toolbar); app_version = (TextView) findViewById(R.id.app_version); app_version.setText("Version " + common_helper.getAppVersion(this) + "\n"); app_version.setTypeface(null, Typeface.BOLD); //toolbar.inflateMenu(R.menu.menu_drawer_main); mDalSettings = new DAL_Settings(DrawerMainActivity.this); /* MenuItem menuItem=toolbar.getMenu().findItem(R.id.action_search); SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem); searchView.setSuggestionsAdapter(mAdapter); searchView.setOnSuggestionListener(new QueryListener()); searchView.setOnQueryTextListener(new QueryListener()); searchView.setQueryHint("Search Here"); changeSearchViewTextColor(searchView); */ initialize(); binddataTOList(); Db_Help dbhelper = new Db_Help(this); if (mDalSettings.getStringPreference("DB_EXISTS", "").equals("")) { dbhelper.MakeDB(); mDalSettings.setStringPreference("DB_EXISTS", "true"); } makeFragmentSelection(0); } protected class QueryListener implements SearchView.OnSuggestionListener, SearchView.OnQueryTextListener { @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { populateAdapter(newText); return false; } @Override public boolean onSuggestionSelect(int position) { return false; } @Override public boolean onSuggestionClick(int position) { return false; } } private void populateAdapter(String query) { final MatrixCursor c = new MatrixCursor(new String[] { BaseColumns._ID, "cityName" }); for (int i = 0; i < SUGGESTIONS.length; i++) { if (SUGGESTIONS[i].toLowerCase().startsWith(query.toLowerCase())) c.addRow(new Object[] { i, SUGGESTIONS[i] }); } mAdapter.changeCursor(c); } private void changeSearchViewTextColor(View view) { if (view != null) { if (view instanceof TextView) { ((TextView) view).setTextColor(Color.WHITE); ((TextView) view).setHintTextColor(Color.WHITE); return; } else if (view instanceof ViewGroup) { ViewGroup viewGroup = (ViewGroup) view; for (int i = 0; i < viewGroup.getChildCount(); i++) { changeSearchViewTextColor(viewGroup.getChildAt(i)); } } } } private void initialize() { toolbar.setTitle(getResources().getString(R.string.app_name)); toolbar.setTitleTextColor(getResources().getColor(android.R.color.white)); toolbar.setOnMenuItemClickListener(new ToolbarMenuclickListener()); mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); //set toggle to naviation drawer on tollbar mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open_drawer, R.string.close_drawer) { /** Called when a drawer has settled in a completely closed state. */ public void onDrawerClosed(View view) { // getActionBar().setTitle(mTitle); // toolbar.setTitle("PSI DataCapture"); } /** Called when a drawer has settled in a completely open state. */ public void onDrawerOpened(View drawerView) { // toolbar.setTitle(Title); } }; mDrawerLayout.setDrawerListener(mDrawerToggle); mDrawerLayout.openDrawer(GravityCompat.START); } public void makeFragmentSelection(int position) { FragmentManager fragmentManager = getSupportFragmentManager(); switch (position) { case 0: toolbar.setTitle(DrawerMenuList[position]); fragmentManager.beginTransaction().replace(R.id.content_frame, new Fragment_Diy_container()).commit(); break; case 1: toolbar.setTitle(DrawerMenuList[position]); fragmentManager.beginTransaction().replace(R.id.content_frame, new Fragment_howtouse()).commit(); break; case 2: //Do action here toolbar.setTitle(DrawerMenuList[position]); fragmentManager.beginTransaction().replace(R.id.content_frame, new Fragment_vocablist()).commit(); break; case 3: //Do action here toolbar.setTitle(DrawerMenuList[position]); fragmentManager.beginTransaction().replace(R.id.content_frame, new Fragment_Settings()).commit(); break; case 4: //Do action here toolbar.setTitle(DrawerMenuList[position]); fragmentManager.beginTransaction().replace(R.id.content_frame, new Fragment_About()).commit(); break; case 5: //Do action here toolbar.setTitle(DrawerMenuList[position]); fragmentManager.beginTransaction().replace(R.id.content_frame, new Fragment_Licenses()).commit(); break; } mDrawerList.setItemChecked(position, true); mDrawerLayout.closeDrawer(mDrawerListLayout); } void binddataTOList() { DrawerMenuList = new String[] { "Play", "How to Use", "Vocab List", "Settings", "About BAMA", "OpenSource Licenses" }; //DrawerIcons=new int[]{R.drawable.ic_calendar, R.drawable.ic_setting,R.drawable.ic_info}; DrawerIcons = new int[] { 1, 2, 3, 4, 5 }; DrawerList_Adapter drawerList_adapter = new DrawerList_Adapter(this, DrawerMenuList, DrawerIcons); drawerList_adapter.notifyDataSetChanged(); mDrawerList.setAdapter(drawerList_adapter); mDrawerList.setOnItemClickListener(new DrawerListItemClickListener()); setListViewHeightBasedOnChildren(mDrawerList); Constant_variables.WORDLIST = dalfactory.getIWordDal("word", DrawerMainActivity.this).getWords(); for (Word word : Constant_variables.WORDLIST) { Constant_variables.CHARLIST.add(word.getVocab()); } } @Override public void onBackPressed() { new MaterialDialog.Builder(this).title("Confirm") //.content("This is for prototypical Purpose Only") .content("Are you sure to want to exit BAMA app?").positiveText("Exit").negativeText("Cancel") .callback(new MaterialDialog.ButtonCallback() { @Override public void onPositive(MaterialDialog dialog) { finish(); } @Override public void onNegative(MaterialDialog dialog) { super.onNegative(dialog); } }).show(); } protected class DrawerListItemClickListener implements AdapterView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { makeFragmentSelection(position); } } protected class ToolbarMenuclickListener implements Toolbar.OnMenuItemClickListener { @Override public boolean onMenuItemClick(MenuItem item) { return false; } } public void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom(); for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); if (listItem instanceof ViewGroup) listItem.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT)); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); } }