Snippets

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

Snippets

Clear Fragment back stack

To clear the Fragments back stack of a FragmentManager, you could iterate through all back stack items and call popBackStack() but there is also a more elegant solution. Please see below: // in my case I get the support fragment manager, it should work with the native one too FragmentManager fragmentManager = getSupportFragmentManager(); // this […]

, ,
Snippets, Solutions

Run code on main/UI thread on android

Android’s UI components are not thread safe so one may need to update Views or other UI components from a secondary thread when returning from an asynchronous database query or a web service call.  If you run the code from a secondary thread you might see that the code crashes almost on each try. Below

, , , ,
Snippets, Solutions

Android: Prevent cut-copy-paste popup to be displayed

If you ever need to disable the popup for cut-copy-paste when you long tap a text from an edit text you can do this by using the code below: yourEditText.setCustomSelectionActionModeCallback(new Callback() { public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; } public void onDestroyActionMode(ActionMode mode) { } public boolean onCreateActionMode(ActionMode mode, Menu menu) {

, , , , ,
Snippets, Solutions

Android Get Application Context

When we are inside an Activity, and we need the application context we can use getApplicationContext()  method. But what if we are not inside an Activity and we need the application context? Well, I will explain in this tutorial how you can get the context of the application outside an Activity. You have to create

, , , ,
Snippets, Solutions

Set custom font on android – from assets

Among other nice things that Android has, we can also load custom fonts. To do that we need to get a font file with the desired font. There are some sites where you can find open source fonts like this or this.Ok, I assume you downloaded this font file angrybirds-regular.ttf . Step 1: – put the

, , , ,
Snippets, Solutions

ExpandableListView, how to hide indicator

If you use an ExpandableListView and want to hide the group indicator (that arrow that appears on the left) you need to put this in your xml file where the ExpandableListView is created: android:groupIndicator=”@null” Below is a complete example of an xml file: <?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_parent” > <ExpandableListView android:id=”@+id/expandable_list” android:layout_width=”fill_parent”

,
Snippets, Solutions, Tutorial

Android Add Views into a ViewGroup Dynamically

As Android developer you will surely  need someday to add views dynamically, instead of creating a ListView. I will show you in this tutorial how to do this :) 1. Create a new project and call your activity “MyActivity” 2. Go to res – layout – main.xml and put the following code: <?xml version=”1.0″ encoding=”utf-8″?> <ScrollView

, , , , , , ,
Snippets, Solutions

AES CB encryption in Android using key and salt

Here is a simple example of AES CB encryption in android using key and salt. I am not expert in encryption but I found how to do this pretty hard and I thought that maybe it will help somebody else if not , it will help me latter again :). // decryption method public static

, , , , , ,
Snippets, Solutions

SQLite Query For Dates Equals To Today

Let’s start with an example: We have a table called StartDate with some dates stored into it as long (milliseconds) like in the image below: To understand better which date represents each record I will write below their dates: 1. Tuesday, February 5, 2013 8:10:17 PM GMT 2. Friday, February 1, 2013 8:10:17 PM GMT 3. Wednesday, February

, , , , ,
Snippets, Solutions

Android Stop Edit Text From Gaining Focus When Activity Starts

In this tutorial I will show you how to make the edit text not gaining focus when the activity starts. Sometimes this auto-focus can be annoying because the keyboard shows up even if we do not want to write something into our edit text. But let’s see what should be done in order to get

, , ,
Snippets, Solutions

Android change indeterminate progress bar(wheel) color

There are times when you want to add a progress bar(wheel type) on a white background and you notice that on some phones the progress bar has white color and is not visible… The issue can be fixed if you apply a color to your progress bar drawable. Here is how you can do it:

, ,
Snippets, Solutions

Android WebView Load URL Ignoring SSL

In this tutorial I will show you how to make a request to an URL. This tutorial might help you also, if you encountered the “Blank Page” problem (I have encountered this issue when I was trying to make a request to a secure http (“https”)). 1. Create a new project and call your java class(the

, ,
Scroll to Top