This project will have you build a gallery app which will display drawable resources in an activity view and use the tools discussed in this module to improve the look and feel of the app.
- work with Android's resource system
- understand Android image requirements
- use Android Studio asset tools
- apply styles and themes
- understand accessibility options
Remember to build, run, and test your app at least at the end of each section below
- Create a new Android Studio project
- Name the project "Gallery App"
- Make sure that the target API level is below that of your testing environment.
- Open
activity_main.xml
- Replace the
ConstraintLayout
with aScrollView
- Give the
ScrollView
aLinearLayout
- Set the width to
match_parent
and the height towrap_content
- Set the orientation to horizontal
- Set the width to
- Give that
LinearLayout
two otherLinearLayout
children- Set the ids to something descriptive (something like left_column and right_column)
- Create a new style in
res\values\styles.xml
with the name ofVerticalColumn
- Provide values for the following items
- Set the
android:layout_width
to 0dp and theandroid:layout_height
towrap_content
- Set
android:layout_weight
to 1 - Set
android:orientation
to vertical
- Set the
- Go back to your layout and apply this new style to both of the
LinearLayout
s you just created using thestyle
attribute.
Remember, the
style
attribute doesn't begin withandroid:
- Download the zip archive of images from the repo and copy them into your drawable directory using your computer's file explorer app
\app\src\main\res\drawable
- Add one
ImageView
for each column - Add a value to the src to point to one of your drawables
- Apply the following attributes and play with them and others to get something you like the look of
- Set the width to
match_parent
and the height towrap_content
- Set the id to something descriptive (this could be focused on the location of the layout item)
- Add padding to give the view a good feel
- Set the width to
Notice the blue boundaries for the imageview are much larger than the image itself
- Add the attribute
adjustViewBounds
and set the value to true. - Once you have something you like. Pull those attributes out into a new style in styles.xml and then apply that style as done before
- Open
MainActivity.kt
- Start in the
onCreate
method - Create an array of new
ImageView
objects
Set the size of the array to be the number of drawables you have left to use
- Step through that list and manually assign drawables to each object in your array.
Your code may look something like this
views[0].setImageDrawable(ContextCompat.getDrawable(this, R.drawable.abstract_abstract_expressionism_art_2505693))
- Now we'll apply our style. Unfortunately, you can't apply a style to a view programmatically. Copy the
ImageView
style fromstyles.xml
and paste it intoMainActivity.kt
as a comment. We'll use this as a reference.
To apply this style, we have to apply each of the attributes manually.
- To apply the height and width, we'll need to create a
LayoutParams
object and pass in constants to representmatch_parent
andwrap_content
as constructor parameters.
That line will look like this
val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
We useLinearLayout.LayoutParams
because the parent is aLinearLayout
- Create an indexed
forEach
loop to step through the Views array.- Access the entry's
layoutParams
property and assign it the value of thelayoutParams
object you created before the loop. - Access the entry's
adjuistViewBounds
property and assign it the valuetrue
. - Call the entry's
setPadding
method and pass in the values for the padding on each side: left, top, right, bottom. - Finally, decide which column each entry should go on. This can be done with any of a number of simple logic techniques using the index.
- Whichever you decide, call the parent view's
addView
method and pass in the entry that you are currenty working on.
- Access the entry's
- Navigate to a theme generation side like https://coolors.co and generate a 3 color theme that you like. It should have a primary color, a dark version of that color and an accent color.
- Open
res\values\colors.xml
, replace the hexadecimal values for the colors there with the colors for your new theme.
You may add additional colors if you'd like, but the 3 colors there, colorPrimary, colorPrimaryDark, and colorAccent are the only ones that will automatically be used.
- Open
styles.xml
and find the style titled "AppTheme" - Use intellisense to select a new parent theme
Delete the entire string and type "Theme." to see possible options
Theme.AppCompat
is a dark theme andTheme.AppCompat.Light
is a light theme
- Right click your drawable directory and select New -> Image Asset
- Make sure "Launcher Icons (Adaptive and Legacy)" is selected from the dropdown at the top.
- Select the "Foreground Layer" tab
- Select the asset type and asset image you want.
- Change the color to whatever you want and resize the image to fit
- Select the "Background Layer" tab
- Select the color or image that you want to be the background
- Click "Next" and review the images
- Click "Finish"
If you didn't change the icon name, it will automatically replace the old one. If you did, you'll need to go into the
AndroidManifest.xml
file and change the value for theandrod:icon
attribute to point to your new icon instead of the old one
- Open the file
res\drawable\ic_launcher_foreground\ic_launcher_foreground.xml
look through the xml to get a feel for how this data is stored.
- Go back to
activity_main.xml
- Add a
contentDescription
attribute to each of theImageView
views. Give them each a description of what the image is - You'll notice the warning "Hardcoded string..." put your cursor inside of the highlighted text and push Alt+Enter (Option+Enter on MacOS) and select "Extract String Resource". Fill in the popup window and click "OK"
- Go to
MainActivity.kt
after assigning images to each of the views, access thecontentDescription
property and assign it a description like you did in the xml. - Put your cursor into each of the new strings and press Alt+Enter (Option+Enter on MacOS) and select "Extract String Resource"
- Open
res\values\strings.xml
and see all the string you've added. - Right click the
strings.xml
file in the sidebar and click "Open Translations Editor" - In the translations editor, click the globe icon in the top left and select another language
- Translate all strings into that language.... just kidding, but understand how and where those translations would be added.