How to "push up" the screen when soft keybord is displayed - android

I have in my LibGDX android application a table, below some textfields (one row) and a button below (Submit).
My question is, how to "slide" up the table, textfiels and the button when the textfield is focuses (click on it) and the soft keyboard is appearing?
In other words, how to make the soft keyboard not to cover my other objects.
This solution might be related with this question:
How can i find the height of soft-keyboard in a LibGDX application
Many thanks.

I found an workaround for my problem, it's working, but I don't think is too clean.
So, my scenario was:
a container table, containing a ScrollPane (another table), under this scrolling table two TextFields and under them a Submit button
container table set to fill parent (full screen)
the text fields and the button aligned to the bottom of the container table
The problem was that when the OnscreenKeybord was visible, it covered the bottom objects (text fields and button) and a part of the scrolling table.
My solution / workaround is:
keyboard = new OnscreenKeyboard() {
#Override
public void show(boolean visible) {
Gdx.input.setOnscreenKeyboardVisible(visible);
// Hmmmm...
container.invalidate();
if (visible) {
// TODO get OsK height somehow!!!
container.padBottom(310);
} else {
container.padBottom(0);
}
}
};
textFld1.setOnscreenKeyboard(keyboard);
textFld2.setOnscreenKeyboard(keyboard);
and on the InputListener of the Submit button (on touchDown) I have a cleanup function like:
textFld1.setText("");
textFld2.setText("");
stage.unfocus(textFld1);
stage.unfocus(textFld2);
keyboard.show(false);
In this way the container table will be padded up when the soft keyboard is displayed and it will be restored after the submit button is pressed; also the soft keyboard is hidden.
Now, there are two problems with this workaround:
I have not found a way to get the soft keyboard height; as you can see in the sample code I have chosen an arbitrary value for the up padding, so the tings to look good on my emulator
if hard button Back or ESC in emulator is pressed, the soft keyboard will hide, but the up-padding of the table will still be there; I didn't find how to determine if the soft keyboard is visible or not
If anyone finds another solution for my problem, please post it.
Thanks.

i was totally wrestling with this problem too, but i think i found a really nice solution.
Solution in theory:
when the keyboard pops up, it makes the screen resize, thus calling libgdx's resize callback.
Advantages:
no need to have logic having to do with keyboard visibility detection
no need to get height of keyboard
handle everythin in the resize callback of the libgdx's screen class
simple to implement (i think)
Disadvantages
app can no longer be fullscreen; the status bar shows and things
How to do it:
Create a main_layout.xml file, (....YourLibGdxThing\android\res\layout\main_layout.xml) such that has the following XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/main_frame_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
modify your android launcher class (....YourLibGdxThing\android\src\your\name\space\android\AndroidLauncher.java) such that the onCreate looks like this:
public class AndroidLauncher extends AndroidApplication
{
#Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.main_frame_layout);
frameLayout.addView(initializeForView(new YourGame(),config));
}
}
change android:windowFullScreen attribute in the style.xml (....YourLibGdxThing\android\res\values\styles.xml) to false; the XML should look like this in style.xml:
<resources>
<style name="GdxTheme" parent="android:Theme">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowFullscreen">false</item> <!-- IMPORTANT -->
</style>
</resources>
add the attribute android:windowSoftInputMode="adjustResize" to the android manifest, into the activity tag...my AndroidManifest (....YourLibGdxThing\android\AndroidManifest.xml) looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.project.reverse.domain.thing"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/GdxTheme" >
<activity
android:name="my.project.reverse.domain.thing.AndroidLauncher"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize" <!-- IMPORTANT -->
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Thanks! I hope it helps!!!!!!!!!!

Set android:windowSoftInputMode to adjustPan in your <activity> block to get the result you want. See docs: https://developer.android.com/guide/topics/manifest/activity-element.html#wsoft

Related

Splash-screen approach in single Activity app

