image always placed in top of dialog - java

I am displaying image in the dialog box which contains the zoom view as well. Here image is placed and zoom is working fine without any issue but problem is image is always placed in the top of the dialog and if i scroll down it comes to middle then zoom happens. I dont want like this instead image should display in the middle and i need to do the zoom.
I am using dynamic imageview and for zoom using chrisbanes photoview library. If i remove the library the image is placed in the center.
#Override
public void onClick(View v) {
Dialog builder = new Dialog(listdisplay, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
builder.getWindow().setBackgroundDrawable(
new ColorDrawable(Color.BLACK));
builder.setCanceledOnTouchOutside(false);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialogInterface) {
//nothing;
}
});
ImageView imageView = new ImageView(listdisplay);
Glide.with(finalConvertView.getContext()).load(Limage).fitCenter()
.diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
DisplayMetrics metrics = new DisplayMetrics();
listdisplay.getWindowManager().getDefaultDisplay().getMetrics(metrics);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(metrics.widthPixels,
metrics.heightPixels);
imageView.setLayoutParams(lp);
//imageView.setScaleType(ImageView.ScaleType.CENTER);
p = new PhotoViewAttacher(imageView);
builder.addContentView(imageView, lp);
builder.show();
}
});
i tried with scaletype for imageview as center but image is placed in top of the dialog.

Use Custom dialog for you own dialog design
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="#+id/image"/>/>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/image"
/>
</RelativeLayout>

Related

How to make discrete seek/progress bar in android?

How to make discrete progress/seek bar like stepper in android. Like we see in whatsapp status stories.
Although it is implemented by so many libraries out there, but i need to implement it without using libraries.
I use views in horizontal linear layout and divide it dynamically based on number of sections or number of status stories need to be shown.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/layout_views"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:orientation="horizontal"
android:padding="4dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Click" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
String TAG = "MainActivity";
Button mButton;
int x=0,height,width,numberOfSections;
LinearLayout layoutViews;
ArrayList<View> viewsList =new ArrayList<>();
#SuppressLint("ClickableViewAccessibility")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//findviews
layoutViews = findViewById(R.id.layout_views);
mButton = (Button) findViewById(R.id.button);
//for getting dimensions of screen
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
height = displayMetrics.heightPixels;
width = displayMetrics.widthPixels;
//suppose we have to cut section into 4 parts
numberOfSections = 10;
width -= (16 + 16*numberOfSections); //reducing length of layout by 16dp from left and right and in between 16dp {in between*number of sections)
width /= numberOfSections;
for(int i=0;i<numberOfSections;i++){
View v = new View(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, 10);
params.setMargins(16,0,0,0); //giving 16dp internal margin between two views
v.setLayoutParams(params);
viewsList.add(v); //adding views in array list for changing color on click of button
v.setBackgroundColor(getResources().getColor(colorAccent));
layoutViews.addView(v);
}
//button onclick function
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonClick();
}
});
}
public void buttonClick(){
viewsList.get(x).setBackgroundColor(getResources().getColor(colorPrimaryDark));
x++;
}
}
Voyella! You have made this dynamic story progress bar, although you can add transition animation in views.

How to display persistent dialogue

I'm trying to display a persistent message at the bottom of my application(through out all activities) when there's no internet connection, and the message disappears once there's a network connection
I thought about using and application dialog, which I was able to do, but then I realised the background was not clickable, I need users to still have access to navigate pages/menu
something as seen in the picture below
What I tried with a custom alert dialog
NoInternetDialog.java
public class NoConnectionDialog {
public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.no_internet_connection_dialog);
Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.BOTTOM;
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
text.setText(msg);
// Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
// dialogButton.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View v) {
// dialog.dismiss();
// }
// });
dialog.show();
}
}
the 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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/zxing_viewfinder_laser"
android:elevation="4dp"
android:orientation="vertical"
android:padding="#dimen/_8sdp"
app:elevation="4dp">
<TextView
android:id="#+id/text_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/no_internet_connection"
android:textColor="#color/colorWhite"
android:textSize="#dimen/dimen_16" />
</LinearLayout>
and how I call it
NoConnectionDialog alert = new NoConnectionDialog();
alert.showDialog(this, "No Internet Connection");
What do you guys suggest?
Don't use a Dialog, just add that layout to your Activity's layout and set the visibility based on whether the user has an internet connection. This will avoid all of the issues you'll run into trying to do this with a Dialog. If you have multiple Activitys, you could use a common superclass for them that includes this functionality.

Custom Layout button is not working in an AlertDialog

I've been trying to fix this for a while now including searching online but I just can't find solution. Everyone saying you should add onClickListener and I did but it just does not work. When I click the button nothing happens. No verbose log ever appears. This is all in one class which implements Runnable so it can do stuff in the background and this EditText onClickListener is implemented inside onCreate of a class.
//Setting up EditText onClick Listeners
oof.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Creating custom layout dialog
AlertDialog.Builder builder = new AlertDialog.Builder(context);
final View inflatedView = getLayoutInflater().inflate(R.layout.dialog_box_popup, null);
builder.setView(R.layout.dialog_box_popup);
builder.setTitle("Edit");
final EditText indc = (EditText)inflatedView.findViewById(R.id.weightInDcEtxt);
Button incrementBtn = (Button)inflatedView.findViewById(R.id.incrementBtn);
Button decrementBtn = (Button)inflatedView.findViewById(R.id.decrementBtn);
//Enabling the increment button in a dialog
incrementBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v("increment", "Button clicked: " + MiscVariables.rocket);
}
});
//Displaying dialog
AlertDialog dialog = builder.create();
dialog.show();
}
});
And here is the custom layout for the dialog using ConstraintLayout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<Button
android:id="#+id/decrementBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="-"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/incrementBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="+"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/weightInDcEtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:gravity="center"
android:inputType="numberDecimal"
android:text="#string/editTextFromDialog"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/incrementBtn"
app:layout_constraintStart_toEndOf="#+id/decrementBtn"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayou
From there I have no idea what to do. If anyone can help me out I would appreciate it thanks!
I've found a solution. Here is the code:
//Setting up EditText onClick Listeners
oof.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Creating custom layout dialog
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(R.layout.dialog_box_popup);
builder.setTitle("Edit");
//Enabling the increment button in a dialog
incrementBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v("increment", "Button clicked: " + MiscVariables.rocket);
}
});
//Displaying dialog
AlertDialog dialog = builder.create();
dialog.show();
final EditText indc =
(EditText)inflatedView.findViewById(R.id.weightInDcEtxt);
Button incrementBtn =
(Button)dialog.findViewById(R.id.incrementBtn);
Button decrementBtn =
(Button)dialog.findViewById(R.id.decrementBtn);
}
});
Inflated view was useless here. All had to be accessed by doing dialog.findViewById.
final View inflatedView = View.inflate(getActivity(), R.layout.dialog_box_popup, null);
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setView(inflatedView);
final EditText indc = (EditText) inflatedView.findViewById(R.id.weightInDcEtxt);
Button incrementBtn = (Button) inflatedView.findViewById(R.id.incrementBtn);
Button decrementBtn = (Button) inflatedView.findViewById(R.id.decrementBtn);
//Enabling the increment button in a dialog
incrementBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Hellloo", Toast.LENGTH_SHORT).show();
}
});
alert.show();
Try this, Hope this solution will be work...!!!

Android - Click Button Inside alertDialog

