How to have a One Common ProgressBar in Android Project - java

I'm currently trying to sort out a way to have a single Common ProgressBar to everywhere(Activity/Fragment) I need in my Android Project. The methods I tried did not promise me good results. Please help me to find a better solution. An example with code for android java is more than welcome.
This is my Custom ProgressBar
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fl_common_probar"
android:elevation="40dp"
android:visibility="gone"
android:layout_centerInParent="true">
<View
android:id="#+id/background_dim_pro"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.5"
android:background="#color/textTitleW"
android:elevation="50dp"/>
<RelativeLayout
android:id="#+id/relative_container_pro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:elevation="60dp">
<ImageView
android:id="#+id/image_test"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/logo_loading" />
<ProgressBar
android:id="#+id/progress_bar_circle_pro"
style="?android:attr/progressBarStyle"
android:layout_width="132dp"
android:layout_height="132dp"
android:indeterminateDrawable="#drawable/circular_progress_bar"
android:indeterminateDuration="#android:integer/config_longAnimTime" />
</RelativeLayout>

Please try with below approach.
1.) Create a layout xml file (dialog_progress.xml) with the below content.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_progress"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
2.) Create AppUtil Java class and create a static method as below.
public class AppUtil {
// progress bar handling
public static Dialog showProgress(Activity activity) {
Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(0));
dialog.setContentView(R.layout.dialog_progress);
ProgressBar progressBar = dialog.findViewById(R.id.progressBar);
progressBar.getIndeterminateDrawable().setColorFilter(activity.getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.MULTIPLY);
dialog.setCancelable(false);
dialog.show();
return dialog;
}
}
3.) When you need to show a ProgressBar in an Activity/Fragment do as below.
Dialog dialog = AppUtil.showProgress(MainActivity.this);
When you need to dissmiss the progressBar
dialog.dismiss();
That's it. Try and let me know your feedback.
Thank you.

Create a Java class Seperately for Progress Dialog
public class ProgressDialog {
private Dialog dialog;
private ProgressBar progressBar;
public ProgressDialog(Context context) {
dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.progress_layout);
dialog.setCancelable(false);
if (dialog.getWindow() != null) {
dialog.getWindow()
.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
progressBar = dialog.findViewById(R.id.progressBar);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
progressBar.getIndeterminateDrawable().setColorFilter(randomColor(), PorterDuff.Mode.MULTIPLY);
new Handler().postDelayed(this, 300);
}
}, 0);
}
public void showDialog() {
if (dialog != null && !dialog.isShowing())
dialog.show();
}
public void hideDialog() {
if (dialog != null && dialog.isShowing())
dialog.cancel();
}}
//Now you can use it in anywhere in you activity/fragment
Activity:
public class ShowProgressBar extends AppCompatActivity{
private ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_web_pages);
progressDialog= new ProgressDialog (ShowProgressBar.this);
}
//Use these methods in your code where you want to show the progress dialog or hide the dialog.
// showing animated dialog
public void showDialog(){
progressDialog.showDialog();
}
// hide animated dialog
public void hideDialog(){
progressDialog.hideDialog();
}

Related

Empty Exposed Dropdown menu when placed inside a custom dialog layout

I'm trying to implement an Exposed Dropdown menu on an AlertDialog with a custom layout defined in dialog_main_createremote.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="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_marginTop="10dp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/dialog_main_textinput"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="Category">
<AutoCompleteTextView
android:id="#+id/dialog_main_dropdown"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:inputType="none"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
As per Material Design Guidelines, items need a layout for the dropdown menu to appear, so I created the following in the dropdown_item.xml file:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="?attr/textAppearanceSubtitle1" />
Finally, I started to generate the AlertDialog with a MaterialAlertDialogBuilder, then defined and instantiated a LayoutInflater which I used to get dialog's custom view and get access to the setAdapter method, but after setting the adapter and showing the dialog all I can get is an Outlined TextInputLayout without any dropdown menu.
So that's it, thanks in advance for any suggestions/solutions, also, as you may have noticed, I'm new to android development, so, if possible and of course, if you have time in your hands, elaborate your answer or point to a specific resource where I could get more info.
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private RemoteListViewModel remoteListViewModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.main_recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
final RemoteAdapter adapter = new RemoteAdapter();
recyclerView.setAdapter(adapter);
remoteListViewModel = new ViewModelProvider(this).get(RemoteListViewModel.class);
remoteListViewModel.getAllRemotes().observe(this, new Observer<List<Remote>>() {
#Override
public void onChanged(#Nullable List<Remote> remotes) {
adapter.setRemotes(remotes);
}
});
ExtendedFloatingActionButton createButton = findViewById(R.id.create_efab);
createButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(MainActivity.this);
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
final View inflatedView = inflater.inflate(R.layout.dialog_main_createremote, null);
AutoCompleteTextView actv = inflatedView.findViewById(R.id.dialog_main_dropdown);
String categories[] = getResources().getStringArray(R.array.categories);
actv.setAdapter(new ArrayAdapter(MainActivity.this, R.layout.dropdown_item, categories));
builder.setView(R.layout.dialog_main_createremote);
builder.setTitle("Set control params");
builder.setNegativeButton("Nope", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setPositiveButton("Okey", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
}
});
}
}

Android start Activity 2 but rest at MainActivity

