Build a recipe app using Android studio. The Data used for recipes should be in
ID: 3864384 • Letter: B
Question
Build a recipe app using Android studio. The Data used for recipes should be in the arrays in string.xml file! On start the app should show a list of recipes. Each row consists of 2 parts. The left is a small icon to show the food, and the right is the recipe name. Tap the image a new screen should be launched with a high resolution image of the icon. Tap the high resolution image to traverse back to the main list. Tap recipe name to show 1 sentence description about the food. should be able to go back using back button. should work on Android marshmallow and up.Explanation / Answer
I made full application there so many class
package com.app.recipeapp;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView mListView;
List<Recipe> recipesItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Context ctx = getApplicationContext();
Resources res = ctx.getResources();
String[] recipeList = res.getStringArray(R.array.recipe_names);
Integer[] recipeIcons = {R.drawable.hunan_chicken,R.drawable.minty_ice_cream,R.drawable.jam_filled_cookies,R.drawable.veggie_hoagie};
recipesItems = new ArrayList<Recipe>();
for (int i = 0; i < recipeList.length; i++) {
Recipe item = new Recipe(recipeIcons[i], recipeList[i]);
recipesItems.add(item);
}
// Create list view
mListView = (ListView) findViewById(R.id.recipe_list_view);
RecipeAdapter recipeAdapter = new RecipeAdapter(this, R.layout.list_item_recipe, recipesItems);
mListView.setAdapter(recipeAdapter);
}
}
2.Recipe.java
package com.app.recipeapp;
/**
* Created by developer on 20/3/17.
*/
public class Recipe {
private int imageId;
private String title;
public Recipe(int imageId, String title) {
this.imageId = imageId;
this.title = title;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
3. RecipeAdapter.java
package com.app.recipeapp;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by developer on 20/3/17.
*/
public class RecipeAdapter extends ArrayAdapter<Recipe> {
Context context;
public RecipeAdapter(Context context, int resourceId, List<Recipe> items) {
super(context, resourceId, items);
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Recipe recipe = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_recipe, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.recipe_list_title);
holder.imageView = (ImageView) convertView.findViewById(R.id.recipe_list_thumbnail);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtTitle.setText(recipe.getTitle());
final String recipeDesc = recipe.getTitle();
holder.imageView.setImageResource(recipe.getImageId());
final int imageID = recipe.getImageId();
holder.txtTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent detailIntent = new Intent(context, RecipeDescriptionActivity.class);
detailIntent.putExtra("recipe",recipeDesc);
context.startActivity(detailIntent);
}
});
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent detailIntent = new Intent(context, RecipeDetailActivity.class);
detailIntent.putExtra("imageID",imageID);
context.startActivity(detailIntent);
}
});
return convertView;
}
}
4.RecipeDescriptionActivity.java
package com.app.recipeapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class RecipeDescriptionActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_description);
TextView description = (TextView) findViewById(R.id.recipe_description);
TextView back = (TextView) findViewById(R.id.back);
Intent intent = getIntent();
description.setText(intent.getStringExtra("recipe"));
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intentBack = new Intent(RecipeDescriptionActivity.this,MainActivity.class);
startActivity(intentBack);
}
});
}
}
5.RecipeDetailActivity.java
package com.app.recipeapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class RecipeDetailActivity extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_detail);
imageView = (ImageView) findViewById(R.id.recipe_image);
Intent intent = getIntent();
int imageName = Integer.valueOf(intent.getIntExtra("imageID",0));
imageView.setImageResource(imageName);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intentBack = new Intent(RecipeDetailActivity.this,MainActivity.class);
startActivity(intentBack);
}
});
}
}
6.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.recipeapp.MainActivity">
<ListView
android:id="@+id/recipe_list_view"
android:layout_height="match_parent"
android:layout_width="match_parent">
</ListView>
</android.support.constraint.ConstraintLayout>
7. activity_recipe_description.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.recipeapp.RecipeDescriptionActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recipe_description"
android:padding="10dp"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="recipe" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/back"
android:padding="10dp"
android:layout_below="@+id/recipe_description"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:textSize="20dp"
android:text="Back" />
</RelativeLayout>
8.activity_recipe_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.recipeapp.RecipeDetailActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recipe_image" />
</RelativeLayout>
9.list_item_recipe.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/recipe_list_thumbnail"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginBottom="6dp"
android:layout_marginStart="4dp"
android:layout_marginTop="6dp"
android:scaleType="centerInside"
tools:src="@mipmap/ic_launcher"
android:contentDescription="@string/thumbnail"/>
<TextView
android:id="@+id/recipe_list_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:maxLines="1"
android:textColor="#000000"
android:padding="10dp"
android:textSize="20sp"
tools:text="Detail"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/recipe_list_thumbnail" />
</RelativeLayout>
10.string.xml
<resources>
<string name="app_name">RecipeApp</string>
<string name="thumbnail">thumbnail</string>
<string-array name="recipe_names">
<item>Hunan Style Chicken</item>
<item>Minty Ice Cream Shamrocks</item>
<item>Jam-Filled Cookies</item>
<item>Veggie Hoagie</item>
</string-array>
</resources>
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.app.recipeapp"
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.