Question

In: Computer Science

Java 12.2 Scrapbooking App. Write A JAVA application. Find four images of famous landmarks using websites...

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

Solutions

Expert Solution

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!
===========================================================================

Related Solutions

USING JAVA and NOTTT using BufferedReader or BufferedWriter Write an application that implements a simple text...
USING JAVA and NOTTT using BufferedReader or BufferedWriter Write an application that implements a simple text editor. Use a text field and a button to get the file. Read the entire file as characters and display it in a TextArea. The user will then be able to make changes in the text area. Use a Save button to get the contents of the text area and write that over the text in the original file. Hint: Read each line from...
In Java and using JavaFX, write a client/server application with two parts, a server and a...
In Java and using JavaFX, write a client/server application with two parts, a server and a client. Have the client send the server a request to compute whether a number that the user provided is prime. The server responds with yes or no, then the client displays the answer.
You are to write a program using Java that will simulate a slot machine with four...
You are to write a program using Java that will simulate a slot machine with four wheels. It will determine the amount of money won for each spin. The four wheels spin and stop showing a value between 0 and 9 inclusive. It costs $2 to play. •You win $500 (but not the $2 you bet) if you get 4 wheels the same. •You win $10 (but not the $2 you bet) if you get exactly 3 of a kind....
Develop a Java application using Parboiled library to write aparser for a customer form. Your...
Develop a Java application using Parboiled library to write a parser for a customer form. Your program should include a grammar for the parser and display the parse tree with no errors.The customer form should include the following structure:First name, middle name (optional), last nameStreet address, city, state (or province), countryPhone numberRules• First name, middle name and last name should start with an uppercase letter followed by lower case letters. Middle name is an optional input meaning such an input...
The project description: As a programmer, you have been asked to write a Java application, using...
The project description: As a programmer, you have been asked to write a Java application, using OOP concepts, for a Hospital with the following requirements: • The Hospital has several employees and each one of them has an ID (int), name (string), address (string), mobile phone number (string), email (string) and salary (double) with suitable data types. • The employees are divided into: o Administration staff: who have in addition to the previous information their position (string). o Doctor: who...
In Java, Write a JavaFX application that draws multiple circles using a rubberbanding technique. The circle...
In Java, Write a JavaFX application that draws multiple circles using a rubberbanding technique. The circle size is determined by a mouse drag. Use the initial mouse press location as the fixed center point of the circle. Compute the distance between the current location of the mouse pointer and the center point to determine the current radius of the circle.Thank you and could you also show the finished product?
As a programmer, you have been asked to write a Java application, using OOP concepts, for...
As a programmer, you have been asked to write a Java application, using OOP concepts, for a Hospital with the following requirements: • The Hospital has several employees and each one of them has an ID (int), name (string), address (string), mobile phone number (string), email (string) and salary (double) with suitable data types. • The employees are divided into: o Administration staff: who have in addition to the previous information their position (string). o Doctor: who have also a...
Choose an application domain. Choose an App to develop in the domain. Write these down. domain...
Choose an application domain. Choose an App to develop in the domain. Write these down. domain :(a) Public Health / Persuasion for Mask Wearing or Public Health / Mass Vaccination Initiative, if possible , For concrete use cases and scenarios, personalize or customize the best, most suitable, appropriate choice for a specific class or segment of users or end-users.
write a c# console application app that reads all the services in the task manager and...
write a c# console application app that reads all the services in the task manager and automatically saves what was read in a Notepad txt file.  Please make sure that this program runs, also add some comments
JAVA Write a Java console application that prompts the user to enter the radius of a...
JAVA Write a Java console application that prompts the user to enter the radius of a circle, then prints its radius, diameter, circumference, and area. The Console Output Enter the radius of the circle: 1.2 The radius is 1.2 The diameter is 2.4 The circumference is 7.5398223686155035 The area is 4.523893421169302 Programmer Notes Write and document your program per class coding conventions. Add an instance variable double radius. Generate its get/set methods. Manually type the methods getDiameter(), getCircumference(), and getArea()....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT