How to define layout in PopupWindow from xml file when PopupWindow method is called from separate class

I would like to be able to define the layout in the PopupWindow from the xml file when the PopupWindow method is called from a separate class. Below code works as needed. EXCEPT the layout is pulled from the java file and not the xml file. I don't know how to properly access the xml layout and implement in the PopupWindow in this situation, or if possible. Advice and concrete suggestions are welcome. Thank you.

showPopup.java

public class showPopup {
Context ctx;
Button btnDismiss;

public showPopup(Context ctx){
    this.ctx = ctx;     
}

public void onCreateView(LayoutInflater layoutInflater, ViewGroup container) {
    View layout = layoutInflater.inflate(R.layout.popup_layout, null);   
    btnDismiss = (Button) layout.findViewById(R.id.btndismissxml);
}

public void goJoe(View parent){ 
    final PopupWindow popup = new PopupWindow(ctx);

    btnDismiss = new Button (ctx);
    btnDismiss.setText("Text is from showPopup.java");

    popup.setContentView(btnDismiss);
    popup.setWidth(400);
    popup.setHeight(580);
    popup.showAtLocation(parent, Gravity.CENTER_HORIZONTAL, 10, 10);        

    btnDismiss.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popup.dismiss();    
        }
    });
  }
}

      

Tab3Fragment.java

public class Tab3Fragment extends Fragment implements OnClickListener{
Button btnPopup;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup containerGroup, Bundle savedInstanceState) {    
    View v = inflater.inflate(R.layout.tab3_fragment, containerGroup, false);   
    btnPopup = (Button)v.findViewById(R.id.btnPopupxml);
    btnPopup.setOnClickListener(this);
    return v;
}
//@Override
public void onViewCreated(View v) {
    btnPopup = (Button)v.findViewById(R.id.btnPopupxml);
    btnPopup.setOnClickListener(this);
}
@Override
public void onClick(View parent) {
    new showPopup(getActivity().getApplicationContext()).goJoe(parent); 
  }
}

      

popup_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" 
android:layout_height="fill_parent"
android:id="@+id/layout">

<Button android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_width="wrap_content" 
    android:id="@+id/btndismissxml"
    android:text="Text is from popup_layout.xml"></Button>
</RelativeLayout>

      

Update (1835, Dec-11): This is a popup. Some of these user-maintainable editable dropdown fields will be available in the application.

enter image description here

+3


source to share


2 answers


Change showPopup

as follows

public class showPopup {
Context ctx;
Button btnDismiss, btnSaveRecord, btnLastRecord;
EditText edSpeciesLookup,edSpeciesLookupRowid;
DBAdapter msdb;
SQLiteDatabase db;

public showPopup(Context ctx){
    this.ctx = ctx; 
    msdb = new DBAdapter(ctx,"gfda", null);     
    db = msdb.getWritableDatabase();
}

public void goJoe(View parent){ 
    LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View popUpView = inflater.inflate(R.layout.popup_layout, null, false);
    final PopupWindow popup = new PopupWindow(popUpView, 400,
                    580, true);         
    popup.setContentView(popUpView);
    btnDismiss = (Button) popUpView.
                    findViewById(R.id.btndismissxml);
    edSpeciesLookup = (EditText) popUpView.
                    findViewById(R.id.editspecieslookupxml);
    edSpeciesLookupRowid = (EditText) popUpView.
                    findViewById(R.id.editspecieslookuprowidxml);
    popup.showAtLocation(parent, Gravity.CENTER_HORIZONTAL, 10, 10);        

    btnDismiss.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popup.dismiss();    
        }
    });

    btnSaveRecord = (Button) popUpView.findViewById(R.id.btnSaveRecordxml);
    btnSaveRecord.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {//SAVE
            String szSpecies = edSpeciesLookup.getText().toString();
        if(szSpecies.matches("")){//checks to see if species field is empty...
            ///nothing happens...
        }else{db.execSQL("INSERT INTO speciesLookupDb (species) VALUES ('"+szSpecies+"')");
            resetForm();
        }
    }
    });

    btnLastRecord=(Button)popUpView.findViewById(R.id.btnLastRecordxml);
    btnLastRecord.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Cursor c = db.rawQuery("SELECT * FROM speciesLookupDb WHERE _id = (SELECT MAX(_id) FROM speciesLookupDb)",null);
            if (c.moveToFirst()){////a record exists (table is not blank)           
                edSpeciesLookupRowid.setText(c.getString(0));
                edSpeciesLookup.setText(c.getString(1));            
            }else{//no record here...table is blank.        
            }
        }
    });
  }
}

      



