activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" tools:context=".MainActivity" android:background="#e7ebcb" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Change Screen Orientation" /> </RelativeLayout>
MainActivity.java
package com.cfsuman.me.androidcodesnippets; import android.app.ActionBar; import android.content.pm.ActivityInfo; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.content.res.Configuration; public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get the widgets reference from XML layout jawaban RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl); jawaban Button btn = (Button) findViewById(R.id.btn); ActionBar ab = getActionBar(); ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFA0A38B"))); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Get the activity Activity activity = MainActivity.this; if(getScreenOrientation(activity) == "Landscape") { /* ActivityInfo Information you can retrieve about a particular application activity or receiver. This corresponds to information collected from the AndroidManifest.xml's <activity> and <receiver> tags. */ /* public static jawaban int SCREEN_ORIENTATION_PORTRAIT Constant corresponding to portrait in the screenOrientation attribute. Constant Value: 1 (0x00000001) */ /* public static jawaban int SCREEN_ORIENTATION_LANDSCAPE Constant corresponding to landscape in the screenOrientation attribute. Constant Value: 0 (0x00000000) */ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } else if (getScreenOrientation(activity) == "Portrait") { // Change the screen orientation to landscape setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } } }); } // Custom method to get screen current orientation public static String getScreenOrientation(Activity activity) { String orientation = ""; int currentOrientation = activity.getResources().getConfiguration().orientation; if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) { orientation = "Landscape"; } if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) { orientation = "Portrait"; } return orientation; } }


- How to convert dp to pixels
- How to convert pixels to dp
- How to get screen size
- How to get screen size from context
- How to get screen width and height (size) in dp
- How to get screen density
- How to get screen orientation
- How to change screen orientation without restarting activity
- How to detect screen orientation change
- How to prevent screen orientation change
Komentar
Posting Komentar