ANDROID Progress Dialog when Loading Image on Dialog/Popup - java

I have this working function that load image from url. The only thing is that it shows no progress while loading the image just the screen become dimmer and after some time only the picture is shown. This makes the user think like it is not working. So how do I add progress dialog while the image is loading or any way that shows the image is loading?
public void showImage() {
Dialog builder = new Dialog(this);
builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
builder.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialogInterface) {
//nothing;
}
});
ImageView imageView = new ImageView(this);
Picasso.with(this).load("http://camranger.com/wp-content/uploads/2014/10/Android-Icon.png").into(imageView);
builder.addContentView(imageView, new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
builder.show();
}

try adding PlaceHolder
add this to layout folder
<RelativeLayout
android:id="#+id/hoteldetails_myContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ProgressBar
android:id="#+id/hoteldetails_progressBar"
style="?android:attr/progressBarStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:visibility="gone"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp" />
<ImageView
android:id="#+id/hoteldetails_imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"/>
</RelativeLayout>
and this to loading code
Picasso.with(this)
.load(hoteImageStr)
.error(R.drawable.loading)
.into(img, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
if (progress != null) {
progress .setVisibility(View.GONE);
}
}
#Override
public void onError() {
}
});

public void showImage() {
Dialog builder = new Dialog(this);
builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
builder.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialogInterface) {
//nothing;
}
});
final ProgressDialog progressDialog = new ProgressDialog(this);
ImageView imageView = new ImageView(this);
Picasso.with(this)
.load("http://camranger.com/wp-content/uploads/2014/10/Android-Icon.png")
.into(imageView, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
// The image has loaded you can make the progress bar invisible
if (progressDialog.isShowing())
progressDialog.dismiss();
}
#Override
public void onError() {
// Show some error message
if (progressDialog.isShowing())
progressDialog.dismiss();
}
});
builder.addContentView(imageView, new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
builder.show();
progressDialog.setMessage("please wait");
progressDialog.setCancelable(false);
progressDialog.show();
}

Related

Add a dynamic button to the view and save it permanently

I have a fragment with this view:
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:tag="general"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#343535"
android:orientation="vertical"
tools:context=".fragments.GeneralFragment">
<Button
android:id="#+id/hello"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:onClick="onClick"
android:text="#string/hello" />
<Button
android:id="#+id/observed"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:onClick="onClick"
android:text="#string/observed" />
<Button
android:id="#+id/thanks"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:onClick="onClick"
android:text="#string/thanks" />
</LinearLayout>
There are 3 buttons. Whenever you click on one of them, its text will be displayed.
Now I would like to add another button but dynamically. It should be added before #+id/hello.
I have tried it with
LinearLayout root = (LinearLayout) view.findViewById(R.id.root);
root.addView();
but it looks completely wrong since some parameters are missing.
For instance, the new button should be like:
XML:
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:onClick="onClick"
android:text="I am a dynamic text" />
This button should be saved permanently in the app. How can I do this?
Update
I am using a dialog, so this is the class:
#SuppressLint("ValidFragment")
public class Dialog extends DialogFragment {
private final int _layout;
private TextInputEditText _customTextField;
#SuppressLint("ValidFragment")
public Dialog(int layout) {
_layout = layout;
}
public interface ICustomTts {
void customTts(String input, Activity activity);
}
public ICustomTts iCustomTts;
public interface ITarget {
void getTarget(String input);
}
public ITarget iTarget;
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(_layout, container, false);
// Display fragment_custom
if (_layout == R.layout.fragment_custom) {
_customTextField = view.findViewById(R.id.customTextField);
Button _customBtn = view.findViewById(R.id.customCta);
_customBtn.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: Clicked on CTA of custom");
String input = _customTextField.getText().toString();
if (!input.equals("")) {
iCustomTts.customTts(input, getActivity());
_dismiss();
}
}
});
MaterialCheckBox customeSave = view.findViewById(R.id.customSave);
customeSave.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public void onClick(View v) {
Log.d(TAG, "customeSave was clicked!");
LinearLayout root = view.findViewById(R.id.root);
Button button = new Button(getContext());
float heightInPixel = 60 * getResources().getDisplayMetrics().density;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) heightInPixel);
params.gravity = Gravity.CENTER;
button.setText("I am a dynamic text");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do the stuff on the click
}
});
root.addView(button, 0);
}
});
}
// Display fragment_target
if (_layout == R.layout.fragment_target) {
_customTextField = view.findViewById(R.id.targetTextField);
Button _customBtn = view.findViewById(R.id.targetCta);
_customBtn.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: Clicked on CTA of target");
String input = _customTextField.getText().toString();
if (!input.equals("")) {
iTarget.getTarget(input);
_dismiss();
}
}
});
}
return view;
}
/**
* Dismissing the dialog
*/
private void _dismiss() {
this.dismiss();
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
if (_layout == R.layout.fragment_custom) {
iCustomTts = (ICustomTts) getActivity();
}
else if (_layout == R.layout.fragment_target) {
iTarget = (ITarget) getActivity();
}
}
catch (ClassCastException e) {
Log.e(TAG, "onAttach: ClassCastException: " + e.getMessage());
}
}
}
You need to use the two-argument version of addView() to determine the order (index) of the button inside the LinearLayout:
LinearLayout root = view.findViewById(R.id.root);
Button button = new Button(requireContext());
float heightInPixel = 60 * getResources().getDisplayMetrics().density;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) heightInPixel);
params.gravity = Gravity.CENTER;
button.setText("I am a dynamic text");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do the stuff on the click
}
});
root.addView(button, 0);

