I'm creating a chat application and facing a problem while creating a group (Recycler View's List item) and to inflate it over the fragment container.
Actually, two kind of views are being inflated. One is the root View that will be inflated in a fragment container and second is inflated as another custom layout for Dialog ask for input group name.
FAB is implemented as a click event to launch Dialog to input channel name. First time when i click a FAB, dialog appears ask for channel name to create group. After passing, an input group is created and Channel/Group (Recycler View's List item) is created and inflated over the container. But second time when I click on FAB, to repeat the process, to create another group/channel, the app crashes and error is encountered on logcat.
"The specified child already has a parent. You must call removeView() on the child's parent first".
I don't have too much knowledge about fragments and don't know what does that error means. Please help to resolve this error.
Thanks
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_open_channel_list, container, false);
CFAlertDialogue_footerView = inflater.inflate(R.layout.cfalertdialog_footer_view_open_channel, null);
editText_ChannelName = CFAlertDialogue_footerView.findViewById(R.id.textInputEditText_channelName);
setRetainInstance(true);
mRecyclerView = rootView.findViewById(R.id.recyclerView_openChannelList);
channelListAdapter = new openChannelListAdapter(getContext());
refreshLayout = rootView.findViewById(R.id.swipe_refresh_open_channel_list);
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
refreshLayout.setRefreshing(true);
refreshChannelList(CHANNEL_LIST_LIMIT);
}
});
actionButton = rootView.findViewById(R.id.fab_addOpenChannel);
actionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CFAlertDialog.Builder dialog = new CFAlertDialog.Builder(getContext())
.setDialogStyle(CFAlertDialog.CFAlertStyle.ALERT)
.setTitle("Create Open Channel")
.setTextGravity(Gravity.CENTER_HORIZONTAL)
.setCornerRadius(5)
.setCancelable(true)
.setFooterView(CFAlertDialogue_footerView)
.addButton("Cancel", Color.parseColor("#000000"), Color.parseColor("#f8f8ff"),
CFAlertDialog.CFAlertActionStyle.NEGATIVE, CFAlertDialog.CFAlertActionAlignment.JUSTIFIED,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.addButton("Create", Color.parseColor("#000000"), Color.parseColor("#8b3a3a"),
CFAlertDialog.CFAlertActionStyle.POSITIVE, CFAlertDialog.CFAlertActionAlignment.JUSTIFIED,
new DialogInterface.OnClickListener() {
#Override
public void onClick(final DialogInterface dialog, int which) {
OpenChannel.createChannelWithOperatorUserIds(editText_ChannelName.getText().toString(),
null, null, null, null, new OpenChannel.OpenChannelCreateHandler() {
#Override
public void onResult(OpenChannel openChannel, SendBirdException e) {
if ( e != null){
Toast.makeText(getContext(), "Error Creating Channel: "+e.getCode()+" "
+e.getMessage(), Toast.LENGTH_SHORT).show();
return;
}
dialog.dismiss();
refreshChannelList(CHANNEL_LIST_LIMIT);
}
});
}
});
dialog.show();
}
});
setUpRecyclerViewAndAdapter();
return rootView;
}
Change to
final AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
b.setView(layout);
b.show();
to
final AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
b.setView(layout);
final AlertDialog alertDialog = b.show();
Try to change something like this
Related
I want to display the alert dialog when onClick of the button in fragment layout. I have tried but the app is crashing when it runs.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentWalletBinding.inflate(inflater, container, false);
Button button = (Button)getView().findViewById(R.id.add_money);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder alertdialog = new AlertDialog.Builder(view.getContext());
alertdialog.setMessage("this is message");
alertdialog.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getContext(),"toast message",Toast.LENGTH_SHORT).show();
}
}).show();
}
});
return binding.getRoot();
}
onCreateView returns View which can be further obtained by getView() method. and you are calling it before onCreateView had a chance to return own View for framework (its still inside method, didn't return binding.getRoot(); yet)
if you are using view binding then obtain your Button from there
Button button = (Button) binding.addMoney;
or even just use strictly (without additional local Button)
binding.addMoney.setOnClickListener(...
and if you REALLY WANT to use findViewById (but why...), then you can look inside binding.getRoot() even when it isn't returned/drawn yet
Button button = (Button) binding.getRoot().findViewById(R.id.add_money);
there is a problem in
(Button)getView().findViewById(R.id.add_money);
please make sure id is same as in XML.
I don't understand DialogFragment at all. How to create one, how to get the user input out of it, and set it into a TextView.
I would like for the TITLE button, when clicked, to bring up a DialogFragment asking the user to enter the title of their Mood Board. The user enters a title. When they click the PostiveButton "Done", the user's title is set into the top left frame of the mood board, which has a TextView with a hint.
Please! Ask questions, because I don't really understand the dialog setup.
Here is a picture of my main_layout, in my MainActivity. Every element has an "#+id/".
The solution you are looking for is a callback:
Create an interface with a method to use as a callback
Implements the interface on the activity
Create the dialog fragment and in onAttach get the interface
Show the dialog fragment on the activity
On dismiss the dialog fragment pass the text using the instance of the interface
interface Callback {
updateText(String text)
}
class CoolActivity... implements Callback
onCreate {
//find your views
showDialogBtn.setOnClickListener(...
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("yourTag");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
DialogFragment dialogFragment = ExampleDialogFragment.newInstance();
dialogFragment.show(ft, "yourTag");
)
}
#Override
updateText(String text) {
youtView.setText(text)
}
class CoolDialogFragment extend DialogFragment {
private Callback callback;
#Override
void onAttach(Context context) {
callback = (Callback) context
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_fragment_example, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//find the views in the dialog fragment
closeBtn.clickListener(...
callback.updateText(edittext.getText().toString())
dismiss()
)
}
}
Here is a gist of a dialog fragment
https://gist.github.com/cutiko/7e307efcf7492ea52ef05cd90f9e3203
The problem is you want to connect a dialog fragment with a another component, and you want to do it straigth forward. This is not considered a good practice because yiu create 2 componentes higly attached, so the best would be to use data persistence and some form of react programming
You can make your mood board title textview static then call it to the alertdialog with edittext to set it text (setText)
like this.
final EditText edittext = new EditText(MainActivity.this);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Input Title")
.setView(edittext)
.setCancelable(false)
.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
YourCustomDialog.your_title_textviewMoodboard.setText(edittext.getText().toString());
}
})
.setNegativeButton("Back", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
in your custom dialog. declare your textview static globally
public static TextView your_title_textviewMoodboard;
I'm currently trying to make a fragment come into view over everything else in the activity. Then, on a button press, the fragment should disappear and reveal everything that was there previously.
Here is my current attempt:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction()
.replace(currentFragment.getId(), fragment);
transaction.addToBackStack(null);
transaction.commit();
and on the button press, I do:
getFragmentManager().popBackStack();
But the issue I'm having it the fragment doesn't totally inflate overtop all the other views, and the button press is not having the desired affect. Any suggestions?
EDIT: It's a bottomNavigationView that's still showing, I'd like to inflate overtop of that.
You can use Dialog fragment like:
public class PopUpDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Context context = getActivity();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Your title")
.setMessage("Your message")
.setPositiveButton("Button name", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Do your stuff here
dismiss();
}
});
return builder.create();
}
}
Then you call it from your activity like:
PopUpDialogFragment fragment = new PopUpDialogFragment();
fragment.show(getFragmentManager(), "popUp");
If you want a Fragment with your own custom view then you create one with onCreateView method.
public class PopUpDialogFragment extends DialogFragment {
private Button button;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.your_layout,
container, false);
button = view.findViewById(R.id.your_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do your stuff here
getDialog().dismiss();
}
});
return view;
}
}
I've come across an issue which I've never had before. I have a Fragment containing a button. This button (ViewBookingButton) shows a popup dialog. When the dialog opens I would like to set a string in an EditText (AllBooking). Unfortunately it crashes and shows NullPointerExecption. What is going wrong? Here is my code:
ViewBookingButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
AlertDialog.Builder ViewBookingHelpBuilder = new AlertDialog.Builder(getActivity());
ViewBookingHelpBuilder.setTitle("view booking:");
LayoutInflater inflater = getActivity().getLayoutInflater();
View DialogLayout = inflater.inflate(R.layout.view_booking, null);
ViewBookingHelpBuilder.setView(DialogLayout);
TextView AllBooking = (TextView)view.findViewById(R.id.AllBooking);
AllBooking.setText("Hello World");//WHEN I REMOVE THIS THE DIALOG WORKS
ViewBookingHelpBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
}
});
AlertDialog helpDialog = ViewBookingHelpBuilder.create();
helpDialog.show();
}
});
Change this
TextView AllBooking = (TextView)view.findViewById(R.id.AllBooking);
to
TextView AllBooking = (TextView)DialogLayout.findViewById(R.id.AllBooking);
TextView belongs to the inflated layout and `findViewById looks for the view in the current view hierarchy.
Can someone tell me how to create the above dialog view similar/exactly to the link [here][1], whereby the focus of the problem is to create the view in the centre of the picture?
I have done some research and it made me wonder should i be using a custom xml to create a custom dialog view or should i be using alertdialog to create the exact view programmability shown above? And even if alertdialog is possible how am i going to accommodate with so many textview messages shown in the middle of the dialog picture given alertdialog limitation? Eg: "builder.setMessage("This is the alert's body");" If you know what i mean!!
Can someone tell me the easiest way to get the exact same view because i'm kinna doing the same app and new to android.. Thanks :)
The best approach will be custom dialog. As it will be helpful creating all those background colours and the effect. I am sure the link you have posted is using custom dialog as well,
cheers
link that might help:
[1] http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
[2] http://androidideasblog.blogspot.com/2010/02/creating-custom-dialog-in-android.html
/// In your code implementations just add this code when you create dialog....after having this just have all TextView arranged in your layout and add that layout id to this below code good luck
//Dialog box creator
private Dialog constructYourDialog()
{
//Preparing views
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.***your_xml_name***, (ViewGroup) findViewById(R.id.***Yout view id***));
//Building dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setPositiveButton("Show Videos", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("","Show Video Click");
dialog.dismiss();
});
builder.setNegativeButton("E-Mail", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("","E-mail Click");
dialog.dismiss();
}
});
builder.setNeutralButton("Show Map", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("","Show Map Click");
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
return alert;
}
Try following code
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Alert !");
builder.setMessage("your text here");
builder.setPositiveButton("Show Video", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
connect = false;
}
});
builder.setNegativeButton("Show Map", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface arg0, int arg1)
{
// TODO Auto-generated method stub
}
});
builder.setNeutralButton("Show Both", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface arg0, int arg1)
{
// TODO Auto-generated method stub
}
});
builder.show();
UPDATE: To show custom title create a layout and inflate it using below code
LayoutInflater mInflater = LayoutInflater.from(mContext);
View layout = mInflater.inflate(R.layout.popup_example, null);
Remove following line from above code
builder.setTitle("Alert !");
and then set using
builder.setCustomTitle(layout)