Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
adding landscape support
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasBielefeld committed Jul 16, 2017
1 parent ca617d3 commit 9cd3bf2
Show file tree
Hide file tree
Showing 12 changed files with 742 additions and 124 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
applicationId "de.tobiasbielefeld.brickgames"
minSdkVersion 9
targetSdkVersion 24
versionCode 13
versionName '2.1.1'
versionCode 14
versionName '2.2'
}
buildTypes {
release {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/assets/changelog.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<h3>Ver. 2.2</h3>
<p>
- Added landscape support
- Added an entry in the settings to set a fixed orientation
</p>

<h3>Ver. 2.1.1</h3>
<p>
- Changed the info text for the physical keyboard input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ protected void attachBaseContext(Context base) {
@Override
public void onResume() {
super.onResume();
// setOrientation(this);
setOrientation(this);
showOrHideStatusBar(this);
}

/*
* Sets the screen orientation according to the settings. It is called from onResume().
* @param activity The activity to apply the orientation on.
*/
public static void setOrientation(Activity activity) {
switch (savedData.getString("pref_key_orientation", "1")) {
case "1": //follow system settings
Expand All @@ -79,17 +79,19 @@ public static void setOrientation(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
}
}*/
}

/**
* Hides the status bar according to the settings. It is called from onResume().
* @param activity The activity to apply the changes on.
*/
public void showOrHideStatusBar(Activity activity) {
if (savedData.getBoolean(getString(R.string.prefKeyHideStatusBar), false))
if (savedData.getBoolean("prefKeyHideStatusBar", true)) {
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
else
}
else {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
}
68 changes: 57 additions & 11 deletions app/src/main/java/de/tobiasbielefeld/brickgames/ui/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
Expand All @@ -36,6 +37,7 @@
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
Expand All @@ -50,7 +52,6 @@
import de.tobiasbielefeld.brickgames.surfaceViews.GameView2;
import de.tobiasbielefeld.brickgames.ui.about.AboutActivity;

import static android.view.View.GONE;
import static de.tobiasbielefeld.brickgames.SharedData.*;
import static de.tobiasbielefeld.brickgames.classes.Game.*;

Expand Down Expand Up @@ -79,13 +80,13 @@ public class Main extends CustomAppCompatActivity implements Runnable, View.OnTo
LinearLayout linearLayoutGameField;
LinearLayout linearLayoutTexts ;
LinearLayout linearLayoutGameExtra ;
LinearLayout linearLayoutButtons ;
LinearLayout layoutButtons1;
FrameLayout layoutButtons2;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
createSoundPool();

mLinearLayoutBackground = (LinearLayout) findViewById(R.id.linearLayoutBackground); //the whole display
Expand All @@ -94,7 +95,8 @@ public void onCreate(Bundle savedInstanceState) {
linearLayoutGameField = (LinearLayout) findViewById(R.id.linearLayoutGameField);
linearLayoutTexts = (LinearLayout) findViewById(R.id.linearLayoutTexts);
linearLayoutGameExtra = (LinearLayout) findViewById(R.id.linearLayoutGameExtra);
linearLayoutButtons = (LinearLayout) findViewById(R.id.linearLayoutButtons);
layoutButtons1 = (LinearLayout) findViewById(R.id.layoutButtons1);
layoutButtons2 = (FrameLayout) findViewById(R.id.layoutButtons2);
vibration = (Vibrator) getSystemService(VIBRATOR_SERVICE);
mToolbar = (Toolbar) findViewById(R.id.toolbar);

Expand Down Expand Up @@ -142,8 +144,6 @@ public void onCreate(Bundle savedInstanceState) {
mButton[i].setOnTouchListener(this);
}

//linearLayoutButtons.setOnTouchListener(this);

setBackgroundColor();
changeButtonColor();

Expand All @@ -154,7 +154,11 @@ public void onCreate(Bundle savedInstanceState) {
mLinearLayoutBackground.post(new Runnable() {
@Override
public void run() {
setUpDimensions();
if (getResources().getConfiguration().orientation==Configuration.ORIENTATION_PORTRAIT) {
setUpDimensions();
} else {
setUpDimensionsLandscape();
}
}
});

Expand Down Expand Up @@ -221,8 +225,13 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data.getIntExtra(getString(R.string.prefKeyHideStatusBar), 0) > 0)
showOrHideStatusBar(this);

if (data.getIntExtra(getString(R.string.prefKeyEnableKeyboardInput),0) > 0)
setUpDimensions();
if (data.getIntExtra(getString(R.string.prefKeyEnableKeyboardInput),0) > 0) {
if (getResources().getConfiguration().orientation==Configuration.ORIENTATION_PORTRAIT) {
setUpDimensions();
} else {
setUpDimensionsLandscape();
}
}
}
}

Expand Down Expand Up @@ -576,6 +585,41 @@ public void handleMessage(Message msg) {
}
}

private void setUpDimensionsLandscape(){
int marginTop;
int totalWidth = mLinearLayoutBackground.getWidth();
int totalHeight = mLinearLayoutBackground.getHeight();

width = (int)( (totalWidth * 0.225)/ (FIELD_WIDTH));
params = new LinearLayout.LayoutParams((FIELD_WIDTH) * width ,totalHeight);

marginTop = (totalHeight - (FIELD_HEIGHT)* width - distanceWidth * 2)/2;

if (savedData.getBoolean(getString(R.string.prefKeyEnableKeyboardInput),false)) {

layoutButtons1.setVisibility(View.GONE);
layoutButtons2.setVisibility(View.GONE);
} else {

layoutButtons1.setVisibility(View.VISIBLE);
layoutButtons2.setVisibility(View.VISIBLE);
}

params = new LinearLayout.LayoutParams((FIELD_WIDTH) * width + distanceWidth * 2,(FIELD_HEIGHT)* width + distanceWidth * 2);
params.setMargins(0, marginTop, 0, 0); //width is okay
linearLayoutGameField.setLayoutParams(params);

linearLayoutGameExtra.setLayoutParams(new LinearLayout.LayoutParams(width * FIELD_WIDTH_2 + distanceWidth * 2, width * FIELD_HEIGHT_2 + distanceHeight * 2));
//
//l1.setLayoutParams(params);
/*linearLayoutMenuButtons.setLayoutParams(new LinearLayout.LayoutParams((totalWidth / 2 - ((FIELD_WIDTH * width) / 2)) - distanceWidth, LinearLayout.LayoutParams.MATCH_PARENT));
linearLayoutGameField.setLayoutParams(new LinearLayout.LayoutParams(FIELD_WIDTH * width + distanceWidth * 2, LinearLayout.LayoutParams.MATCH_PARENT));
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.setMargins(width, 0, 0, 0);
linearLayoutTexts.setLayoutParams(params);
linearLayoutGameExtra.setLayoutParams(new LinearLayout.LayoutParams(width * FIELD_WIDTH_2 + distanceWidth * 2, width * FIELD_HEIGHT_2 + distanceHeight * 2));*/
}

private void setUpDimensions(){
int marginTop;
int totalWidth = mLinearLayoutBackground.getWidth();
Expand All @@ -586,10 +630,12 @@ private void setUpDimensions(){

if (savedData.getBoolean(getString(R.string.prefKeyEnableKeyboardInput),false)) {
marginTop = (totalHeight - params.height)/2;
linearLayoutButtons.setVisibility(View.GONE);
layoutButtons1.setVisibility(View.GONE);
layoutButtons2.setVisibility(View.GONE);
} else {
marginTop = width;
linearLayoutButtons.setVisibility(View.VISIBLE);
layoutButtons1.setVisibility(View.VISIBLE);
layoutButtons2.setVisibility(View.VISIBLE);
}

params.setMargins(0, marginTop, 0, 0); //width is okay
Expand Down
26 changes: 24 additions & 2 deletions app/src/main/java/de/tobiasbielefeld/brickgames/ui/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ public class Settings extends AppCompatPreferenceActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//((ViewGroup) getListView().getParent()).setPadding(0, 0, 0, 0); //remove huge padding in landscape
((ViewGroup) getListView().getParent()).setPadding(0, 0, 0, 0); //remove huge padding in landscape
addPreferencesFromResource(R.xml.pref_settings);
showOrHideStatusBar();

Expand Down Expand Up @@ -189,6 +188,9 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
case "prefKeyEnableKeyboardInput":
getIntent().putExtra(getString(R.string.prefKeyEnableKeyboardInput), 1);
break;
case "pref_key_orientation":
setOrientation();
break;
}

setResult(RESULT_OK, getIntent());
Expand All @@ -207,4 +209,24 @@ private void showOrHideStatusBar() {
else
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

/**
* Applies the user setting of the screen orientation.
*/
private void setOrientation() {
switch (savedData.getString("pref_key_orientation", "1")) {
case "1": //follow system settings
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
break;
case "2": //portrait
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case "3": //landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case "4": //landscape upside down
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
}
}
}
Loading

0 comments on commit 9cd3bf2

Please sign in to comment.