MainMenu
package com.example.basicrecipes;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class MainMenu extends Activity implements OnClickListener {
public static final String RecipeNamesPref = "RecipeNames";
public static final String NamePref = "Name";
public static final String DescriptionPref = "Description";
public static final String PrefSteps = "How to Prepare";
public static final String CuisinePref = "American";
public static String SelectedRecipe = "SelectedRecipe";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View aboutButton = findViewById(R.id.main_about_button);
aboutButton.setOnClickListener(this);
Button newRecipe = (Button) this.findViewById(R.id.main_new_button);
newRecipe.setOnClickListener(this);
Button exitApp = (Button) this.findViewById(R.id.main_exit_button);
exitApp.setOnClickListener(this);
Button listRecipe = (Button) this.findViewById(R.id.main_list_button);
listRecipe.setOnClickListener(this);
SharedPreferences selectedRecipe;
selectedRecipe = getSharedPreferences(MainMenu.SelectedRecipe, RecipeEntry.MODE_PRIVATE);
SelectedRecipe = selectedRecipe.getString(MainMenu.SelectedRecipe, "New Recipe");
if ("New Recipe" != SelectedRecipe) {
SharedPreferences thisRecipe = getSharedPreferences(SelectedRecipe + "_Detail", RecipeEntry.MODE_PRIVATE);
EditText NameText = (EditText) this.findViewById(R.id.name_new);
NameText.setText(thisRecipe.getString(MainMenu.NamePref, ""));
EditText DescriptionText = (EditText) this.findViewById(R.id.description_new);
DescriptionText.setText(thisRecipe.getString(MainMenu.DescriptionPref, ""));
EditText TextSteps = (EditText) this.findViewById(R.id.new_steps);
TextSteps.setText(thisRecipe.getString(MainMenu.PrefSteps, ""));
Spinner CuisineSelect = (Spinner) this.findViewById(R.id.cuisine_new);
CuisineSelect.setSelection(thisRecipe.getInt(MainMenu.CuisinePref, 0));
}
}
@Override
public void onClick(View thisView) {
switch (thisView.getId()) {
case R.id.main_about_button:
Intent showAbout = new Intent(this, About.class);
startActivity(showAbout);
break;
case R.id.main_list_button:
Intent doMenuClick = new Intent(this, RecipeList.class);
startActivity(doMenuClick);
break;
case R.id.main_new_button:
doMenuClick = new Intent(this, RecipeNew.class);
startActivity(doMenuClick);
break;
case R.id.main_exit_button:
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inf = getMenuInflater();
inf.inflate(R.menu.menu, menu);
// menu.findItem(R.id.main_menu_search).setIntent(new Intent(this, SearchRecipe.class));
menu.findItem(R.id.main_menu_options).setIntent(new Intent(this, Options.class));
menu.findItem(R.id.main_menu_new).setIntent(new Intent(this, RecipeEntry.class));
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem itm) {
super.onOptionsItemSelected(itm);
Intent menuIntent = itm.getIntent();
if (menuIntent != null)
startActivity(menuIntent);
return true;
}
}
RecipeList
package com.example.basicrecipes;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class RecipeList extends ListActivity implements OnItemClickListener {
// Store the recipes as an ArrasyList
private static final ArrayList<String> _RecipeListContents = new ArrayList<String>();
private ListView recipeListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipe_list);
// Retrieve the string containing all recipe names from SharedPreferences
SharedPreferences recipeNames = getSharedPreferences(MainMenu.RecipeNamesPref, RecipeEntry.MODE_PRIVATE);
// Convert the string list of recipes into an array
String[] recipeList = recipeNames.getString(MainMenu.RecipeNamesPref, "Make New").split(",");
// For each recipe in the list, add it to the ArrayList of recipes
for (String recipe: recipeList) {
_RecipeListContents.add(recipe);
}
// Instance the recipe list ListView
recipeListView = (ListView) findViewById(android.R.id.list);
recipeListView.setAdapter(new ListViewAdapter(this));
recipeListView.setTextFilterEnabled(true);
// Add an OnItemClickListener to respond to recipe selections
recipeListView.setOnItemClickListener(this);
}
//setListAdapter(new ListViewAdapter(this));
private static class ListViewAdapter extends BaseAdapter {
private LayoutInflater recipeInflater;
public ListViewAdapter(Context context) {
recipeInflater = LayoutInflater.from(context);
}
// Required method
public int getCount() {
return _RecipeListContents.size();
}
// Required method
public Object getItem(int position) {
return position;
}
// Required method
public long getItemId(int position) {
return position;
}
public View getView(int position, View view, ViewGroup group) {
ListContent contents;
if (view == null) {
view = recipeInflater.inflate(R.layout.recipe_list_inflate, null);
contents = new ListContent();
contents.text = (TextView) view.findViewById(R.id.recipe_first);
contents.text.setCompoundDrawables(view.getResources().getDrawable(R.drawable.arrow), null, null, null);
view.setTag(contents);
} else {
contents = (ListContent) view.getTag();
}
contents.text.setText(_RecipeListContents.get(position));
return view;
}
static class ListContent {
TextView text;
}
}
// When a list item is clicked
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// Set the selected recipe for the next Activity to use
SharedPreferences selectedRecipe = getSharedPreferences(MainMenu.SelectedRecipe, RecipeEntry.MODE_PRIVATE);
Editor selectedEdit = selectedRecipe.edit();
selectedEdit.putString(MainMenu.SelectedRecipe, _RecipeListContents.get(arg2));
selectedEdit.commit();
// Start the RecipeNew activity
Intent doRecipeClick = new Intent(this, RecipeNew.class);
startActivity(doRecipeClick);
}
}
RecipyEntry
package com.example.basicrecipes;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.view.View;
import android.view.View.OnClickListener;
public class RecipeEntry extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipe_tab_new);
Button saveButton = (Button)this.findViewById(R.id.save_new_recipe);
saveButton.setOnClickListener(this);
Button cancelButton = (Button)this.findViewById(R.id.cancel_new_recipe);
cancelButton.setOnClickListener(this);
}
public void saveRecipe() {
// Create or Open a Preference for all Recipe Names
SharedPreferences recipeName = getSharedPreferences(MainMenu.RecipeNamesPref, RecipeEntry.MODE_PRIVATE);
// Get the recipe list. If empty, the list will contain only "New Recipe"
String recipeList = recipeName.getString(MainMenu.RecipeNamesPref, "New Recipe");
SharedPreferences.Editor recipeNameEdit = recipeName.edit();
EditText newRecipeNameView = (EditText)this.findViewById(R.id.name_new);
String newRecipeName = (String)newRecipeNameView.getText().toString();
// We must have a name in order to save
if (null != newRecipeName) {
// Preserve the existing list and append new name
recipeList = recipeList + "," + newRecipeName;
recipeNameEdit.putString(MainMenu.RecipeNamesPref, recipeList);
recipeNameEdit.commit();
// Create or open the new recipe as a SharedPreferece:<name>_detail to identify the recipe record
String newRecipeDetail = newRecipeName + "_Detail";
SharedPreferences newRecipe = getSharedPreferences(newRecipeDetail, RecipeEntry.MODE_PRIVATE);
SharedPreferences.Editor newRecipeEdit = newRecipe.edit();
// Store the recipe data for this recipe
newRecipeEdit.putString(MainMenu.NamePref, newRecipeName);
newRecipeEdit.putString(MainMenu.DescriptionPref, ((EditText)this.findViewById(R.id.description_new)).getText().toString());
newRecipeEdit.putInt(MainMenu.CuisinePref, ((Spinner)this.findViewById(R.id.cuisine_new)).getSelectedItemPosition());
newRecipeEdit.putString(MainMenu.PrefSteps, ((EditText)this.findViewById(R.id.new_steps)).getText().toString());
newRecipeEdit.commit();
}
}
public void onClick(View thisView){
Intent doMenuClick;
switch (thisView.getId()) {
case R.id.save_new_recipe:
// Save the recipe first;
saveRecipe();
doMenuClick = new Intent(this, MainMenu.class);
startActivity(doMenuClick);
break;
case R.id.cancel_new_recipe:
doMenuClick = new Intent(this, MainMenu.class);
startActivity(doMenuClick);
break;
}
}
}
RecipeNew
package com.example.basicrecipes;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
public class RecipeNew extends TabActivity implements OnTabChangeListener {
TabHost _tabHost;
Resources _res;
TabHost.TabSpec _tabSpec;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipe_tabs);
_tabHost = getTabHost();
_res = getResources();
_tabHost.setOnTabChangedListener(this);
_tabSpec = _tabHost.newTabSpec("recipes").setIndicator("", getResources().getDrawable(R.drawable.recipes_tab)).setContent(new Intent(this, RecipeEntry.class));
_tabHost.addTab(_tabSpec);
_tabSpec = _tabHost.newTabSpec("ingredients").setIndicator("", getResources().getDrawable(R.drawable.ingredients_tab)).setContent(new Intent(this, RecipeIngredients.class));
_tabHost.addTab(_tabSpec);
_tabSpec = _tabHost.newTabSpec("specials").setIndicator("", getResources().getDrawable(R.drawable.specials_tab)).setContent(new Intent(this, SpecialIngredients.class));
_tabHost.addTab(_tabSpec);
for(int i=0; i<_tabHost.getTabWidget().getChildCount(); i++) {
_tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.DKGRAY);
}
_tabHost.getTabWidget().setCurrentTab(1);
_tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.DKGRAY);
}
public void onTabChanged(String tabId){
for (int i=0; i<_tabHost.getTabWidget().getChildCount(); i++){
_tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.DKGRAY);
}
_tabHost.getTabWidget().getChildAt(_tabHost.getCurrentTab()).setBackgroundColor(Color.GRAY);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="@color/mainmenu_bg"
>
<LinearLayout
android:id="@+id/mainsub_layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/main_search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_search_button"
/>
<EditText
android:id="@+id/main_search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/main_search_text"
android:lines="1"
/>
</LinearLayout>
<ScrollView
android:id="@+id/main_scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp"
>
<LinearLayout
android:id="@+id/mainscroll_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/main_new_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/main_new_button" />
<Button
android:id="@+id/main_list_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/main_list_button" />
<Button
android:id="@+id/main_help_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/main_help_button" />
<Button
android:id="@+id/main_about_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/main_about_button" />
<Button
android:id="@+id/main_exit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/main_exit_button" />
</LinearLayout>
</ScrollView>
</LinearLayout>
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/main_menu_new"
android:title="@string/main_menu_new"
android:alphabeticShortcut="@string/main_menu_newshortcut"
android:orderInCategory="1"
>
</item>
<item
android:id="@+id/main_menu_search"
android:title="@string/main_menu_search"
android:alphabeticShortcut="@string/main_menu_searchshortcut"
android:orderInCategory="2"
>
</item>
<item
android:id="@+id/main_menu_options"
android:title="@string/main_menu_options"
android:alphabeticShortcut="@string/main_menu_optionsshortcut"
android:orderInCategory="3"
>
</item>
</menu>
recipe_tab_new.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:padding="10dp"
android:background="@color/mainmenu_bg" >
<LinearLayout
android:id="@+id/newsub_layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<TextView
android:id="@+id/new_recipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/recipenew_text7"
android:textSize="20sp"
/>
</LinearLayout>
<ScrollView
android:id="@+id/mainscroll_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp"
>
<LinearLayout
android:id="@+id/recipe_table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/title_new"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/title_new"
/>
<EditText
android:id="@+id/name_new"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/name_new"
android:lines="1"
/>
<EditText
android:id="@+id/description_new"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="3"
android:hint="@string/description_new"
/>
<Spinner
android:id="@+id/cuisine_new"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="@array/opt_cuisine_list"
android:prompt="@string/cuisine_new"
/>
<EditText
android:id="@+id/new_steps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="3"
android:hint="@string/new_steps"
/>
<TableLayout
android:id="@+id/recipe_tablelayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center"
android:stretchColumns="1"
android:paddingTop="20dp"
>
<TableRow>
<Button
android:id="@+id/save_new_recipe"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="@string/recipenew_text5"
/>
<Button
android:id="@+id/cancel_new_recipe"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="@string/recipenew_text6"/>
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
recipe_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="@+id/list_child"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center"
android:paddingTop="10dp"
android:paddingBottom="10dp"
>
<TextView
android:id="@+id/list_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Recipes"
android:textSize="20sp"
>
</TextView>
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
I have no idea what is going on here??? I have my inflate classes set perfectly and everything seems to look ok, but IDK? The error keeps being thrown by: MainMenu Line 50. Any one have any ideas? If so that would be freaking awesome!!! :)