Prevent softkeyboard appear from other activity - android

I got a EditText on my first tab. Of course, soft keyboard appearing when I touch on my EditText. But when I access to second tab, it means second tab activity, soft keyboard stay appearing on my first tab and don't disappear even though there is no EditText on my second tab activity.
I want to prevent softkeyboard from appearing when I access to my second tab.
How can I solve this problems?
Here's my manifest files
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidaccountbook"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.example.androidaccountbook.AccountTabActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden">
<!-- screenOrientation prevent from rotation -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.androidaccountbook.accountMainThread"
android:windowSoftInputMode="stateHidden" />
<!-- activity for intent -->
<activity android:name="com.example.androidaccountbook.TodayExpenseList"
android:windowSoftInputMode="stateHidden" />
<activity android:name="com.example.androidaccountbook.settingThread"
android:windowSoftInputMode="stateHidden"/>
<activity android:name="com.example.androidaccountbook.FailedConnectDatabase"
android:windowSoftInputMode="stateHidden"/>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>

I figured how to works on it and I tried it, it works.
Because I want to access to second activity, and surely life-activity onPause method will occurs. So, I wrote the following hideKeyboard method on my onPause method.
This is how onPause method looks like
#Override
protected void onPause(){
super.onPause();
hideKeyboard(this,editText);
}
This is my hideKeyboard method looks like
public void hideKeyboard(Context context, EditText text){
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(text.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
}

The best solution would be: (In the Manifest file)
<activity android:name=".MainActivity"
android:windowSoftInputMode="stateHidden" />

I use this and it always works:
<activity
android:name="MyProfileActivity"
android:windowSoftInputMode="stateHidden|adjustPan" />

Related

Can't get soft keyboard to go away, constantly shows up in android app despite calling InputMethodManager hideSoftInputFromWindow() method

In each of my activities that extend AppCompatActivity, my Android tablet's(Galaxy Tab A7) soft key board automatically shows up due to edit text and search views being in the layouts. In the respective activities I have a hideSoftKeyboard() method (as seen below) which I am calling in the onCreate() of each class to prevent the keyboard from showing up automatically until someone hits a search view/edit text.
public void hideSoftKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = this.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
System.out.println("made it hideSoftKeyboard");
}
how I call it in onCreate()
hideSoftKeyboard(this);
I have also tried adding the android:windowSoftInputMode="stateHidden" in my Android Manifest file's application tag (and in each individual activity tag at one point)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.receiptreader">
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:windowSoftInputMode="stateAlwaysHidden"
android:theme="#style/Theme.ReceiptReader">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:windowSoftInputMode="stateHidden"
android:theme="#style/Theme.ReceiptReader.ActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:windowSoftInputMode="stateHidden"
android:name=".StoreUserItemsActivity"
android:label="#string/store_user_items_activity"
android:theme="#style/Theme.ReceiptReader.ActionBar">
</activity>
<activity
android:windowSoftInputMode="stateHidden"
android:name=".ShoppingListUserItemsActivity"
android:label="#string/store_user_items_activity"
android:theme="#style/Theme.ReceiptReader.ActionBar">
</activity>
<activity
android:windowSoftInputMode="stateHidden"
android:name=".ShoppingListUserItemHistoryActivity"
android:label="#string/shopping_list_user_item_history"
android:theme="#style/Theme.ReceiptReader.ActionBar">
</activity>
<activity
android:windowSoftInputMode="stateHidden"
android:name=".ShoppingListsHistoryActivity"
android:label="#string/shopping_lists_history"
android:theme="#style/Theme.ReceiptReader.ActionBar">
</activity>
<activity
android:windowSoftInputMode="stateHidden"
android:name=".AddShoppingListUserItemActivity"
android:label="#string/add_shopping_list_user_item_activity"
android:theme="#style/Theme.ReceiptReader.ActionBar">
</activity>
</application>
</manifest>
As well as make the search views in my landing page(and edit texts and search views in my other layouts) not focusable by default
<SearchView
android:id="#+id/shopping_list_search_bar"
android:layout_width="wrap_content"
android:layout_height="33dp"
android:layout_gravity="center_vertical"
android:layout_marginEnd="5dp"
android:layout_weight="2"
android:background="#color/white_two"
android:focusable="false"
android:focusableInTouchMode="true"
android:focusedByDefault="false"
android:inputType="text"
android:queryHint="Search For a Shopping List" />
None of this seems to make it go away and everytime I navigate within the app it shows up again and I have to keep telling it to go down manually. I cannot have such an annoying issue for good UI design and have tried various solutions on stack overflow. Please let me know how to fix this issue or if you have any ideas to solve this or reasons for why this is happening. If anyone needs more or the full code for my classes/XML layouts let me know.

