2017-10-12 1 views
1

J'ai un WebView et charge iframe dans loadData. Lorsqu'un panneau de touches s'affiche, la hauteur de la variable Webview change, mais lorsque celle-ci disparaît, la hauteur du WebView ne change pas. Comment changer la hauteur du WebView en plein écran?WebView (avec iframe à l'intérieur) ne redimensionne pas lorsque la touche programmable disparaît

Mon code:

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    WebView webView = (WebView) findViewById(R.id.wbv); 
    webView.getSettings().setJavaScriptEnabled(true); 
    String html = "<html><body><iframe src=\"https://api.motion.ai/webchat/85714?color=EFB426&sendBtn=%D0%9E%D1%82%D0%B2%D0%B5%D1%82%D0%B8%D1%82%D1%8C&inputBox=%D0%9D%D0%B0%D0%BF%D0%B8%D1%88%D0%B8%D1%82%D0%B5%20%D0%B2%D0%B0%D1%88%20%D0%B2%D0%BE%D0%BF%D1%80%D0%BE%D1%81%2F%D0%BE%D1%82%D0%B2%D0%B5%D1%82&token=9b32ca23536d9816b448d2e4c53434dc\" width="100%" height="100%"></iframe></body></html>"; 
    webView.loadData(html,"text/html","UTF-8"); 

} 

}

xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="app.hr_bot.MainActivity"> 

    <WebView 
     android:id="@+id/wbv" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_marginBottom="0dp" 
     android:layout_marginLeft="0dp" 
     android:layout_marginRight="0dp" 
     android:layout_marginTop="0dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 
</android.support.constraint.ConstraintLayout> 

Captures d'écran:

enter image description here enter image description here

EDIT:

Donc, maintenant j'ai quelque chose comme ceci:

final EKRAN myEkran = new EKRAN(MainActivity.this); 
     MAIN = findViewById(R.id.rootLayout); 

     MAIN.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 

       Rect r = new Rect(); 
       MAIN.getWindowVisibleDisplayFrame(r); 
       int screenHeight = MAIN.getRootView().getHeight(); 
       int keypadHeight = screenHeight - r.bottom; 

       Log.d(String.valueOf(1) , "keypadHeight = " + keypadHeight); 

       if (keypadHeight > screenHeight * 0.10) { // 0.15 ratio is perhaps enough to determine keypad height. 
        // keyboard is opened 
        Toast.makeText(MainActivity.this , "COOL1 " , Toast.LENGTH_SHORT).show(); 
        ViewGroup.LayoutParams params1 = webView.getLayoutParams(); 
        params1.width = myEkran.W(100); // IN % 
        params1.height = myEkran.H(45); // IN % 
        webView.setLayoutParams(params1); 
        webView.requestLayout(); 
       } 
       else { 
        Toast.makeText(MainActivity.this , "COOL2 " , Toast.LENGTH_SHORT).show(); 
        // keyboard is closed 

        ViewGroup.LayoutParams params1 = webView.getLayoutParams(); 
        params1.width = myEkran.W(100); // IN % 
        params1.height = myEkran.H(92); // IN % 
        webView.setLayoutParams(params1); 
        webView.requestLayout(); 
       } 
      } 
     }); 


     webView = (WebView) findViewById(R.id.wbv); 
     webView.getSettings().setJavaScriptEnabled(true); 
     String html = "<html><body><iframe src=\"https://api.motion.ai/webchat/85714?color=EFB426&sendBtn=%D0%9E%D1%82%D0%B2%D0%B5%D1%82%D0%B8%D1%82%D1%8C&inputBox=%D0%9D%D0%B0%D0%BF%D0%B8%D1%88%D0%B8%D1%82%D0%B5%20%D0%B2%D0%B0%D1%88%20%D0%B2%D0%BE%D0%BF%D1%80%D0%BE%D1%81%2F%D0%BE%D1%82%D0%B2%D0%B5%D1%82&token=9b32ca23536d9816b448d2e4c53434dc\" width=\"100%\" height=\"100%\"></iframe></body></html>"; 
     webView.loadData(html,"text/html","UTF-8"); 
+0