Buttons disappear from DialogFragment when the displayed list requires scrolling

AlertDialog displays the content and buttons perfectly until the contents grow to the point that scrolling is required to display it all. At that point the buttons disappear, even though the list shows in its entirety.
I see that in years gone by this question was asked (this one is essentially identical to mine (DialogFragment buttons pushed off screen API 24 and above), but the answers all seemed hit or miss, haphazard even: setting layout_weight, switching from setMessage() to setTitle(), and finally switching away from AlertDialog to Dialog.
None of these are working form me, and avoiding AlertDialog is NOT an appealing option because I am using the setMultiChoiceItems() method that seems (incredibly to me, perhaps given my extremely limited knowledge) available only to AlertDialog.
Thus in desperation and frustration I appeal directly to the collective brains of stackoverflow.
This is a pared down version of my DialogFragment:
public class SurfaceDialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = requireActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.surface_dialog, null))
.setTitle(mMessage)
.setMultiChoiceItems(mSurfaceList, mCheckedSurfaces, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
mCheckedSurfacesPositions.add(which);
} else {
mCheckedSurfacesPositions.remove(mCheckedSurfacesPositions.indexOf(which));
}
}
});
// builder.setCancelable(false);
builder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
ProcessDialogSelections(mCheckedSurfacesPositions);
dismiss();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
listener.onSurfaceDialogSelectedSent(mUnitSurfaces);
dismiss();
}
});
return builder.create();
}
That not working, I then coded my own buttons and displayed them:
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = requireActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.surface_dialog, null);
builder.setView(dialogView)
.setTitle(mMessage)
.setMultiChoiceItems(mSurfaceList, mCheckedSurfaces, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
mCheckedSurfacesPositions.add(which);
} else {
mCheckedSurfacesPositions.remove(mCheckedSurfacesPositions.indexOf(which));
}
}
});
Button positiveButton = dialogView.findViewById(R.id.positive_button);
Button negativeButton = dialogView.findViewById(R.id.negative_button);
positiveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ProcessDialogSelections(mCheckedSurfacesPositions);
dismiss();
}
});
negativeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onSurfaceDialogSelectedSent(mUnitSurfaces);
dismiss();
}
});
return builder.create();
}
and here is my revised XML file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ScrollView 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="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
android:orientation="vertical">
</ScrollView>
<Button
android:id="#+id/positive_button"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_width="wrap_content"
android:text="Done"
app:layout_constraintBaseline_toBaselineOf="#+id/negative_button"
app:layout_constraintEnd_toStartOf="#+id/negative_button"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/negative_button"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_width="wrap_content"
android:text="Cancel"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/positive_button" />
</androidx.constraintlayout.widget.ConstraintLayout>
And with THAT not working, I then followed the advice of Vikas Singh (AlertDialog Buttons not visible when using Custom layout). This is what the new code looks like (essentially creating the layout in Java rather than XML.
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// New code from Vikas Singh
LinearLayout rootLayout = new LinearLayout(getActivity());
rootLayout.setOrientation(LinearLayout.VERTICAL);
ScrollView scrollView = new ScrollView(getActivity());
LinearLayout layout = new LinearLayout(getActivity());
layout.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(layout);
rootLayout.addView(scrollView);
builder.setNegativeButton("Cancel", null);
builder.setPositiveButton("Go", null);
builder.setView(rootLayout);
builder//.setView(dialogView)
//.setTitle(mMessage)
.setMultiChoiceItems(mSurfaceList, mCheckedSurfaces, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
mCheckedSurfacesPositions.add(which);
} else {
mCheckedSurfacesPositions.remove(mCheckedSurfacesPositions.indexOf(which));
}
}
});
return builder.create();
}
Still no buttons when I have to scroll.
Why not and what can I do about it?
Here is the solution:
public class SurfaceDialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//LayoutInflater inflater = requireActivity().getLayoutInflater();
builder
//.setView(inflater.inflate(R.layout.surface_dialog, null)) didn't work
// nor did setView with android.R.layout.select_dialog_multichoice
// nor android.R.layout.simple_list_item_multiple_choice work
.setTitle(mMessage)
.setMultiChoiceItems(mSurfaceList, mCheckedSurfaces, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
mCheckedSurfacesPositions.add(which);
} else {
mCheckedSurfacesPositions.remove(mCheckedSurfacesPositions.indexOf(which));
}
}
});
// builder.setCancelable(false);
builder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
ProcessDialogSelections(mCheckedSurfacesPositions);
dismiss();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
listener.onSurfaceDialogSelectedSent(mUnitSurfaces);
dismiss();
}
});
return builder.create();
}
The multiChoiceItems DialogFragment now works like a charm!
All I had to do was simply comment out the above lines.
As a result, there is no layout explicitly inflated, no view set.
But it works nonetheless.
Now I would love it if someone could explain to me
the details of why this works,
why the other did not, and
in light of the earlier failure, how could a custom layout be implemented to achieve this result of not losing the action buttons when the view scrolled