I am trying to decide on the best approach for creating an app splash-screen while taking into consideration Google's latest recommendations on choosing a single Activity app whenever possible.
See here:
"The new approach is to use one-activity structure whenever possible."
and here:
"Today we are introducing the Navigation component as a framework for structuring your in-app UI, with a focus on making a single-Activity app the preferred architecture."
Any good splash-screen approaches I have found have a dedicated Activity for the splash screen:
See here
and here
Has anyone else had any experience creating a splash screen in a single Activity app? Does the the single Activity recommendation include the splash-screen or is it a special case? Does anyone have any good examples or advice on this?
Cheers,
Paul.
The approach I use is the following:
First define a drawable for the background:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/green"/>
<item>
<bitmap
android:gravity="center"
android:src="#mipmap/ic_launcher"/>
</item>
</layer-list>
2. Define a new style to use in the splashScreen:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
3. Make your activity implement use the splash theme:
<activity
android:name=".MainActivity"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
4. In on create, before the super invocation and before the set content view set the default app theme:
override fun onCreate(savedInstanceState: Bundle) {
setTheme(android.R.style.AppTheme)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main);
}
This approach is the one I've been using even with multiple Activities, since it follows the guidelines laid down by google: itshows the splash right away and doesn't stay longer than needed.
If you are using ConstraintLayout in your layouts, you can use the Group class of Android to group multiple views. Please refer to the following link for more information.
https://developer.android.com/reference/android/support/constraint/Group
This class controls the visibility of a set of referenced widgets. Widgets are referenced by being added to a comma separated list of ids, e.g:
<android.support.constraint.Group
android:id="#+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
app:constraint_referenced_ids="button4,button9" />
FYI - Multiple groups can reference the same widgets -- in that case, the XML declaration order will define the final visibility state (the group declared last will have the last word).
Hope this helps you resolve the issue.

disabling multitouch across the appwide in android xamarin

Xamarin forums question link:
https://forums.xamarin.com/discussion/60321/disabling-multitouch-across-the-appwide-in-android-xamarin#latest
Is there a way to disable multi-touch App wide in Xamarin Android.
I currently have a PCL, project. With shared code implemented using Xamarin forms UI.
I just want to prevent users from clicking more than 1 button in Android.
All our buttons are ICommand based and called from the viewModel. They are all async Tasks (we wanted to make them non-blocking and run in the background thread). --> This approach works fine in iOS, just want to disable it App wide in Android.
I tried the following in MainActivity.cs
public override bool OnTouchEvent(MotionEvent e)
{
if(e.PointerCount > 1)
{
return false;
}
else
{
return base.OnTouchEvent(e);
}
}
But it has not made any difference. Any help would be appreciated
public override void OnUserInteraction()
{
base.OnUserInteraction();
}
works but does not give me the number of touch events
Apply an application theme that disables multi-touch (derived from this answer).
Under Resources/Values add a file named Styles.xml with the following code:
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="MyAppTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:windowEnableSplitTouch">false</item>
<item name="android:splitMotionEvents">false</item>
</style>
</resources>
android:windowEnableSplitTouch indicates if touches can be split across other windows that also support split touch.
android:splitMotionEvents indicates if a ViewGroup should split MotionEvents to separate child views during touch event dispatch.
Applying these at the application theme level will forced single touch for all views in your application.
And then in your applications manifest apply the style to the android:theme attribute on the application:
Sample manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
package="com.companyname.testapp">
<uses-sdk android:minSdkVersion="11" />
<application
android:allowBackup="true"
android:icon="#mipmap/icon"
android:label="#string/app_name"
android:theme="#style/MyAppTheme">
</application>
</manifest>
This will disable multi-touch throughout the application.

Android soft keyboard keeps pushing action bar off the screen

I got a fragment which is contained inside a PageViewer which is inside a TabHost inside an activity. The fragment contains only a linear layout with a list view and under it an edit text. I have added a border around the edit text by using an xml of a rectangle shape and adding it as a background.
The activity itself is declared like this:
<activity
android:name=".MyAcivity"
android:label="#string/app_name"
android:theme="#style/CustomActivityTheme"
android:windowSoftInputMode="adjustPan">
</activity>
The theme:
<style name="CustomActivityTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:actionMenuTextColor">#color/blue</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
When pressing the edit text, the soft keyboard is appearing and it's hiding both the action bar and the tab host. It's also hiding the bottom part of the border around the edit text.
I have already tried switching from adjustPan to adjustResize but adjustResize got no effect at all on the layout and is acting the same as if I didn't declare it at all.
What can be done?
I eventually didn't even need the WindowSoftMode property. What confused me was the fact that the EditText was getting smaller when I pressed it and the soft keyboard was shown. Inside the fragment I had a list which had a height weight of 0.9. When I changed it to 0.85 it was fixed. I guess it was too much for edit text to handle when the keyboard was shown.

Android - Disable dummy starting window of application

