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.
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...
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
In Java Develop, test, and execute a graphics application for simulations using Java. Create a Java...
In Java Develop, test, and execute a graphics application for simulations using Java. Create a Java application. Given a set of events, choose the resulting programming actions. Understand the principles behind Java. Understand the basic principles of object-oriented programming including classes and inheritance. Deliverables .java files as requested below. Requirements Create all the panels. Create the navigation between them. Start navigation via the intro screen. The user makes a confirmation to enter the main panel. The user goes to the...
Creation of an app for a car rental and swap service by four people. Write a...
Creation of an app for a car rental and swap service by four people. Write a operation plan for 5 years. Include answers to the following questions: 1. How will we make or deliver the product or service? 2. What are our distribution channels? 3. Locations, equipment needs. 4. Labour’s, contractor’s requirement. 5. Is there a ‘pay as you go’ option to gradually ramp up production and/or distribution? (Make it in a table with time deadlines)
Write a Java program using using WHILE loop to find the sum of all integers between...
Write a Java program using using WHILE loop to find the sum of all integers between 200 to 250 which are divisible by 7. Sample Output: Numbers between 200 and 250, divisible by 7: 203 210 217 224 231 238 245 The sum is: 1568
Using the Library link or any biology websites to find an enzyme that is related to...
Using the Library link or any biology websites to find an enzyme that is related to either a disease or a condition (human, animal or plant) and place a summarizing paragraph on the thread below. Consider including: 1 point description of disease or condition 1 point for a brief description of the symptoms of the disease or expression in plants 1 point for description of the protein involved 1 point for anecdote(story) related to the disease or interesting information about...
Q1:Write a Java program to find Fibonacci Series using 1) using for loop 2) using while...
Q1:Write a Java program to find Fibonacci Series using 1) using for loop 2) using while loop To Understand what the Fibonacci series is: The Fibonacci sequence is a series of numbers where a number is the sum of the previous two numbers. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. Q2: Write a Java program to find the Factorial of a number using a while loop. To...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT