Simple android showDialog example

A dialog can have a custom layout with buttons, text views, list, images, checkboxes, etc. You can create a layout for your dialog and show the dialog with the following simple method.

The showDialog method can be a part of your activity class. In that case you don’t need to explicitly declare the context as parameter. Just use this in case of context in the method body.

public void showDialog(Context context, String title, String message)
{
    final Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.custom_dialog_layout);
    dialog.setTitle(text);
    TextView messageBox = (TextView ) dialog.findViewById(R.id.dialog_message);
    messageBox.setText (message);
    Button dialogButton = (Button) dialog.findViewById(R.id.dialog_button_ok);
    dialogButton.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // do something here when ok is pressed
            dialog.dismiss();
        }
    });
    dialog.show();
}

Tha lyout filename is dialog_message.xml in this example.


       
    
    

You need to use HTML in your message. Simply referencing a String with HTML in your messageBox will not work. If you want to show html formated text in your dialog, you can use the following method to set the text of your text view.

    messageBox.setText (Html.fromHtml(message));

The following HTML tags are supported By TextView:


, , , 
,
, , ,
, , ,

,

,

,

,

,
, , ,

, , , , , , ,

Alternatively, you can use the great HTMLDialog library for you html dialog. (https://github.com/msoftware/HtmlDialog). The HtmlDialog lib simplifies the display of HTML in a DialogFragment. It is useful for displaying Open Source Licenses, EULAs, Terms of Service pages, etc.

The HTMLDialog is licensed under the Apache License, Version 2.0

Oracle reverse engineering

To reverse engineere the oracle database, you can use the Oracle data dictionary like follows.

-- Get table information from oracle DB
select T.* from USER_TABLES T;

-- Get column information from oracle DB
select C.* from USER_TAB_COLUMNS C;

-- Get constraint information from oracle DB
SELECT cons.constraint_type, cols.table_name, cols.column_name, cols.position, cons.status, cons.owner
FROM all_constraints cons, all_cons_columns cols
WHERE cons.constraint_name = cols.constraint_name
ORDER BY cols.table_name, cols.position;

-- oracle constraint types
--C (check constraint on a table)
--P (primary key)
--U (unique key)
--R (referential integrity)
--V (with check option, on a view)
--O (with read only, on a view)

-- Get all referential constraints from oracle DB including referencing table
select UCC.TABLE_NAME, UCC.CONSTRAINT_NAME, UCC.COLUMN_NAME, UC.CONSTRAINT_TYPE, UC.SEARCH_CONDITION, UC2.TABLE_NAME as REFERENCES_TABLE
from USER_CONS_COLUMNS UCC, USER_CONSTRAINTS UC, USER_CONSTRAINTS UC2
where UCC.CONSTRAINT_NAME = UC.CONSTRAINT_NAME
and UC.R_CONSTRAINT_NAME = UC2.CONSTRAINT_NAME(+) and UC.CONSTRAINT_TYPE = 'R'
order by UCC.CONSTRAINT_NAME;

-- Get ddl of a table from oracle by name
SELECT DBMS_METADATA.GET_DDL('TABLE','MY_TABLE_NAME') FROM DUAL;

-- Get all referential constraints from oracle DB including referencing table and column     
SELECT CONS.CONSTRAINT_NAME, CONS.TABLE_NAME, COLS.COLUMN_NAME,
    CONS.R_CONSTRAINT_NAME, CONS_R.TABLE_NAME R_TABLE_NAME, COLS_R.COLUMN_NAME R_COLUMN_NAME
FROM USER_CONSTRAINTS CONS
    LEFT JOIN USER_CONS_COLUMNS COLS ON COLS.CONSTRAINT_NAME = CONS.CONSTRAINT_NAME
    LEFT JOIN USER_CONSTRAINTS CONS_R ON CONS_R.CONSTRAINT_NAME = CONS.R_CONSTRAINT_NAME
    LEFT JOIN USER_CONS_COLUMNS COLS_R ON COLS_R.CONSTRAINT_NAME = CONS.R_CONSTRAINT_NAME
-- returns only foreign key constraints
WHERE CONS.CONSTRAINT_TYPE = 'R' -- AND CONS.TABLE_NAME = 'MY_TABLE_NAME'
ORDER BY CONS.TABLE_NAME, COLS.COLUMN_NAME;

Export sharedPreferences to file

With the getAll function of sharedPreferences it is possible to save the shared preferences to a file. The following code example is probably the easyest way to do it. With the saveSharedPreferences method you can save all shared preferences a file to the root of your sdcard.

Make sure your App has the following permission. It allows the application to write to external storage.

 
/*
 Possible modes: 
 MODE_PRIVATE
 MODE_WORLD_READABLE
 MODE_WORLD_WRITEABLE
 MODE_MULTI_PROCESS
*/

/* Execute save SharedPreferences example code */
...
String name = "MyName";
int mode = MODE_PRIVATE;
File path = new File(Environment.getExternalStorageDirectory().toString());
File file = new File(path, "MySharedPreferences");
saveSharedPreferences (name, mode, file);
...


private void saveSharedPreferences(String name, int mode, File file)  
{
    SharedPreferences prefs = getSharedPreferences(name, mode);
    try
    {
        FileWriter fw = new FileWriter(file);
        PrintWriter pw = new PrintWriter(fw);
        Map prefsMap = prefs.getAll();
        for(Map.Entry entry : prefsMap.entrySet())
        {
            pw.println(entry.getKey() + ": " + entry.getValue().toString());            
        }
        pw.close();
        fw.close();
    }
    catch (Exception e)
    {
        Log.wtf(getClass().getName(), e.toString());
    }
}

Ripple effect on touched item

Google has started to use ripple animations in Material Design UIs. The setHotspot() method is added in API Level 21. This teaches the drawable a “hot spot”, and the RippleDrawable apparently uses this as the emanation point for the ripple effect. The setHotspot() method of a view take the x,y values from the MotionEvent to set the hot spot coordinates.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  view.setOnTouchListener(new View.OnTouchListener() {
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public boolean onTouch(View v, MotionEvent event) {
      v
        .findViewById(R.id.row_content)
        .getBackground()
        .setHotspot(event.getX(), event.getY());

      return(false);
    }
  });
}

Source: http://commonsware.com/

Android Reflection-API Constructor without parameters

If you have to create an object with default constructor, you can invoke the newInstance() method on a Class.
This simple class creates the instance of a named class using the empty default constructor by calling the newInstance method:

public class ReflectionTester throws InstantiationException, IllegalAccessException, ClassNotFoundException 
{
   public static Object createObject(String className) {
      return Class.forName(className).newInstance();
   }
}

Hide Keyboard

Request to hide the soft input window from the context of the window that is currently accepting input. This should be called as a result of the user doing some actually than fairly explicitly requests to have the input window hidden.

/**
* This method hides the soft input window, if it's open.
*
* @param activity Activity to hide soft input window
*/
public static void hideKeyboard(Activity activity) {
    if (activity != null && activity.getCurrentFocus() != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }
}

Source: https://www.reddit.com/r/androiddev/wiki/utilities