Determinate ProgressDialog in Android - android

I want to have a ProgressDialog with determinate progress bar instead of default spinner. It seems to be easy: ProgressDialog has method setIndeterminate, and method show accepts a boolean to indicate indeterminateness. But these way don't work for me! The dialog is still indeterminate with a spinner. How to change the behaviour?

You need call:
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
Check out the official dev guide Showing a progress bar with the example source code attached to that section.

Oh Sorry.. Try this
1) Create a layout file with below code
<?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="wrap_content" android:id="#+id/RelativeLayout_DownloadProgress" android:paddingLeft="20dp" android:paddingBottom="20dp" android:paddingRight="10dp" android:paddingTop="20dp">
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/downloading_video_tv" android:text="#string/downloading_video" android:textStyle="bold" android:textColor="#color/COLOR_WHITE"></TextView>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/download_progress_tv" android:text="#string/zero_percent" android:layout_below="#+id/downloading_video_tv" android:textColor="#color/COLOR_WHITE"></TextView>
<Button
android:id="#+id/download_cancel_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/downloading_video_tv"
android:text="#string/Cancel"
android:textColor="#color/COLOR_CANCEL_BTN" android:layout_marginLeft="5dp">
</Button>
<ProgressBar
android:id="#+id/download_progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="13dp"
android:layout_below="#id/download_progress_tv"
android:layout_toLeftOf="#id/download_cancel_btn"
android:indeterminateOnly="false"
android:max="100"
android:progressDrawable="#drawable/dwnld_progress" >
</ProgressBar>
</RelativeLayout>
2) In your code do the following
i) in oncreate() method add the following lines..
//Download progress layout inflate
RelativeLayout relativelayout_DownloadProgress = (RelativeLayout)_inflater.inflate(R.layout.downloadprogress, null);
TextView downloadProgress_PercentageTV = (TextView)relativelayout_DownloadProgress.findViewById(R.id.download_progress_tv);
Button downloadProgress_CancelBtn = (Button)relativelayout_DownloadProgress.findViewById(R.id.download_cancel_btn);
ProgressBar download_progressBar = (ProgressBar)relativelayout_DownloadProgress.findViewById(R.id.download_progressbar);
downloadProgress_CancelBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
dismissDialog(0);
}
});
ii) //Creating Dialogs i.e., below onCreate() method
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new AlertDialog.Builder(_activity)
.setView(relativelayout_DownloadProgress)
.create();
}//switch end
return null;
}//onCreateDialog()
iii) on any click event add the following lines.
showDialog(0);download_progressBar.setProgress(0);
Sorry i know its too long code but its working code in application...Try it might need modifications like variables must be declared in class scope.

ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Registering. Please wait...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
Try this code should work fine...

Related

How to set the layout and size of a PopupWindow

I'm writing my first program from android, and having difficulty getting a PopupWindow to be positioned and sized as I intend. As it currently is the popup will be activated after clicking a button on the menu bar. After clicking I was hoping to have the popup display centralised, however currently when clicked the result is the picture below (can't post image due to <10 reputation):
https://www.box.com/s/7d4qk8tqlvhiog7576mc
Java Popup Method and Listener:
public void showPopup(View add){
PopupWindow popup = new PopupWindow(this);
setContentView(R.layout.add);
popup.setBackgroundDrawable(null);
popup.showAtLocation(add, Gravity.CENTER,0,0);
popup.setFocusable(true);
popup.setOutsideTouchable(true);
View hideAdd = findViewById(R.id.add_task);
hideAdd.setVisibility(View.INVISIBLE);
}
public boolean onOptionsItemSelected(MenuItem menuItem){
switch(menuItem.getItemId()){
case R.id.add_task:
View addTaskView = findViewById(R.id.add_task);
showPopup(addTaskView);
return true;
default:
return false;
}
}
}
Add Layout Xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/title_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/task_title"
android:textSize="25sp"
android:textColor="#android:color/holo_blue_bright"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/holo_blue_bright"/>
<EditText android:id="#+id/task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/task_prompt">
</EditText>
</LinearLayout>
Any help will be greatly appreciated.
There is something called a custom dialog... For example.:
You can design a layout and set it to a dialog..
Dialog dialog=new Dialog();
dialog.setContentView(the layout u designed here);
Convert an activity into dialog.. check my answer to this post..
.. So there is endless possibilities with the dialogs..
Try to change all "android:layout_width="match_parent"" in your xml to "android:layout_width="wrap_content""
I think it's because you set the visibility of the view underneeth to invisible. try commenting that and running again.

How to add layouts to dialog

