Android intent for software - android

Thata code insted
keyboardview or one of its extended class. You have not shared your RelativeLayout but i think The exception may be because of some problem with your RelativeLayout.
You can put a button to the right of Keyboard like this(res/layout/input.xml):
< com.android.example.LatinKeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keyboard"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:keyBackground="#drawable/kb_exagon_base"

You can use any View/ViewGroup as a keyboard layout, Infact you can even set the keyboard layout to a view which is not an instance of keyboardview or one of its extended class. You have not shared your RelativeLayout but i think The exception may be because of some problem with your RelativeLayout.
You can put a button to the right of Keyboard like this(res/layout/input.xml):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keyboardLayout"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
>
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/ic_launcher"/>
And then return an instance of this layout from OnCreateInputView.
Update:
The statement mInputView = (KeyboardView) getLayoutInflater().inflate( R.layout.input, null); in your below comment will throw ClassCastException as the inflated view is not an instance of KeyboardView.
you need to change it like this:
public View onCreateInputView() {
mInputView = (View)getLayoutInflater().inflate( R.layout.input, null);
mKeyboardView = (KeyboardView)mInputView.findViewById(R.id.keyboardView);
mKeyboardView.setOnKeyboardActionListener(this);
mKeyboardView.setKeyboard(mQwertyKeyboard);
return mInputView;
}

the keyboard pops up from the bottom and you can define the behavior in your manifest using the following line android:windowSoftInputMode:
<activity
android:name=".NAME"
android:label="#string/appName"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="#style/Theme.HOLOorAnyThemeYouWant"
android:windowSoftInputMode="stateAlwaysHidden" >
from documentation :
android:windowSoftInputMode
How the main window of the activity interacts with the window containing the on-screen soft keyboard. The setting for this attribute affects two things:
The state of the soft keyboard — whether it is hidden or visible — when the activity becomes the focus of user attention.
The adjustment made to the activity's main window — whether it is resized smaller to make room for the soft keyboard or whether its contents pan to make the current focus visible when part of the window is covered by the soft keyboard.

Related

Keyboard does not push layout above EditText

In my application I have the following layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<EditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/text"
android:gravity="center_horizontal"
android:hint="#string/input_hint"
android:inputType="textVisiblePassword"
android:padding="#dimen/margin"
android:singleLine="true"
android:textColor="#color/input_text"
android:textColorHint="#color/input_hint">
</EditText>
</LinearLayout>
When EditText is clicked, the keyboard pushes the screen up including the ActionBar so that EditText is just above the keyboard. What I want is for only EditText to come up sitting on the keyboard, but the FragmentContainer that is above remains intact "as background". I got some of this by using android: windowSoftInputMode = "adjustResize" in the AndroidManifest.xml file, but my application is fullscreen and this tag apparently does not work with fullscreen.
As far as I am aware, there's no way to achieve what you're describing.
These are the choices available to you for windowSoftInputMode:
adjustNothing: When the keyboard appears, the window is not adjusted at all. This will stop your fragment_container from being moved or resizing, but will also cause the keyboard to cover your text input field.
adjustResize: When the keyboard appears, the window is "shrunk" vertically. This will cause your fragment_container to occupy less space than when the keyboard is closed, potentially affecting your fragment layout.
adjustPan: When the keyboard appears, the screen is "pushed" upwards. This will cause your fragment_container's top portion to be clipped by the edge of the screen.
adjustUnspecified: Allow the system to choose between the above three options.
Below are some pictures that (hopefully) help illustrate these attributes.
The layout with keyboard closed
From left to right: adjustNothing, adjustResize, and adjustPan

windowSoftInputMode="adjustResize" causes problems with custom keyboard layout

