android what is missing to show my pop up - android

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);

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");
}
});
}
}

Closing a PopupWindow with the back button?

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);

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);
}

Inflate a layout with ImageButton inside Fragment programmatically

I am new on android development, and i was currently creating an application using tabhost and a TabActivity to switch between different activities. This application was working fine, but I recently discover that TabActivity are deprecated and I should use fragment instead.
So, I follow this great tutorial explaining how to do so :
http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/
Most of it works fine, but there is my problem some of my old activities which are now fragments were inflating ImageButton using this kind of methods :
private void displayLevelButtons() {
LayoutInflater lf = getLayoutInflater();
btns = lf.inflate(R.layout.mallmapactivity_level_buttons, null, false);
mBtnLevelDown = (ImageButton) btns.findViewById(R.id.btn_level_down);
mBtnLevelUp = (ImageButton) btns.findViewById(R.id.btn_level_up);
mBtnLevelDown.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
levelDown();
updateLevelButtons();
}
});
mBtnLevelUp.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
levelUp();
updateLevelButtons();
}
});
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL | Gravity.RIGHT;
addContentView(btns,params);
}
In order to be able to compile this method inside a fragment, I changed:
LayoutInflater lf = getLayoutInflater();
to
LayoutInflater lf = getActivity().getLayoutInflater();
and
addContentView(btns,params);
to
getActivity().addContentView(btns,params);
But using my method displayLevelButtons() like this make my buttons appears in the right bottom of my general view, not in the right bottom of my fragment.
I also tried to specify a root when inflating my layout with image buttons, my all fragment class looking like this :
public class MyFragment extends Fragment {
private View btns;
private ImageButton mBtnLevelUp;
private ImageButton mBtnLevelDown;
private ViewGroup myViewGroup;
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
myViewGroup = container;
v = inflater.inflate(R.layout.mallmaptile, container, false);
return v;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
//Do some stuff and then :
displayLevelButtons();
}
private void displayLevelButtons() {
LayoutInflater lf = getActivity().getLayoutInflater();
btns = lf.inflate(R.layout.mallmapactivity_level_buttons, myViewGroup, false);
mBtnLevelDown = (ImageButton) btns.findViewById(R.id.btn_level_down);
mBtnLevelUp = (ImageButton) btns.findViewById(R.id.btn_level_up);
mBtnLevelDown.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
levelDown();
updateLevelButtons();
}
});
mBtnLevelUp.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
levelUp();
updateLevelButtons();
}
});
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL | Gravity.RIGHT;
getActivity().addContentView(btns,params);
}
Doing so my buttons are still display in the bottom of my general view not in the bottom of my fragment, and if I try to change the attachToRoot boolean value to true :
btns = lf.inflate(R.layout.mallmapactivity_level_buttons, myViewGroup, true);
I get the error : java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I am sure, I missed something important but I don't what. Any suggestions would be really appreciated.
Thanks in advance,
If you are facing problem in inflating layout then you can use following code:
View headerView = View.inflate(this, R.layout.layout_name, null);
this will give you complete parent layout then you can use that layout to fetch whatever view you want or you can get layout param and then set layout param to any views (In your case button I think).
I had same problem and find my answer in this link:
problem with inflater
you can do it without any dependency
LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.view_with_merge_tag, this);

Categories

Resources