0

I currently have a SurfaceView (named BoardView) that is being stored in a FrameLayout. There is another LinearLayout (l1) that is stored in this FrameLayout(f1) which contains an EditText. I want to be able to bring the EditText to the front from within my BoardView. Is this possible? I tried using getParent().bringChildToFront(); but it didn't work. Any ideas?

public class Board extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    //use a frame layout so you can also display a dialog box
    // allows more than one view to be used
    FrameLayout f1 = new FrameLayout(this);
    LinearLayout l1 = new LinearLayout(this);
    EditText edit = new EditText(this);

    l1.setOrientation(LinearLayout.VERTICAL);
    l1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT));
    edit.setText("Enter your name!");
    l1.addView(edit);

    f1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
            LayoutParams.FILL_PARENT));
    f1.addView(l1);
    f1.addView(new BoardView(this));

    setContentView(f1);

    //setContentView(new BoardView(this));
}

}

skaffman
  • 398,947
  • 96
  • 818
  • 769
Hani Honey
  • 2,101
  • 11
  • 48
  • 76

1 Answers1

1

Sounds rather silly, but try l1.bringToFront() and see if that works? Alternatively just add l1 second:

f1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
            LayoutParams.FILL_PARENT));
    f1.addView(new BoardView(this));
    f1.addView(l1);

and the edit text will be on top.

Let me know if it works.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • The thing is, it says "l1" cannot be resolved. I want to show l1 ONLY when something happens in the SurfaceView. Basically, when the game ends, I want to show this new View. I guess I could make the current view invisible, right? – Hani Honey May 08 '11 at 16:13
  • Ah, so that is the question: show the code where you are making the switch – Femi May 08 '11 at 16:14
  • Well, the piece of code is rather long, really. I have a custom SurfaceView - there is not enough room to post the code here :/ What should I do to show you? My class title looks something like this. public class BoardView extends SurfaceView implements SurfaceHolder.Callback{ – Hani Honey May 08 '11 at 16:19
  • Is there a problem because it is a custom class? Surely there must be some way to do this. – Hani Honey May 08 '11 at 16:22
  • Shouldn't matter. The not resolve issue is just because your custom class doesn't have visibility of the `l1` object. I'd suggest assigning IDs to `l1` and then using the id: so do `l1.setId(1);` in the `onCreate` above and then in your custom class use `((View)getParent()).findViewById(1).bringToFront();` to update the z-order. – Femi May 08 '11 at 16:27
  • I tried using setVisiblity(INVISIBLE) from inside my surface view, but it only made my program force close. Is there a problem perhaps, because I have threads running? – Hani Honey May 08 '11 at 16:29
  • No, I expect you're just not referencing the right view when you are bringing to the front. – Femi May 08 '11 at 16:37
  • Okay, i'm getting an error saying that it is being called from the wrong thread. "Only the original thread that created a view hierarchy can touch its views". I have posted a new topic so you can see all the code. http://stackoverflow.com/questions/5928755/called-from-wrong-thread-exception – Hani Honey May 08 '11 at 16:37
  • Ah. You're doing things on the wrong thread. Answered in the other thread. – Femi May 08 '11 at 16:42