I'm developing a custom keyboard and would like to add a TextView above the keyboard to show what the user already has typed or suggestions for words he could want to type.
To do that I have the following layout:
<?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="wrap_content"
>
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:keyPreviewLayout="#layout/preview"
android:keyBackground="#drawable/key_background"
android:background="#color/color_primary"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/keyboard"
android:padding="8dp"
android:gravity="center"
android:background="#color/color_primary_dark"
android:textColor="#android:color/white"
android:text="some sample text"
/>
</RelativeLayout>
and the following code at the InputMethodService:
public class FancyInputMethodService extends InputMethodService {
#Override
public View onCreateInputView() {
final RelativeLayout layout = (RelativeLayout) getLayoutInflater().inflate(R.layout.keyboard_layout, null);
final KeyboardView keyboardView = (KeyboardView) layout.findViewById(R.id.keyboard);
final Keyboard keyboard = new Keyboard(this, R.xml.qwerty);
keyboardView.setKeyboard(keyboard);
return layout;
}
}
At a normal EditText at the top of the screen the keyboard looks fine and works well:
But if the EditText is in an Activity which uses the flag android:windowSoftInputMode="adjustResize" in the manifest, the keyboard view seems to cover the actual view instead of being transparent.
The left image shows the actual view with the soft keyboard closed, the image in the middle shows the weird behavior when the keyboard is open and the image on the right shows the default keyboard with the behavior I would expect.
I already tried to set the layouts background to transparent, but that didn't help.
The problem appears at several apps, e.g. WhatsApp, Hangouts, Facebook etc...Am I missing something or what's wrong?
tl;dr
You are not using InputMethodService as intended, use the CandidateView framework instead.
Full answer:
Your keyboard layout should not include the text suggestions.
Override the InputMethodService#onCreateCandidatesView. It should look like this:
public View onCreateCandidatesView() {
mYourView = getLayoutInflater().inflate(R.layout.your_view, null);
return mYourView;
}
3. When you want to show/hide your candidate view use setCandidatesViewShown(boolean show)

How to prevent lifting listview when keyboard goes up

This is code MainActivity java and main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:gravity="center"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#75F575">
<LinearLayout
android:clickable="true"
android:layout_gravity="center"
android:orientation="vertical"
android:layout_width="250dp"
android:layout_height="100dp"
android:background="#C69817"
android:id="#+id/secondLayout">
<ListView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/mainListView1"/>
</LinearLayout>
</LinearLayout>
Java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView) findViewById(R.id.mainListView1);
ArrayAdapter<String> a = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1 ,
new String [] {"item1","item2"});
lv.setAdapter(a);
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
When keyboard is shown, listview lifting up.How to prevent this.
Without listview - all normal(layout NOT go up).
Thank you.
UPDATE QUESTION with answer to FOliveira.
Unfortunately i can't remove java code (setSoftInputMode) in my real app. Java code must be and layout with listview must NOT GO UP. Try to remove listview, and you will see the layout not moving absolutely, Why layout with listview is moving? How prevent this according my conditions?
You need to add android:windowSoftInputMode="adjustPan" to your tag in the AndroidManifest.xml file.
The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.
If this option does not fit your needs, you can allways check Android documention about soft input mode
I'm using android:windowSoftInputMode="stateVisible|adjustNothing" , but this only for API higher 10(unfortunately).
Update:
After adding to listview this attributes:
android:focusableInTouchMode="false"
android:isScrollContainer="false"
I'm completely solved my problem.

android. Autofocused textview on activity startup

