Snippets

Small pieces of code that can be copy/paste-ed on your project

Snippets

Get screen size programatically in Android

So if you ever need to get the screen size(width and height) programatically on Android, note that there are a few options out there. Please see below some of them. Get Android screen size from DisplayMetrics The following snippet helps you load the screen size in Android using the Display class. This is quite old,

, , , , , ,
Snippets, Solutions, Tutorial

Add links to a TextView in Android

Maybe you need to add links to a TextView on Android but you don’t want to use Linkify for some reasons and you also want that link to be opened when the user taps it. Here is a simple solution: yourTextView.setText(Html.fromHtml( “Text with a ” + “<a href=\”http://www.myandroidsolutions.com/\”>link to My Android Solutions</a> “)); // this is

, , , ,
Snippets, Solutions

Android: How To Change Hint’s Size

If you need to set a smaller hint for your edit text, you can set the hint this way: myEditText.setHint(Html.fromHtml( “<small><small><small>” + getString(R.string.hint) + “</small></small></small>”)); Also, you can set the size in the string resource file where is the string for the hint. <string name=”edit_text_hint”><font size=”15″>My hint string</font></string> and in the xml file just set

, , ,
Snippets

Get Android OS Version

If you ever need to get the version of the OS on which your app runs, you can try this out: android.os.Build.VERSION.SDK_INT And you can use it against(for more check this out): android.os.Build.VERSION_CODES.[BASE-KITKAT]   Finally you end up with something like: if(android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.HONEYCOMB){ // do whatever you want here }

, , ,
Snippets, Solutions

Errors after update to Android Studio 0.8

I have just updated my Android Studio to Android Studio 0.8.2. But, after the update I kept getting these 3 errors while I was trying to run my project: 1.  First error Error:Execution failed for task > java.io.FileNotFoundException: C:\Program Files (x86)\Android\android-studio\sdk\tools\proguard\proguard-android-optimize.txt (The system cannot find the path specified) 2. Second error (this happened only after the

, ,
Snippets, Solutions

Share image Intent

Whenever you need to share an image from the external storage you can use the startActivity method and an Intent to do that. Doing so, the user will be able to chose whatever app he likes to share the photo. Intent share = new Intent(Intent.ACTION_SEND); share.setType(“image/png”); share.putExtra(Intent.EXTRA_STREAM, Uri.parse(“file:///”+imagePath)); startActivity(Intent.createChooser(share, “Share Image”)); If you don’t append the

, , ,
sponsored
Exit mobile version