I wonder how to click a Button inside of AlertDialog in Android and this is my code
activity_float_info.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
MainActivity.java
QR_ = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_float_info, null);
MAIN_QR_SCAN = (LinearLayout) findViewById(R.id.MAIN_QR_SCAN);
MAIN_QR_SCAN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new AlertDialog.Builder(MainActivity.this)
.setCancelable(Boolean.TRUE)
.setNegativeButton(getString(R.string.CANCEL), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.setView(R.layout.activity_float_info)
.show();
button = (Button)QR_.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button.setText("TEst");
}
});
}
});
I have inflate the layout..
I think the main problem is on
button = (Button)QR_.findViewById(R.id.button);
try this create a layout for dialog box
<TextView android:id="#+id/dialogtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#android:color/black"
android:text="Please enter the email address you used for the account"
/>
<EditText
android:id="#+id/emailedittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:ems="10"
android:padding="5dp"
android:cursorVisible="true"
android:singleLine="true"
android:background="#android:color/white"
android:textColor="#android:color/black"
android:hint="Enter Mail id"
android:textSize="20dp" >
<requestFocus />
</EditText>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<Button
android:id="#+id/cancelbtn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="CANCEL"/>
<Button
android:id="#+id/okbtn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Ok"/>
</LinearLayout>
Then create a dialog box using this layout and handle button clicks
final Dialog dialog = new Dialog(MainActivity.this);
// Include dialog.xml file
dialog.setContentView(R.layout.forgotpassword);
// Set dialog title
dialog.setTitle("ALERT!!");
// set values for custom dialog components - text, image and button
Button okbtn = (Button) dialog.findViewById(R.id.okbtn);
Button cancelbtn = (Button) dialog.findViewById(R.id.cancelbtn);
final EditText emailedittext = (EditText) dialog.findViewById(R.id.emailedittext);
dialog.show();
dialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
// if decline button is clicked, close the custom dialog
cancelbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
okbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email=emailedittext.getText().toString();
//do something more here
}
});
Refer: https://coderzpassion.com/android-show-alertdialog/
Here is a hack to use AlertDialog:
public class CustomDialog extends AlertDialog {
protected CustomDialog(Context context) {
super(context);
}
}
And then in your Activity:
CustomDialog dialog = new CustomDialog(this);
View view = getLayoutInflater().inflate(R.layout.custom_dialog_layout,null);
dialog.setView(view);
Button button = (Button)view.findViewById(R.id.custom_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(YourActivity.this,"Your message", Toast.LENGTH_LONG).show();
}
});
dialog.show();
And the layout of the dialog (which has nothing in it barring the Button):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/custom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="New Button" />
</RelativeLayout>
And you should be able to see a Toast when you click on the button. Hope this helps.
Dialog is like a popup window to show some options to users(options like accept/decline).
Using class android.app.Dialog to create dialog.
Using dialog.xml file to create custom dialog layout.
Example:
res/layout/dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dp" />
<TextView
android:id="#+id/textDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="#+id/imageDialog"/>
<Button
android:id="#+id/declineButton"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Submit "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/textDialog"
android:layout_toRightOf="#+id/imageDialog"
/>
</RelativeLayout>
Java Code
// Create custom dialog object
final Dialog dialog = new Dialog(CustomDialog.this);
// Include dialog.xml file
dialog.setContentView(R.layout.dialog);
// Set dialog title
dialog.setTitle("Custom Dialog");
// set values for custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.textDialog);
text.setText("Custom dialog Android example.");
ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
image.setImageResource(R.drawable.image0);
dialog.show();
Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});

OnClickListener display ImageView in Full screen in other Intent/Fragment

I am using fragment, I would like to display the imageView I've got in thumbnail on my Fragment in another view or Dialog ? or something for displaying :)
I would like when we tap on the ImageView, the new view displays, and when we tap on Button, that returns on the main Fragment.
I have implemented my onClickListener, and that works but I don't know how to pass data or whatever for displaying ImageView in full screen...
Here is my code for onClickListener :
mImageReport.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (zoomOut) {
zoomOut = false;
} else {
zoomOut = true;
}
}
});
No need to create new window for displaying full screen image. You can use android default gallery image viewer. Just set full image path or image URI to "mImageReport" as a tag, inside its onClick you can retrieve it. Use following line of code to display full screen image as
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/external/images/media/16"))); /** replace with your own uri */
you can set full screen dialog to show image like below
final Dialog nagDialog = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
nagDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
nagDialog.setCancelable(false);
nagDialog.setContentView(R.layout.preview_image);
Button btnClose = (Button)nagDialog.findViewById(R.id.btnIvClose);
ImageView ivPreview = (ImageView)nagDialog.findViewById(R.id.iv_preview_image);
ivPreview.setBackgroundDrawable(dd);
btnClose.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
nagDialog.dismiss();
}
});
nagDialog.show();
xml code for layout containing image view and close button only:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/iv_preview_image" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/close"
android:id="#+id/btnIvClose" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />

Categories