Nikkolasg
Nikkolasg

Reputation: 454

SoftInput keyboard hides at startup activity but always shows itself after using viewpager

I've seen a LOT of topics on the subjects but still can't figure out why it happens. I have a fragment on the left side which displays a list , and a view pager (with 2 page) on the right side.

When I first launch my activity keyboard doesn't show. That's great. But when I click on a list item (that will show a new thing in viewpager => myPager.setAdapter(..) ), or when I swipe right (here it doesn't show yet ) then left to go back to the first page displayed, keyboard start showing again, and continue to show for the rest of the time whatever the action I do.

I've tried put android:focusableInTouchMode="true" in everylayout I could put in but still ...

A thing strange is that it's NOT the edit text inside my layout that takes focus it's .. something else. a layout perhaps but I dont know (and dont know how to know that =) . It's a bit strange since Ive tried a lot of solutions, but it still doesnt work.

If anyone has any ideas...

Thank you !!!!

Here the relevant part of my code .

manifest.xml

<activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:windowSoftInputMode="stateHidden" >

main_activity.xml

(DateList contains the list view and others things below (that dont take focus))

<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="horizontal" >

<fragment android:name="com.example.main.courante.e.DateList"
 android:id="@+id/list_frag"
 android:tag="list"
 android:layout_width="0dip"
 android:layout_height="fill_parent"
 android:layout_weight="1"
/> 

<TabHost
    android:id="@+id/tabhost"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="3"
    android:paddingLeft="5dp"
    android:paddingTop="5dp" >  
    
    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:layout_gravity="center">
        
        <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"  
            android:layout_gravity="center" 
            android:tabStripEnabled="true"
             />
    
          <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
          
        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            />
        </FrameLayout>
    </LinearLayout>
</TabHost>
</LinearLayout>

Layout shown in viewpager :

( a bit messy i'll admit but just to show you if there is focusablemode etc )

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:orientation="vertical"
android:scrollbars="vertical"
>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center"
android:id="@+id/linear"
android:paddingLeft="30dp"
android:paddingRight="30dp"
 >

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/padding_small"
android:layout_marginBottom="@dimen/padding_small"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/top_lin">
<!--     android:background="@drawable/rounded_border_fiche_element" -->
 

 <ImageView 
     android:id="@+id/perm_img"
     android:layout_width="75dp"
     android:layout_height="75dp"
     android:layout_marginLeft="50dp"
     android:padding="@dimen/padding_small"
     
     android:src="@android:drawable/ic_notification_overlay" 
     android:contentDescription="TODO"
     android:background="@drawable/image_bg"/> 

   <TextView
        android:id="@+id/perm_nom"
        style="@style/textview_info"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium"
         />
   
    <TextView
        style="@style/textview_info"
        android:id="@+id/perm_prenom"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium"
         />
    
    <TextView
        style="@style/textview_info"
        android:id="@+id/perm_matricule"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingRight="@dimen/padding_medium"
         />

</LinearLayout>

<View
     android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="15dp" />

<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_gravity="center"
    android:id="@+id/table_check">
<!--         android:background="@drawable/rounded_border_fiche_element" -->
     

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
       android:gravity="center">           

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/question_m1"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/question_m2"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <CheckBox
            android:id="@+id/checkBox2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:gravity="center">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/question_m3"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <CheckBox
            android:id="@+id/checkBox3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />
        
        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/question_m4"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <CheckBox
            android:id="@+id/checkBox4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/question_m5"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <CheckBox
            android:id="@+id/checkBox5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/question_m6"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <CheckBox
            android:id="@+id/checkBox6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />

    </TableRow>
    
</TableLayout>

<View
     android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="15dp" />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:id="@+id/bot_lin">
<!--     android:background="@drawable/rounded_border_fiche_element"> -->

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bot_lin_lin"
    android:focusableInTouchMode="true"
    android:focusable="true" >

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/securite" />

    <EditText
        android:id="@+id/securite"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="textMultiLine"
        android:lines="5" >

    </EditText>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bot_lin_lin2"
    android:focusableInTouchMode="true"
    android:focusable="true" >

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/client" />

    <EditText
        android:id="@+id/clients"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="textMultiLine"
        android:lines="5"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bot_lin_lin3"
    android:focusableInTouchMode="true"
    android:focusable="true" >

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/entretien" />

    <EditText
        android:id="@+id/entretien"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="textMultiLine" 
        android:lines="5"/>

</LinearLayout>

</LinearLayout>

</LinearLayout>
</ScrollView>

EDIT :

I noticed a thing. when I swipe trhough my pager, second button to the left of the space button in the keyboard quickly change from " :-) " to " : " and goes back to " :-) " when the scrolling is finished. The second one " : " is from a searchwidget I have on my action bar. Maybe if I could stop this one from getting focus it'd ok ? How would I do that ? Because it's defined like this :

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_settings"
    android:title="@string/menu_settings"
    android:orderInCategory="100"
    android:showAsAction="never" />

<item android:id="@+id/menu_search"
      android:title="@string/menu_search"
      android:icon="@drawable/ic_menu_search"
      android:showAsAction="ifRoom"
      android:actionViewClass="android.widget.SearchView"
      android:inputType="number" />

EDIT 2 :

To prevent searchwidget from gaining focus I've put theses two lines of code

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(searchView.getWindowToken(),0); 

everywhere I call myPager.setAdapter() i.e. When i click on a list item, and I HAD to put it also in public void onPageScrolled() from the viewpager ... But it is really NOT fluent as you see quickly the keyboard showing and disappearing in less than a second... If anyone has another ideas..

Upvotes: 0

Views: 926

Answers (1)

Akash Singh
Akash Singh

Reputation: 5241

i have one logical idea to avoid to open SoftInput keyboard on

Add this view to your Layout shown in viewpager :

<LinearLayout
        android:id="@+id/ll_focus"
        android:layout_width="0px"
        android:layout_height="0px"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:visibility="visible" >
    </LinearLayout>

This is work well. try this my dear friend

Upvotes: 1

Related Questions