I have an idea in mind but im not sure about how to implement it
first i have a dialog
final Dialog dialog = new Dialog(mContext);
i also have a layout
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textViewDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textViewWhen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
what i want is to add this layout in the dialog, i may also want to add more of the same layout right under it inside that dialog
how can i do that?
for example how can i add two of this layout in one dialog?
something like
Dialog Title
Large Text
Small Text
Medium Text
Large Text
Small Text
Medium Text
Something like this:
LayoutInflater li = LayoutInflater.from(SomeActivity.this);
someLayout = (LinearLayout)li.inflate(R.layout.some_layout, null);
alert = new AlertDialog.Builder(SettingsActivity.this);
alert.setView(someLayout);
This is an example from my application:
public class ConfirmDialog extends DialogFragment {
public static String TAG = "Confirm Dialog";
public interface ConfirmDialogCompliant {
public void doOkConfirmClick();
public void doCancelConfirmClick();
}
private ConfirmDialogCompliant caller;
private String message;
public ConfirmDialog(ConfirmDialogCompliant caller, String message){
super();
this.caller = caller;
this.message = message;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.confirm_dialog, container, false);
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
((TextView) view.findViewById(R.id.textview_confirm)).setText(message);
((Button) view.findViewById(R.id.ok_confirm_button)).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
caller.doOkConfirmClick();
}
});
((Button) view.findViewById(R.id.cancel_confirm_button)).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
caller.doCancelConfirmClick();
}
});
return view;
}
}
where the inflated layout is confirm_dialog.xml.
You inflate your layout in the onCreateView method.
In this case I used DialogFragment (which I suggest you to use...see the support library so that you don't have to worry about your target SDK) but the same applies to Dialog.
Hope it helps you!
You can check this documentation page which explain how to add a custom layout on dialog
http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
The key is the setContentView method:
dialog.setContentView(R.layout.custom_dialog);
Check out this one :
how to get customized alert dialog , like the one shown in image?
Refer the answer which I've given (Aamir Shah)
Use a DialogFragment, which allows you to, just like any other Fragment, completely customize the layout. It is available in the v4 support library.
http://developer.android.com/reference/android/app/DialogFragment.html
http://developer.android.com/tools/extras/support-library.html

Dialog box not renderd properly in android

i have a dialog box, but some how there is an unknown background image. How can i remove that image. Please guide me.
You have to extends Dialog Class, build your xml File for your dialog something like
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Do you Want to bookmark?"
android:gravity="center"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
<Button
android:id="#+id/button_yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes" />
</LinearLayout>
</LinearLayout>
of course you can make some customization
for your custom dialog class you can do like this
public class CustomizeDialog extends Dialog implements OnClickListener {
Button okButton;
public CustomizeDialog(Context context) {
super(context);
/** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
yesButton = (Button) findViewById(R.id.button_yes);
yesButton.setOnClickListener(this);
noButton = (Button) findViewById(R.id.button_no);
noButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button_yes:
dismiss();
//doSomething
break;
case R.id.button_no:
dismiss();
//doSomethingElse
break;
}
}
Hope this will help you
you have to post your code to figure out why these background box appear for you but acting like i mention should resolve the problem for you
That is probably happening because you use the standard AlertDialog and set a content view + no title(although you don't set a title the space for it will remain in the dialog).
Extend the Dialog class instead and build your dialog like you want. Also if you want to use your own background for the Dialog then implement a theme that extends Theme.Dialog and override:
<item name="android:windowBackground">#android:drawable/panel_background</item>
with your own drawable.

Remove unnecessary margins from a custom dialog

I have a problem that I am designing a custom dialog for this. I am creating a xml for this as Framelayout is the root layout, and another framelayout with a gray background image is used for the contents, in which I have added a textview and two buttons Ok and Cancel and use all of this through dialog.setContentView(desired Xml Resource);
But when I generate that particular dialog then it shows extra spaces from each side, or we can say that extra margins are there but I don't know how it will removed? Please review the image attached with this question and suggest me the right solution.
Xml Layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_height="wrap_content" android:layout_width="wrap_content">
<FrameLayout android:id="#+id/rel"
android:layout_gravity="center_vertical" android:background="#drawable/dialog_box_bg" android:layout_width="wrap_content" android:layout_height="189dp">
<TextView android:id="#+id/tv_LogoutDialog_Text"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="#424242"
android:text="Are you sure want to logout?" android:textSize="20dip" android:layout_gravity="center_vertical|center_horizontal"></TextView>
<Button android:id="#+id/btn_LogoutDialog_Cancel" android:background="#drawable/dialog_cancel_btn"
android:layout_marginLeft="20dip" android:layout_width="120dip" android:layout_height="42dip" android:layout_gravity="bottom|left" android:layout_marginBottom="15dip"></Button>
<Button android:id="#+id/btn_LogoutDialog_Ok"
android:background="#drawable/dialog_ok_btn_hover"
android:layout_width="120dip"
android:layout_height="42dip" android:layout_marginLeft="180dip" android:layout_gravity="bottom|right" android:layout_marginBottom="15dip" android:layout_marginRight="20dip"></Button>
</FrameLayout>
</FrameLayout>
Code:
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
dialog = new Dialog(HomeScreenActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.logoutdialog);
btn_cancel = (Button)dialog.findViewById(R.id.btn_LogoutDialog_Cancel);
btn_ok = (Button)dialog.findViewById(R.id.btn_LogoutDialog_Ok);
btn_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
dismissDialog(0);
}
});
btn_logout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(HomeScreenActivity.this,LoginScreen.class);
startActivity(intent);
}
});
return dialog;
}
return null;
}
Thanks in advance.
Don't use
dialog.setContentView(R.layout.logoutdialog);
use
LayoutInflater class to set Dialog content view
Here is link (Check) you can get the idea May this helps you.
Change it by adding a parameter false like in the code below.
dialog.customView(R.layout.dialog_blueprint, false)
The second argument (false) is wrapTheDialogBoxInScrollView. If the content in the dialog box is small and does not require a ScrollView set it to false.

timed modeless dialog

Is there any way to show a modeless dialog--a dialog that allows the user to interact with whatever was on the screen before the dialog but also allows the user to interact with the dialog if pressed?
I know of Toasts, but they don't allow interaction with the popup.
I know of Dialogs, but they're modal and don't allow interaction with the background.
I know of Notifications, but I want something that is visibile on screen.
I basically want to be able to be playing a game or something and a popup appears that I have a new email or something. I can click it to view my email, but I can wait for it to go away if I just want to continue playing my game. Is this possible in Android?
Yes, create an Activity with style Theme.Dialog. This is a normal activity which looks like a dialog, while being modeless and accepting events.
An example:
<activity android:name=".activity.dialog.PhotoDialog"
android:label="#string/photo_dialog_title"
android:theme="#android:style/Theme.Dialog"/>
Edited:
Indeed Theme.Dialog blurs the underlying activity and makes it unaccessible. I had a similar requirement here I had to show upload progress dialog with text and cancel button. The main catch is in setting WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL and resetting WindowManager.LayoutParams.FLAG_DIM_BEHIND.
Created a Dialog with custom content:
if (progressDialog == null) {
progressDialog = new Dialog(activityRequestingProgressDialog);
progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
progressDialog.setContentView(R.layout.progress_upload);
progressBar = (ProgressBar) progressDialog.findViewById(R.id.progressBar);
progressText = (TextView) progressDialog.findViewById(R.id.progressText);
progressText.setText("0 %");
progressText.setTextSize(18);
Button buttonCancel = (Button) progressDialog.findViewById(R.id.btnCancel);
buttonCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
cancelProgressDialog();
stopUpload("Upload cancelled.");
}
});
Window window = progressDialog.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.setGravity(Gravity.BOTTOM);
progressDialog.show();
}
progressText.setText(text);
progressBar.setProgress(percent);
And this is the layout for this Dialog:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/progressDialog"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="18sp"
android:padding="10dp"
android:text="#string/progress_title"/>
<LinearLayout android:id="#+id/progressDialog"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_centerVertical="true">
<ProgressBar android:id="#+id/progressBar"
android:layout_width="150dp"
android:layout_height="34dp"
android:paddingRight="10dp"
android:max="100"
android:progress="0"
android:fadingEdge="vertical"
style="?android:attr/progressBarStyleHorizontal"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/progressText"
android:paddingRight="10dp"/>
<Button android:layout_height="40dp"
android:layout_width="80dp"
android:id="#+id/btnCancel"
android:text="#string/dialog_cancel"/>
</LinearLayout>
</LinearLayout>
just add FLAG_NOT_TOUCH_MODAL flag to your dialog
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
My implementation which was a little more hackish but also allows button presses to be caught by background window
_wm = this.getWindowManager();
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
_view = layoutInflater.inflate(R.layout.notification, null);
_params = new WindowManager.LayoutParams();
_params.height = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
_params.width = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
_params.flags =
// this is to keep button presses going to the background window
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
// this is to enable the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
// background transparant
_params.format = PixelFormat.TRANSLUCENT;
_gravity = Gravity.TOP;//Gravity.BOTTOM;
_wm.addView(_view, _params);

Categories

Resources