I have a activity, which view have a several EditText components.
When activity starts, the first of EditTexts became focused and keyboard appears. I would like to avoid this 'feature' ( I mean I want that there will be no popup keyboard after activity started)
Create a LinearLayout (I dunno if other kinds of Layout's will work). Set the attributes android:focusable="true" and android:focusableInTouchMode="true".
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true" android:focusableInTouchMode="true"
android:layout_width="0px" android:layout_height="0px"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="#+id/text"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:nextFocusUp="#+id/text" android:nextFocusLeft="#+id/text"/>
Try adding
android:windowSoftInputMode="adjustPan"
to the activity element in your manifest.

How to stop EditText from gaining focus when an activity starts in Android?

I have an Activity in Android, with two elements:
EditText
ListView
When my Activity starts, the EditText immediately has the input focus (flashing cursor). I don't want any control to have input focus at startup. I tried:
EditText.setSelected(false);
EditText.setFocusable(false);
No luck. How can I convince the EditText to not select itself when the Activity starts?
Adding the tags android:focusableInTouchMode="true" and android:focusable="true" to the parent layout (e.g. LinearLayout or ConstraintLayout) like in the following example, will fix the problem.
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="#+id/autotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:nextFocusUp="#id/autotext"
android:nextFocusLeft="#id/autotext"/>
Is the actual problem that you just don't want it to have focus at all? Or you don't want it to show the virtual keyboard as a result of focusing on the EditText? I don't really see an issue with the EditText having a focus on the start, but it's definitely a problem to have the softInput window open when the user did not explicitly request to focus on the EditText (and open the keyboard as a result).
If it's the problem of the virtual keyboard, see the AndroidManifest.xml <activity> element documentation.
android:windowSoftInputMode="stateHidden" - always hide it when entering the activity.
or android:windowSoftInputMode="stateUnchanged" - don't change it (e.g. don't show it if it isn't already shown, but if it was open when entering the activity, leave it open).
A simpler solution exists. Set these attributes in your parent layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
And now, when the activity starts this main layout will get focused by default.
Also, we can remove focus from child views at runtime (e.g., after finishing child editing) by giving the focus to the main layout again, like this:
findViewById(R.id.mainLayout).requestFocus();
Good comment from Guillaume Perrot:
android:descendantFocusability="beforeDescendants" seems to be the
default (integer value is 0). It works just by adding
android:focusableInTouchMode="true".
Really, we can see that the beforeDescendants is set as default in the ViewGroup.initViewGroup() method (Android 2.2.2). But not equal to 0. ViewGroup.FOCUS_BEFORE_DESCENDANTS = 0x20000;
Thanks to Guillaume.
The only solution I've found is:
Create a LinearLayout (I don't know if other kinds of Layout will work)
Set the attributes android:focusable="true" and android:focusableInTouchMode="true"
And the EditText won't get the focus after starting the activity
The problem seems to come from a property that I can only see in the XML form of the layout.
Make sure to remove this line at the end of the declaration within the EditText XML tags:
<requestFocus />
That should give something like that :
<EditText
android:id="#+id/emailField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress">
//<requestFocus /> /* <-- without this line */
</EditText>
using the information provided by other posters, I used the following solution:
in the layout XML
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:id="#+id/linearLayout_focus"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- AUTOCOMPLETE -->
<AutoCompleteTextView
android:id="#+id/autocomplete"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:inputType="textNoSuggestions|textVisiblePassword"/>
in onCreate()
private AutoCompleteTextView mAutoCompleteTextView;
private LinearLayout mLinearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
//get references to UI components
mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout_focus);
}
and finally, in onResume()
#Override
protected void onResume() {
super.onResume();
//do not give the editbox focus automatically when activity starts
mAutoCompleteTextView.clearFocus();
mLinearLayout.requestFocus();
}
Try clearFocus() instead of setSelected(false). Every view in Android has both focusability and selectability, and I think that you want to just clear the focus.
The following will stop EditText from taking focus when created but grab it when you touch them.
<EditText
android:id="#+id/et_bonus_custom"
android:focusable="false" />
So you set focusable to false in the xml, but the key is in the java, which you add the following listener:
etBonus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.setFocusable(true);
v.setFocusableInTouchMode(true);
return false;
}
});
Because you are returning false, i.e. not consuming the event, the focusing behavior will proceed like normal.
Late but simplest answer, just add this in the parent layout of the XML.
android:focusable="true"
android:focusableInTouchMode="true"
Upvote if it helped you! Happy Coding :)
I had tried several answers individually but the focus is still at the EditText. I only managed to solve it by using two of the below solution together.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
( Reference from Silver https://stackoverflow.com/a/8639921/15695 )
and remove
<requestFocus />
at EditText
( Reference from floydaddict https://stackoverflow.com/a/9681809 )
None of these solutions worked for me. The way I fix the autofocus was:
<activity android:name=".android.InviteFriendsActivity"
android:windowSoftInputMode="adjustPan">
<intent-filter >
</intent-filter>
</activity>
Simple solution:
In AndroidManifest in Activity tag use
android:windowSoftInputMode="stateAlwaysHidden"
You can just set "focusable" and "focusable in touch mode" to value true on the first TextView of the layout. In this way when the activity starts the TextView will be focused but, due to its nature, you will see nothing focused on the screen and, of course, there will be no keyboard displayed...
The following worked for me in Manifest. Write,
<activity
android:name=".MyActivity"
android:windowSoftInputMode="stateAlwaysHidden"/>
I needed to clearly focus on all fields programmatically. I just added the following two statements to my main layout definition.
myLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
myLayout.setFocusableInTouchMode(true);
That's it. Fixed my problem instantly. Thanks, Silver, for pointing me in the right direction.
Add android:windowSoftInputMode="stateAlwaysHidden" in the activity tag of the Manifest.xml file.
Source
If you have another view on your activity like a ListView, you can also do:
ListView.requestFocus();
in your onResume() to grab focus from the editText.
I know this question has been answered but just providing an alternative solution that worked for me :)
Try this before your first editable field:
<TextView
android:id="#+id/dummyfocus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/foo"
/>
findViewById(R.id.dummyfocus).setFocusableInTouchMode(true);
findViewById(R.id.dummyfocus).requestFocus();
Add following in onCreate method:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Write this line in your Parent Layout...
android:focusableInTouchMode="true"
Being that I don't like to pollute the XML with something that is related to functionality, I created this method that "transparently" steals the focus from the first focusable view and then makes sure to remove itself when necessary!
public static View preventInitialFocus(final Activity activity)
{
final ViewGroup content = (ViewGroup)activity.findViewById(android.R.id.content);
final View root = content.getChildAt(0);
if (root == null) return null;
final View focusDummy = new View(activity);
final View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View view, boolean b)
{
view.setOnFocusChangeListener(null);
content.removeView(focusDummy);
}
};
focusDummy.setFocusable(true);
focusDummy.setFocusableInTouchMode(true);
content.addView(focusDummy, 0, new LinearLayout.LayoutParams(0, 0));
if (root instanceof ViewGroup)
{
final ViewGroup _root = (ViewGroup)root;
for (int i = 1, children = _root.getChildCount(); i < children; i++)
{
final View child = _root.getChildAt(i);
if (child.isFocusable() || child.isFocusableInTouchMode())
{
child.setOnFocusChangeListener(onFocusChangeListener);
break;
}
}
}
else if (root.isFocusable() || root.isFocusableInTouchMode())
root.setOnFocusChangeListener(onFocusChangeListener);
return focusDummy;
}
Late, but maybe helpful. Create a dummy EditText at the top of your layout then call myDummyEditText.requestFocus() in onCreate()
<EditText android:id="#+id/dummyEditTextFocus"
android:layout_width="0px"
android:layout_height="0px" />
That seems to behave as I expect. No need to handle configuration changes, etc. I needed this for an Activity with a lengthy TextView (instructions).
Yeah, I did the same thing - create a 'dummy' linear layout which gets the initial focus. Furthermore, I set the 'next' focus IDs so the user can't focus it anymore after scrolling once:
<LinearLayout 'dummy'>
<EditText et>
dummy.setNextFocusDownId(et.getId());
dummy.setNextFocusUpId(et.getId());
et.setNextFocusUpId(et.getId());
a lot of work just to get rid of focus on a view.
Thanks
For me, what worked on all devices is this:
<!-- fake first focusable view, to allow stealing the focus to itself when clearing the focus from others -->
<View
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
Just put this as a view before the problematic focused view, and that's it.
<TextView
android:id="#+id/textView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
style="#android:style/Widget.EditText"/>
This is the perfect and easiest solution. I always use this in my app.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
The simplest thing I did is to set focus on another view in onCreate:
myView.setFocusableInTouchMode(true);
myView.requestFocus();
This stopped the soft keyboard from coming up and there was no cursor flashing in the EditText.
Write this code inside the Manifest file in the Activity where you do not want to open the keyboard.
android:windowSoftInputMode="stateHidden"
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.project"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="24" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
**android:windowSoftInputMode="stateHidden"**
android:label="#string/app_name" >
</activity>
</application>
</manifest>
The easiest way to hide the keyboard is using setSoftInputMode
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
or you can use InputMethodManager and hide the keyboard like this.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
At onCreate of your Activity, just add use clearFocus() on your EditText element.
For example,
edittext = (EditText) findViewById(R.id.edittext);
edittext.clearFocus();
And if you want to divert the focus to another element, use requestFocus() on that.
For example,
button = (Button) findViewById(R.id.button);
button.requestFocus();

Categories

Resources