about.me/davidcorrado mobile meetup organizer ...files.meetup.com/3082222/android tips-tricks.pdffor...

29
Android Tips/Tricks @DavidCorrado About.me/DavidCorrado Mobile Meetup Organizer

Upload: others

Post on 15-Mar-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Android Tips/Tricks@DavidCorrado

About.me/DavidCorradoMobile Meetup Organizer

Page 2: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

IDE

● Don’t Use Eclipse● Use either Android Studio/IntelliJ

○ They are basically the same thing. They are both built off of IntelliJ

○ Still in beta but everyone uses it● I recommend Android Studio. It takes out

the stuff that is not android related and dumbs things down =)

Page 3: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Attach Debugger Button

● If you have built the application on the device/emulator you can press this button at any time and it will attach the debugger○ After a crash you can put a breakpoint and attach to

the debugger and dig into what is happening.

Page 4: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Crashlytics

● Bug Reporting○ Tons of information on crashes

● QA Distributionsoutside of app store

● Analytics

Page 5: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Emulator Slow● When creating an emulator

○ Use Intel X86 version.○ Check Hardware acceleration

● Use Genymotion to run Android in a virtual machine○ Its kind of buggy. I had to run android 4.2.2○ http://www.genymotion.com/

● Run on a device○ Before going to production you have to run on a device anyway○ But keep in mind different screen sizes

● I use Genymotion and devices

Page 6: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Clean Code on Save● Every Time you save it will clean up your code to match google’s standard● Create Macro

○ Menu->Edit->Macro->Start Recording● Choose Menu->Code

○ Reformat Code,Auto Indent, Optimize Import, Rearrange Code● Stop Recording

○ Menu->Edit->Macro->Stop Recording● Override Save Button

○ Menu->Android Studio->Preferences> Search Keymap->Find Macros and the macro you saved and set the key to be save all command

● I then recommend you reformat the entire app so after that all your checkins are clean

Page 7: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Distribute Default settings

● You can save your settings and distribute them to your team.○ Menu->Export Settings

■ Be careful what you choose here. You will want to test this before distributing to the team. I think we ran into a problem with the SDK tables

Page 8: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Git

● Try to use Android studio for everything with Git.○ History,Merge,Commit,Push/Pull, Stash

● Example if you made a few changes to a file. You want to revert just a single function. You can compare with previous revision. Then the buttons to choose previous for that function

Page 9: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Auto Update Android SDK● When building your application can automatically

download Android SDK/Play Services/Build Tools/Platform Tools etc○ https://github.com/JakeWharton/sdk-manager-plugin

● Example. The lead developer updates any of the above. When you pull down the code it will give you errors that you need to update any of the above

● With the Tool above it automagically works

Page 10: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Browse Android SDK Source

● You can do this easily by downloading the Android SDK Source from the SDK Manager. This will make it so you can browse the android source just as easily as your own.○ Example you can browse how the Button works

Page 11: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Communication Between Fragments/Activities

● It is not very easy to communicate between activities and fragments etc.

● EventBus sends an event that we can listen to anywhere.○ https://github.com/greenrobot/EventBus

● If you use otto or something else and prefer it over greenrobots Event bus contact me after so you can do a talk on it at the mobile meetup.

Page 12: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Automagically Handle Network

● You can theoretically never touch JSON. You send it a model. It returns a model

● Automatically does request on separate thread and returns data via callback

● REST,Multipart form● Retrofit

○ http://square.github.io/retrofit/

Page 13: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Database● I prefer to never touch the database except in the DAO. So I only work

with models.● You can manually do this or there are frameworks that do this

○ Green DAO is popular http://greendao-orm.com/○ Thing I do not like about it is there is a build process to generate DAO

objects● Interesting framework that you just do annotations and the basic methods

DAO methods are created without a build process. I haven’t used this but its interesting○ https://github.com/emilsjolander/sprinkles

Page 14: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Global Context?● You have context being sent all around the application.

It seems some places you can just use the application context.

● For Example if you needed ○ context.getString(R.string.hello);

● If you were doing the above inside of a method you would have to pass the context to the method. Or if you use the example on the next slide you can do MyApplication.getContext().getString(R.string.hello); Or create a helper that does essentially the above

Page 15: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Global Context Example//In Manifest

