Closing a PopupWindow with the back button? - android

I've got a popupWindow that I'm using to display google maps but once it's open I can't seem to close it, I'm trying to set the back button to close it but can't call the onBackButtonClick override as I'm inside a fragment. Doesn't seem to do anything though
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_go, container, false);
mRouteBtn = (Button) rootView.findViewById(R.id.routeBtn);
mRouteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
View popupView = getActivity().getLayoutInflater().inflate(R.layout.fragment_map, null);
mPopupWindow = new PopupWindow(popupView,
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// If the PopupWindow should be focusable
mPopupWindow.setFocusable(true);
mPopupWindow.showAtLocation(getActivity().findViewById(R.id.routeBtn), Gravity.CENTER, 0, 0);
mPopupWindow.update(0, 0, 800, 800); //don't hardcode
Back button code part:
mPopupWindow.getContentView().setOnKeyListener( new View.OnKeyListener(){
#Override
public boolean onKey( View v, int keyCode, KeyEvent event ){
if( keyCode == KeyEvent.KEYCODE_BACK ){
mPopupWindow.dismiss();
return true;
}
return false;
}
} );
}
});
Alright sorted, after following Deepak Singh's advice I got it working although when I reopened it I had issues with crashing due to trying to recreate a view that still exists. here's my working code for future reference:
FIXED VERSION:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_go, container, false);
mRouteBtn = (Button) rootView.findViewById(R.id.routeBtn);
mRouteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mPopupView != null) {
ViewGroup parent = (ViewGroup) mPopupView.getParent();
if (parent != null)
parent.removeView(mPopupView);
}
try {
mPopupView = getActivity().getLayoutInflater().inflate(R.layout.fragment_map, null);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
mPopupWindow = new PopupWindow(mPopupView,
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.showAtLocation(getActivity().findViewById(R.id.routeBtn), Gravity.CENTER, 0, 0);
mPopupWindow.update(0, 0, 800, 800); //don't hardcode
mPopupView.findViewById(R.id.doneBtn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mPopupWindow.dismiss();
}
});
}
});

Add a button on your pop up window layout and find the id of button using popupView.findviewbyid(id),then set clicklisener on this button and call mPopupWindow.dismiss();.
Also you can try this:
popupView.setOutsideTouchable(true);
popupView.setFocusable(true);

add to your code popupWindow.setOutsideTouchable(true);

According to this answer you need to set a background drawable to the popup window.
I tested it out and you don't even need to handle the OnKeyListener by yourself.
This should be enough to make 'back button' work:
popupWindow.setBackgroundDrawable(new ColorDrawable());
popupWindow.setFocusable(true);

Related

onClickListener and setOnTouchListener doesn't work in React Native

In my Android Native code I have written an OnClick Listener for a PopUp I need to display in my onCreate().
I want to write this in native rather than JS.
public class MainActivity extends ReactActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater layoutInflater =
(LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.pointzipreviewtoolbar,null);
PopupWindow popupWindow = new PopupWindow(
popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
ViewGroup rootLayout = (ViewGroup) findViewById(android.R.id.content);
popupView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("Popup was Clicked", "I clicked here");
}
});
}
}
The popup shows, however the onClickListner doesn't get triggered and also the setTouchListener doesn't work either. Am I missing some files??
You have set the root of the inflated popupView layout to null, which means it might not have correct layoutparams which may affect a touch interaction.
Try rearranging it so you can do this:
ViewGroup rootLayout = (ViewGroup) findViewById(android.R.id.content);
View popupView = layoutInflater.inflate(R.layout.pointzipreviewtoolbar, rootLayout, false);
That way the popupView will inherit the layoutparams from rootLayout, but will still not be attached to that view as the final argument (attachToRoot) is false. See if the onClickListener responds after that.
You've also not used setContentView(View view) in onCreate, though maybe that's normal when you inherit from ReactActivity, not sure.
I managed to solve this issue by setting the pointzipreviewtoolbar layout elements to an onClick. Which in my case, inside the pointzipreviewtoolbar had an ImageButton called preViewCapture. I then set this to on OnClickListner and it got triggered.
public class MainActivity extends ReactActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater layoutInflater =
(LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.pointzipreviewtoolbar,null);
PopupWindow popupWindow = new PopupWindow(
popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
ViewGroup rootLayout = (ViewGroup) findViewById(android.R.id.content);
ImageButton image = (ImageButton) popupView.findViewById(R.id.preViewCapture);
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("Popup was Clicked", "I clicked here");
}
});
}
}

Cannot resolve method 'findViewById(int)' in Fragment

