- Home /
Using Java Plugin to add an Android Edittext view
I believe I have a fundamental problem as I have worked on it for days.
Objective: Add a Android EditText View to the bottom of my UnityPlayer activity by creating a Java Plugin. I need to do this because I will be using Android IME (voice) functionality in conjunction with the edittext view.
Strategy (See Code Comments): I created a Unity Java plugin that is primiraly composed of the "addEditTextView" Method below. Then in unity I have a GUI button to call "addEditTextView" Method. As expected the EditText view shows up at the bottom of the screen when the button is touched.
Problem: When I touch the edittext view is gets covered by the softkeyboard instead of moving upward. Note the UnityActivity content added by unity does pan/move upward as expected.
Experiments:
create a standalone Android Eclipse project with a basic activity. I then implemented this exact function again and it worked (Edittext stayed just above the softkeyboard). This leads me to believe that the Unity Activity is different in some way than my experimental standalone basic activity.
made the EditText box really tall (ie. 200). The softkeyboard covered the bottom half, which showed it is visible but not moving.
tried centering the textedit with "CENTER_IN_PARENT" (below) to see if I could learn something. It worked in the standalone Android project, but the edittext remained at the top for the unity project. Why - This is really confusing?
Code:
final Activity _unityActivity; // Assign to the UnityPlayerActivy in contructor
final Context _context; // Assign to the Unity Context in consturctor
public void addEditTextView()
{
_unityActivity.runOnUiThread(new Runnable()
{
public void run()
{
// Create LayoutParams that will place EditText at bottom of parent RelativeLayout (ie. ALIGN_PARENT_BOTTOM)
RelativeLayout.LayoutParams localLayoutParams;
(localLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT)).addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
// When I try centering the view with this line the edittext view remains at the top. This leads me
// to believe I am missing a basic concept.
//(localLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT)).addRule(RelativeLayout.CENTER_IN_PARENT);
// Create a RelativeLayout View to insert into UnityActivity
RelativeLayout localRelativeLayout = new RelativeLayout(_context);
EditText voiceText = new EditText(_context);
voiceText.setHeight(200);
// Add localLayoutParams created above to voiceText
((EditText)voiceText).setLayoutParams(localLayoutParams);
localRelativeLayout.addView((View)voiceText);
// Create LayoutParams for the RelativeLayout
ViewGroup.LayoutParams vlp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// Add the localRelativeLayout containing voiceText to the unityActivity View
_unityActivity.getWindow().addContentView(localRelativeLayout, vlp);
}
});