TreeMap in Java

import java.util.TreeMap;   public class dimensionsExample { public static void main(String[] args) { TreeMap<String,String> dimensions = new TreeMap<String,String>(); dimensions.put("NC", "2333"); dimensions.put("NY", "2334"); dimensions.put("SC", "2335"); dimensions.put("CA", "2336"); System.out.println("CA: " + dimensions.get("CA")); dimensions.put("AZ", "2337"); System.out.println("Size of dimensions: " + dimensions.size()); dimensions.remove("SC"); System.out.println("Size of dimensions: " + dimensions.size()); } }
Continue reading…

Enjoyed this post? Share it!

 
 

HashMap in Java

import java.util.HashMap;   public class prices { public static void main(String[] args) { HashMap<String,Integer> prices = new HashMap<String,Integer>(); prices.put("walmart", 88); prices.put("target", 100); prices.put("kmart", 77); prices.put("lowes", 90); System.out.println("walmart’s price: " + prices.get("walmart")); System.out.println("Number of prices: " + prices.size()); if (prices.containsKey("target")) { prices.remove("target"); System.out.println("Record successfully removed."); } System.out.println("number of prices: " + prices.size()); } }
Continue reading…

Enjoyed this post? Share it!

 
 

HashSet in Java

import java.util.HashSet;   public class myHashsetExample { public static void main(String[] args) { HashSet<String> languages = new HashSet<String>(); languages.add("hindi"); languages.add("english"); languages.add("spanish"); System.out.println("The number of languages is " + languages.size()); for (String name : languages) { System.out.println(name); } languages.remove("hindi"); languages.add("tamil"); String name = "hindi"; if (languages.contains(name)) { System.out.println(name + " is in the collection."); } else […]
Continue reading…

Enjoyed this post? Share it!

 
 

PriorityQueue in Java

import java.util.PriorityQueue;   public class Cars { public static void main(String[] args) { PriorityQueue<String> car = new PriorityQueue<String>(); car.add("lincoln"); car.add("ford"); car.add("suzuki"); car.add("toyota"); car.add("nissan"); System.out.println("The car: "); for (String name : car) { System.out.println(name); } car.poll(); System.out.println("The car: "); for (String name : car) { System.out.println(name); } car.remove("suzuki"); System.out.println("The car: "); for (String name : car) […]
Continue reading…

Enjoyed this post? Share it!

 
 

TreeSet in Java

import java.util.TreeSet;   public class myTreeSet { public static void main(String[] args) { TreeSet<String> animals = new TreeSet<String>(); animals.add("Dog"); animals.add("Cat"); animals.add("Sparrow"); animals.add("Iguana"); animals.add("Dragon"); animals.add("Peacock"); System.out.println("Number of animals = " + animals.size()); for (String name : animals) { System.out.println(name); } System.out.println("Animal before Sparrow: " + animals.lower("Sparrow")); System.out.println("Animal after Sparrow: " + animals.higher("Sparrow")); System.out.println("First element: " + […]
Continue reading…

Enjoyed this post? Share it!

 
 

Arraylist in Java

import java.util.ArrayList;   public class myArrayList { public static void main(String[] args) { ArrayList<Integer> vals = new ArrayList<Integer>(); vals.add(100); vals.add(90); vals.add(80); vals.add(70); vals.add(60); int total = 0; /*for(int i = 0; i < vals.size(); ++i) { total += vals.get(i); }*/ for (Integer val : vals) { total += val; } double average = total / […]
Continue reading…

Enjoyed this post? Share it!

 
 

Populating Android ListView from Java

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >     <ListView android:id="@+id/statesList" android:layout_width="match_parent" android:layout_height="fill_parent" />   </LinearLayout> package com.w3mentor.android;   import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast;   public class MyListViewActivity extends Activity { /** Called when the activity is first created. */ @Override public […]
Continue reading…

Enjoyed this post? Share it!

 
 

Android TableLayout with ImageView

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >   <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="States of The USA" />   <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <TableRow>   <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/california" />   <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/nevada" />   <ImageView android:id="@+id/imageView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/washington" />   <ImageView android:id="@+id/imageView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ohio" />     </TableRow> <TableRow> […]
Continue reading…

Enjoyed this post? Share it!

 
 

Android RelativeLayout example

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/wrapperLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="3dp">   <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="User Name:" />   <EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" > </EditText>   <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_below="@+id/editText1" android:text="Passoword" />   <EditText android:id="@+id/editText2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_below="@+id/textView2" />   <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" […]
Continue reading…

Enjoyed this post? Share it!

 
 

Android LinearLayout example

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center_vertical">   <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="White" android:textAppearance="?android:attr/textAppearanceLarge" android:background="@color/White" android:textColor="#000000" android:gravity="center_horizontal" android:layout_weight=".5"/>   <TextView android:id="@+id/textView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Green" android:textAppearance="?android:attr/textAppearanceLarge" android:background="@color/Green" android:gravity="center_horizontal" android:layout_weight=".16"/>   <TextView android:id="@+id/textView3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Yellow" android:textAppearance="?android:attr/textAppearanceLarge" android:background="@color/Yellow" android:textColor="#000000" android:gravity="center_horizontal" android:layout_weight=".16"/>   <TextView android:id="@+id/textView4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Red" android:textAppearance="?android:attr/textAppearanceLarge" android:background="@color/Red" android:gravity="center_horizontal" android:layout_weight=".16"/>   </LinearLayout>
Continue reading…

Enjoyed this post? Share it!

 
 

Create maximizable, resizable ExtGWT Window

@Override public void onModuleLoad() {   // create and set up window Window basicWindow = new Window(); basicWindow.setHeading("My maximizable, resizable ExtGWT Window"); basicWindow.setClosable(true); basicWindow.setSize(150, 100); xWindow.setDraggable(true); xWindow.setResizable(true); xWindow.setMaximizable(true); xWindow.setModal(true); xWindow.setBlinkModal(true);   // prepare content to show LayoutContainer textPanel = new VerticalPanel(); textPanel.setStyleAttribute("padding", "10px"); textPanel.addText("This is my maximizable, resizable ExtGWT Window …");   // add content […]
Continue reading…

Enjoyed this post? Share it!

 
 

Display basic window using Ext GWT

@Override public void onModuleLoad() {   // create and set up window Window basicWindow = new Window(); basicWindow.setHeading("My ExtGWT Window"); basicWindow.setClosable(true); basicWindow.setSize(150, 100);   // prepare content to show LayoutContainer textPanel = new VerticalPanel(); textPanel.setStyleAttribute("padding", "10px"); textPanel.addText("This is my window …");   // add content and show window basicWindow.add(textPanel); basicWindow.show(); }
Continue reading…

Enjoyed this post? Share it!

 
 

Audio recorder in Android

// initialize the MediaRecorder MediaRecorder mediarecorder = new MediaRecorder(); mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mediarecorder.setOutputFile("/sdcard/myaudiorecord.3gpp"); mediarecorder.prepare(); mediarecorder.start(); //To stop recording use below code.. //mediarecorder.stop(); //mediarecorder.reset(); //After MediaRecorder is no longer needed, release it: //mediarecorder.release();
Continue reading…

Enjoyed this post? Share it!

 
 

Playing music/audio file in Android

string mediaFilePath = "PATH TO YOUR MUSIC/AUDIO FILE"; File mediafile = new File(mediaFilePath); short[] audio = new short[(int) (mediafile.length()/2)]; AudioTrack audiotrack = new AudioTrack( AudioManager.STREAM_MUSIC, 11025, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, audio.length, AudioTrack.MODE_STREAM );   audiotrack.play(); audiotrack.write(audio, 0, audio.length); // To pause use code below //audiotrack.pause(); // To stop use code below //audiotrack.stop(); // release all resources audiotrack.release();
Continue reading…

Enjoyed this post? Share it!

 
 

Exception class hierarchy in Java

Exception AclNotFoundException ActivationException UnknownGroupException UnknownObjectException AlreadyBoundException ApplicationException AWTException BadLocationException ClassNotFoundException CloneNotSupportedException ServerCloneException DataFormatException ExpandVetoException GeneralSecurityException CertificateException CertificateEncodingException CertificateExpiredException CertificateNotYetValidException CertificateParsingException CRLException DigestException InvalidAlgorithmParameterException InvalidKeySpecException InvalidParameterSpecException KeyException InvalidKeyException KeyManagementException KeyStoreException NoSuchAlgorithmException NoSuchProviderException SignatureException UnrecoverableKeyException IllegalAccessException InstantiationException InterruptedException IntrospectionException InvocationTargetException IOException ChangedCharSetException CharConversionException EOFException FileNotFoundException InterruptedIOException MalformedURLException ObjectStreamException InvalidClassException InvalidObjectException NotActiveException NotSerializableException OptionalDataException StreamCorruptedException WriteAbortedException ProtocolException RemoteException […]
Continue reading…

Enjoyed this post? Share it!