how can i make a confirm dialog?

I have created a todo app in android studio. when you click on the list of task item, it deletes itself. but i want to show a confirm dialog before that.i am using two java classes. MainActivity and Filehelper for this todo app.
so i used this code to delete the item and it was working:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
items.remove(position);
adapter.notifyDataSetChanged();
Toast.makeText(this, "Deleted", Toast.LENGTH_SHORT).show();
}
And now i am using this code with confirm dialog :
#Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Confirm dialog demo !");
builder.setMessage("You are about to delete all records of database. Do you really want to proceed ?");
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface parent, int position) {
items.remove(position);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You've changed your mind to delete tasks", Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
but when i run it on my android . i see the dialog. but when i press yes it crash itself. so can you give me the right code. and also tell me the problem of my code?.
MainActivity Code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
private EditText ItemET;
private Button btn;
private ListView itemList;
private ArrayList<String> items;
private ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ItemET = findViewById(R.id.item_edit_text);
btn = findViewById(R.id.add_btn);
itemList = findViewById(R.id.item_list);
items = FileHelper.readData(this);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
itemList.setAdapter(adapter);
btn.setOnClickListener(this);
itemList.setOnItemClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add_btn:
String ItemEntered = ItemET.getText().toString();
if (ItemEntered.trim().isEmpty()) {
Toast.makeText(getApplicationContext(), "Please Enter some detail", Toast.LENGTH_LONG).show();
} else {
adapter.add(ItemEntered);
ItemET.setText("");
FileHelper.writeData(items, this);
Toast.makeText(this, "item Added", Toast.LENGTH_SHORT).show();
}
break;
}
}
// Confirm box
#Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Confirm dialog demo !");
builder.setMessage("You are about to delete all records of database. Do you really want to proceed ?");
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface parent, int position) {
items.remove(position);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You've changed your mind to delete all records", Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
}
FileHelper Code :
public class FileHelper {
public static final String FILENAME = "listinfo.dat";
public static void writeData(ArrayList<String> items, Context context){
try {
FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(items);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static ArrayList<String> readData(Context context) {
ArrayList<String> itemList = null;
try {
FileInputStream fis = context.openFileInput(FILENAME);
ObjectInputStream ois = new ObjectInputStream(fis);
itemList = (ArrayList<String>) ois.readObject();
} catch (FileNotFoundException e) {
itemList = new ArrayList<>();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return itemList;
}
}
here is also my MainActivity.xml
<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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="12dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="12dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/item_edit_text"
android:hint="Enter Item"
android:layout_width="0dp"
android:layout_weight="1.9"
android:layout_marginRight="20dp"
android:layout_height="wrap_content" />
<Button
android:background="#drawable/layout_bg"
android:id="#+id/add_btn"
android:text="add"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</LinearLayout>
<ListView
android:background="#drawable/layout_bg"
android:id="#+id/item_list"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textAlignment="center"
android:layout_marginBottom="24dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
You can create a method and call it whenever you need it
void confirmDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Discard Data!");// Dialog header
builder.setMessage("Are you sure you want to discard this data?"); //disclaimer text
// yes button
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
edit_text.getText().clear(); //clear EditText
}
});
//No button
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.create().show();
}
Send the logcat picture please. Then it will be easier to identify the problem. Also check
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface parent, int position) {
Toast.makeText(getApplicationContext(), "Check if toast is working or crush. ", Toast.LENGTH_SHORT).show();
}
});
Check toast working when "Yes" button pressed or crushed. Then you will be more sure about the problem.

