Sé que estoy respondiendo a un hilo antiguo, pero estaba buscando en Google para encontrar una respuesta a esta pregunta, pero no pude encontrarla en ningún lado. Ahora he creado una solución yo mismo. Aquí está cómo hacerlo en Android de una manera elegante :) Estoy creando un ApplicationBundle para agrupar interfaces para agregar cosas específicas de la plataforma. También puedes hacer esto en iOS si quieres hacer uso de RoboVM.
Mi solución:
crear una interfaz SizeChangeListener en el proyecto central:
public interface SizeChangeListener {
public void onSizeChange(float width, float height);
}
crear una interfaz de Vista en el proyecto central:
public interface View {
public void onSizeChange(float width, float height);
public void addListener(SizeChangeListener sizeChangeListener);
public float getWidth();
public float getHeight();
}
crear una AndroidView implementación de la interfaz Vista :
public class AndroidView implements View {
private ArrayList<SizeChangeListener> listeners = new ArrayList<SizeChangeListener>();
private float width, height;
public AndroidView(int width, int height) {
this.width = width;
this.height = height;
}
public void addListener(SizeChangeListener listener) {
listeners.add(listener);
}
public void onSizeChange(float width, float height) {
this.width = width;
this.height = height;
for(SizeChangeListener listener : listeners)
listener.onSizeChange(width, height);
}
public float getWidth() {
return width;
}
public float getHeight() {
return height;
}
}
crear un ApplicationBundle en el proyecto central
public class ApplicationBundle {
private final View view;
public ApplicationBundle(View view) {
this.view = view;
}
public View getView() {
return view;
}
}
Hacer las importaciones necesarias del proyecto central. En el AndroidLauncher en el proyecto Android agregar lo siguiente:
public class AndroidLauncher extends AndroidApplication {
private View rootView;
private AndroidView androidView;
private int width, height;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
rootView = this.getWindow().getDecorView().getRootView();
Rect rect = new Rect();
rootView.getWindowVisibleDisplayFrame(rect);
width = rect.width();
height = rect.height();
androidView = new AndroidView(width, height);
rootView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right,
int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
Rect rect = new Rect();
rootView.getWindowVisibleDisplayFrame(rect);
if(!(width == rect.width() && height == rect.height())) {
width = rect.width();
height = rect.height();
androidView.onSizeChange(width, height);
}
}
});
initialize(new DigMeApp(new ApplicationBundle(androidView)), config);
}
}
en su MiApl principal en el proyecto básico del método create() añadir una aplicación SizeChangeListener a la vista que tienes desde el constructor.
public class MyApp extends Game { // or ApplicationAdapter
private View view;
private Stage stage;
// your own variables
public MyApp(ApplicationBundle applicationBundle) {
view = applicationBundle.getView();
}
@Override
public void create() {
stage = new Stage();
// add some textfields
final TextField tf1 = new TextField("", skin);
final TextField tf2 = new TextField("", skin);
tf1.setWidth((float)view.getWidth() * 0.6f);
tf2.setWidth((float)view.getWidth() * 0.6f);
tf1.setHeight((float)view.getHeight() * 0.05f);
tf2.setHeight((float)view.getHeight() * 0.05f);
view.addListener(new SizeChangeListener() {
@Override
public void onSizeChange(float width, float height) {
Gdx.app.log("INFO", "Visible area: " + width + " " + height);
Gdx.app.log("INFO", "Stage area: " + stage.getWidth() + " " + stage.getHeight());
float keyboardHeight = getKeyboardHeight();
// MOVE THEM OUT OF THE WAY :)
tf1.addAction(Actions.moveTo(width/2 - tf1.getWidth()/2.0f, keyboardHeight + (6 * (height/8)), 1, Interpolation.sineOut));
tf2.addAction(Actions.moveTo(width/2 - tf2.getWidth()/2.0f, keyboardHeight + (7 * (height/8)), 1, Interpolation.sineOut));
// Gdx.gl20.
// tf.setPosition(width/2 - (tf.getWidth()/2.0f), 0);
}
});
}
Tal vez crear un método poco heigt teclado como lo hice:
private float getKeyboardHeight() {
return stage.getHeight() - view.getHeight();
}
posible duplicado de [Detectar si el teclado blando es visible en la pantalla] (http://stackoverflow.com/questions/4745988/ detect-if-soft-keyboard-is-visible-on-screen) –
Esto no tiene nada que ver con libgdx, quiere escuchar un evento Android nativo.Esta pregunta se ha realizado y respondido –
Un poco, pero si no lo proporciona LibGDX, el esfuerzo de implementación para escuchar este evento es mucho más grande;). – dom