Skip to main content

Posts

Shuffling a list in Java

I had a list of words which I wanted to shuffle in random order. When I tried swapping random elements of the list, I needed two random numbers and ensure they are not equal. Was taking a lot of time. But here is a better way private ArrayList shuffleWordList(ArrayList wrdList) { ArrayList lst = new ArrayList(); ArrayList tempList = (ArrayList) wrdList.clone(); int sz = wrdList.size(); for (int i = 0; i < sz; i++) { int listSize = tempList.size(); int n = random.nextInt(listSize); lst.add(tempList.get(n));//fill random word into new list tempList.remove(n);//remove selected word } return lst; } What we are doing here is,  Copy the list into a temporary list - tempList  Create another empty list- lst  Select one random word from temporary list  Add this word to new list  Remove the word from temporary list        Repeat ...

Using progressbar

You might have used ProgressDialog in your android app. This can be used to tell the user that some long processing is going on like file operation or accessing data from net. A progressbar is similar to a progress dialog. It is used to show the progress of a lengthy operation.  Progress bar can be horizontal or indeterminate - which is a wheel like thing which keeps rotating. We can specify which we need horizontal instead of indeterminate with style option          <ProgressBar             android:id="@+id/time"             style="?android:attr/progressBarStyleHorizontal"             android:layout_width="fill_parent"                        android:layout_heigh...

Test your memory - new app

I have published a brain puzzle in the android market by this name.This android game shows a set of five numbers/words for 3 seconds. In the next screen these are shown along with 5 more options.You should tap words/numbers in the correct order. Each correct answer will add a score of 20. The game stops with a wrong answer. There are 2 levels in Numbers option. First level displays numbers from 0 to 99 and second level displays numbers from 0 to 999. Words option has 3 levels with different set of words. Download the game from android market : Downloadgame

Let android help you in case of emergency

In the event of Delhi rape case and the protests regarding the same, it is better for girls and ladies to have at least one app which helps them in case someone tries to attack them. I came across two apps in this category. bSafe and Life360familylocator .  Be sure to download these or similar apps in your phone for your safety.

Saving a file in sdcard

Many a times, you want to save some contents permanently from your program. If the data is few words, you can save them in shared preferences. But for larger contents, you need to use a file. To save a file in sdcard, first of all you should have the permission in your app. You should add this line in AndroidManifest.xml file.  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>    Next in code, you should open the file. File fl = new File("/mnt/sdcard/yourdirname","yourfilename"); try { FileWriter wr = new FileWriter(fl); wr.write(str);//str is the string to be written to file wr.close(); } catch (IOException e) { e.printStackTrace(); } For the File constructor, first parameter is directory name and second is filename. It is always better to write to another directory in sdcard. What happens if the directory does not exist? Your program will crash. First ensur...

Drawables in Android - Layer drawable

Let us see how to use layer drawable. You can have two or more bitmaps on different layers to create such a drawable Using xml : You should use layer-list in your xml file to create layerdrawable. Here is layer.xml <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <bitmap android:src="@drawable/whiteicon" android:gravity="top|left"/> </item> <item> <bitmap android:src="@drawable/blueicon" android:gravity="top|left"/> </item> <item> <bitmap android:src="@drawable/redicon" android:gravity="top|left"/> </item> </layer-list> We are using three different bitmaps whiteicon.png, redicon.png and blueicon.png which are present in /res/drawable/mdpi folder. All these are of different sizes and aligned to top left. Thi...

Spinner and its settings

Spinner is quite a handy widget when you have a set of options to select from. In the layout file, add the spinner widget with its width and height. Dropdown items of spinner can be taken from an array. Use an array adapter to attach these values to the spinner. ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(               this, R.array.lorem, android.R.layout.simple_spinner_item); myspinner.setAdapter(adapter); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); But I faced a  problem that text color and gravity for spinner. Text color was  black and gravity was left even after setting proper values in xml file. Now I created a theme in /res/values/styles.xml file as follows <style parent="@android:style/Theme" name="my_theme">  <item name="android:textColor">@android:color/white</item>  <item name="android:gravity">center_ho...