Académique Documents
Professionnel Documents
Culture Documents
: 7 Date: 27/02/2018
AIM:
To develop an app that uses the different android menus such as the context menu
(floating and action mode), the options menu and pop-up menu, and to implement preferences.
This app has been designed as a note-taking app where the user can view, add and delete
notes. Three types of menu have been implemented in this app. There is a context menu that
allows you to choose what to do with the newly created notes, i.e., should they be edited or
deleted. A pop-up menu asks if the note is to be saved, or if the user would like to access the
settings. The settings can also be directly accessed when the user selects the option menu. The
user may change also preferences of the app, such as the theme, font, and the title text displayed.
SOURCE CODE:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/techie"
tools:context="com.example.nigil.sharedpreferences.MainActivity">
<ImageView
android:id="@+id/img_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:layout_gravity="center"
android:src="@drawable/techie_top"/>
<TextView
android:id="@+id/tv_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="50dp"
android:text="Hello User!"
/>
<!--<Button
android:id="@+id/btn_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:layout_weight="1"
android:layout_marginBottom="20dp"
android:layout_gravity="center"
android:onClick="goToSettings"/>-->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="UPDATE"
android:onClick="showpopup"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listview"></ListView>
<!--<ImageView
android:id="@+id/img_bottom"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="end"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@drawable/techie_bottom" />-->
</LinearLayout>
</LinearLayout>
Activity_settings.xml:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_settings"
android:name="com.example.anantha.sharedpreferences.SettingsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
MainActivity.java:
package com.example.nigil.sharedpreferences;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.preference.PreferenceManager;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
TextView tvHello;
ImageView bottom_image;
LinearLayout layout;
ListView listView;
ArrayAdapter<String> adapter;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpSharedPreferences();
listView = findViewById(R.id.listview);
list.add("Monitor");
list.add("Keyboard");
list.add("Mouse");
list.add("Mother Board");
list.add("Hard Disk");
R.layout.simple_expandable_list_item_1,list);
listView.setAdapter(adapter);
registerForContextMenu(listView);
// startActivity(startSettingsActivity);
// }
public void setUpSharedPreferences(){
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(this);
setItalic(sharedPreferences.getBoolean("italicize",true));
setMyTheme(sharedPreferences.getString("theme","nature"));
setMyName(sharedPreferences.getString("name","User"));
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
tvHello = findViewById(R.id.tv_hello);
if(b)
spannableString.setSpan(new
StyleSpan(Typeface.ITALIC),0,spannableString.length(),0);
//tvHello.setTypeface(null, Typeface.ITALIC);
else
spannableString.setSpan(new
StyleSpan(Typeface.NORMAL),0,spannableString.length(),0);
//tvHello.setTypeface(null,Typeface.NORMAL);
tvHello.setText(spannableString);
top_image = findViewById(R.id.img_top);
// bottom_image = findViewById(R.id.img_bottom);
layout = findViewById(R.id.layout);
if(myTheme.equals("nature")){
top_image.setImageResource(R.drawable.nature_top);
// bottom_image.setImageResource(R.drawable.nature_bottom);
layout.setBackgroundColor(getResources().getColor(R.color.nature));
else if(myTheme.equals("solar")){
top_image.setImageResource(R.drawable.solar_top);
// bottom_image.setImageResource(R.drawable.solar_bottom);
layout.setBackgroundColor(getResources().getColor(R.color.solar));
else if(myTheme.equals("techie")){
top_image.setImageResource(R.drawable.techie_top);
// bottom_image.setImageResource(R.drawable.techie_bottom);
layout.setBackgroundColor(getResources().getColor(R.color.techie));
@Override
if(s.equals("italicize"))
setItalic(sharedPreferences.getBoolean("italicize",true));
if(s.equals("theme")){
setMyTheme(sharedPreferences.getString("theme","nature"));
if(s.equals("name")){
setMyName(sharedPreferences.getString("name","User"));
inflater.inflate(R.menu.main_menu,menu);
return true;
inflater.inflate(R.menu.main_context_menu,menu);
AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId())
case R.id.delete_id:
list.remove(info.position);
adapter.notifyDataSetChanged();
return true;
// Handle action bar item clicks here. The action bar will
int id = item.getItemId();
//noinspection SimplifiableIfStatement
startActivity(startSettingsActivity);
return super.onOptionsItemSelected(item);
@Override
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPrefere
nceChangeListener(this);
super.onDestroy();
popupMenu.setOnMenuItemClickListener((PopupMenu.OnMenuItemClickListener)
MainActivity.this);
inflater.inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.show();
@Override
switch(menuItem.getItemId())
case R.id.add:
Toast.makeText(getBaseContext(),"You selected
p1",Toast.LENGTH_LONG);
list.add("hari");
adapter.notifyDataSetChanged();
return true;
case R.id.remove:
list.remove(2);
Toast.makeText(getBaseContext(),"You selected
p2",Toast.LENGTH_LONG);
return true;
case R.id.p3:
startActivity(startSettingsActivity);
Toast.makeText(getBaseContext(),"You selected
p3",Toast.LENGTH_LONG);
return true;
SettingsActivity.java:
package com.example.nigil.sharedpreferences;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
ActionBar actionBar = this.getSupportActionBar();
if(actionBar != null){
actionBar.setDisplayHomeAsUpEnabled(true);
getSupportFragmentManager().beginTransaction().replace(android.R.id.content,n
ew SettingsFragment()).commit();
// SharedPreferences sharedPref =
PreferenceManager.getDefaultSharedPreferences(this);
SettingsFragment.java:
package com.example.nigil.sharedpreferences;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.preference.CheckBoxPreference;
import android.support.v7.preference.EditTextPreference;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.preference.PreferenceScreen;
@Override
addPreferencesFromResource(R.xml.pref_settings);
SharedPreferences sharedPreferences =
getPreferenceScreen().getSharedPreferences();
for(int i=0;i<count;i++){
setPreferenceSummary(pref,value);
if(prefIndex>=0){
listPreference.setSummary(listPreference.getEntries()[prefIndex]);
preference.setSummary(value);
@Override
if(pref != null){
setPreferenceSummary(pref,value);
@Override
super.onCreate(savedInstanceState);
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChange
Listener(this);
@Override
super.onDestroy();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChan
geListener(this);
RESULT:
The app was created, with the different menu types and shared preferences implemented
successfully.