Create a Dialog box using DialogFragment in Android

Say you want to create a dialog box that lets you remove an item from a list, or some other similar activity.

1.  Create a DialogFragment class

public class DeleteItemDialogFragment extends DialogFragment {

    @Override

    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // Use the Builder class for convenient dialog construction

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(R.string.remove_rent) .setPositiveButton(R.string.remove, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) {

                   // Send the positive button event back to the host activity

                   mListener.onDialogPositiveClick(DeleteItemDialogFragment.this); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) {

                   // Send the negative button event back to the host activity

                   mListener.onDialogNegativeClick(DeleteItemDialogFragment.this); } });

        // Create the AlertDialog object and return it

        return builder.create(); }

    /* The activity that creates an instance of this dialog fragment must

 

     * implement this interface in order to receive event callbacks.

 

     * Each method passes the DialogFragment in case the host needs to query it. */

public interface DeleteItemDialogListener { public void onDialogPositiveClick(DialogFragment dialog); public void onDialogNegativeClick(DialogFragment dialog); } DeleteItemDialogListener mListener;

    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener

 

    @Override

    public void onAttach(Activity activity) { super.onAttach(activity);

        // Verify that the host activity implements the callback interface

        try {

            // Instantiate the DeleteItemDialogListener so we can send events to the host

            mListener = (DeleteItemDialogListener) activity; } catch (ClassCastException e) {

            // The activity doesn’t implement the interface, throw exception

            throw new ClassCastException(activity.toString()

                    + ” must implement DeleteItemDialogListener”);

        } } }

2.   Set up your listener in your activity:

public class ManageRentActivity extends FragmentActivity implements DeleteItemDialogFragment.DeleteItemDialogListener {
}

3.   Use the implemented methods to set up the ‘Confirm’ and ‘Cancel’ methods:

@Override
public void onDialogPositiveClick(DialogFragment dialog) {  
         //removeRentEntry();  }
@Override 
public void onDialogNegativeClick(DialogFragment dialog) { 
         // Cancel and Return to Activity }

3. Call your DialogFragment from your Activity:

DialogFragment removeDialogFragment = new DeleteItemDialogFragment();
removeDialogFragment.show(getSupportFragmentManager(),  "DeleteItemDialogFragment");
Advertisements

Android SQLite database – A tutorial

1.   Create the DatabaseHelper class. This class is responsible for creating the database. The onUpgrade() method will simply delete all existing data and re-create the table. It also defines several constants for the table name and the table columns.

public class DatabaseHelper extends SQLiteOpenHelper {

	  private static final String DATABASE_NAME = "property.db";
	  private static final int DATABASE_VERSION = 1;
	  
	  public static final String TABLE_PROPERTY = "property";
	  public static final String COLUMN_ID = "_id";
	  public static final String COLUMN_NICKNAME = "nickname";
	  public static final String COLUMN_TENANT = "tenant";
	  public static final String COLUMN_ADDRESS = "address";
	  public static final String COLUMN_LOAN = "loan";

	  // Database creation sql statement
	  private static final String DATABASE_CREATE = "create table "
	      + TABLE_PROPERTY + "(" + COLUMN_ID
	      + " integer primary key autoincrement, " 
	      + COLUMN_NICKNAME + " text not null"
	      + COLUMN_TENANT + " text"
	      + COLUMN_ADDRESS + " address not null"
	      + COLUMN_LOAN + " integer);";

	  public DatabaseHelper(Context context) {
	    super(context, DATABASE_NAME, null, DATABASE_VERSION);
	  }

	  @Override
	  public void onCreate(SQLiteDatabase database) {
	    database.execSQL(DATABASE_CREATE);
	  }

	  @Override
	  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	    Log.w(DatabaseHelper.class.getName(),
	        "Upgrading database from version " + oldVersion + " to "
	            + newVersion + ", which will destroy all old data");
	    db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROPERTY);
	    onCreate(db);
	  }

	} 

Displaying Git commit version in Rails

At times, you’re doing rapid development and you’d like to frequently update the GIT commit version that is in use

1.  Create a ‘config/initializers/git_revision.rb’ file and update the following:

module MyProject
     REVISION=`git log --pretty=format:'%h' -n 1`
end

2.  In your _footer.html.erb or equivalent view file, call the following:

Version: <%= MyProject::REVISION %>

This should display the version as follows:

Version: 774318e

Route ‘Sign In’ to the previous page

Ever noticed after doing an elaborate search, your website asks you to do a ‘Sign in’.  After logging in, it forgets all about your search results and takes you back to your home page.

When using Rails with Devise, you may have noticed that ‘signing in’ takes you to the home page of the controller.

In order to go to the last page that redirects you to the ‘devise sign in ‘ page, make the following changes to your Devise ‘Sessions Controller’

#Take me to the referring page after sign in
def after_sign_in_path_for(resource)
   redirect_to request.referrer
end

This should redirect to the ‘referring’ or the ‘calling’ page.