Inflate image on top of a popupview object - android

Currently for inflating images (zoom in animation) we use this library.
while on top of regular activities it looks fine (Simple layout), on top of a popupview the image is "behind" the popup.
Tried to change the elevation parameters, but no luck there either.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="2dp"
tools:context="com.MapActivity">
<com.vatsal.imagezoomer.ImageZoomButton
android:id="#+id/bt_picture"
android:layout_width="20dp"
android:layout_height="17dp"
android:layout_alignTop="#id/tx_description"
android:layout_marginTop="1dp"
android:layout_alignLeft="#+id/bt_date_time"
android:layout_marginLeft="-25dp"
android:elevation="10dp"
android:background="#drawable/ic_picture" />
</RelativeLayout>
And this is how the popupview and windows are inflated:
popupView = getLayoutInflater().inflate(R.layout.job, null);
popupWindow = new PopupWindow(popupView,
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
Should we try and use a different approach? Mabye ditch the library and inflate it differently?
Thanks

We've followed mmdreza's advice, here's a snippet of the implementation.
As the documantation states, the alertdialog will be on top of the popupview object.
myHolder.jobImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(v.getContext());
LayoutInflater inflater =
(LayoutInflater)v.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mView = inflater.inflate(R.layout.inflated_picture_dialog, null);
Picasso.get().load(jobView.getJobImageURL()).fit().into(((ImageButton)mView.findViewById(R.id.inflated_picture)));
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.show();
}
});

Related

Views not found for Alert Dialog in fragments

I am trying to populate an Alert Dialog (with a layout file having an EditText & Button) within a fragment of Activity.
The components are not being found by calling findViewById() in the fragment's corresponding class. I have written this code inside the class :
AlertDialog.Builder builder=new AlertDialog.Builder(context);
View v=getLayoutInflater().inflate(R.layout.layout_alert_dialog,null);
final EditText txtAddNew=v.findViewById(R.id.txtAddNew); //null
final TextView txtErr = v.findViewById(R.id.txtErr); //null
Button btnAdd=v.findViewById(R.id.btnAdd); //null
I think the problem raises from this line of code:
View v=getLayoutInflater().inflate(R.layout.layout_alert_dialog,null);
The alertDialog XML code is below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/txtAddNew"
android:hint="Category name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtErr"
/>
<Button
android:id="#+id/btnAdd"
android:text="Add Category" />
</LinearLayout>
Can anyone find what the actual problem is? Thanks!
Yes, that's correct
Do something like this :
LayoutInflater layout = LayoutInflater.from(this);
final View view = layout.inflate(R.layout.dialog_layout, null);
final AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setView(view);
dialog.show();
You can do it like below.
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getActivity());
alertBuilder.setView(getLayoutInflater().inflate(R.layout.layout_alert_dialog,null));
AlertDialog alertDialog = alertBuilder.create();
EditText txtAddNew = alertDialog.findViewById(R.id.txtAddNew);
TextView txtErr = alertDialog.findViewById(R.id.txtErr);
Button btnAdd = alertDialog.findViewById(R.id.btnAdd);
alertDialog.show();
Try using dialog.setContentView(v)
See the difference between setView(v) and setContentView(v) here:
What is difference between Dialog.setContentView( View ) & AlertDialog.setView( View )

how to make custom layout for alertdialog wrap content

