Monday, December 15, 2014

Android enable and disable orientation (Portrait or Landscape or Full Mode)

In case of if you would like enable or disable the orientation:

The one of decent way is to create a method that enabling and disabling the orientation base on input argument:
See the source code below as reference

//set up a method to enable or disable rotation

protected void controlForOrientation(string rotatedmod)
 {     
 if(rotatedmod.equalsIgnoreCase("portrait"))
 {
         // Set the screen orientation to only allow Portrait modes.
 getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
 }
        else if(rotatedmod.equalsIgnoreCase("landscape"))
 {
        //Set the screen orientation to only allow Landscape modes.
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
 }
 else
 {
        // Set the screen orientation to allow both Portrait and Landscape modes.
 getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);  
        }
}

//call for the method above

//only allow portrait
 controlForOrientation("portrait");
//only allow landscape
 controlForOrientation("landscape");
//both allow
 controlForOrientation("release");

Friday, December 5, 2014

Android: adjust scrollview's height with dialog view for different size of Device Programatically

The Source Code in xml layout file:
File: dialog_alert_scrollable.xml:

 <LinearLayout
        android:id="@+id/linearLayoutScrollViewContentScrollable"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerVertical="true"
        >

    <TextView ...  
        />
    </ScrollView>
    </LinearLayout>


The Source code in java file for Dialog:
ScrollView_Dialog.java

1. Create a Dialog:
final Dialog dialog = new Dialog(context);

        Window window = dialog.getWindow();

        window.requestFeature(Window.FEATURE_NO_TITLE);

        dialog.setContentView(R.layout. dialog_alert_scrollable);

2. Get Width and Height of Screen in order to adjust height programmatically:
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

        Display display = wm.getDefaultDisplay();

        Point size = new Point();

        display.getSize(size);

        int width = size.x;

        int screenHeight = size.y;


3. Set up height of Linearlayout programmatically base on Height of Screen:

   LinearLayout linearLayoutScrollView= (LinearLayout) dialog.findViewById(R.id.linearLayoutScrollViewContentScrollable);

        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) linearLayoutScrollView.getLayoutParams();

   // here I set up scrollview height as half of screen size, you can set up any height you want     

   lp.height=screenHeight / 2;



You should all set now, Enjoy, Please, use comment below if you have any question or concern !