5

I am creating a layout pretty much dynamically. What I am doing is, creating an EditText, adding it to a RelativeLayout object and putting it to UI through WindowManager. How do I get the edittext to be able to type after I do this? Here is a jist of my code

 RelativeLayout layout = new RelativeLayout(this);
 private EditText response = new EditText(this); 
 **Set width/height/extra**
 layout.addView(response, params);  //Params are set properly
 WindowManager myWindow = (WindowManager) getSystemService(WINDOW_SERVICE);
 myWindow.addView(layout, params_window);

EDIT: I am trying to achieve a popup message like in the app LilyPad (I am not sure if you know about it). I am trying to change the text dynamically. Here is what I did:

  LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  myView = layoutInflater.inflate(R.layout.popups, null );
  message = (TextView)myView.findViewById(R.id.message);
  message.setText(text);

Then after that, I am following @android developer's method. But I am getting a null pointer exception in the message.setText(text) part. Message is a initialized as a TextView object and I checked there is a TextView id'd message in the popups.xml file. THIS IS FIXED I did not initiate the windowManager. Sorry ignore this please Please see my issue in Edit III

EDIT II: Here is another picture of what I want. https://i.stack.imgur.com/K4maH.jpg The gray boxes are in RelativeLayout in an XML file. Ex. if I am on my facebook wall I want the facebook wall to appear on the black area on top and want it to be able scrollable in the facebook wall without my app interfering (like in LilyPad). If I do

      LayoutInflater layoutInflater =    
      (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  myView = layoutInflater.inflate(R.layout.popups, null );

then configure WindowManager with the Transparent w.e flags, and add my view, the background screen is not functional. Background doesn't pause but I cannot like scroll through homescreens for example. The problem seems to be that the RelativeLayout takes up the entire screen. This is my RelativeLayout properties defined in the xml file

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 

EDIT IV: https://lh4.ggpht.com/gnNfCsUU7cTCTedhny-wej-nbGmL1fktcw5qo92CCOLQ9ySG5t4pCRKYSt7u--kjpw=h900-rw Here is a pictureo f LilyPad

AndroidLove
  • 123
  • 4
  • 11

2 Answers2

0

here's how you put a view on top of everything:

final WindowManager.LayoutParams param=new WindowManager.LayoutParams();
param.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
final View view=...
final ViewGroup parent=(ViewGroup)view.getParent();
if(parent!=null)
  parent.removeView(view);
param.format=PixelFormat.RGBA_8888;
param.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
param.gravity=Gravity.TOP|Gravity.LEFT;
param.width=view.getLayoutParams().width;
param.height=view.getLayoutParams().height;
final WindowManager wmgr=(WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
wmgr.addView(view,param);
// TODO handle overlapping title bar and/or action bar
// TODO you must add logic to remove the view
// TODO you must use a special permission to use this method :android.permission.SYSTEM_ALERT_WINDOW

you can put the view inside XML if you wish to easily see how it should look like. the code i've written will detach it from your layout and put it on the window itself.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • I got it thank you very much. I am not seeing where you are detaching an XML file and putting on the window? Say I have an XML file called my_activity.xml which has textviews, buttons, and editext. You want me to set that xml file to View? Like using a Layout inflater? Also, by doing this I will be able to type text in the editext without stopping the ongoing activity (ex. a game?)? – AndroidLove Aug 25 '13 at 20:46
  • about the detaching, you can do what you wish. i only said it's possible . for example, you can inflate or use "setContentView" and then detach the view from it (first find it using findViewById) . about the editText question, yes, it should still work. one thing i didn't say is that in order to help your app stay and not being killed by heavy apps, you should make the app have a foreground service . – android developer Aug 25 '13 at 20:51
  • Yes I am running my app as a service. If I run it as a service, I wouldn't be able to use findViewById. So I guess I should use inflate. But if I use inflate, how could I gain access to that edittext in the xml file so I can say like myedittext.getText() for whats being typed? Any clue? – AndroidLove Aug 25 '13 at 20:53
  • inflating doesn't mean you can't use findViewById. you just run it on the view that was inflated... findViewById is available to any view : http://developer.android.com/reference/android/view/View.html#findViewById(int) . also , it's usually a good thing to have an activity for this purpose, even if it will be destroyed right when it was created. it's weird for services to have a UI. only their notifications have UI, and even then it's limited. – android developer Aug 25 '13 at 20:56
  • I think you may have just solved my issue. All this time I forgot I can create an EditText object and reference it to the EditText in the xml by using the View object. I am going to try this. Would be you mind if I maybe later comment this if I have any issues with this? Thank you very much best of luck to you – AndroidLove Aug 25 '13 at 21:02
  • i don't understand. if you have code to show and ask about, please don't put it in the comment. put it in the question as a new paragraph (preferably with a "----" horizontal line ) , so that i can read and see what's wrong. – android developer Aug 25 '13 at 21:04
  • 2
    -1, There is no reason why you should need to use `TYPE_SYSTEM_ALERT` in order to achieve this. You should not use this unless your app needs to draw over other applications, and the OP never specified this as a requirement. Also, you should use `PixelFormat.TRANSLUCENT` instead of `PixelFormat.RGBA_8888`. Also, why use `FLAG_NOT_FOCUSABLE`? That will prevent the window (and therefore any of its children views) from being able to gain focus. If the window isn't able to gain focus, the input method manager won't be able to show a keyboard for the window. This solution seems like a giant hack. – Alex Lockwood Aug 25 '13 at 21:09
  • @AlexLockwood i've answered this question in this way since it was refered from a previous question he asked , here : http://stackoverflow.com/a/18432826/878126 . he specifically said that this is what he wanted. of course, he can change any flags he needs. it's just a sample... about the editText, i'm quite sure i've tested the same code before and it worked fine. – android developer Aug 25 '13 at 21:31
  • @androiddeveloper "I'm quite sure I've tested the code and it worked fine." So the `EditText` was able to gain focus in a non-focusable window? I'd be interested to see that code. – Alex Lockwood Aug 25 '13 at 22:02
  • 1
    @AlexLockwood hmmmm... yes you are right. in fact, i had this exact same problem before, which i've (according to what i remember) overcome by making a textView appear like an editText, yet when you click on it, it would show a dialog (or activity) that would allow you to edit a different editText. here's the old post i've written about it: http://stackoverflow.com/a/10267371/878126 – android developer Aug 25 '13 at 22:26
  • @user2693419 can you please post the entire code? the parts you've shown seems ok to me. about Lilypad, does it show a dialog for the editText? – android developer Aug 26 '13 at 05:20
  • @androiddeveloper Yes it has a dialog box and edittext at the bottom. Look at my edit for a picture of LilyPad – AndroidLove Aug 26 '13 at 05:36
  • @androiddeveloper do you mind if I post my code in Google Doc and share with you. Not comfortable sharing the entire code on a public forum and against my university policy I believe – AndroidLove Aug 26 '13 at 05:38
  • Please see if you can help me with Edit III. Thank you very much for your continous support – AndroidLove Aug 26 '13 at 06:04
  • @user2693419 i meant, does lilypad have a dialog when you start typing. about sharing the code, just post the relavant code, or create a totally new project to show your problem (this way you might even find the fix for it). about the relative layout , i don't think orientation does much (or at all). you should use the normal alignment parameters for it : http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html – android developer Aug 26 '13 at 07:06
  • @androiddeveloper Yes LilyPad has a dialog. like it keeps updating real time with the chat. Thanks. What did u mean by normal parameters? Can I use normal parameters even after doing it in XML? – AndroidLove Aug 26 '13 at 13:20
  • @user2693419 relativeLayout has its own LayoutParams, and i don't think orientation does anything to it since it belongs to LinearLayout . about LilyPad , doesn't it look odd in an end user's perspective to see a dialog for it? i wonder if you could "cheat" here and show an invisible dialog with invisible editText , that would allow you to make it look like you write directly to where you wanted... however, it might be problematic since the user might touch the real editText while writing... – android developer Aug 26 '13 at 14:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36274/discussion-between-user2693419-and-android-developer) – AndroidLove Aug 26 '13 at 17:08
0

I just added PixelFormat.RGBA_8888 to layoutParams then the keyboard is shown

ABADA S
  • 35
  • 4