Set Auto Background for ImageView - Android User Interface

Android examples for User Interface:ImageView

Description

Set Auto Background for ImageView

Demo Code

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.view.Display;
import android.view.WindowManager;
import android.widget.ImageView;

public class Main {
  private static String TAG = "";

  public static void AutoBackground(Activity activity, ImageView view, int Background_v, int Background_h) {
    int orient = ScreenOrient(activity);
    if (orient == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { // ????
      view.setImageResource(Background_v);
    } else {/*  w  w w. j  a v  a2s . c o  m*/
      view.setImageResource(Background_h);
    }
  }

  public static int ScreenOrient(Activity activity) {
    int orient = activity.getRequestedOrientation();
    if (orient != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE && orient != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
      WindowManager windowManager = activity.getWindowManager();
      Display display = windowManager.getDefaultDisplay();
      int screenWidth = display.getWidth();
      int screenHeight = display.getHeight();
      orient = screenWidth < screenHeight ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
          : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    }
    return orient;
  }
}

Related Tutorials