@NikolaLukic Je pense c'est un WebView, parce que je lui ai mis une couleur de fond personnalisée et après déformation bg était blanc –

+0

Essayez comme ceci: YOUR_CONTROL.getLayoutParams(). height = Intro runOnUiThread assurez-vous également sur le contenu html. –

+0

Important: Vous devez utiliser FrameLayout (uniquement pour les positions personnalisées - absolues). –

Répondre

1

Détection clavier isHihhen ou visible:

// MAIN is root view see in design window (also enter id for root view): 

View MAIN; 

... 

    //######################## 
    // Put this intro OnCreate 
    //######################## 

    MAIN = findViewById(R.id.main); 

    MAIN.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 

      Rect r = new Rect(); 
      MAIN.getWindowVisibleDisplayFrame(r); 
      int screenHeight = MAIN.getRootView().getHeight(); 
      int keypadHeight = screenHeight - r.bottom; 

      Log.d(String.valueOf(1) , "keypadHeight = " + keypadHeight); 

      if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height. 
       // keyboard is opened 
       Toast.makeText(context_pass , "COOL1 " , 1); 
      } 
      else { 
       Toast.makeText(context_pass , "COOL2 " , 1); 
       // keyboard is closed 
      } 
     } 
    }); 

Si votre WebView est une utilisation déformée ce code après le clavier cache:

ViewGroup.LayoutParams params1 = YOUR_CONTROL.getLayoutParams(); 
        params1.width = MY_EKRAN.W(100); // IN % 
        params1.height = MY_EKRAN.H(100); // IN % 
        YOUR_CONTROL.setLayoutParams(params1); 


OR 


    runOnUiThread(new Runnable() { 
      public void run() { 

       // runs on UI thread 
       YOUR_CONTROL.getLayoutParams().height = (MY_EKRAN.HEIGHT()/100*10); 

       } 
      }); 

DEF:

//global 
    EKRAN MY_EKRAN; 

// in onCreate 
    MY_EKRAN = new EKRAN(context_pass); 

classe pour les dimensions et les positions diamétrales (ajouter comme nouveau fichier - nouvelle classe java):

import android.content.Context; 
import android.graphics.Point; 
import android.util.DisplayMetrics; 
import android.util.TypedValue; 
/** 
* Created by nikola on 10/17/16. 
*/ 
//############################################## 
// SCREEN - EKRAN CLASS 
//############################################## 
public class EKRAN { 

    DisplayMetrics dm = new DisplayMetrics(); 
    Point size_ = new Point(); 
    static int width; 
    static int height; 

    EKRAN(Context CONTEXT_) { 

     dm = CONTEXT_.getResources().getDisplayMetrics(); 
     int densityDpi = dm.densityDpi; 
     height = dm.heightPixels; 
     width = dm.widthPixels; 

    } 

    public static int WIDTH() { 

     return width; 

    } 
    public static int HEIGHT(){ 

     return height; 

    } 
    public int W(int PER_){ 

     return width/100*PER_; 

    } 
    public int H(int PER_ ){ 


     return height/100*PER_; 

    } 

    ////////////////// 
    //extras 
    ///////////////// 
    public int GET_PIX_FROM_DP (float DP_VALUE) 
    { 
     return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DP_VALUE , dm); 
    } 

    public int GET_PIX_FROM_DP2 (float DP_VALUE) 
    { 
     float res = DP_VALUE * (dm.ydpi/160f); 
     return (int) res; 
    } 



} 
+1

J'apprécierais si vous me dites aussi comment détecter quand le clavier logiciel se cache. J'ai essayé différentes solutions, mais je n'ai trouvé aucun travail. –

+0

Il rend le webview en plein écran, mais pas redimensionnable. Donc, maintenant j'essaie de mettre deux fois layoutparams dans 'onGlobalLayout()' (Quand le clavier est ouvert, je mets params1.height à 45, et quand le clavier est fermé, je mets params1.height à 100), le problème c'est WebView ne veut pas changer la hauteur à 100 à nouveau. J'ai essayé d'utiliser 'webView.requestLayout();' après 'webView.setLayoutParams (params1);' mais cela ne m'aide pas. J'ai mis à jour mon code dans le post. –

+0

Ne pas oublier runOnUiThread! –