onBackPressed() Orientation Issue - android

Activity supports Landscape mode
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:screenOrientation="landscape"
android:label="#string/app_name" >
where I have allocated 50% space to Video Player (using FrameLayout) and rest 50% to ListView.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/video_frame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<ListView
android:id="#+id/video_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
Now, I have started playing video > moved to full screen mode > pressed back to exit full screen mode > getting Activity in Portrait mode (whereas I was expecting to get it in Landscape mode)
boolean isFullScreen = false;
#Override
public void onGoToFullscreen() {
isFullScreen = true;
videoListView.setVisibility(View.INVISIBLE);
}
#Override
public void onReturnFromFullscreen() {
videoListView.setVisibility(View.VISIBLE);
}
#Override
public void onBackPressed() {
Log.d("boolean:-", Boolean.toString(isFullScreen));
if(isFullScreen) {
imaPlayer.getContentPlayer().setFullscreen(false);
}
else {
super.onBackPressed();
}
}

/* I am not sure,can you put this line of code in on back pressed method ? */
Log.d("boolean:-", Boolean.toString(isFullScreen));
if(isFullScreen) {
imaPlayer.getContentPlayer().setFullscreen(false);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else {
super.onBackPressed();
}

Related

How to hide certain view while android keyboard is shown?

I am trying to write a code to hide certain ui elements while Android Keyboard is shown , i tried to add "adjustResize" in the manifest but it re-sizing the elements automatically but not hiding some of the views in the layout.
MyCode:
package com.example.code;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks whether a hardware keyboard is available
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "No above", Toast.LENGTH_SHORT).show();
}
} }
XML :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.MainActivity">
<ScrollView
android:id="#+id/scroll"
android:layout_width="wrap_content"
android:layout_height="300dp">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/image_back"/>
</ScrollView>
<TextView
android:id="#+id/xyz"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:layout_alignParentBottom="true"
android:background="#android:color/holo_orange_dark"
android:gravity="center_horizontal"
android:text="Test Layout"
android:textSize="40sp" />
<EditText
android:id="#+id/abc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/xyz"
android:layout_alignParentLeft="true"
android:background="#android:color/holo_blue_bright"
android:ems="10"
android:gravity="center_horizontal"
android:text="Please enter text"
android:textSize="40sp" >
<requestFocus />
</EditText>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.code"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.code.MainActivity"
android:windowSoftInputMode="adjustResize"
android:label="#string/app_name"
android:configChanges="orientation|screenSize|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Screenshots:
So , these are above codes and screenshot of my app , so i am want hide the "Test Layout" textview while keyboard is open. So , please suggest me some technique and solution for my issue.
You can try something like this and update the visibility of your layout according to the visibility of the keyboard.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks whether a hardware keyboard is available
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
}
}
/*Hide button when keyboard is open*/
view.getViewTreeObserver().addOnGlobalLayoutListener(new
ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
view.getWindowVisibleDisplayFrame(r);
int heightDiff = view.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 244) { // if more than 100 pixels, its probably a keyboard...
//ok now we know the keyboard is up...
buttonLayout.setVisibility(View.GONE);
} else {
//ok now we know the keyboard is down...
buttonLayout.setVisibility(View.VISIBLE);
}
}
});

Clearing Focus does not work

Today i found some posts about how to celar the focus of an EditText.
It was recommed to do this:
Set focusable and focuableInTouchMode to true for the parent layout:
<LinearLayout
android:id="#+id/my_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true" >
<EditText
android:id="#+id/calc_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
And to use this in the DialogFragment:
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.textEditCalcInput = (EditText) view.findViewById(R.id.calc_input);
this.textEditCalcInput.clearFocus();
view.findViewById(R.id.my_layout).requestFocus();
}
But unfortunately this does not work. The textfield always has the focus when the ui appears.
So how to remove the focus?
Here i want the AlertDialog to be shown and the Keyboard to be hidden.
Instead of
android:focusable="true"
android:focusableInTouchMode="true"
in LinearLayout use:
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
Note that you can also use
android:windowSoftInputMode="stateHidden"
in AndroidManifest.XML.
Another solution is adding
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
to onCreate of the Activity in order to hide keyboard whenever the activity starts.
Try this in manifest:
<activity
android:name=".YourActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="stateHidden" >
</activity>
and if doesn't work try this in your Activity:
#Override
public void onConfigurationChanged(Configuration mConfig) {
super.onConfigurationChanged(mConfig);
int orientation = getResources().getConfiguration().orientation;
switch(orientation) {
case Configuration.ORIENTATION_LANDSCAPE:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//hide keyboard
break;
case Configuration.ORIENTATION_PORTRAIT:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//do whatever you want
break;
default:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
//do whatever you want
break;
}
}

how to close the slidind drawer with the return button - android

