2

I am trying to have my EditText Focused after the animation to expand it takes place. I tried programmicly setting the focus and in Xml have tried. I have run out of ideas that could fix this but someone out there has to know

MainActivity,

public void onSearchExpand(View view){
    w_SearchBarFullScreen.setVisibility(View.VISIBLE);
    SearchBarExpand = AnimationUtils.loadAnimation(MainActivity.this,R.anim.web_search_expand);
    w_SearchBarFullScreen.startAnimation(SearchBarExpand);
    w_Toolbar.startAnimation(FadeOut);
    w_SearchBarFullScreen.setFocusable(true);
}

XMl,

<EditText 
android:id="@+id/w_SearchBarFullScreen"
android:layout_width="360dp"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/web_search_bar"
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLines="1"
android:singleLine="true"
android:text="   "
android:elevation="5dp"
android:focusable="true"
xmlns:android="http://schemas.android.com/apk/res/android" />

The end goal is that when the Animation finishes it will have the SoftKeyBoard visible and ready to type something into it.

Ended up figuring it out with this,

public void onSearchExpand(View view){
    w_SearchBarFullScreen.setVisibility(View.VISIBLE);

   SearchBarExpand = AnimationUtils.loadAnimation(MainActivity.this,R.anim.web_search_expand);
   w_SearchBarFullScreen.startAnimation(SearchBarExpand);
   w_Toolbar.startAnimation(FadeOut);

   if(w_SearchBarFullScreen.getVisibility() == View.VISIBLE){
       w_SearchBarFullScreen.requestFocus();


   }

}

but now the issue is having the keyboard show with it getting focus any suggestions?

With the Use of a Animation Listener i am left with this error

01-31 13:35:27.411 5950-5950/com.example.mickeyt.onyxbrowser 
E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.example.mickeyt.onyxbrowser, PID: 5950

java.lang.IllegalStateException: Could not execute method for 
android:onClick
                                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                               at android.view.View.performClick(View.java:5623)
                                                                               at android.view.View$PerformClick.run(View.java:22433)
                                                                               at android.os.Handler.handleCallback(Handler.java:751)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6247)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
                                                                            Caused by: java.lang.reflect.InvocationTargetException
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                               at android.view.View.performClick(View.java:5623) 
                                                                               at android.view.View$PerformClick.run(View.java:22433) 
                                                                               at android.os.Handler.handleCallback(Handler.java:751) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                               at android.os.Looper.loop(Looper.java:154) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6247) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.animation.Animation.setAnimationListener(android.view.animation.Animation$AnimationListener)' on a null object reference
                                                                               at com.example.mickeyt.onyxbrowser.MainActivity.onSearchExpand(MainActivity.java:338)
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                               at android.view.View.performClick(View.java:5623) 
                                                                               at android.view.View$PerformClick.run(View.java:22433) 
                                                                               at android.os.Handler.handleCallback(Handler.java:751) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                               at android.os.Looper.loop(Looper.java:154) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6247) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 
EquiWare
  • 127
  • 2
  • 14

3 Answers3

1

You need a listener, which will be triggered when animation is finished so do this

FadeOut.setAnimationListener(new AnimationListener() {
    public void onAnimationStart(Animation anim){};
    public void onAnimationRepeat(Animation anim){};
    public void onAnimationEnd(Animation anim)
    {
        // as mentioned by lib4
        w_SearchBarFullScreen.requestFocus();
    };
}); 

because, currently you are requesting for focus , before animation is finished

or you can try adding it via xml as

<EditText  ...>
    <requestFocus/>
</EditText>
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
1

You could probably try this, one important think you can use requestFocus() to gain focus.

SearchBarExpand.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
               w_SearchBarFullScreen.requestFocus();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

SearchBarExpand.start();

If incase EditText gain focus but didnt show up the keyboard then you should try the below in onAnimationEnd():-

@Override
public void onAnimationEnd(Animation animation) {

    if(w_SearchBarFullScreen.requestFocus()) {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
}
lib4backer
  • 3,337
  • 3
  • 18
  • 16
-1

Using this worked for me.

 public void onSearchExpand(View view){
    w_SearchBarFullScreen.setVisibility(View.VISIBLE);

   w_SearchBarFullScreen.startAnimation(SearchBarExpand);
   w_Toolbar.startAnimation(FadeOut);

   if(w_SearchBarFullScreen.getVisibility() == View.VISIBLE){
       w_SearchBarFullScreen.requestFocus();
       w_SearchBarFullScreen.setText("   ");
       InputMethodManager imm = (InputMethodManager)   getSystemService(Context.INPUT_METHOD_SERVICE);
       assert imm != null;
       imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
   }
EquiWare
  • 127
  • 2
  • 14