I have MainActivity which has got a button that opens Activity 2. Everything from Activity 2 should be calculated and be run but the user should rest at MainActivity? How do I do this?
I found a solution where I dont run the "setContentView()" but then my app crashes.
You want to change the view of application but does not want the user to change activity.
Use two different layout in xml file with id :
<?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">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/first_view">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:id="#+id/button"
android:background="#358a32" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/second_view"
android:visibility="gone">
</LinearLayout>
<LinearLayout>
Then onclick of button :
public class MainActivity extends Activity {
boolean firstViewOff = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
LinearLayout first_view = (LinearLayout) findViewById(R.id.first_view);
LinearLayout second_view = (LinearLayout) findViewById(R.id.second_view);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
firstViewOff = true;
first_view.setVisibility(View.GONE);
second_view.setVisibility(View.VISIBLE);
}
});
}
#override
public void onBackPressed(){
super.onBackPressed();
if(firstViewOff){
second_view.setVisibility(View.GONE);
first_view.setVisibility(View.VISIBLE);
firstViewOff = false;
}
}
}
Why i attached backpressed :
Because when user backpress then it will just directly show firstview without closing,

Custom dialog class does't show the dialog

In order to make my app as much low maintenance as possible I've created a class Cl_dialog, that is supposed to manage the creation of each Dialog of my app:
public class Cl_Dialog
{
private Activity activity;
private AlertDialog alertDialog;
private AlertDialog.Builder builder;
private View layout;
public Cl_Dialog( Activity act )
{
this.activity = act;
builder = new AlertDialog.Builder( act );
alertDialog = builder.create();
}
public void dialogShowDatePicker()
{
setContentView( R.layout.dialog_datepicker);
( (ImageButton) layout.findViewById( R.id.btn_confirm ) ).setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick( View view )
{
close();
}
});
}
private void setContentView( int idLayout)
{
LayoutInflater inflater = (LayoutInflater) activity.getSystemService( activity.LAYOUT_INFLATER_SERVICE );
layout = inflater.inflate( idLayout, (ViewGroup ) activity.findViewById( R.id.mainLayout ) );
builder.setView( layout );
builder.create();
}
public void show()
{
alertDialog.show();
}
public void close()
{
alertDialog.dismiss();
}
}
The class is called sometimes by Fragment sometimes by Activity.
This is an example of layout.xml I use:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<DatePicker
android:id="#+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:layout_width="55dp"
android:layout_height="35dp"
android:src="#drawable/ic_navigation_check"
android:layout_margin="15dp"
android:id="#+id/btn_confirm"
android:background="#color/colorPrimary"/>
</LinearLayout>
And here an example of how I call it:
cl_dialog = new Cl_Dialog(activity.this);
cl_dialog.dialogShowDatePicker();
cl_dialog.show();
Any clue about where I am wrong in the code?
Anyway I am an old programmer and I am trying to arrange old code I wrote year ago, is still this the suggested way to create a custom dialog in android?
Thank you!
In setContentView you call builder.create() without setting the result for the alertDialog

How to create custom progressDialog

At any devices we have different position of progressBar inside of the progressDialog. So I need to customize it.
When I try like this:
public class CustomProgressDialog extends ProgressDialog {
public CustomProgressDialog(Context context) {
super(context);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_fragment_dialog);
}
}
<?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">
<ProgressBar
android:layout_width="wrap_content"
android:layout_gravity="center"
android:background="#color/colorPrimary"
android:layout_height="match_parent"
android:id="#+id/progressBar" />
</LinearLayout>
This progress dialog shows only progressBar without dialog. But I need dialog too.
Create custom dialog with progress bar in it for making it consistent for different devices.
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - title, ProgressBar and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("COUSTOM PROGRESS TITLE");
ProgressBar prog = (ProgressBar) dialog.findViewById(R.id.myprogress);
prog.setVisibilty(VISIBLE);
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" >
<ProgressBar
android:id="#+id/myprogress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
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/myprogress"
/>
</RelativeLayout>

onKeydown is not allowed to go back when the dialog is start

i have create simple dialog that showing when i click on the button
every thing is working fine
but i want the onKeydown is not allowed to go back
this is my activity class
public class MainActivity extends Activity implements OnClickListener {
private Button showdialog;
private Dialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showdialog=(Button)findViewById(R.id.showdialog);
showdialog.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
dialog = new Dialog(MainActivity.this,
android.R.style.Theme_Translucent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.dialog);
dialog.show();
}
}
and this is the Activity xml
<RelativeLayout 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:background="#000424"
tools:context=".MainActivity" >
<Button
android:id="#+id/showdialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:text="showdialog" />
</RelativeLayout>
and this is the dialog 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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="300dp"
android:layout_marginTop="200dp"
android:text="this is the dialog"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="100sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
Take a look at onKeyListener:
public class MainActivity extends Activity implements OnClickListener {
private Button showdialog;
private Dialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showdialog=(Button)findViewById(R.id.showdialog);
showdialog.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
dialog = new Dialog(MainActivity.this,
android.R.style.Theme_Translucent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(false);
dialog.setContentView(R.layout.dialog);
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
#Override
public boolean onKey(DialogInterface arg0, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//DO NOTHING
}
return true;
}
});
dialog.show();
}
}

Categories

Resources