Reputation: 2396
I am trying to create a custom Dialog using the following implementation.
The problem is Android is not rendering the layout even though I do set the layout with setContentView().
Example of when .show() is called:
I searched for a possible solution. It appears most use AlertDialog.Builder although I opted for another easier and reusable route i.e. ProgressDialog
. Also, this solution suggest to place the layout resource on the constructor - which I am skeptical of, but naturally did not work.
Code files below:
The source file ProgressDialog.java
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.example.android.R;
public class ProgressDialog extends Dialog {
private ProgressBar progressBar;
private TextView textView;
public ProgressDialog(@NonNull Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_dialog);
progressBar = findViewById(R.id.progressBarDialog);
progressBar.setProgress(0);
textView = findViewById(R.id.progressBarText);
}
public void setProgress(int progress){
if (progressBar != null) {
progressBar.setProgress(progress);
}
}
public void setText(String text){
if (textView != null) {
textView.setText(text);
}
}
}
and with the progress_dialog.xml layout as follows:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/progressBarText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/progressBarDialog"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBarText" />
</androidx.constraintlayout.widget.ConstraintLayout>
What is causing this problem, why does Android not render the dialog content?
Update from @md-asaduzzaman's comment
How to use:
ProgressDialog dialog = new ProgressDialog (SomeActivityHere.this);
dialog.show();
// optional
dialog.setProgress(0);
dialog.setText(getString(R.string.someString));
Alternatively one can pass in a bundle of arguments
Upvotes: 1
Views: 874
Reputation: 15423
It's a problem of your layout
. Change layout_width
of your view from 0dp
to wrap_content
or match_parent
or any fixed size according your requiremnet.
android:layout_width="wrap_content"
Beside this your Dialog is not work as expected as setProgress
and setText
execute before the dialog created. So, Change your dialog implementation like below:
public class ProgressDialog extends Dialog {
private ProgressBar progressBar;
private TextView textView;
private String mText;
private int mProgress;
public ProgressDialog(@NonNull Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.progress_dialog);
progressBar = findViewById(R.id.progressBarDialog);
progressBar.setProgress(mProgress);
textView = findViewById(R.id.progressBarText);
textView.setText(mText);
}
public void setProgress(int progress){
mProgress = progress;
if (progressBar != null) {
progressBar.setProgress(mProgress);
}
}
public void setText(String text){
mText = text;
if (textView != null) {
textView.setText(mText);
}
}
}
Output:
Upvotes: 2
Reputation: 832
the Problem is in android:layout_width="0dp" change it to particular size like 100dp or match parent
<TextView
android:id="@+id/progressBarText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/progressBarDialog"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBarText" />
</androidx.constraintlayout.widget.ConstraintLayout>
Upvotes: 0
Reputation: 123
The layout is loaded, but custom dialog requires fixed height and width if a style theme has not been set. Quick solution is to set both height and width.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/linearLayout"
android:layout_width="200dp"
android:layout_height="200dp"
>
<TextView
android:id="@+id/progressBarText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<ProgressBar
android:id="@+id/progressBarDialog"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBarText"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Also :
ProgressDialog dialog = new ProgressDialog(this);
dialog.show();
// optional
dialog.setProgress(0);
dialog.setText("testing");
Upvotes: 0