Quantcast
Channel: Android Mode - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 941

Android Input Box

$
0
0

Hi. I am trying to code an android Input Box. The G4P lib uses libs that are not available on android. I did manage to write a working code in plain Processing code, but the "key" keyword doesn't read coded keys like BACKSPACE and other characters like 'á'. on my device. Then I found a really nice code of @akenaton here , and with the kind help of @olivi55 who teached me how to edit the TextField, I wrote an example code for an InputBox. I now need to figure out how to hide the textbox as soon as I enter the End key.( Enter) , how to link that. In code below it will hide the keyboard., but not the textbox. The textbox will hide and write the input string on the screen only after I hit the input button.

CODE:

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.LinearLayout;
import android.text.Editable;
import android.graphics.Color;
import android.widget.EditText;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.view.View;
import android.widget.TextView;
import android.view.ViewGroup;
import android.view.ViewParent;

EditText edit;
Activity act;
Context mC;
FrameLayout fl;
boolean bflag = true;
String txt;
int flag = 1, xbut, ybut, wbut = 110, hbut = 35;

@Override
  public void onStart() {
    super.onStart();
    act = this.getActivity();
    mC= act.getApplicationContext();
    act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    edit = new EditText(mC);
    edit.setLayoutParams (
      new RelativeLayout.LayoutParams (
        RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT
      )
    );
    edit.setHint("Write Here!");
    edit.setTextColor(Color.rgb(0, 0, 0));
    edit.setHintTextColor(Color.rgb(170, 170, 170));
    edit.setBackgroundColor(Color.WHITE);      // edit.getBackground().setAlpha(255);
    fl = (FrameLayout)act.findViewById(0x1000);
    edit.getLayoutParams().width=14*width/26;
    edit.getLayoutParams().height=height/17;
    edit.setX(width/3);
    edit.setY(height/6);
    edit.requestFocus();
    edit.setInputType(android.text.InputType.TYPE_CLASS_TEXT);
    fl.addView(edit);
  }

void setup() {
  fullScreen();
  orientation(LANDSCAPE);
  background(0, 0, 255);
  xbut = width/10;
  ybut = height/6;
  inputButton(xbut, ybut, wbut, hbut);
}

void draw() {
  if (flag == 1) {
    showInputBox();
    flag = 3;
  }
  if (flag == 2) {
    hideInputBox();
    flag = 3;
  }
}

void showInputBox() {
  background(0, 0, 255);
  String txt = edit.getText().toString();
  edit.getText().clear();
  text("Please type some input.", width/3, height/12);
  inputButton(xbut, ybut, wbut, hbut);
  act.runOnUiThread(
    new Runnable() {
      public void run() {
        edit.setVisibility(View.VISIBLE);
        edit.requestFocus();
      }
    }
  );
}

  public void onResume() {
    super.onResume();
      act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
  }


void hideInputBox() {
  background(0, 0, 255);
  inputButton(xbut, ybut, wbut, hbut);
  String txt = edit.getText().toString();
  text("Your input was:  " + txt, width/3, height/12);
  act.runOnUiThread(
    new Runnable() {
      public void run() {
        edit.setVisibility(View.GONE);
      }
    }
  );
}

void mousePressed() {
  if (mouseX >= xbut && mouseX <= xbut + wbut &&  mouseY >= ybut && mouseY <= ybut + hbut) {
    if (bflag) {
      flag = 2;
      bflag = false;
    } else {
      flag = 1;
      bflag = true;
    }
  }
}

void inputButton (int x, int y, float w, float h) {
  textSize(20);
  stroke(127);
  rect(x, y, w, h);
  color c2 = color(130, 0, 0);
  color c1 = color(255, 85, 0);
  for (int i = y; i <= y+h; i++) {
    float inter = map(i, y, y+h, 0, 1);
    color c = lerpColor(c1, c2, inter);
    stroke(c);
    line(x, i, x+w, i);
  }
  fill(255);
  text("Input", x + 30, y + 25);
}

I loaded the AIDE App, to learn Java. but I don't want to lett Processing down because I love it for my artwork and arduino projects. I really would appreciate some help. Emmanuel


Viewing all articles
Browse latest Browse all 941

Trending Articles