Reputation: 601
I'm trying to create a custom dialog where the content inside the dialog is dynamic. So I pass a view to the dialog and it will add that view to the dialog. Currently, it doesn't draw the custom view and its saying that the view has a width and height of 0.
I pass the custom view through the Application class along with a bunch of other customizations.
Here is my dialog class:
public class LoadingActivity extends Dialog
{
private LoadingActivity(Context a) {
super(a, android.R.style.Theme);
}
public static LoadingActivity show(Context context) {
LoadingActivity checkerLoader = new LoadingActivity(context);
View currentView = View.inflate(context, R.layout.loading_activity, null);
FrameLayout linearLayout = (FrameLayout) currentView.findViewById(R.id.circular_progress_bar);
RelativeLayout viewLayout = (RelativeLayout) currentView.findViewById(R.id.view_layout);
if (DataManager.getInstance().getAppConfigurations().getLoadingActivity() != null) {
View loadingView = DataManager.getInstance().getAppConfigurations().getLoadingActivity();
loadingView.setBackgroundColor(Color.RED);
loadingView.invalidate();
if(loadingView.getParent()!=null) {
((ViewGroup) loadingView.getParent()).removeView(loadingView);
}
viewLayout.addView(loadingView);
}
checkerLoader.requestWindowFeature(Window.FEATURE_NO_TITLE); //before
checkerLoader.setContentView(currentView);
checkerLoader.setCancelable(false);
checkerLoader.getWindow().getAttributes().gravity = Gravity.CENTER;
WindowManager.LayoutParams lp = checkerLoader.getWindow().getAttributes();
lp.dimAmount=0.2f;
checkerLoader.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
checkerLoader.getWindow().setAttributes(lp);
checkerLoader.show();
return checkerLoader;
}
}
Heres an example of my custom view:
public class Spinner extends LinearLayout
{
public Spinner(Context context) {
super(context);
init(context);
}
public Spinner(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}
public Spinner(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
View aView = View.inflate(context, R.layout.loading_activity, null);
ImageView logoImage = (ImageView) aView.findViewById(R.id.loading_image);
logoImage.setImageResource(R.drawable.loading_chicken2);
}
}
Upvotes: 1
Views: 145
Reputation: 6460
The Custom View Spinner doesn't draw anything cause the inflated layout 'R.layout.loading_activity' isn't added to a parent .
changing
View aView = View.inflate(context, R.layout.loading_activity, null);
to:
View aView = View.inflate(context, R.layout.loading_activity, this);
might help
Upvotes: 1