I am trying to implement a button into a fragment in order to use the soundPool in order to play a sound with a button. At the moment the playSound1 is coming up as never used and I have tried to implement the onClick Method but it is now coming up saying it cannot resolve the method. How do I link the soundPool to a button in a fragment? this is the .java file
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Clubb1 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb1Id = Clubb1.load(getActivity(), R.raw.clubb1, 1);
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_one_layout, container, false);
Button buttonA = (Button) findViewById(R.id.buttonA);
buttonA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
public void playSound1()
{Clubb1.play(clubb1Id,1,1,1,0,1);}
});
Change your method to :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Clubb1 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb1Id = Clubb1.load(getActivity(), R.raw.clubb1, 1);
View rootView = inflater.inflate(R.layout.fragment_one_layout, container, false);
Button buttonA = (Button) rootView.findViewById(R.id.buttonA);
buttonA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Clubb1.play(clubb1Id, 1, 1, 1, 0, 1);
}
});
return veiw;
}
You did a lot of mistakes here. So im not sure that my approach help you enough ) Write your program result into comment and we`ll try more.
There are multiple mistakes in your code.
You return a value way to early, the code beyond your return statement isn't executed
You're inflating the View, but all your inner elements eg your button is in that view so you have to find that view by id in that view.
I corrected it:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_one_layout, container, false);
Clubb1 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb1Id = Clubb1.load(getActivity(), R.raw.clubb1, 1);
Button buttonA = (Button) root.findViewById(R.id.buttonA);
buttonA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
public void playSound1()
{Clubb1.play(clubb1Id,1,1,1,0,1);}
});
return root;
}

Showing a popup window from Fragment

I want to show a popup window from my PlaceHodler class extending Fragment when the button is clicked. For a test, I wrote this code which really works, but I guess it's ridiculous (using Button object as a parent view and so on... I couldn't find another way to make it work...). Please look at this code and advice me how to improve it. Please don't judge me because I'm a very beginner in programming.
My code:
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
final Button button1 = (Button)rootView.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Log.i("ilog", "onClick() works");
PopupWindow pw = new PopupWindow(getActivity());
TextView tv = new TextView(getActivity());
LayoutParams linearparams1 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(linearparams1);
tv.setText("Testing");
pw.setContentView(tv);
pw.setWidth(400);
pw.setHeight(180);
pw.showAtLocation(button1, Gravity.CENTER_HORIZONTAL, 25, 25);
pw.update();
}
});
return rootView;
}
}
popwindow in fragment and activity almost the same except they way you get contrext , in activity this , fragment getActivity()
here is the code to create popWindow
View popupView = LayoutInflater.from(getActivity()).inflate(R.layout.popup_layout, null);
final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
// define your view here that found in popup_layout
// for example let consider you have a button
Button btn = (Button) popupView.findViewById(R.id.button);
// finally show up your popwindow
popupWindow.showAsDropDown(popupView, 0, 0);
reference PopupWindow

android PopupWindow from a fragment

I have an app that uses fragments, its working ok,
but I have now to implement some pop ups when a button gets tapped,
I am following this tutorial "Example of using PopupWindow"
But I get this errors:
Multiple markers at this line
- LAYOUT_INFLATER_SERVICE cannot be resolved to a variable
- The method getBaseContext() is undefined for the type new
View.OnClickListener(){}
here my .java
public class Tab2HeadH1 extends Fragment implements OnClickListener{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_2_head_buttons, container,false);
//Buttons
Button buttonNose = (Button) view.findViewById(R.id.button_pop_nose);
buttonNose.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
//aqui tus tareas,,
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); //ERRORS HERE!!
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}
});
Button buttonEye = (Button) view.findViewById(R.id.button_pop_eye);
buttonEye.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
// onLoginClicked(v);
Toast.makeText(getActivity(), "ss9 eye",
Toast.LENGTH_SHORT).show();
}
});
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((TabActivity)getActivity()).setHeader("TAPING APPLICATION");
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
}
}
}
SO, how can I fix this problem??, to show my pop up from tapped button in my fragment??
call
LayoutInflater layoutInflater = (LayoutInflater)**getActivity()**.getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
Atleast it won't show error onBaseContext() as now it take from the associated activity
EDIT
Try this,
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View myFragmentView = inflater.inflate(R.layout.yourlayout, container,false);
//inside button onclick write the code given below
View popupView = inflater.inflate(R.layout.address, null);
}

android what is missing to show my pop up

I have an app with fragments working ok,
now I need to implement some pop up views,
Im following this tutorial Example of using PopupWindow
all seems fine, but The pop up is not showing yet, as I have a doubt on how to show the "popupWindow"
my .java:
public class Tab2HeadH1 extends Fragment implements OnClickListener {
//testeo popero
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_2_head_buttons, container,false);
//Buttons
Button buttonNose = (Button) view.findViewById(R.id.button_pop_nose);
buttonNose.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
//aqui tus tareas,,
//LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); //old errors
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}
});
Button buttonEye = (Button) view.findViewById(R.id.button_pop_eye);
buttonEye.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
// onLoginClicked(v);
Toast.makeText(getActivity(), "ss9 eye",
Toast.LENGTH_SHORT).show();
}
});
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((TabActivity)getActivity()).setHeader("TAPING APPLICATION");
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
}
}
}
So what is missing to show my pop up?
thanks a lot!
If you want to generate Custom pop up window you can do this use AlertDialog.Builder
private AlertDialog.Builder scheduleBuilder;
private AlertDialog dialog_Creater;
LayoutInflater myLayoutInflater = getLayoutInflater();
View v = myLayoutInflater.inflate(R.layout.schedule_creator, null); //Replace your layout R.layout.youLayout
scheduleBuilder = new AlertDialog.Builder(v.getContext());
scheduleBuilder.setTitle("Edit your Schedule Memo");
scheduleBuilder.setView(v); //set view
dialog_Creater = scheduleBuilder.create();
dialog_Creater.show();
While you setup the Popup fine, you aren't actually calling any method to show it anywhere. Call whichever one of the following methods suits your needs to open it:
showAtLocation()
showAsDropDown()
showAsDropDown() (Different from #2)
try this:-
PopupWindow pw;
View layout = inflater.inflate(R.layout.popnumber,
(ViewGroup) findViewById(R.id.lytpopuppoppop));
// your popup layout and id of root layout
pw = new PopupWindow(layout, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, true);
// your main layout where you display popup
pw.showAtLocation(findViewById(R.id.lytboard), Gravity.CENTER,
0, 0);

Categories

Resources