If you want to reject popup

when touching anywhere on the screen, you must add these lines after the setContentView

popup and beforeshowAtLocation

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                    R.drawable.any_drawable);
popup.setBackgroundDrawable(new BitmapDrawable(getResources(),
                    bitmap));
popup.setOutsideTouchable(true);
popup.setFocusable(true);

      

+5


source


You can try to use DialogFragment .

From the API docs, a snippet that displays a dialog floating on top of an activity window. This snippet contains a Dialog object that is displayed appropriately based on the state of the snippet. Controlling the dialog (deciding when to show, hide, dismiss it) should be done through the API here and not with direct calls to the dialog.

Implementations must override this class and implement onCreateView (LayoutInflater, ViewGroup, Bundle) to provide the contents of the dialog. Alternatively, they can override onCreateDialog (Bundle) to create a fully custom dialog like AlertDialog with their own content.

1.Create your own popup layout.

2.Creating a custom dialog fragment.

3. Create an interface for clicking a button.

4.Set it to your fragment.

1. Create your own popup layout.



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/app_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="14dp"
    android:text="TITLE"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/popupMsg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/app_title"
    android:layout_below="@+id/app_title"
    android:layout_marginTop="30dp"
    android:text="Content" />

<Button
    android:id="@+id/navBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/popupMsg"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="22dp"
    android:text="OK" />

 </RelativeLayout>

      

... 2. Create your own dialog box fragment.

import android.app.Dialog;

import android.app.DialogFragment;

.....

public class CustomAlert extends DialogFragment {

public OnSubmitListener mListener;
Context context;
String Title = "";
String content = "";

public CustomAlert (Context context, String title, String message) {
    this.context = context;
    this.Title = title;
    this.content = message;

  }

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Dialog dialog = new Dialog(getActivity());
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    dialog.setContentView(R.layout.popup_layout);
    Window window = dialog.getWindow();
    window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    dialog.getWindow().setBackgroundDrawable(
            new ColorDrawable(Color.TRANSPARENT));
    dialog.show();
    TextView title = (TextView) dialog.findViewById(R.id.app_title);
    TextView msg = (TextView) dialog.findViewById(R.id.popupMsg);
    Button btn = (Button) dialog.findViewById(R.id.navBtn);
    title.setText(Title);
    msg.setText(content);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            mListener.setOnAlert(content);
            dismiss();
        }
    });
    return dialog;
  }
  }

      

3. Create an interface for clicking a button.

public interface OnSubmitListener{

  void setOnAlert(String arg);

 }

      

4.Place it on your piece.

  public class Tab3Fragment extends Fragment implements OnClickListener, OnSubmitListener{
  Button btnPopup;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup containerGroup, Bundle     savedInstanceState) {   

     View v = inflater.inflate(R.layout.tab3_fragment, containerGroup, false);   
     btnPopup = (Button)v.findViewById(R.id.btnPopupxml);

     return v;
   }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        btnPopup.setOnClickListener(getActivity());
  }
   @Override
   public void onClick(View v) {
    ShowDialog("Title", "Sample content");
   }


  private void ShowDialog(String title, String msg) {
    CustomAlert fragment1 = new CustomAlert (
            getActivity(), title, msg);
    fragment1.mListener = this;
    fragment1.show(getFragmentManager(), "");
  }

    @Override
    public void setOnAlert(String arg) {
        Toast.makeText(getActivity(),
                "Button Clicked:"+arg,
                Toast.LENGTH_LONG).show();
    }

}

      

5.Happy coding .....

+1


source







All Articles