<application android:name="com.xyz.MyApplication">

</application>

//then write the class

public class MyApplication extends Application{

private static Context context;

public void onCreate(){

super.onCreate();

MyApplication.context = getApplicationContext();

}

public static Context getAppContext() {

return MyApplication.context;

}

}

Page 16: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Dependency Injection

● Dagger is popular○ http://square.github.io/dagger/

● I am not really the best person to speak on this. Its definately something to look into.

● You could use this instead of a global context like I demoed before

Page 17: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Automagically Handle Images

● Load Images in one line of code. Something like○ Picasso.with(context).load(urlString).into

(imageView);● It can handle resizing. Making images into

circles etc● Picasso

○ http://square.github.io/picasso/

Page 18: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Multiple Image Resources● mdpi,hdpi,xhdpi etc● Create an image at your highest supported resolution

and then use one of the below tools● http://romannurik.github.io/AndroidAssetStudio/

○ There are a lot of cool tools in here look through it● Write or google for a script that automatically creates all

of the images you need and puts them in the appropriate folder

Page 19: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Cleaner Code with Butterknife● Someone should do a whole talk on this. Essentially its

helps you reference your views easier● Butterknife

○ http://jakewharton.github.io/butterknife/● There are a lot of different cases where you can use this

Page 20: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Cleaner Code With Butterknife Example

//Old wayButton button= (Button) findViewById(R.id.submit);button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Do Onclick logic }});//With Butterknife@OnClick(R.id.submit)public void submit() { //Do Onclick logic}

Page 21: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Code Completion

● You can auto create models getter and setters○ Right click in code ->Generate->Getters and Setters○ You can do this for a lot of different methods

● You can also type in the class and have it automatically complete override methods

● Android Intentions on next slide

Page 22: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Android Intentions● This is the automagic creation of code that they think you want to.

○ On mac you can do this with hotkey alt+enter○ You can also put your cursor on the area that you want the intention to

run on. Then you should see a lightbulb to have it tell you the intentions

● Android team recommends you look through these so you use them. Menu->Android Studio->Preferences->Search Intentions

● This does things like create string resource, implement unimplemented methods, etc

Page 23: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Handling Configuration Changes● If an android device is rotated everything is destroyed and recreated again. There are alot of

ways to handle this○ Put all data into model every time the activity is destroyed and save it in the parcelable

data.○ Each built in view if it has an id. It will restore some state. If you have a custom view you

will have to write your own restore state. Also you can not have multiple of the same view id used on the activity. So if you are dynamically adding the same view you will have to generate a dynamic id. Google how to do this.

○ You can also make it so your application never does configuration changes by adding configuration flags to all activities but that is pretty hacky

○ Some use this framework as well but it does not solve all your problemshttps://github.com/square/mortar

● Let me know after the talk if you know a good way to do this because. I think its annoying =)

Page 24: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Android Standard New Instance

● When creating things that need an instance created. Android generally creates a static method in the class that creates the new instance.

● Android does this when it you use the Activity generator

● This is so you do not have new instance code everywhere

Page 25: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Example New Instancepublic static UserFragment newInstance(PublicUser user) { UserFragment fragment = new UserFragment(); Bundle args = new Bundle(); args.putParcelable(PUBLIC_USER_TAG, user); fragment.setArguments(args); return fragment;}

Page 26: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Too Many abstract methods

● You can create a class that implements all the abstract methods so your code does not have empty methods

● A great example of this is the Text Watcher● Next slide shows the method that you would

implement instead of TextWatcher

Page 27: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Too Many abstract methods Exampleabstract public class BaseTextWatcher implements TextWatcher {

@Override

public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override

public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override

public void afterTextChanged(Editable s) {

}

}

Page 28: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Quick Tips

● You can click in logcat to keep your location○ If you dont do this it will scroll you to the bottom

● You can setup a proxy from your device to your computer with a tool such as fiddler or Charles

● You can right click on a png to create a 9 patch

Page 29: About.me/DavidCorrado Mobile Meetup Organizer ...files.meetup.com/3082222/Android Tips-Tricks.pdfFor Example if you needed context.getString(R.string.hello); If you were doing the

Library Recommendations

● https://github.com/square● https://github.com/JakeWharton●