I have created a custom layout with 3 buttons for alertdialog which is working fine. I am trying to make the layout so that width and height layout will be only wrap its content i.e. no extra width/height but I am unable to do so. Can you help me on this please?
Here is my custom layout for alertdialog:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#00ffffff"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/rectangle_menu"
>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/wowButtonId"
android:layout_marginLeft="5dp"
android:src="#drawable/love_icon"
android:background="#drawable/round_button_for_round_menu_like_button"
android:layout_gravity="center"
/>
<ImageButton
android:layout_width="40dp"
android:layout_height="35dp"
android:id="#+id/blehButtonId"
android:layout_marginLeft="5dp"
android:src="#drawable/bleh"
android:background="#drawable/round_button_for_round_menu_like_button"
android:layout_gravity="center"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/dislikeButtonId"
android:layout_marginLeft="5dp"
android:src="#drawable/dislike_icon"
android:background="#drawable/round_button_for_round_menu_like_button"
android:layout_gravity="center"
/>
</LinearLayout>
</LinearLayout>
alertDialog implement in adapter code:
AlertDialog.Builder builder=new AlertDialog.Builder(context);
AlertDialog alertDialog=builder.create();
View view1=LayoutInflater.from(context).inflate(R.layout.layout_for_long_like_button_option,null);
ImageButton wow=(ImageButton) view1.findViewById(R.id.wowButtonId);
ImageButton disLike=(ImageButton) view1.findViewById(R.id.dislikeButtonId);
ImageButton bleh=(ImageButton) view1.findViewById(R.id.blehButtonId);
wow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context,"wow",Toast.LENGTH_SHORT).show();
}
});
disLike.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context,"dislike",Toast.LENGTH_SHORT).show();
}
});
bleh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context,"bleh",Toast.LENGTH_SHORT).show();
}
});
alertDialog.setView(view1);
alertDialog.show();
try this on your class file where you inflate your alert dialog...
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = alertDialog.getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
Summary: Use RelativeLayout tag at the root in your custom layout
I had two alert dialogs in the application I was writing. One of them did not wrap content, while the other did. The one with the LinearLayout tag at its root, was expanding a tad more in height, than the space its contents really occupied.
I tried setting the width and height properties to wrap_content on the LinearLayout but to no avail.
The one with the RelativeLayout tightly wrapped its contents. The code structure to achieve this would look like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<!-- More widgets -->
</LinearLayout>
</RelativeLayout>
Although care must be taken, that the inner LinearLayout should still specify wrap_content. You can of course specify match_parent if your intention is to present an elongated dialog, the intentions for doing so, which I would leave it to the developer.
Android Studio still warns me that The LinearLayout layout or its RelativeLayout parent is useless. I do not consider this a solution to the problem, rather it's just a fix. And, I should mention that I've always had these layout problems working with Android. This, I feel, is a more saner approach as it keeps the code modifications down to the layout.
You can use below code for set layout and show dialog
public void showDialog() {
LayoutInflater li = LayoutInflater.from(MainActivity.this);
View promptsView = li.inflate(R.layout.layout_for_long_like_button_option, null);
final TextView txtOk = (TextView) promptsView.findViewById(R.id.txtOk);
final TextView txtCancel = (TextView) promptsView.findViewById(R.id.txtCancel);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(BaseActivity.this);
alertDialogBuilder.setView(promptsView);
final AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
txtOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
txtCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
You can try setting the width of the inflated view directly when the dialog is shown and therefore has a window
val dialog = AlertDialog.Builder(context)
.setView(myView)
.create()
dialog.setOnShowListener {
dialog.window?.setLayout(
myView.width,
ViewGroup.LayoutParams.WRAP_CONTENT
)
}

Find a View of an AlertDialog

I want to get a View from an AlertDialog:
Button R = (Button) findViewById(R.id.toggleButtonA);
But if I do this, it always is null. What can I do to get and edit the view?
Here are the relevant parts of code:
What creates the Dialog:
public void openSelection(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
LayoutInflater inflater = this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialayout, null));
AlertDialog dialog = builder.create();
dialog.show();
}
The dialayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/toggleButtonA"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:onClick="select"
android:text="All"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toggleButtonM" />
</android.support.constraint.ConstraintLayout>
</LinearLayout>
You need to assign your inflated view to a variable first. Example:
public void openSelection(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
View v = inflater.inflate(R.layout.activity_main, null); // this line
builder.setView(v);
AlertDialog dialog = builder.create();
dialog.show();
Button RR = (Button) v.findViewById(R.id.toggleButtonA); // this is how you get a view of the button
}
Please reneame your button variable name R to another name perhaps because R is a reserved word.
Inflate the parent layout first,
LayoutInflater inflater = this.getLayoutInflater();
View parent = inflater.inflate(R.layout.activity_main, null);
then set your builder to that parent view.builder.setView(parent);
you can then perform any view finding with that parent inflated view. Or you can call AlertDialog diag = builder.create(); and perform a find with the diag.findViewById(R.id.name);
so Button btR = (Button) diag.findViewById(R.id.toggleButtonA);
or Button btR = (Button)parent.findViewById(R.id.toggleButtonA);

Android scaling popup window

i am writing a simple 2D Android Game (sort of puzzles). A user is drawing lines during the game and that is why I decided to an approach in which i resize in runtime to scale correctly on differrent devices. I prepared graphics for 1280x720 and I simply resize them in runtime inside:
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
FrameLayout container = (FrameLayout) findViewById(R.id.main);
LinearLayout mainView = (LinearLayout) findViewById(R.id.main_main_view);
TransformationOld transform = new TransformationOld();
transform.scaleViewToFitInContainerIsotropically(container, mainView);
}
}
The inspiration comes from: http://www.vanteon.com/downloads/Scaling_Android_Apps_White_Paper.pdf
It works fine for Activities, but I cannot have it working for popups.
The way I create popups:
public void onClickButtonMainAbout(View view) {
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.popup_about_backup_with_pixels, null, false);
final PopupWindow pw = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
pw.showAtLocation(popupView, Gravity.CENTER, 0, 0);
Button button_ok = (Button) popupView.findViewById(R.id.about_button_ok);
button_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pw.dismiss();
}
});
}
The popup in xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_about2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/popup_about_main_view2">
<TableRow
android:id="#+id/tableRow1"
...
...
Any hints how to have such scaling working for popups to? Now it is simply displayed in 1280x720. I would be thankful for any advice.

How to make the switch button center in dialog

I want to switch button center in dialog,but it's failed...
Why is this?
Switch sw = new Switch(MainActivity.this);
sw.setTextOn("start");
sw.setTextOff("close");
sw.setGravity(Gravity.CENTER_HORIZONTAL);
AlertDialog.Builder myDialog = new AlertDialog.Builder(MainActivity.this);
myDialog.setTitle("title");
myDialog.setMessage("message");
myDialog.setView(sw);
Try with below code
Switch sw = new Switch(MainActivity.this);
sw.setTextOn("start");
sw.setTextOff("close");
LinearLayout linearLayout = new LinearLayout(MainActivity.this);
linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
linearLayout.addView(sw);
AlertDialog.Builder myDialog = new AlertDialog.Builder(MainActivity.this);
myDialog.setTitle("title");
myDialog.setMessage("message");
myDialog.setView(linearLayout);
myDialog.show();
Use the custom_layout instead.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Use LayoutInflater to inflate the layout in dialog
View v = LayoutInflater.from(getActivity()).inflate(R.layout.custom_layout, null);
myDialog.setView(v);
Use RelativeLayout instead, set android:centerInParent=true for SwitchButton

Categories

Resources