Office Hours — Today, October 10

Tuesday, October 8

Oct 10
8:25 AM
Mark M.
has entered the room
Mark M.
turned on guest access
puneet
has entered the room
Mark M.
hello, puneet!
how can I help you today?
puneet
hello sir
i am new to learn kotlin for android development
8:30 AM
puneet
i want to know some tricks to understand more
Mark M.
ummmm... can you be more specific?
puneet
like kpotlin courotines
coroutines
Mark M.
I wrote about coroutines in *Elements of Kotlin Coroutines* -- you have access to that as part of your Warescription
future updates to that book will include some Android-specific applications of coroutines
puneet
ok sir
Mark M.
also, the *Exploring Android* tutorials use coroutines
and there are a few samples in *Elements of Android Jetpack* that use coroutines
puneet
ok sir
Mark M.
in those cases, it is mostly using coroutines with Room, for local database access
puneet
thank you very much
8:35 AM
Mark M.
if you have specific questions, you can use these office hours chats, or the other options that I have in the "Ask" menu of the Warescription site
puneet
ok sir
8:40 AM
Mark M.
if you have any specific questions now, I can try to answer them -- these office hours chats are mostly for people with particular questions for which they are looking for my help
8:45 AM
Ed
has entered the room
Ed
Good morning sir
Mark M.
hello, Ed!
how can I help you today?
Ed
Your SimplePrefs demo, I have a question about that....
Mark M.
are you referring to the one in *Elements of Android Jetpack*?
Ed
HomeMotor extends AndroidViewModel
Mark M.
we need a Context to get at SharedPreferences, so I used AndroidViewModel
Ed
androidx.lifecycle.ViewModelProviders made me think there was something else there...
haven't played with the lifecycle api to any extent
8:50 AM
Ed
the Android docs really suck big time on SharedPreferences right now
Mark M.
I haven't looked recently, but that does not surprise me
Ed
android.content.SharedPreferences doesn't throw any exceptions and looks callable from Android 10
Mark M.
you may be better served looking at the SharedPreferences material in *The Busy Coder's Guide to Android Development*, insofar as it does not assume that you are familiar with the ViewModel stuff
Ed
if they want me to call a different API you would think they would do an API check and then throw a RunTimeException telling me to do it differently
thanks for the tip
Mark M.
well, the SharedPreferences class itself has been around since API Level 1
Ed
correct but they moved it to AndroidX
Mark M.
not
not really
they moved the UI pieces to AndroidX
SharedPreferences itself is still a framework class
Ed
androidx.preference.PreferenceManager
Mark M.
yes, but that still returns a framework SharedPreferences
and it has the same basic API as the framework PreferenceManager
Ed
interesting... good point... just drilled down and found...
View paste
package androidx.preference;

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP_PREFIX;

import android.content.Context;
import android.content.SharedPreferences;
Mark M.
PreferenceManager.getDefaultSharedPreferences(...), where ... is a Context, gives you the default SharedPreferences for your app
8:55 AM
Ed
final question for you and I can wait for puneet
if they want to ask you something
Mark M.
puneet: do you have another question?
Ed: go ahead with your next question, and I'll take a question from puneet if one gets posted
Ed
I'm going to ask my last question really quick if that is ok.... I'll try to be quicker than in the past. :-)
View paste
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="2dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="200dp"
        android:layout_height="200dp" />

</RelativeLayout>
that is my RecyclerView row
then I'm creating two columns like so...
View paste
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(), 2);
        _recyclerView.setLayoutManager(gridLayoutManager);
9:00 AM
puneet
has left the room
Ed
View paste
    public class ItemViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public ItemViewHolder(View view) {
            super(view);
            imageView = view.findViewById(R.id.image);
        }
    }
you with me so far or should I show more?
Mark M.
I'm with you, though I'm not certain where this is going
Ed
the two columns don't 'cleanly' tack across phones
oop... track
Mark M.
"tack"?
Ed
so on a Google pixel is looks great
on another device with a bigger screen there is more spacing and it is not as tight
Mark M.
you have match_parent for the width of your RelativeLayout
I'm not certain that is a good idea for a grid
Ed
what I want is 2 vertical columns where col A and col B both take 50% of the width
so do a List of Lists?
Mark M.
other than the 2dp padding, is there a particular reason for having that RelativeLayout?
Ed
none
So do a Vertical List that contains a Horizontal list of two items?
Mark M.
you might try removing it, adding 2dp margin to the ImageView instead, and using wrap_content for the ImageView size
Ed
ok
Mark M.
your grid should be OK
it is your cells that is your problem
hard-coding image dimensions guarantees that on larger screens "there is more spacing and it is not as tight"
9:05 AM
Mark M.
so you need to do something to allow the image sizes to adapt to the environment
if you stick with the grid, wrap_content for the image size may suffice
Ed
so instead do this...
View paste
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/image"
        android:layout_width="200dp"
        android:layout_height="200dp" />

</RelativeLayout>
Mark M.
View paste
no, do this:

 <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
the RelativeLayout is not doing anything
if you get frustrated with the grid, you could use ConstraintLayout for a row in a list-style RecyclerView, with each row having two images
as ConstraintLayout gives you lots of control for setting up your 50% sizing, maintaining the aspect ratios, etc.
Ed
That seems like the better choice
Mark M.
however, you'll need to handle the case where there are an odd number of images, so your last row looks the way that you want
Ed
correct
so in my adapter I would just grab two at a time and handle the issue where I might have a 1/2 image
oops.. I mean 1/2 row... or 1 image
Mark M.
that could be a matter of just loading a transparent image into the second position as a placeholder
Ed
thanks Mark!
Mark M.
you're welcome!
9:10 AM
Ed
have a great day
Mark M.
you too!
Ed
cheers
Ed
has left the room
9:25 AM
Mark M.
turned off guest access

Tuesday, October 8

 

Office Hours

People in this transcript

  • Ed
  • Mark Murphy
  • puneet