Why does this android java popupWIndow doesn't show up? - java

The following code does not make the popupWindow popup, most of the things I tried to make it work make it crash, some of them are commented, the rest just doesn't make the cut. Any leads?
// Popup the login
private void loginPopup(){
// Get the contents
RelativeLayout loginLayout = (RelativeLayout) findViewById(R.id.login_popup_layout);
EditText usernameTxt = (EditText) findViewById(R.id.login_popup_username);
EditText passwordTxt = (EditText) findViewById(R.id.login_popup_password);
Button registerBtn = (Button) findViewById(R.id.login_popup_register);
Button loginBtn = (Button) findViewById(R.id.login_popup_login);
Button GloginBtn = (Button) findViewById(R.id.login_popup_G);
Button FBloginBtn = (Button) findViewById(R.id.login_popup_FB);
//loginLayout.addView(usernameTxt);
// Popup the login menu
//View popupView = getLayoutInflater().inflate(R.layout.login_popup_layout, null);
PopupWindow loginPopup;
loginPopup = new PopupWindow(loginLayout, 100, 100);// The last true is to make it focusable
loginPopup.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
//loginPopup.setContentView(loginLayout);
// Center the popup and display it.
Display display = getWindowManager().getDefaultDisplay();
Point size=new Point();
display.getSize(size);
loginPopup.showAtLocation((RelativeLayout)findViewById(R.id.displayLayout), Gravity.NO_GRAVITY, 50,50 );//(size.x/2)-loginPopup.getWidth()/2 , (size.y/2)-loginPopup.getHeight()/2 );}

I was using it wrongly, my idea of a PopupWindow wasn't a window that pops out of a view, I was thinking of a whole window that covered a big portion of the screen that had a separated layout with its's own classes and etcetera...
In the end I made a new activity, with custom width and height, and made the black area, that wasn't covered by the activity, transparent. It seems that this is the correct implementation.
In the code attached, the window didn't popup because I didn't inflate it.

Related

Registering multiple click events programatically (Android/Java)

So i have this program which create my list of cards which are relative layouts and they look like this.
Here is the code of it creation. Ps its in a for loop
LayoutInflater mInflater = (LayoutInflater) atv.getSystemService(atv.LAYOUT_INFLATER_SERVICE);
LinearLayout relativeLayoutz = (LinearLayout) mInflater.inflate(R.layout.rtl_enterprise, parent);
RelativeLayout rl = (RelativeLayout) relativeLayoutz.findViewById(R.id.rl_empresa);
rl.setId(i);
ImageView img = (ImageView) rl.getChildAt(0);
//
//Performance bottleneck needs fixing/ not loading all images and overloading thread
//
//loadImageByUrl(atv, enterprises.getEnterprises().get(k).getPhoto().toString(), img);
TextView txt = (TextView) rl.getChildAt(1);
txt.setText(enterprises.getEnterprises().get(i).getEnterpriseName());
TextView txt2 = (TextView) rl.getChildAt(2);
txt2.setText(enterprises.getEnterprises().get(i).getEnterpriseType().getEnterpriseTypeName());
TextView txt3 = (TextView) rl.getChildAt(3);
txt3.setText(enterprises.getEnterprises().get(i).getCountry());
LinearLayout relativeLayout = (LinearLayout) mInflater.inflate(R.layout.rtl_enterprise, parent);
relativeLayout.setId(i);
everything there works kinda ok, but i have a problem, i need to create a
onClick listener
Why?
I would call a method which needs some info about the card that has been clicked, and then redirect to a new activity which contains the info about the card that he clicked.
But i would need that onclick listener for each of these relative layouts, and i assume it would need to be initialized when the card is created since it create + 50 cards, but i have no idea of how to setup each listener.
Why do you create it like that?
a better solution to use RecyclerView.
and in the adapter, you can listen for the item click. write the code one time and when any item clicked. you will know the position of the clicked item.
see this tutorial
https://developer.android.com/guide/topics/ui/layout/recyclerview
https://www.androidhive.info/2016/01/android-working-with-recycler-view/

Android: Is it possible to set parent of PopupWindow?

I displayed PopupWindow in fragment and it works well except when side menu appears. Side menu is displayed behind the PopupWindow which I don't want.
So I think it would be good to set parent of PopupWindow or maybe zOrder.
LinearLayout contentView = (LinearLayout) inflate(getContext(), R.layout.callout_merge_destination, null);
PopupWindow mCallout = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mCallout.showAsDropDown(view3, x, y);
Please let me know the best solution.

Android Studio: Button always appears at the front

I have a RelativeLayout to which I add Views.
I added a button to it, and the button always appears in front of all the other Views that are added to it, regardless of the order in which things were added. How come?
I'm coding purely in Java, no XML.
Here's a simple example, the button will appear here in front of the text, even though the text was added last:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
Button button = new Button(this);
TextView text = new TextView(this);
button.setText("Button");
text.setText("Text");
layout.addView(button);
layout.addView(text);
setContentView(layout);
}
Starting with Lollipop, a StateListAnimator controlling elevation was added to the default Button style. In my experience, this forces buttons to appear above everything else regardless of placement in XML (or programmatic addition in your case). That might be what you're experiencing.
To resolve this you can add a custom StateListAnimator if you need it or simply set it to null if you don't.
XML:
android:stateListAnimator="#null"
Java:
Button button = new Button(this);
button.setText("Button");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
button.setStateListAnimator(null);
}
More details:
Android 5.0 android:elevation Works for View, but not Button?
In the Android 5.0 (API 21) and above, you must add android:elevation into the view.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
Button button = new Button(this);
TextView text = new TextView(this);
button.setText("Button");
text.setText("Text");
button.setElevation(3.0f); // add this line, you could try with values > 3.0f
layout.addView(button);
layout.addView(text);
setContentView(layout);
}
From android developer docs :
By default, all child views are drawn at the top-left of the layout, so you must define the position of each view using the various layout properties available from RelativeLayout.LayoutParams.
http://developer.android.com/guide/topics/ui/layout/relative.html
Try the following snippet :
RelativeLayout layout = new RelativeLayout(this);
Button button = new Button(this);
TextView text = new TextView(this);
button.setId(View.generateViewId());
text.setId(View.generateViewId());
button.setText("Button");
text.setText("Text");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, text.getId());
button.setLayoutParams(params);
layout.addView(button);
layout.addView(text);
The button appears to float to the forefront of the RelativeLayout it's in so...
Try this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = new Button(this);
button.setText("Button");
RelativeLayout groupContainingButton = new RelativeLayout(this);
groupContainingButton.addView(button);
TextView text = new TextView(this);
text.setText("Text");
RelativeLayout activityLayout = new RelativeLayout(this);
activityLayout.addView(groupContainingButton);
activityLayout.addView(text);
setContentView(activityLayout);
}
Check the button's state (enabled/disabled):
loginButton.setEnabled(false);