i try to close the sliding drawer when i click on the return back key but its not working
here is the code i used
#Override
public void onBackPressed() {
Log.d("onBackPressed Called", FROM_SETTINGS_KEY);
slidingDrawer.close();
}
the xml :
<SlidingDrawer
android:id="#+id/slidingDrawer1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="197dp"
android:content="#+id/content"
android:handle="#+id/handle" >
<Button
android:id="#+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Handle" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</SlidingDrawer>
so how can i make the return back key close this sliding drawer ?
You're overriding Activity.onKeyDown, which Activity already overrides when implementing Activity.onBackPressed, then returning false for KeyEvent.KEYCODE_BACK which indicates that you have not handled this event and it should continue to be propagated.
To fix your problem stop overriding Activity.onKeyDown. Also, change your Activity.onBackPressed to call super if the SlidingDrawer isn't opened.
private SlidingDrawer slidingDrawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
slidingDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer1);
}
#Override
public void onBackPressed() {
if (slidingDrawer.isOpened()) {
slidingDrawer.close();
} else {
super.onBackPressed();
}
}

Android: How to display a fullscreen layer (hide onClick)?

I'd like to pop up a layer in fullscreen mode (overlays the whole desktop) and hide on click.
I'm new in Android developing and looking for something like this (pseudocode):
$('#layer').on('click', function() {
this.hide();
});
<div id="layer" class="fullscreen">I'm a annoying layer, click me!</div>
Does anyone have a solution, code snippet, tutorial or some keywords to google for? I follow some basic tutorials right now and would like to know in which direction I've to focus.
Thanks in advance!
This is a simple activity that will close itself when the screen is touched.
activity_popup.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="#dimen/padding_medium"
android:text="Hello World!" />
</RelativeLayout>
PopupActivity.java
public class PopupActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup);
}
#Override
public boolean onTouchEvent(MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN){
finish();
return true;
}
return false;
}
}
androidmanifest.xml
<activity
android:name="com.your.package.name.PopupActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
</activity>

Splash screen involving finish() and back button

Alright, so I have a splash screen on my app. I was wondering if there was a way for me to have it where it only appears the first initial time the app opens (or if they force close it and such), and so that way if they press the back key, it doesn't take them to the splash screen.
I can make it so the user cant go back to the splash screen by just typing finish() and, though I cant remember it right now, I can make it where it only loads once. Once I call finish(), the splash will reappear upon reentering the app. Is there any way to fix this?
Yes, you can do that by calling finish() on the onPause method of your splashActivity.
Or another way to do this is to add android:noHistory="true" on splashActivity of your manifest.xml
With a SplashScreenActivity you're wasting time because they pause the initialization for 2-3seconds before they continue.
I prefer adding a Splash Screen Layout on top of my main_activity.xml. I detect the first start of the application by extending Application. If it´s the first start, I show my Splash-Screen while the UI is build in the background... (Use background threads if the ProgressBar lags!)
//Extend Application to save the value. You could also use getter/setter for this instead of Shared Preferences...
public class YourApplication extends Application {
public static final String YOUR_APP_STARTUP = "APP_FIRST_START";
#Override
public void onCreate() {
super.onCreate();
//set SharedPreference value to true
SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = mPreferences.edit();
editor.putBoolean(YOUR_APP_STARTUP, true);
editor.apply();
...
}
Check for your first start in your MainActivity
public class YourMainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//hide actionbar and other menu which could overlay the splash screen
getActionBar().hide();
setContentView(R.layout.activity_main);
Boolean firstStart = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean(TVApplication.YOUR_APP_STARTUP, true);
if (firstStart) {
//First app start, show splash screen an hide it after 5000ms
final RelativeLayout mSplashScreen = (RelativeLayout) findViewById(R.id.splash_screen);
mSplashScreen.setVisibility(View.VISIBLE);
mSplashScreen.setAlpha(1.0f);
final FrameLayout mFrame = (FrameLayout) findViewById(R.id.frame_container);
mFrame.setAlpha(0.0f);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Animation fadeOutAnimation = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_out_animation);
fadeOutAnimation.setDuration(500);
fadeOutAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
mFrame.setAlpha(1.0f);
getActionBar().show();
}
#Override
public void onAnimationEnd(Animation animation) {
mSplashScreen.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
mSplashScreen.startAnimation(fadeOutAnimation);
}
}, 5000); //<-- time of Splash Screen shown
} else {
((RelativeLayout) findViewById(R.id.splash_screen)).setVisibility(View.GONE);
getActionBar().show();
}
Insert the SplashScreen at top in your main.xml. I prefer RelativeLayout for that. In the example, SplashScreen is placed on to of a layout with the Navitgation Drawer, which we really love, don`t we?
//main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- The main content view -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer list -->
<ListView
android:id="#+id/slider_list"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_gravity="start"
android:background="#color/tvtv_background"
android:choiceMode="singleChoice"
android:divider="#drawable/nav_bar_divider"
android:dividerHeight="1dp"
android:listSelector="#android:color/transparent" />
</android.support.v4.widget.DrawerLayout>
<RelativeLayout
android:id="#+id/splash_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:background="#color/tvtv_white"
android:visibility="visible" >
<ImageView
android:id="#+id/splash_screen_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:scaleType="fitCenter"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/splash_screen_text"
style="#style/TVTextBlueContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/splash_screen_logo"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:text="Awesome splash shiat" />
<ProgressBar
android:id="#+id/splash_screen_loader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/splash_screen_text"
android:layout_centerHorizontal="true"
android:clickable="false"
android:indeterminate="true" />
</RelativeLayout>
</RelativeLayout>

Categories

Resources