I want to create an activity that will hide the keyboard. The activity will keep on running in the background as a service and prevent the keyboard from being visible for all the apps. As in the keyboard will not come up unless the activity is stopped. Can anyone point me towards the correct source?
put below code in your manifest file:
<activity
android:name=".View.ViewRegistration"
android:configChanges="keyboardHidden"
android:windowSoftInputMode="stateHidden" />
I hope its useful to you.and you can call below method in every activity onresume() and onPause().
public static void hideKeyboard(final Activity activity) {
InputMethodManager inputManager = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (activity.getCurrentFocus() != null) {
inputManager.hideSoftInputFromWindow(activity.getCurrentFocus()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
An Activity can not just be kept "running in the background as a service".
From the documentation:
An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you...
You would need to create an Service. I haven't tried hiding the keyboard via a service, but a wild guess is, that it isn't possible.
A solution could be to create your own keyboard, that the user would then need to switch to.
You can add this one line of code in your manifest for all those activities where you need to hide the keyboard:
android:windowSoftInputMode="stateHidden"
For example:
<activity
android:name="com.example.yourappname.MainActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden"
>
Related
I created an android app and i want to start my app at the MainActivity every time my app is launched? That is if my app is at activity B and the user press the home key, then when the app is launched again, it starts at MainActivity instead of activity B? how i can do that ?
There is a flag for exactly this purpose. In the <activity> declaration for your MainActivity add:
android:clearTaskOnLaunch="true"
This will cause the app to start at MainActivity whenever it is relaunched.
There are few ways to do this -
In your activity B override the 'onPause' method and add 'finish()'
In your manifest file where you have added <activity> tag for your activity B, you can add 'android:noHistory=true'. Check this link to understand more - https://developer.android.com/guide/topics/manifest/activity-element
I hope this will solve your issue.
Here is my source code of launcher activity.
I search a lot on stack overflow but nothing getting good answer
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_lang);
init();
}
private void init(){
spnLanguage = (Spinner)findViewById(R.id.spnLanguage);
btnNext = (Button)findViewById(R.id.btnNext);
btnNext.setOnClickListener(this);
}
Android apps do take some amount of time to start up, especially on a cold start. There is a delay there that you may not be able to avoid
https://www.bignerdranch.com/blog/splash-screens-the-right-way/
In the AndroidManifest file of your starting Activity, mention a transparent Theme
<activity
android:name="Your activity name"
android:theme="#android:style/Theme.Translucent.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
In my case delay was bit more. After searching a lot I found that it is because of android studio auto run feature. After disabling the auto run issue is solved.
I had a similar problem some time ago.
The activity is displayed to the user when it reaches the onStart() method. Your init() seems to take some time so until it finishes, you will only see the empty activity.
You could either trigger all heavy background work (that is not needed for the initial GUI setup) in a later method or first call another activity acting as splash screen if you have to keep all content of init() where it is.
I had also same issue please find below the question as well as the resolution:
White screen is displayed first time on gradle build.
I am developing an Android app with a Navigation Drawer working this way: the user opens the app and a Tutorial (an activity with a pager sliding fragments, called ScreenSlideActivity.java) pops up; when the user finishes sliding the tutorial, he presses the "finish" button resulting in the initialization of the MainActivity (creating a drawerLayout, drawerToggle etc.)
What I need to do is opening the Tutorial activity just once, after the app's first launch.
I've tried with this code in the main Activity:
if (savedInstanceState == null) {
SelectItem("tutorial");
}
making sure that ScreenSlideActivity.java is immediately started. The problem with this solution is that when the tutorial is opened, from there I'm not able to access MainActivity.java anymore, neither from the "up" botton neither from the Tutorial's last page's "finish" button, probably because for some reason I don't have the main as the parent activity anymore.
Then I've tried this solution change application's starting activity - Android modifying the manifest xml file. Adding:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
to ScreenSlideActivity.
The problem with this solution is that it changes the structure of my project, making my ScreenSlideActivity.java as the Main and Launching Activity (and therefore from here I cannot access the MainActivity anymore) while all I want to do is to display it once.
What else can I do?
You can use SharedPreferences to check/set if it's the first time opening the app
String preferences_name = "isFirstTime";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
firstTime();
}
...
public void firstTime(){
SharedPreferences sharedTime = getSharedPreferences(preferences_name,0);
if (sharedTime.getBoolean("firstTime",true))
{
//Your tutorial code
sharedTime.edit().putBoolean("firstTime",false).apply();
}
else
{
//When not using tutorial code
}
}
Similiar question here : https://stackoverflow.com/a/13237848/4555911
What I usually to deal with temporary screens is similar to your first solution:
In your MainActivity check if it is the first time the user launches the application. The most common way to do this is using SharedPreferences and checking if a boolean is true or false.
If it is the first time, you start a new activity for your ScreenSlideActivity class with no particular flags. your MainActivity will be in the stack:
MainsActivity -> ScreenSlideActivity
When the users press the finish button you can call the finish() method on your ScreenSlideActivity which will remove the activity from the stack and bring back your MainActivity.
I am attempting to use a swipe gesture to finish a fragment, and if the fragment is displaying the keyboard, the keyboard will be hidden. The following is a sample:
public void finishFragment() {
View focus = getCurrentFocus();
InputMethodManager imm = null;
if (focus != null) {
imm = (InputMethodManager) focus.getContext().
getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(focus.getWindowToken(), 0);
}
}
if (getFragmentManager().getBackStackEntryCount() > 1) {
getFragmentManager().popBackStack();
}
}
Unfortunately, since I am calling a service along side the fragment stack, the service is hanging up the UI display, thus giving it a laggy appearance. Does anyone know how I can hide the keyboard without calling a service or should I call an Async process? Even better, does anyone know how onBackPressed() removes the keyboard?
There are only 2 ways to control the keyboard on Android:
Programmatically with the InputMethodManager like you are doing (which will as far as I know always requires you to call .getSystemService() to use)
Using android:windowSoftInputMode="X" to specify the behavior of the keyboard in the context of the given activity tag in the AndroidManifest
So to answer your questions of: "Does anyone know how I can hide the keyboard without calling a service"? You could do the following in the AndroidManifest, where ActivityX is the activity you want to have this type of behavior.
AndroidManifest.xml:
<activity
android:name="com.namespace.of.ActivityXYZ"
android:windowSoftInputMode="stateHidden" />
This will serve to hide the keyboard initially for ActivityX, but I am not sure if this will give you the desired behavior/lack of laggyness when swiping to finish the fragment. Also try changing "stateHidden" to "stateAlwaysHidden" or "stateUnchanged". Checkout what these do here if you do not know.
Another approach would be: instead of checking the focus and getting the keyboard programmatically like you are doing, why not just finish() the activity managing the fragment you wish to end? This will also hide the keyboard if it was open.
Hope this helps!
I have a ListFragment which opens an Activity when it is clicked. Now, my problem is that my listener is lost when the screen is rotated. The click events does not respond. I have tried android:configChanges , it fixes the listener problem but the layout of the whole activity looks weird. Any possible solutions to set listener again on configuration change?
Best solution: The better way to handle the problem by using onSaveInstanceState().see this link Handling Runtime Changes
No.2: You can detect the change and then handle it through the below method:
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
// Checks the orientation
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape mode", Toast.LENGTH_SHORT).show();
} else if (config.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait mode", Toast.LENGTH_SHORT).show();
}
}
The forced solution: if you don't want to recycle the activity lifecycle on orientation change then you can add this in your manifest:
<activity android:name=".YourActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">
The last one is the worst solution, but works.
As my suggestion never use the last one because you can't able to do any kind of recycle things in future. but for emergency solution its good.