Trying to make ImageView full size in Dialog doesn't work

I'm using Fragments, I would like to get full size image mImageReport when I tap on ImageView on my fragment. My second layout is test.xml (for Dialog).
I'm getting image from URL using Picasso
But the problem is my image doesn't appear on the new dialog (mImageReport). I don't know how to link the imageView from test.xml with the image I want in full screen in my fragment...
Here is my code.
Reports.java :
public class Reports extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (container == null) {
return null;
}
View view = inflater.inflate(R.layout.reports, container, false);
ButterKnife.inject(this, view);
final Dialog nagDialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
nagDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
nagDialog.setCancelable(false);
nagDialog.setContentView(R.layout.test);
Button btnClose = (Button)nagDialog.findViewById(R.id.btnIvClose);
ImageView ivPreview = (ImageView)nagDialog.findViewById(R.id.iv_preview_image);
// Loading image from url in ImageView ... HERE IS THE PROBLEM
Picasso.with(mImageReport.getContext()).load(mCurrentReportString.getUrlImages()).into(ivPreview);
mImageReport.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nagDialog.show();
}
});
btnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
nagDialog.dismiss();
}
});
return view;
}
test.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/iv_preview_image" />
<Button android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#drawable/fileclose"
android:id="#+id/btnIvClose" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
Call this method from fragment onCreateView or according to your requirements...
private void showImage() {
final Dialog nagDialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
nagDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
nagDialog.setCancelable(false);
nagDialog.setContentView(R.layout.test);
Button btnClose = (Button)nagDialog.findViewById(R.id.btnIvClose);
ImageView ivPreview = (ImageView)nagDialog.findViewById(R.id.iv_preview_image);
// Loading image from url in ImageView ... HERE IS THE PROBLEM
Picasso.with(getActivity()).load(mCurrentReportString.getUrlImages()).into(ivPreview);
btnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
nagDialog.dismiss();
}
});
nagDialog.show();
}

why progress bar fit the screen width ?

I wrote below method to show progress bar but the progress bar rounded by white box fits the screen width and i do not know why.
public void showProgressBar() {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
ProgressBar pbar = new ProgressBar(activity, null,
android.R.attr.progressBarStyleLargeInverse);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30, 50);
pbar.setLayoutParams(params);
builder.setView(pbar);
builder.setCancelable(true);
progressBar = builder.create();
progressBar.show();
}
when execute the above method the progress shown as this image.
progress bar problem
How i can solve this issue?
You should use a ProgressDialog and no, it doesn't fit the screen width.
ProgressDialog pd=new ProgressDialog(activity);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setIndeterminate(true);
pd.setCancelable(false);
pd.show();
This should give you what you want.
Do this:
ProgressDialog pd;//declare as a global variable
public void showProgressBar() {
pd=new ProgressDialog(activity);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setIndeterminate(true);
pd.setCancelable(false);
pd.show();
}
Dismiss the ProgressDialog wherever you want later.
I solved it by stupid way but it works fine at me.
public abstract class AbstractScreen implements IActivity {
protected Response response;
protected FragmentActivity activity;
private CustomProgressDialog progress;
public AbstractScreen(FragmentActivity activity) {
this.activity = activity;
}
public abstract void loadScreen();
#Override
public void preExecution() {
// TODO Auto-generated method stub
}
#Override
public void postExecution(Response response) {
// TODO Auto-generated method stub
}
public void showProgressBar() {
progress = new CustomProgressDialog(activity);
progress.show();
}
public void closeProgress() {
progress.dismiss();
}
public FragmentActivity getActivity() {
return activity;
}
private class CustomProgressDialog extends Dialog {
private Context context;
public CustomProgressDialog(Context context) {
super(context);
this.context = context;
loadScreen();
}
public void loadScreen() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.progress);
setCancelable(false);
}
}
}
add this layout file also.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_marginRight="-7dp"
>
<ProgressBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleLarge"
android:id="#+id/progressbar"
android:layout_gravity="center_horizontal"
android:max="100"
/>
</LinearLayout>

Categories

Resources