Default Page will not change

My home page needs to show two buttons: login & register. The page Login Activity with both buttons should appear first, but only the register screen comes up.
I am very new to coding. So, please be nice :( I've been stuck on this for days.
Below is the AndroidManifest.xml. Can anyone spot what I am doing wrong?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thrd">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RegisterActivity"></activity>
<activity android:name=".UserAreaActivity"></activity>
</application>
</manifest>
You manifest file is ok. Probably you have some logical errors in your code. In LoginActivity. So it launches RegisterActivity (probably in onCreate), instead of showing itself.

all activity reverse when press back button

In my project I create several class and I am able to going from one activity to other , but the problem is when I press back menu all activity reversed I don't want that!!! below is manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bones.samples.android"
android:versionCode="1"
android:versionName="1.0">
<application android:label="#string/app_name" android:icon="#drawable/icon48x48" android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<activity android:label="#string/app_name" android:screenOrientation="landscape" android:name=".stage">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="LevelOne" android:screenOrientation="landscape" ></activity>
<activity android:name="dialogue" android:theme="#android:style/Theme.Dialog" ></activity>
<activity android:name="FailDialogue" android:theme="#android:style/Theme.Dialog" ></activity>
<activity android:name="LevelTwo" android:screenOrientation="landscape" ></activity>
<activity android:name="LevelThree" android:screenOrientation="landscape" ></activity>
</application>
<uses-sdk android:minSdkVersion="10" />
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens= "true"
android:anyDensity="true"
/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.VIBRATE"/>
</manifest>
I want that when I am on leveltwo or dilogeue activity and press back button it directly go to stage activity but not in reverse all activities.
If you call one activity after another without calling finish, the activities are preserved in the back stack for further access.
If you dont want the activities to show up when the user presses back, keep calling finish method after calling the activity you want.
For Ex:-
Intent intent =new Intenet(this, NextActivity.class);
startActivity(intent);
finish();
This will close the current activity and will start the next activity. Now, when the user presses back, it will go the the activity earlier than this activity else, your application will close.
If you want to go to a specific activity, you can override the onBackPressed method and call a specific activity too.
Use:
YourActivity.this.finish()
after calling the intent to next activity, for all activities you dont want to display on BackPress.
if the above is not feasible, you can define the parent activity for up navigation:
Beginning in Android 4.1 (API level 16), you can declare the logical parent of each activity by specifying the android:parentActivityName attribute in the element.
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
This is what the Docs say
startActivity(CurrentActivity.this,NextActivity.class);
finish();
or
set flag on your manifest file
read below thread for more info
http://developer.android.com/guide/components/tasks-and-back-stack.html
or
Use fragment. u can replace the fragment . finally u can finish the activity.
Use this into Manifest...
android:noHistory="true"
as below...
<activity android:name="LevelOne" android:screenOrientation="landscape"
android:noHistory="true" ></activity>
<activity android:name="dialogue" android:theme="#android:style/Theme.Dialog"
android:noHistory="true" ></activity>
<activity android:name="FailDialogue" android:theme="#android:style/Theme.Dialog"
android:noHistory="true" ></activity>
This attribute will remove the current Activity from the Activity Stack when another Activity will start from this Activity...So, when you will press back button then it will not return to that previous Activity.

Android app always opens the first activity on resuming

I'm working on an application that currently has two activites, a log in activity and a "frontpage" activity after that.
Whenever I switch away from the application and resume it either by relaunching it or using the application switcher, it will always re-open the log in activity. Even if I switch to my app from within the frontpage activity, it opens the log in activity.
None of my other apps have ever done this, even though they obviously all have a filter for the launcher in AndroidManifest.xml. I am aware that it is supposed to do this when the activity is killed, but I cannot comprehend why it would do that.
This is my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.vertinode.facepunch"
android:versionCode="1"
android:versionName="1.0">
<!-- Android 2.2 -->
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" />
<!-- Internet access -->
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="#drawable/icon" android:label="#string/app_name" android:name=".FPApp">
<!-- Login form -->
<activity android:name="nl.vertinode.facepunch.LoginActivity" android:launchMode="singleTask" android:theme="#android:style/Theme.Light.NoTitleBar" android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Frontpage view -->
<activity android:name="nl.vertinode.facepunch.FrontpageActivity" android:theme="#android:style/Theme.Light.NoTitleBar" android:windowSoftInputMode="stateHidden">
</activity>
</application>
</manifest>
What could I be doing wrong?
I'm going to guess this has to do with your android:launchMode being singleTask.
Try singleTop instead and see if you have the same problem. Also it could have to do with some code you have in your onPause for the other activities. Does it go back to your Login activity after going to sleep/waking back up as well?

Prevent screen rotation on Android

I have one of my activities which I would like to prevent from rotating because I'm starting an AsyncTask, and screen rotation makes it restart.
Is there a way to tell this activity "DO NOT ROTATE the screen even if the user is shaking his phone like mad"?
Add
android:screenOrientation="portrait"
or
android:screenOrientation="landscape"
to the <activity> element/s in
the manifest and you're done.
You can follow the logic below to prevent auto rotate screen while your AsyncTask is running:
Store your current screen orientation inside your activity using getRequestedOrientation().
Disable auto screen orientation using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR).
Run/execute your AsyncTask.
At the end of your AsyncTask restore your previous orientation status using setRequestedOrientation(oldOrientation).
Please note that there are several ways to access Activity (which runs on UI thread) properties inside an AsyncTask. You can implement your AsyncTask as an inner class or you can use message Handler that poke your Activiy class.
The easiest way I found to do this was to put
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
within onCreate, just after
setContentView(R.layout.activity_main);
so...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
In your Manifest file, for each Activity that you want to lock the screen rotation add: if you want to lock it in horizontal mode:
<activity
...
...
android:screenOrientation="landscape">
or if you want to lock it in vertical mode:
<activity
...
...
android:screenOrientation="portrait">
Rather than going into the AndroidManifest, you could just do this:
screenOrientation = getResources().getConfiguration().orientation;
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
... AsyncTask
screenOrientation = getResources().getConfiguration().orientation;
#Override
protected void onPostExecute(String things) {
context.setRequestedOrientation(PlayListFragment.screenOrientation);
or
context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
}
The only drawback here is that it requires API level 18 or higher. So basically this is the tip of the spear.
Activity.java
#Override
public void onConfigurationChanged(Configuration newConfig) {
try {
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// land
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// port
}
} catch (Exception ex) {
}
AndroidManifest.xml
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="QRCodeActivity" android:label="#string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
User "portrait" in your AndroidManifest.xml file might seem like a good solution. But it forces certain devices (that work best in landscape) to go into portrait, not getting the proper orientation. On the latest Android version, you will get wearing an error. So my suggestion it's better to use "nosensor".
<activity
...
...
android:screenOrientation="nosensor">
Add the following to your AndroidManifest.xml
[ app > src > main > AndroidManifest.xml ]
<activity android:name=".MainActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"/>
Example:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xxx.zzzzzz.yyyyy">
<uses-permission android:name="A-PERMISSION" />
<application>
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="orientation">
</activity>
</application>
</manifest>
The following attribute on the ACTIVITY in AndroidManifest.xml is all you need:
android:configChanges="orientation"
So, the full activity node would be:
<activity android:name="Activity1"
android:icon="#drawable/icon"
android:label="App Name"
android:excludeFromRecents="true"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Add:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
...
...
...
}
Prevent Screen Rotation just add this following line in your Manifests.
<activity
android:name=".YourActivity"
android:screenOrientation="portrait" />
This works for me.
android:screenOrientation="portrait"
on application tag <application
and
<activity
...
...
android:screenOrientation="locked">
If you are using Android Developer Tools (ADT) and Eclipse
you can go to your AndroidManifest.xml --> Application tab --> go down and select your activity. Finally, select your preferred orientation.
You can select one of the many options.
You have to add the following code in the manifest.xml file.
The activity for which it should not rotate, in that activity add this element
android:screenOrientation="portrait"
Then it will not rotate.
You can try This way
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itclanbd.spaceusers">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Login_Activity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Use AsyncTaskLoader to keep your data safe even if the activity changes, instead of using AsyncTask that is a better way to build apps than preventing screen rotation.

Categories

Resources