In: Computer Science
Java 12.2 Scrapbooking App. Write A JAVA application. Find four images of famous landmarks using websites such as flicker. Create an app similar to the welcome app in which you arrange images in a collage. Add text that identifies the landmark. You can use images that are part of your project or you can specify the URL of an image that's online
Source code for Scrapbooking app:
MainActivity.java
package com.bsn.scrapbooking;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private String mListSelected;
private Context context;
private LinearLayout baseLL;
private Menu menu;
ActionBarDrawerToggle mActionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setTitle(R.string.app_name);
setContentView(R.layout.activity_main);
baseLL = (LinearLayout) findViewById(R.id.imageList);
mDrawerList = (ListView)findViewById(R.id.navList);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mListSelected = "NEPAL";
toggleDrawer();
menuClicked();
new ViewLayout(this, baseLL, mListSelected);
}
private void menuClicked(){
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
baseLL.removeAllViews();
if(id==1){
mListSelected = "AMERICA";
}else if(id == 2){
mListSelected = "CANADA";
}else{
mListSelected = "NEPAL";
}
new ViewLayout(context, baseLL, mListSelected);
mDrawerLayout.closeDrawers();
}
});
}
private void toggleDrawer(){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
//Create Drawer Toggle
mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open_drawer, R.string.close_drawer){
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
}
// create an action bar button
@Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
getMenuInflater().inflate(R.menu.topmenu, menu);
if(getCurrentLocale().getISO3Language().equals("eng")){
this.menu.findItem(R.id.nepali).setVisible(true);
}else{
this.menu.findItem(R.id.english).setVisible(true);
}
return super.onCreateOptionsMenu(menu);
}
// handle menu activities
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mActionBarDrawerToggle.onOptionsItemSelected(item)){
return true;
}
int id = item.getItemId();
if (id == R.id.nepali) {
this.menu.findItem(R.id.nepali).setVisible(false);
Locale locale = new Locale("ne");
setLocale(locale);
this.menu.findItem(R.id.english).setVisible(true);
}else if(id == R.id.english){
this.menu.findItem(R.id.english).setVisible(false);
Locale locale = new Locale("en");
setLocale(locale);
this.menu.findItem(R.id.nepali).setVisible(true);
}
return super.onOptionsItemSelected(item);
}
@TargetApi(Build.VERSION_CODES.N)
public Locale getCurrentLocale(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
return getResources().getConfiguration().getLocales().get(0);
} else{
return getResources().getConfiguration().locale;
}
}
private void setLocale(Locale locale){
Resources resources = getResources();
Configuration configuration = resources.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(locale);
} else {
configuration.locale = locale;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
context.getApplicationContext().createConfigurationContext(configuration);
} else {
resources.updateConfiguration(configuration, null);
}
Intent refresh = new Intent(MainActivity.this, MainActivity.class);
startActivity(refresh);
finish();
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mActionBarDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
mActionBarDrawerToggle.onConfigurationChanged(newConfig);
}
}
Image.java
package com.bsn.scrapbooking;
public class Image {
private String mImage;
private String mText;
private String mContentDescription;
private String mCountry;
public Image(String mImage, String mText, String mContentDescription, String mCountry) {
this.mImage = mImage;
this.mText = mText;
this.mContentDescription = mContentDescription;
this.mCountry = mCountry;
}
public String getImage() {
return mImage;
}
public void setImage(String mImage) {
this.mImage = mImage;
}
public String getText() {
return mText;
}
public void setText(String mText) {
this.mText = mText;
}
public String getContentDescription() {
return mContentDescription;
}
public void setContentDescription(String mContentDescription) {
this.mContentDescription = mContentDescription;
}
public String getCountry() {
return mCountry;
}
public void setCountry(String mCountry) {
this.mCountry = mCountry;
}
}
ViewLayout.java:
package com.bsn.scrapbooking;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ViewLayout {
private Context context;
private LinearLayout baseLL;
private String listSelected;
private Image[] mImageBank = new Image[]{
new Image("bhaktapur", "Bhaktapur", "Old city of Nepal", "NEPAL"),
new Image("gokyo_ri", "Gokyo Ri", "Sagarmatha National Park", "NEPAL"),
new Image("swayambhunath", "Swayambhunath", "Stupa in Kathmandu", "NEPAL"),
new Image("phewa_lake", "Phewa Lake", "Phewa lake in Pokhara", "NEPAL"),
new Image("lady_liberty", "Lady Liberty", "Statue of lady liberty", "AMERICA"),
new Image("brooklyn_bridge", "Brooklyn Bridge", "Brooklyn Bridge in New York", "AMERICA"),
new Image("golden_gate", "Golden Gate", "Golden Gate in San Fransisco", "AMERICA"),
new Image("white_house", "White House", "White House in Washington DC", "AMERICA"),
new Image("rocky_mountain", "Rocky Mountain", "Rocky Mountain in Canada", "CANADA"),
new Image("banff_np", "Banff National Park", "Banff National Park in Canada", "CANADA"),
new Image("canadian_rockies", "Canadian Rockies", "Canadian Rockies in Canada", "CANADA"),
new Image("cn_tower", "CN Tower", "CN Tower in Canada", "CANADA"),
};
ViewLayout(Context context, LinearLayout baseLL, String listSelected) {
this.baseLL = baseLL;
this.context = context;
this.listSelected = listSelected;
LinearLayout[] imageContainer = new LinearLayout[mImageBank.length];
int count = 0;
LinearLayout ll = this.setLinearLayout();
for (int i = 0; i < mImageBank.length; i++) {
if (mImageBank[i].getCountry() == this.listSelected) {
if (count == 2) {
ll = this.setLinearLayout();
count = 0;
}
ImageView resultView = this.setImageView(i);
TextView resultText = this.setTextView(i);
RelativeLayout resultRelative = this.setRelativeLayout();
resultRelative.addView(resultView);
resultRelative.addView(resultText);
ll.addView(resultRelative);
imageContainer[i] = ll;
count++;
}
}
for (int j = 0; j < imageContainer.length; j++) {
if (imageContainer[j] != null && imageContainer[j].getParent() == null) {
this.baseLL.addView(imageContainer[j]);
}
}
}
public LinearLayout setLinearLayout() {
LinearLayout linearL = new LinearLayout(this.context);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 375
);
linearParams.setMargins(0, 60, 0, 0);
linearL.setLayoutParams(linearParams);
linearL.setPadding(0, 5, 0, 5);
linearL.setBaselineAligned(false);
linearL.setOrientation(LinearLayout.HORIZONTAL);
return linearL;
}
public RelativeLayout setRelativeLayout() {
RelativeLayout relativeL = new RelativeLayout(this.context);
LinearLayout.LayoutParams relativeParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT,
1
);
relativeParams.setMargins(2, 1, 2, 1);
relativeL.setLayoutParams(relativeParams);
relativeL.setFocusable(true);
return relativeL;
}
public ImageView setImageView(Integer i) {
ImageView imageView = new ImageView(this.context);
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
imageView.setLayoutParams(imageParams);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setAdjustViewBounds(true);
String mDrawableName = mImageBank[i].getImage();
int resID = this.context.getResources().getIdentifier(mDrawableName, "drawable", this.context.getPackageName());
imageView.setImageResource(resID);
imageView.setContentDescription(mImageBank[i].getContentDescription());
return imageView;
}
public TextView setTextView(Integer i) {
TextView textView = new TextView(this.context);
RelativeLayout.LayoutParams textViewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
textViewParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
textViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
textViewParams.addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
textViewParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
textViewParams.addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
textView.setLayoutParams(textViewParams);
textView.setTextSize(15);
textView.setTextColor(Color.WHITE);
textView.setBackgroundColor(Color.parseColor("#3F51B5"));
textView.setPadding(10, 10, 10, 10);
textView.setGravity(Gravity.CENTER);
textView.setText(mImageBank[i].getText());
textView.setContentDescription(mImageBank[i].getText());
return textView;
}
}
Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! ===========================================================================