I want to know how to achieve this effect.
My app (default everything), when launcher icon is clicked, right away display some kind of a empty dummy window, where nothing is happening and then loads layout into it.
"Heavier" apps like YouTube, Drive, Dropbox etc. when started, seem to wait after launch, without showing that dummy window and load right into ready layout.
Any idea how to do this or where should I look into?
Thanks
//EDIT: this has nothing to do with something like loading database, where I should display progressBar, imho this has to do with stuff before activity exists.
I suggest you don't disable the dummy loading window using:
<style name="Theme.MyTheme" parent="android:style/Theme" >
....
<item name="android:windowDisablePreview">true</item>
....
</style>
because you could run into several problems. For example, if you try to animate an AlertDialog, enter animation will not work (personal experience).
The right solution is to set the theme of your application like to your main activity screen (same ActionBar style, same background color).
In my application after severals experiments i found the right solution for my needs.
(I have a main activity with blank background, no ActionBar, just full screen personal layout)
1 - styles.xml: add new style like this (remove ActionBar and set the same background color of my main activity)
<style name="LoadingTheme" parent="#android:style/Theme.Light.NoTitleBar.Fullscreen">
<item name="android:windowBackground">#color/white_smoke</item>
</style>
2 - AndroidManifest.xml: set my theme for main activity to "LoadingTheme"
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.company.appname.MainActivity"
android:label="#string/app_name"
android:theme="#style/LoadingTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
....
</application>
And finally i have full blank dummy loading window without ActionBar with soft MainActivity load and working animations.
Hope to be helpful.
<style name="Theme.MyTheme" parent="android:style/Theme" >
....
<item name="android:windowDisablePreview">true</item>
....
</style>
but use with caution
This will create a custom preview window for your activity.
Create a style in styles.xml like
<style name="MyTheme" parent="#android:style/Theme.Holo.NoActionBar">
<item name="android:windowBackground">#color/myBackgroundColor</item>
</style>
In your Manifest file use the theme created above.
For eg:
<activity android:name=".MyActivity"
android:label="#string/app_name"
android:theme="#style/MyTheme"
/>
In your activity change the background of the window back to null in onCreate()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setBackgroundDrawable(null);
setContentView(R.layout.main);
}
You can call the setContentView method anywhere you'd like in your activity. Before calling this method it will display a blank screen. If you wanted a splash screen, you can call the setContentView method within the onCreate method to display the splash screen view. Then when you're ready to display the "main" view you can use a LayoutInflater to display it. I also suggest using a ProgressDialog to show the user that information is loading.
Load your resources before setting the contentview. There will be a dummy window until you would not set view using setContentView.
EDIT
To avoid this screen. Make there changes to your code.
1) Do not do any heavy work in activity onCreate method. Run a separate thread for it.
2) Set content view right after the super.onCreate() line
super.onCreate(savedInstanceState);
setContentView (R.layout.splash_screen);
3) Apply following theme to your application.
Define theme in style
<resources>
<style name="Theme.MyTheme" parent="android:style/Theme" >
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowBackground">#drawable/any_default_background</item>
</style>
</resources>
In your Manifest
<application
....
android:theme="#style/Theme.MyTheme"
....
>
Thats It.

How to hide title bar from the beginning

I am using following code to replace title bar.
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
And it's working fine once UI loaded. Problem is however when I start the app, the ugly gray bar appears for 1-2 seconds until UI loaded. Is there any way to specify not showing the default title bar at all?
If you want the titlebar to be gone in every activity within your app, then add
<application android:name=".YourAppNameHere"
android:label="#string/app_name"
android:icon="#drawable/icon"
android:theme="#android:style/Theme.NoTitleBar">
to your manifest. Not 100% sure though that this will prevent the titlebar from showing up momentarily, but it should work.
In the Manifest file, add this line inside the application tag
android:theme="#android:style/Theme.NoTitleBar"
It will hide the bar from all the activities. If you want to hide it from a specific activity, add the same line to that activity's tag.
Good Luck !
You should add a line to your AndroidManifest which states that you use a theme (standard android or extended)
<application android:name=".YourAppNameHere"
android:label="#string/app_name"
android:icon="#drawable/icon"
android:theme="#style/MyTheme">
and then you can have a themes.xml in your res/values/ folder where you extend the: Theme.NoTitleBar and add custom rules to them (for example like windowBackground)
<resources>
<style name="MyTheme" parent="android:Theme.NoTitleBar">
<item name="android:windowBackground">#drawable/my_background</item>
</style>
<resources>
Have fun

Categories

Resources