Create a button dynamically within the same layout by clicking on an existing button in android

I want to add a new button to the view by clicking on an existing static button within the same view. And the button should stay there in the view until we manually delete it.
Browsing for help on this for a while and all I can get to is creating dynamic buttons. Hope any of you can help me out in what I am trying to achieve.
On your existing button click listener call this function.
private void addButton(){
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout);//create a layout in you content view
layout.setOrientation(LinearLayout.VERTICAL);
Button newButton = new Button(this);
newButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
newButton.setText("Button Text");
newButton.setId(1);//some id
layout.addView(row); // add button in layout
}
You must access to layout with findView and add your button to that like this code which located in onCreate function in your activity :
Button staticBtn = (Button)(findViewById(R.id.staticBtnID));
LayoutParam staticBtnLayoutParam = staticBtn.getLayoutParam();
Button btn = new Button(this);
int width = 0; //you can use staticBtnLayoutParam.getWidth(); or .getX(); to get staticBtn parameters and set your param related to that
int height = 0 ;
btn.setLayoutParams(new LayoutParams(width , height));
LinearLayout layout = (LinearLayout)(findViewById(R.id.linearLayoutID));
layout.addView(button);
Best Regards

Full Screen AlertDialog changes the layout color

I'm using the code from another answer here:
AlertDialog.Builder adb = new AlertDialog.Builder(this);
Dialog d = adb.setView(new View(this)).create();
// (That new View is just there to have something inside the dialog that can grow big enough to cover the whole screen.)
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.FILL_PARENT;
lp.height = WindowManager.LayoutParams.FILL_PARENT;
d.show();
d.getWindow().setAttributes(lp);
I really find it usefl to make an alertDialog full screen, but the colors end up being a black background with white text, instead of a white background with black text. I have no idea how this code could be changing the color. Could anyone provide some info?
In the line:
Dialog d = adb.setView(new View(this)).create();
you ceate a new View which defaults to black background.
Then you use this view attributes everywhere:
lp.copyFrom(d.getWindow().getAttributes());
d.getWindow().setAttributes(lp);
Solution:
After creating the new view, set the background:
View view = new View(this);
view.setBackgroundColor(...);
Dialog d = adb.setView(view).create();
Regards.

Categories