Payday Loan Payday Loan

Archive for March, 2012

The following regex expression will help you to determine if there is a valid e-mail address or not in your edit text.

public static boolean isValid(String email) {
 
    String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
    CharSequence inputStr = email;
    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    if (matcher.matches()) {
        return true;
    }
    else{
    return false;
    }
}
||||| 0 I Like It! |||||

For controlling the hide/show comportment of soft keyboard you may use the following code the works in any circumstances:

// Show soft-keyboard:
 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
 
// Hide soft-keyboard:
 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Also for closing you may use this:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
||||| 0 I Like It! |||||

Hi,
Today i want to show you how you can serialize or deserialize an Object. I will take like example a String object, but there can be any type of Object.
To serialize an object means to make it a binary, and to deserialize it, means to make it from binary to the Object.
So let’s start.
This is the method for serializing a generic Object.

public class SerializeClass {
 public static byte[] serializeObject(Object o) { 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
 
    try { 
      ObjectOutput out = new ObjectOutputStream(baos); 
      out.writeObject(o); 
      out.close(); 
 
      // Get the bytes of the serialized object 
      byte[] buffer = baos.toByteArray(); 
 
      return buffer; 
    } catch(IOException ioe) { 
 
      return null; 
    } 
 }
 public static Object deserializeObject(byte[] b) { 
    try { 
      ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(b)); 
      Object object = in.readObject(); 
      in.close(); 
 
      return object; 
    } catch(ClassNotFoundException cnfe) { 
      Log.e("deserializeObject", "class not found error", cnfe); 
 
      return null; 
    } catch(IOException ioe) { 
      Log.e("deserializeObject", "io error", ioe); 
 
      return null; 
    } 
  } 
}

To call this methods you will do like this:
Your class must implement Serializable for this to work

public class MyClass implements Serializable {
 String text;
 
MyClass myClass = new MyClass();
byte[] binaryObject = SerializeClass.serializeObject(myClass);
MyClass deserializedMyClass = SerializeClass.deserializeObject(binaryObject);
||||| 1 I Like It! |||||

As you observed, there is no direct method to limit the number of characters that can be typed in am EditText.
However, there is a solution using InputFilter. Follow the below script and you can set up maximum numbers of characters you allow to be entered in an EditText.

  int maxLength = 50;
  InputFilter[] FilterArray = new InputFilter[1];
  FilterArray[0] = new InputFilter.LengthFilter(maxLength);
  editText.setFilters(FilterArray);

To use InputFilter, you must import android.text.InputFilter;

||||| 0 I Like It! |||||

Many times you needed to know the position in the listView where you long tapped, so that you know what to do in the context menu.
For finding this, there is a simple way that not involves using adapters, or listView at all. Look at code below:

  @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.edit_route_context_menu, menu);
        //below line is the key line. you write this and in info variable you have the position wished
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        int position = info.position;//do what you want with the position
    }
||||| 0 I Like It! |||||

How to create a custom progress dialog

Hi,
I will show you how to create a custom progress dialog. Below is the code for doing this:

import java.util.Timer;
import java.util.TimerTask;
 
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
 
public class MyApp extends Activity {
    Timer test;
    int ticks = 0;
    boolean counting = false;
    Dialog dialog;
    TextView text1;
    TextView text2;
    ProgressBar mProgress;
 
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
 
        //Create new daemon timer
        test = new Timer(true);
        test.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                TimerMethod();
                }//run
            }, 1000, 1000);//schedule
 
        Button start = (Button)findViewById(R.id.button_start);
        start.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                ticks = 0;
                counting = true;
                dialog = new Dialog(MyApp.this);
                dialog.setContentView(R.layout.custom_dialog);
                dialog.setTitle("Load Progress");
                mProgress = (ProgressBar) dialog.findViewById(R.id.progressbar);
                text1 = (TextView) dialog.findViewById(R.id.text1);
                text1.setText("0%");
                text2 = (TextView) dialog.findViewById(R.id.text2);
                text2.setText("0Mb/0Mb");
                dialog.show();
 
                }
            });
        }
    private void TimerMethod() {
        //This method is called directly by the timer
        //and runs in the same thread as the timer.
 
        //We call the method that will work with the UI
        //through the runOnUiThread method.
        this.runOnUiThread(Timer_Tick);
        }//timermethod
 
    private Runnable Timer_Tick = new Runnable() {
        public void run() {
            //This method runs in the same thread as the UI.
            //Do something to the UI thread here
        	if (counting) {
        	    ticks++;
        	    int percent = (int)(100f * ((float)ticks/10f));
        	    String newRatio = String.format("%1$dMb/%2$dMb", ticks, 10); 
        	    String newPercent = String.format("%1$d%%", percent);
        	    text1.setText(newPercent);
        	    text2.setText(newRatio);
        	    mProgress.setMax(10);
        	    mProgress.setProgress(ticks);   
        	    if (ticks == 10) {
        	    	counting = false;
        	    	dialog.dismiss();
        	        }//ending
        	    }//counting
 
            }//run
        };//runnable
 
    //TODO: Fill In Methods Etc.
    }//class

And here is the custom_dialog.xml code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/layout_root" android:orientation="vertical"
 android:layout_width="fill_parent" android:layout_height="wrap_content" 
android:padding="10dp">
  <ProgressBar android:id="@+id/progressbar"
 android:layout_width="fill_parent" android:layout_height="wrap_content"
 style="@android:style/Widget.ProgressBar.Horizontal" android:paddingTop="20dp" 
android:paddingBottom="20dp" /> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" android:layout_height="wrap_content"
 android:padding="10dp">
  <TextView android:id="@+id/text1" android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:textColor="#FFF" 
android:layout_alignParentLeft="true" /> 
  <TextView android:id="@+id/text2" android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:textColor="#FFF"
 android:layout_alignParentRight="true" /> 
  </RelativeLayout>
  </LinearLayout>
||||| 0 I Like It! |||||

You can test out your app, in a very brutal mode, so that you not need to try hard to find bugs.
This app will automatically test your app, by sending touches, key presses, and other stuff in a random way so that your app can be tested in a fast way.
You need to connect your device to adb and open a console by cmd command.
In cmd, go to your location of android-sdk-windows folder. Here you must find your adb.exe. Usually is located at platform-tools folder.
So, if in cmd you are located at ../android-sdk-windows/platform-tools/
then type adb shell monkey -p your.package.name -v 500
This command will start monkey runner tool from adb.
500 means how many actions will take place…so you can change it as you wish.
If you have connected your phone trough usb in debugging mode, then your app should be started and clicks and keys must be started to automatically test your android app.

||||| 1 I Like It! |||||

Search

Popular

Sponsors