The Ultimate Android Developer Cheat Sheet

Suhel Chakraborty
Feb 23, 2017 · 2 min read

Being able to develop apps for mobility devices is a big plus for today’s software industry. And on the top, sits the two major platforms for the same, Google’s Android and Apple’s iOS.

Each platform has its own perks and disadvantages. Not wanting to start a heated battle, but every Android developer will agree, some very simple tasks in Android Development that we almost take for granted, are surprisingly missing from both, tutorials at Android Developers website and tools in Android Studio IDE.

As an Android developer for 6 years, I have spent sleepless nights surfing billions of stackoverflow problems, experimenting and coming out with solutions of some puny but daunting problems that any developer might face while pursuing the same. To save some precious time of developers, here are some guidelines.

NOTE: This is not a step by step tutorial of setting up a project, it only comprises concise tricks.

The developer nightmare

Change Font in TextView & EditText

Yes! There is no official drop down menu where you can change the font of any text in Android. I am going to show you the way for TextView. Process for EditText is exactly same.

Precursor

Right click app folder and choose New>Folder>Assets Folder, and click Finish.

Create the Assets Folder
There goes the assets folder

Paste the *.ttf and *.otf font files you want in this folder.

For single TextView

Say you want to apply lato.ttf font to myTextField.

Typeface t = Typeface.createFromAsset(context.getAssets(), "lato.ttf");
myTextField.setTypeface(t);

For multiple TextViews

Create a custom TextView with code

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class CustomTextView extends TextView {

public CustomTextView(Context context) {
super(context);
}

public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);

initView(context, attrs);
}

public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

initView(context, attrs);
}

private void initView(Context context, AttributeSet attrs) {
try {

TypedArray arr = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTextView, 0, 0);

String typefacePath = arr.getString(R.styleable.CustomTextView_typefacePath);
this.setTypeface(Typeface.createFromAsset(context.getAssets(), typefacePath));

} catch (Exception e) {
e.printStackTrace();
}
}

}

In res>values create attrs.xml with code

<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="CustomTextView">
<attr name="typefacePath" format="string" />
</declare-styleable>

</resources>

Rebuild the project once and then use it in your layout with this code.

<com.example.project.CustomTextView
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:id=
"@+id/myTextField"
app:typefacePath=
"lato.ttf"
android:text=
"Hello World!" />

There you go.


I will keep adding cheats as they hit me.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade