I have a view defined as "public class ChessBoard extends View" in this class in one of the code flows I want to popup a Dialog box and then get a result from that dialog box. I have tried the answer here: How to create a Custom Dialog box in android? but I have no Activity to pass to the constructor.
            Asked
            
        
        
            Active
            
        
            Viewed 45 times
        
    -1
            
            
        - 
                    If you're building an app for chess then I am ready to contribute if you open source it on GitHub. I have experience in .Net and a little mini-max, alpha-beta pruning. – taimur alam Aug 16 '18 at 20:06
- 
                    A `View` shouldn't be showing a `Dialog` on its own. The `Activity` should be handling that. – Mike M. Aug 16 '18 at 22:26
- 
                    Taimur alam: I am just building a simple logging app, I will github it as soon as there is something worthwhile to github – Peter Chikov Aug 19 '18 at 20:16
- 
                    Mike M: You are probably correct but rewriting the whole app to use an Activity is not feasible – Peter Chikov Aug 19 '18 at 20:17
- 
                    Taimur alam: https://github.com/Chikov2/ChessLoggerCode basically it's a chess logging app but it has a fairly extensive code (no bugs found yet) to test whether the move is valid. It can possibly be reused for any chess app. – Peter Chikov Sep 17 '18 at 13:33
2 Answers
1
            since your chessboard is a view there should be a getContext()-method (see here: https://developer.android.com/reference/android/view/View.html#getContext() ). you can cast the result to an activity:
Activity activity = (Activity) getContext()
 
    
    
        Doflaminhgo
        
- 577
- 5
- 16
0
            
            
        Use this code 
///-----------------------------------------------------
 dialog_=new Dialog(this);
                 dialog_.setContentView(R.layout.dialog_submit);//this is path of xml file
                 dialog_.show();
                 Button submit_btn=dialog_.findViewById(R.id.but_submit); //button on dialog
                 Button cancel_btn = dialog_.findViewById(R.id.but_cancel);
                 final EditText edit_username=dialog_.findViewById(R.id.edit_name);
 
    
    
        Vishal Sharma
        
- 1,051
- 2
- 8
- 15
