My popup Window remains without background.
I want a white popup saying whom won the game.
Java:
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(layoutInflater.inflate(R.layout.winner_popup,null, false), LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
pw.showAtLocation(findViewById(R.id.main_layout), Gravity.CENTER, 0, 0);
TextView winnerText = (TextView) pw.getContentView().findViewById(R.id.winnerTextView);
winnerText.setText("Player 1 is the winner"); //Update the TextView
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/white">
<TextView
android:id="#+id/winnerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/winnerTextViewString"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/winnerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/winnerButtonString"
android:layout_marginLeft="45dp"/>
</LinearLayout>
Is it possible or should I use the alert-window instead?
Br
Related
want to add close button on BottomSheetDialog to close it.
I'm using with custom view(Linearlayout) for BottomSheetDialog.
filterDialog = new BottomSheetDialog(context);
filterDialog.setContentView(rootView);
filterDialog.getWindow().setWindowAnimations(R.style.CustomDialogAnimation);
filterDialog.show();
below image is my filterDialog:
And this is what I want to achieve:
Any help would be greatly appreciated.
I have solved this by using Dialog(full screen) with custom(bottom) animations.
Design:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorTransparent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<ImageView
android:id="#+id/img_close"
android:layout_width="#dimen/fifty_dp"
android:layout_height="#dimen/thirty_dp"
android:src="#drawable/ic_close_filter"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<!-- All your required widgets here-->
</LinearLayout> </LinearLayout>
Java Code:
Dialog filterDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View rootView = inflater.inflate(R.layout.dialog_filter, null);
final ImageView close = (ImageView) rootView.findViewById(R.id.img_close);
builder.setView(rootView);
filterDialog = builder.create();
WindowManager.LayoutParams lp2 = new WindowManager.LayoutParams();
Window window = filterDialog.getWindow();
filterDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
lp2.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp2.width = ViewGroup.LayoutParams.MATCH_PARENT;
lp2.height = ViewGroup.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp2);
Window dialogWindow = filterDialog.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
dialogWindow.setGravity(Gravity.BOTTOM );
filterDialog.getWindow().setWindowAnimations(R.style.CustomDialogAnimation);
filterDialog.show();
You can do it as -
<!--you bottom sheet linear layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--Layout contains button-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#android:color/transparent"
android:clickable="false"
>
<!--Your button-->
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="your drawable"
android:background="#android:color/transparent"
android:clickable="true"/>
</LinearLayout>
<!--put Your other views in below linear layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
Note : clickable false is very important point. Doing this you can access view behind your linear layout. Also set background color to transparent
I have two layouts, i inflate one layout into another and want to display this inflated layout including textview as many times as condition is true . I set the textview value as value of i and also set the layout top margin. but it displaying value of i in one row only and rest are blank,also its setMargin() also not working.Below is what i have done yet.
public void displayRows(){
int n= 6;
for(int i=0;i< n;i++){
LinearLayout container = (LinearLayout)findViewById(R.id.layoutContainer);
View hiddenInfo = getLayoutInflater().inflate(R.layout.dynamic, container, false);
container.addView(hiddenInfo);
LinearLayout hiddenLayout = (LinearLayout)findViewById(R.id.dynamic);
TextView textView = (TextView) findViewById(R.id.text);
textView.setText("Text"+i);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 30, 0, 0);
hiddenLayout.setLayoutParams(layoutParams);
hiddenLayout.addView(textView);
}
container.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/layoutContainer"
android:animateLayoutChanges="true"
android:layout_gravity="center|top"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/li1"
android:layout_gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="#color/colorAccent"
android:onClick="displayRows"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:textColor="#color/colorPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_gravity="center"
android:text="Rows"/>
</LinearLayout>
</LinearLayout>
dynamic.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/dynamic"
android:background="#color/colorAccent"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text"
android:text=""
android:textColor="#color/colorPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
I also want to know how to remove all the generated dynamic rows on click remove button.
Replace
LinearLayout hiddenLayout = (LinearLayout)findViewById(R.id.dynamic);
TextView textView = (TextView) findViewById(R.id.text);
with
LinearLayout hiddenLayout = (LinearLayout) hiddenInfo.findViewById(R.id.dynamic);
TextView textView = (TextView) hiddenInfo.findViewById(R.id.text);
And remove this line
hiddenLayout.addView(textView);
I want to make candidateView inside button but
you see log cat:
please share code
My Code
SoftKeyboard.java
#Override
public View onCreateCandidatesView() {
LayoutInflater li = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View wordBar = li.inflate(R.layout.wordbar, null);
LinearLayout ll = (LinearLayout) wordBar.findViewById(R.id.words);
Button btn = (Button) wordBar.findViewById(R.id.button1);
btn.setOnClickListener(this);
mCandidateView = new CandidateView(this);
mCandidateView.setService(this);
setCandidatesViewShown(true);
mCandidateView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
ll.addView(mCandidateView);
return wordBar;
}
I want to make like this:
Layout wordBar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/words"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Thanks for advance
See this line
LinearLayout ll = (LinearLayout) wordBar.findViewById(R.id.words);
You are casting a TextView into LinearLayout
<TextView
android:id="#+id/words"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
R.id.words is a TextView
Solution
Add an id to your LinearLayout
Example :android:id="#+id/wordsLayout"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="#+id/wordsLayout"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/words"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
And use that id
LinearLayout ll = (LinearLayout) wordBar.findViewById(R.id.wordsLayout);
I have founded many solutions for this problem but none of them have worked for me. I want to show a PopupWindow inside a Fragment. This is my code
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View popupView = layoutInflater.inflate(R.layout.pop_up_cargando, null,false);
this.popupWindow = new PopupWindow(popupView, popupView.getWidth(),popupView.getHeight());
this.popupWindow.setFocusable(true);
int location[] = new int[2];
this.btnInventario.getLocationOnScreen(location);
this.popupWindow.showAtLocation(this.btnInventario, Gravity.NO_GRAVITY, location[0], location[1] + btnInventario.getHeight()); // this.btnInventario is the button that calls this code
this.popupWindow.update(0, 0, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
But the PopupWindow never appears.
Edit: This is the content of pop_up_cargando
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_recomendar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical" >
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:id="#+id/cargando"/>
<TextView
android:id="#+id/txtTexto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_gravity="center"
android:text="#string/vacio"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
Any suggestions? What I'm doing wrong? Your help will be very appreciated.
Your popupView.getWidth() and popupView.getHeight() values are equals to 0 because the view has not been drawn yet.
Before asking for the width and the height of your view, you have to ensure that it has been drawn. For that you can call the following method after it has been inflated:
popupView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
After that, the size of your view is available with the methods getMeasuredWidth() and getMeasuredHeight().
I have a popup window that should be displaying two button and a textview with this xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/pe"
android:paddingTop="100dp"
android:paddingBottom="100dp"
android:paddingLeft="180dp"
android:paddingRight="180dp"
android:background="#444444"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="26dp"
android:text="Are you sure?" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/bYesClear"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:text="Yes" />
<Button
android:id="#+id/bNoClear"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:text="No" />
</LinearLayout>
</LinearLayout>
And this code is calling it:
private PopupWindow pw;
private void initiatePopupWindow() {
try {
LayoutInflater inflater = (LayoutInflater) Options.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popupareyousure, (ViewGroup) findViewById(R.id.pe));
pw = new PopupWindow(layout, 470, 300, true);
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
yesClearButton = (Button) layout.findViewById(R.id.bYesClear);
noClearButton = (Button) layout.findViewById(R.id.bNoClear);
yesClearButton.setOnClickListener(this);
noClearButton.setOnClickListener(this);
} catch (Exception e) {
e.printStackTrace();
}
}
It does display the popup window with the correct size, however, it does not display the text view, nor the buttons within.