I create a class for custom dialog and I used PrettyDialog. I want an edittext on dialog. I use Inflater but Error: getActivity -> mHost:null. I tried create getTextDialogFragment() method, then getLayoutInflater error mHost null. What can I do fix that?
TextDialogFragment:
public class TextDialogFragment extends DialogFragment {
private EditText edtDialog;
public interface SingleChoiceListener{
void onPositiveButtonClicked();
void onNegativeButtonClicked();
}
TextDialogFragment.SingleChoiceListener mListener;
#NonNull
public PrettyDialog onCreateTextDialog(Context context) {
PrettyDialog prettyDialog = new PrettyDialog(context);
LayoutInflater inflater=getActivity().getLayoutInflater();
View view=inflater.inflate(R.layout.dialog_text,null);
prettyDialog.setContentView(view);
prettyDialog.setMessage("sdf");
prettyDialog.setIcon(R.drawable.question_icon);
prettyDialog.setCanceledOnTouchOutside(false);
prettyDialog.addButton("EVET", R.color.pdlg_color_white, R.color.pdlg_color_green, new PrettyDialogCallback() {
#Override
public void onClick() {
mListener.onPositiveButtonClicked();
prettyDialog.dismiss();
}
});
prettyDialog.addButton("HAYIR", R.color.pdlg_color_white, R.color.pdlg_color_red, new PrettyDialogCallback() {
#Override
public void onClick() {
mListener.onNegativeButtonClicked();
prettyDialog.dismiss();
}
});
edtDialog=view.findViewById(R.id.edtDialog);
prettyDialog.show();
return prettyDialog;
}
public void setListener(TextDialogFragment.SingleChoiceListener singleChoiceListener){
mListener=singleChoiceListener;
}
}
this layout that I want add to dialog. Dialog has to work edittext+prettydialog:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_margin="8dp">
<EditText
android:id="#+id/edtDialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#null"
android:focusable="true"
android:hint="Giriş yapınız"
android:gravity="center"
android:imeOptions="actionDone"
android:inputType="text"
android:textColor="#android:color/black"
android:textColorHint="#android:color/darker_gray"
android:textSize="40sp">
</EditText>
</RelativeLayout>
Try below code
View view=LayoutInflater.from(context).inflate(R.layout.dialog_text,null);
Instead of
LayoutInflater inflater=getActivity().getLayoutInflater();
View view=inflater.inflate(R.layout.dialog_text,null);
I hope this can help you!
Thank You.
Related
I have to set a message and title in onPreExcecute() but am not able to do it. ProgressDialog had the setMessage() method, but ProgressBar does not.
protected void onPreExecute() {
progressBar.
super.onPreExecute();
}
Use ProgressBar to show progress and TextView to show message:
<ProgressBar
android:id="#+id/indeterminateBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
Create your own ProgressBar and put a Textview next to it. A library from https://github.com/Q115/DelayedProgressDialog will do the trick.
Usage:
DelayedProgressDialog progressDialog = new DelayedProgressDialog();
progressDialog.show(getSupportFragmentManager(), "tag");
You can use AlertDialog by overriding onCreateDialog after extending DialogFragment. You should design an xml that contains a ProgressBar and a TextView(for the message). Then call the setView() method of the AlertDialog on the xml like below.
Your layout in xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar
android:id="#+id/indeterminateBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/progress_bar_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="loading..."
/>
</LinearLayout>
Your DialogFragment Class
public class Dialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new
AlertDialog.Builder(getActivity());
//use layoutinflater to inflate xml
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.xml_containing_progressbar,null));
return builder.create();
}
}
you can now call .show on an instance of the DialogFragment Class
Dialog dialog = new Dialog();
dialog.show(getSupportFragmentManager(), "tag");
source: https://developer.android.com/guide/topics/ui/dialogs
I know it's late but better late than never...
I had the same problem so I created a custom ProgressBar.
First you make the class for the view:
public class CustomProgressBar extends RelativeLayout {
private ProgressBar progressBar;
private TextView textView;
public CustomProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.custom_progress_bar, this);
progressBar = findViewById(R.id.progressBarCustom);
textView = findViewById(R.id.pbTV);
init(attrs);
private void init(#Nullable AttributeSet attrs) {
if (attrs != null) {
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.CustomProgressBar);
String textAttrib = ta
.getString(R.styleable.CustomProgressBar_text);
textView.setText(textAttrib);
ta.recycle();
}
}
public void setProgressBar(ProgressBar progressBar) {
this.progressBar = progressBar;
}
public String getText() {
return textView.getText().toString();
}
public void setText(String text) {
textView.setText(text);
}
public void setText(int resid) {
textView.setText(resid);
}
}
Then, in order to be able to set this class attributes from the layout, we need to make it styleable. So you need to create an xml file called "attrs.xml" within the directory "values".
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomProgressBar">
<attr name="text" format="string"/>
</declare-styleable>
</resources>
Now we create the layout for our custom view:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">
<ProgressBar
android:id="#+id/progressBarCustom"
style="?android:attr/progressBarStyle"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerInParent="true"/>
<TextView
android:id="#+id/pbTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"/>
</RelativeLayout>
Finally, just use it wherever you want:
<com.example.mypackage.CustomProgressBar
android:id="#+id/radioGroupProgressBar"
android:layout_width="match_parent"
android:layout_height="150dp"
app:text="Loading..."
android:layout_gravity="center_horizontal"
/>
And that's it :)
Is it possible to change the location of the button on the dialog to the outside of the dialog itself? something like this (the red squares are buttons):
I know I can get the button with :
dialog.getButton(AlertDialog.BUTTON_NEGATIVE)
but I couldn't find on the manual the way to change it's location.
<?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"
android:gravity="center"
android:padding="20dp"
android:background="#00000000">
<LinearLayout
android:background="#drawable/border_background"
android:layout_gravity="center"
android:gravity="center"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical">
<TextView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="#string/update_app"
android:textSize="18sp"
android:textColor="#color/white"
android:layout_gravity="center_horizontal"
android:gravity="center" />
</LinearLayout>
<Button
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_marginTop="20dp"
android:background="#123456"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="14sp"
android:onClick="onUpdateClicked"
android:text="Button" />
Instead of using default alert dialog, make a custom layout something like my layout here. And perform desired action on button.
You can call n show this layout without inflating like this.
EDIT:1
public void showUpdateLayout() {
mParentView = (ViewGroup) findViewById(android.R.id.content);
if (mParentView != null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
mUpdateLayout = inflater.inflate(R.layout.upadte_layout, mParentView, false);
mParentView.addView(mUpdateLayout);
if (mUpdateLayout != null) {
mUpdateLayout.setVisibility(View.VISIBLE);
}
}
Write this method in ur public Class (or Custom Aprent Activity). and call this method when u need to alert.
you should make custom dialog and set it's root view background color to be transparent: android:background="#android:color/transparent"
You will need to create a custom DialogFragment. Below I will give an analytical example of how to implement one and call it with several parameters each time, so you won't need to repeat code each time you want an Dialog with different message.
CustomAlertDialog.java
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Custom DialogFragment class
*/
public class CustomAlertDialog extends DialogFragment implements
View.OnClickListener {
/**
* Interface for receiving the wanted callbacks
* */
public interface CallbacksListener
{
public void onPositiveButtonClicked();
public void onNegativeButtonClicked();
}
private CallbacksListener callbacksListener;
public void setCallbacksListener(CallbacksListener callbacksListener)
{
this.callbacksListener = callbacksListener;
}
public CustomAlertDialog()
{
//empty constructor
}
private String titleString;
private String messageString;
private String positiveString;
private String negativeString;
#Override
public void setArguments(Bundle bundle)
{
titleString = bundle.getString("titleString");
messageString = bundle.getString("messageString");
positiveString = bundle.getString("positiveString");
negativeString = bundle.getString("negativeString");
}
public static CustomAlertDialog newInstance(AlertDialogStrings alertDialogStrings)
{
CustomAlertDialog customAlertDialog = new CustomAlertDialog();
Bundle b = new Bundle();
b.putString("titleString", alertDialogStrings.titleString);
b.putString("messageString", alertDialogStrings.messageString);
b.putString("negativeString", alertDialogStrings.negativeString);
b.putString("positiveString", alertDialogStrings.positiveString);
customAlertDialog.setArguments(b);
return customAlertDialog;
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View v = getActivity().getLayoutInflater().inflate(R.layout.custom_alert_dialog, null);
TextView titleTV = (TextView) v.findViewById(R.id.title_customAlertDialog);
TextView messageTV = (TextView) v.findViewById(R.id.message_customAlertDialog);
Button positiveButton = (Button) v.findViewById(R.id.okBtn_customAlertDialog);
Button negativeButton = (Button) v.findViewById(R.id.cancelBtn_customAlertDialog);
titleTV.setText(titleString);
messageTV.setText(messageString);
positiveButton.setText(positiveString);
negativeButton.setText(negativeString);
positiveButton.setOnClickListener(this);
negativeButton.setOnClickListener(this);
builder.setView(v);
return builder.create();
}
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.okBtn_customAlertDialog:
callbacksListener.onPositiveButtonClicked();
dismiss();
break;
case R.id.cancelBtn_customAlertDialog:
callbacksListener.onNegativeButtonClicked();
dismiss();
break;
default:
break;
}
}
#Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
try
{
callbacksListener = (CallbacksListener) activity;
}
catch (ClassCastException e)
{
throw new ClassCastException(activity.toString()
+ " must implement CallbacksListener");
}
}
#Override
public void onDetach()
{
super.onDetach();
callbacksListener = null;
}
/**
* Class for saving the wanted Strings we want to have on our CustomDialog implementation
* */
public static class AlertDialogStrings
{
public String titleString;
public String messageString;
public String positiveString;
public String negativeString;
public AlertDialogStrings(String title, String message, String positiveString, String negativeString)
{
this.messageString = message;
this.titleString = title;
this.positiveString = positiveString;
this.negativeString = negativeString;
}
}
}
custom_alert_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="22sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="My Title Here"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:id="#+id/title_customAlertDialog"/>
<TextView
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="7dp"
android:id="#+id/message_customAlertDialog"
android:layout_below="#id/title_customAlertDialog"
android:textColor="#color/darkGray"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_marginTop="7dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:measureWithLargestChild="true"
android:layout_below="#+id/message_customAlertDialog"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<Button
android:layout_height="wrap_content"
style="?android:attr/buttonBarButtonStyle"
android:textSize="13sp"
android:textColor="#color/primaryColorDark"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:text="#string/cancel"
android:id="#+id/cancelBtn_customAlertDialog"/>
<Button
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:layout_marginRight="10dp"
android:textSize="13sp"
android:textColor="#color/primaryColorDark"
android:layout_height="wrap_content"
style="?android:attr/buttonBarButtonStyle"
android:text="#string/ok"
android:id="#+id/okBtn_customAlertDialog"/>
</LinearLayout>
To show your customAlertDialog:
private void popUpAlertDialog()
{
String title = "My title here?";
String message = "My Message here";
String positiveString = "OK";
String negativeString = "Cancel";
CustomAlertDialog.AlertDialogStrings customDialogStrings =
new CustomAlertDialog.AlertDialogStrings
(title, message, positiveString, negativeString);
CustomAlertDialog customAlertDialog =
CustomAlertDialog.newInstance(alertDialogStrings);
customAlertDialog.show(getSupportFragmentManager(), "customAlertDialog");
customAlertDialog.setCallbacksListener(new CustomAlertDialog.CallbacksListener()
{
#Override
public void onPositiveButtonClicked()
{
//do something
}
#Override
public void onNegativeButtonClicked()
{
//do something
}
});
}
The AlertDialogStrings class helps us maintain our wanted strings in a way that we can re-use our class with different strings each time and the CallbacksListener helps as settle the way of the OnClick responds each time. Note that this design follows the Material Design Dialog style principles.
Yes.
All you have to do is create a custom dialog layout.
In order to achieve that you would create a LinearLayout with Transparent BG color and inside it you can do whatever is it that you want.
A quick example:
<LinearLayout android:orientation="vertical"
android:layout_width="300"
android:layout_height="500"
android:background="#android:color/transparent">
<LinearLayout android:orientation="vertical"
android:layout_width="270"
android:layout_height="200">
... your content here
</LinearLayout>
<Button android:layout_width="270"
android:layout_height="200"
android:margin_top="10"/>
</LinearLayout>
If you are using a builder to create your Dialog , you would go for this:
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.your_dialog_layout, null))
...otherwise
Dialog newDialog = new Dialog();
newDialog.setContentView(R.layout.your_dialog_layout);
I'd like to build..
A reusable dialog
Fully customizable layout (custom color or typeface, etc)
Maintain its lifecycle with referenced Activity
Use overloading which I can create several variations of dialog, i.e. combination of title, message and callbacks
In a simple way (if possible)
Currently my custom dialog class looks like :
public class CustomAlert {
public interface OnSingleClickedListener {
public void onPositiveClicked();
}
public interface OnDualClickedListener {
public void onPositiveClicked();
public void onNegativeClicked();
}
/**
* Show simple alert without callback.
* #param context
* #param msg
*/
public static void showAlert(Context context, String msg) {
final Dialog dialog = new Dialog(context);
// Do some stuff
ok.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
/**
* Show simple alert with callback.
* #param context
* #param msg
* #param listener
*/
public static void showAlert(Context context, String msg, final OnSingleClickedListener listener) {
final Dialog dialog = new Dialog(context);
// Do some stuff
ok.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
listener.onPositiveClicked();
}
});
dialog.show();
}
// Some other methods..
}
and I call these alerts from Activity like this :
if(!isFinishing()) {
CustomAlert.showAlert(MainActivity.this, getResources().getString(R.string.network_no_connection));
}
Even though I'm calling isFinishing() to check if the host Activity is running, I keep seeing BadTokenException, is your Activity running?, and I thought maybe isFinishing() is not enough.
I found this article which is using DialogFragment, but I feel like this is quite a lot of code for such a small task when I consider above requirements.
What is the most recommended and effective solution to solve this problem?
Thanks in advance!
Create a custom class like this,i have created this dialog for thumbs up and thumbs down in my application named Debongo - Restaurant Finder on play store
//recommend dialog
public void showRecommendDialog(Context mContext)
{
dialog = new Dialog(mContext,android.R.style.Theme_Holo_Dialog_NoActionBar);
dialog.setContentView(R.layout.recommend_dialog);
dialog.show();
btnThumbsUp = (ImageButton) dialog.findViewById(R.id.btn_Yes);
btnThumbsDown = (ImageButton) dialog.findViewById(R.id.btn_no);
txtMsg=(TextView) dialog.findViewById(R.id.txtMsg);
ImageButton cancelRecommend=(ImageButton) dialog.findViewById(R.id.cancelRecommend);
cancelRecommend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
dialog=null;
}
});
btnThumbsUp.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//do what you want
}
});
btnThumbsDown.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//do what you want
}
});
}
Here is the layout file for the same
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:gravity="center"
android:padding="5dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageButton
android:id="#+id/cancelRecommend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:contentDescription="#null"
android:src="#drawable/com_facebook_close" />
<TextView
android:id="#+id/txtMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/cancelRecommend"
android:layout_marginBottom="#dimen/viewSpace3"
android:layout_marginLeft="#dimen/viewSpace3"
android:layout_marginRight="#dimen/viewSpace3"
android:text="Do You Want To Recommend This Restaurant ?"
android:textColor="#000000"
android:textSize="#dimen/titlebar_textSize"
tools:ignore="HardcodedText" />
<View
android:layout_width="0dip"
android:layout_height="#dimen/viewSpace3" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtMsg"
android:layout_centerHorizontal="true"
android:layout_gravity="center" >
<ImageButton
android:id="#+id/btn_Yes"
android:layout_width="#dimen/viewSpace5"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:contentDescription="#null"
android:src="#drawable/unselected_thumbs_up" />
<View
android:id="#+id/vvv"
android:layout_width="#dimen/viewSpace1"
android:layout_height="#dimen/viewSpace3"
android:layout_toRightOf="#id/btn_Yes" />
<ImageButton
android:id="#+id/btn_no"
android:layout_width="#dimen/viewSpace5"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/vvv"
android:background="#android:color/transparent"
android:contentDescription="#null"
android:src="#drawable/unselected_thumbs_down" />
</RelativeLayout>
</RelativeLayout>
Now when you want to open this dialog,you can just call it by using
showRecommendDialog(this);
I have a problem when declaring a button.
I will try to explain as specific as possible.
In my main Layout I have a Fragment containing a secondary Layout. In which I have several buttons.
My intention is that my main fichero.java to declare the buttons within the fragment.
Here we put the main Layout:
<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:baselineAligned="false">
<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="josejuansosarodriguez.radioecca.conocecanarias.TrueoFalseFragment"
android:id="#+id/fragmentTrueFalse"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Grp1"
android:id="#+id/textGrp1"
android:textSize="25sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"/>
<fragment
android:layout_width="fill_parent"
android:layout_height="450dp"
android:name="josejuansosarodriguez.radioecca.conocecanarias.Grp1FragmentP1"
android:id="#+id/fragmetaskGRP1"
android:layout_below="#+id/textGrp1"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp" />
</RelativeLayout>
Here we put the secondary Layout that has the buttons:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/fragment_TrueorFalse">
<Button
android:layout_width="120sp"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="#string/buttontrue"
android:id="#+id/buttontrue"
android:layout_marginLeft="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="120sp"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="#string/buttonfalse"
android:id="#+id/buttonfalse"
android:layout_marginRight="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
And here I leave the my main activity:
public class Grp1Fragment extends Fragment {
private Button buttonTrue;
private Button buttonFalse;
private Button buttonNextAsk;
private View view;
public Grp1Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
buttonTrue = (Button) view.findViewById(R.id.buttontrue);
buttonTrue.setOnClickListener(this);
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_grp1, container, false);
return view;
}
My problem I find in the following line:
buttonTrue.setOnClickListener (this);
The message reads: View can not be in Applied to josejuansosarodriguez.radioecca.conocecanarias.Grp1Fragment
I hope I have spread far.
Thank you very much for everything, this forum is amazing.
Try this:
Note that you have to FIRST inflate the layout to make accesible the widgets of the layout.
Then you can "bind" the widgets, and finallly in this case, set the listeners to the buttons.
I set you a Log, if you need change it to Toast, or code
public class Grp1Fragment extends Fragment {
private Button buttonTrue;
private Button buttonFalse;
private Button buttonNextAsk;
private View view;
public Grp1Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_grp1, container, false);
//Declare your widgets (Buttons)
buttonTrue = (Button) view.findViewById(R.id.buttontrue);
buttonFalse = (Button) view.findViewById(R.id.buttonfalse);
//Set the Listeners
buttonTrue.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Toast or Log, or whateber you need
Log.d("Fragment1" , "Button: True");
}
});
buttonFalse.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Toast or Log, or whateber you need
Log.d("Fragment1" , "Button: FAlse");
}
});
//return the view
return view;
}
void setOnClickListener(View.OnClickListener l)
requires a OnClickListener, which Grp1Fragment is not.
Use something like
buttonTrue.setOnClickListener(new View.OnClickListener() {
void onClick(View v) {
... // reaction on the button
}
});
I´m trying to implement a basic custom Alert Dialog.
It should look like this
With the following XML Code:
<?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" >
<LinearLayout
android:id="#+id/tv_custom_dialog_event"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#drawable/actionbar_background"
android:gravity="top"
android:orientation="horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="100dp"
android:text="Delete Event ?"
android:textColor="#color/white"
android:textSize="20sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#DFDFDF" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/btn_custom_dialog_events_cancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/ic_action_cancel" />
<ImageButton
android:id="#+id/btn_custom_dialog_events_true"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/ic_action_accept" />
</LinearLayout>
</LinearLayout>
However when I run it on my emulator it looks like this:
Does anybody know why this happens and how I can fix that?
Assuming you have a class that extends Dialog you can do the following:
First define a style in styles.xml with something like this:
<style name="CustomDialogThemeTrasparent" parent="#android:style/Theme.Dialog">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:windowBackground">#color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
Then in the constructor of your custom dialog class you set this theme:
public class MyCustomDialog extends Dialog {
public MyCustomDialog(final Context context)
{
super(context, R.style.CustomDialogThemeTrasparent);
}
To set your custom layout as view you can make a function called setTheme() and then called in the show() of your dialog, even with parameters and a nice done layout you can make a more generic class that you can use to show your custom dialogs all over your app, something like this:
//Function to set the layout when the dialog is instantiated, here we
//set the layout and if you want you can set parameter to
//show/hide controls/views and can show different types of dialogs with the same class but with a unified style
private void setTheme()
{
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = layoutInflater.inflate(R.layout.custom_dialog, null);
this.setContentView(mView);
}
then you can make function(s) to show your dialog(s), like:
public void showDeletionDialog(String pMessage)
{
this.setTheme();
this.show();
}
hope it help you
Try this approach. You can inflate your layout:
public AlertDialog displayLayoutDialog(int layout,final Context context, int theme){
AlertDialog.Builder builder = new AlertDialog.Builder(context, theme);
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(layout, null);
builder.setView(view);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog= builder.create();
dialog.show();
Button tb = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
tb.setOnClickListener(new CustomListener(dialog, context));
return dialog;
}
And your Listener where all the validation and anything else that you need happens:
public class CustomListener implements View.OnClickListener {
private final Dialog dialog;
private Context context;
public CustomListener(Dialog dialog, Context context) {
this.dialog = dialog;
this.context = context;
}
#Override
public void onClick(View v) {
Toast.makeText(context, "Custom Layout", Toast.LENGTH_LONG).show();
}
}
Hope it helps!!!