Android - Intent or DialogFragment in a Fragment? - android

I dont know what path to take, I have a MainActivity class which host my ViewPagerTabStrip and each tabs are fragments, one tab consist of ListView and a button. When I click that button I want to either inflate a UI where the user fills in editfields or dialogFragment similiar process. Working with fragments what is best way to implement this? and How can I? I have set a onClickListener on the button and is working.
I have a method in my DBHelper that puts the users input into an SQLite database.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_notes, container, false);
add = (Button) view.findViewById(R.id.addbtn);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getActivity(), "onClick works", Toast.LENGTH_SHORT).show();
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
LayoutInflater inflater= getActivity().getLayoutInflater();
//this is what I did to added the layout to the alert dialog
View layout= inflarter.inflate(R.layout.alert_dialog,null);
alert.setView(layout);
final EditText titleInput=(EditText)layout.findViewById(R.id.dialog_title);
final EditText bodyInput=(EditText)layout.findViewById(R.id.dialog_body);
}
});
return view;
}
What do I call in the Fragment

Simplest way would be to use an AlertDialog with a custom layout, which has has some EditTexts. When the OK button in the dialog is clicked, get the values from the EditTexts, and put them in your database.

Related

Give a certain EditText focus

I'm using 2 EditText next to each other, the left one gains focus on the fragment startup, I want to give the right one focus I've tried to call requestFocus() on the right EditText but it's not working
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sandwich_fragment,container, false);
sandwichNameEditText = view.findViewById(R.id.sandwich_name_edit_text);
sandwichPriceEditText = view.findViewById(R.id.sandwich_price_edit_text);
insertSandwichImageView = view.findViewById(R.id.insert_sandwich_btn);
sandwichListView = view.findViewById(R.id.sandwich_list);
dbHandler = new DBHandler(getContext(),null);
sandwichArrayList = dbHandler.getSandwiches();
final SandwichListAdapter adpater = new SandwichListAdapter(getContext(),
R.layout.sandwich_item, sandwichArrayList);
sandwichListView.setAdapter(adpater);
insertSandwichImageView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if(sandwichNameEditText.getText().toString().equals("") || sandwichPriceEditText.getText().toString().equals("")){
Toast.makeText(getContext(),"No empty strings.", Toast.LENGTH_SHORT).show();
return;
}
Sandwich sandwich = new Sandwich(sandwichNameEditText.getText().toString(),
Double.parseDouble(sandwichPriceEditText.getText().toString()));
dbHandler.addSandwich(sandwich);
adpater.add(sandwich);
sandwichNameEditText.setText("");
sandwichPriceEditText.setText("");
sandwichNameEditText.requestFocus(); // working here
}
});
sandwichNameEditText.requestFocus(); // not working here
return view;
Try to call requestFocus in the onViewCreated method.
The request for focus is something you should do once your View is created.
You can find some insights about the difference between onCreateView and onViewCreated methods for a Fragment here.
That said, you should move your view elements initialisations in the onViewCreated as well, since they're something you want to do after the view is created and not while it's being created. Just leave the inflate logic there, and do the other logic once the View is there.

How to use onClickListener when the buttons are in different layout?

I'm trying to use onClickListener but I keep getting Null exceptions.
This is the call to onClickListener -
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// code here
}
}
};
button1 = findViewById(R.id.button1);
button1.setOnClickListener(onClickListener);
'button1' is found on a different layout which opens (as a Dialog) when the user press on some other button.
What can I do to in this case?
Thanks! :)
You would have to inflate the layout with the dialogue's buttons.
for example, this is my method for displaying a popup.
I have named the method showPopup;
Dialogue ViewSview_student_dialogue;
view_student_dialogue = new Dialogue(getApplicationContext());
private void showStudentPopup(String name,String id_gender) {
TextView cancelpopup,st_name,id_and_gender;
view_student_dialogue.setContentView(R.layout.custom_student_popup);
cancelpopup = (TextView)view_student_dialogue.findViewById(R.id.cancel);
st_name = (TextView)view_student_dialogue.findViewById(R.id.st_name);
id_and_gender = (TextView)view_student_dialogue.findViewById(R.id.id_and_gender);
st_name.setText(name);
id_and_gender.setText(id_gender);
cancelpopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
view_student_dialogue.dismiss();
}
});
view_student_dialogue.getWindow().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.white)));
view_student_dialogue.show();
}
Please take note that custom_student_popup is a layout file that i have created in the res layout folder.
Use layout inflator to inflate that view first then set listener.
The idea here is to get a reference to the view for your Dialog.
First, you inflate the xml layout for your Dialog:
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.dialog_layout, null);
You then set that as the view for your new Dialog:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(dialogLayout);
You can then get a reference to the Button from the inflated View and set you OnClickListener:
Button yourButton = (Button)dialogLayout.findViewById(R.id.yourbutton);
/// do the click listener assignment
Show your Dialog to the user:
builder.show();

Inflating a RecyclerView inside a Dialog layout

In my activity, on click of a button I am opening a Dialog which has a RecyclerView in it.
final Dialog dialog = new Dialog(MyActivity.this);
dialog.setTitle("Details");
dialog.setContentView(R.layout.details_popup);
RecyclerView detailsRecyclerView = (RecyclerView) dialog.findViewById(R.id.detailsRecyclerView);
Button detailsCloseButton = (Button) dialog.findViewById(R.id.detailsCloseButton);
//feedbackResponseSubList contains n items
**DetailsAdapter detailsAdapter = new DetailsAdapter(R.layout.detail_item, feedbackResponseSubList);
detailsRecyclerView.setAdapter(detailsAdapter);**
detailsCloseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
The close button is visible but the recyclerView is not getting populated with data. Even the methods onCreateViewHolder(), onBindViewHolder() and getItemCount() of Detailsdapter are not getting called.
I have figured out the problem. After setting layout manager in recyclerview, it works fine.
LinearLayoutManager layoutManager = new LinearLayoutManager(dialog.getContext());
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
detailsRecyclerView.setLayoutManager(layoutManager);
The problem is that the Dialog is already created when you try to modify the adapter, because of that It's not getting populated with your data.
You have two options, create your own Dialog which extend Dialog class and in onCreate() put your code to populate the list or add adapter.notifyDataSetChanged() after setAdapter() to tell that your data inside the list was updated.
Use a dialog fragment instead of Dialog. An dialog fragment gives all the features of a dialog while providing the functionality of a fragment.
public class AlertDialogFragment extends DialogFragment {
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//inflate layout with recycler view
View v = inflater.inflate(R.layout.details_popup, container, false);
RecyclerView detailsRecyclerView = (RecyclerView) getActivity().findViewById(R.id.detailsRecyclerView);
DetailsAdapter detailsAdapter = new DetailsAdapter(R.layout.detail_item, feedbackResponseSubList);
detailsRecyclerView.setAdapter(detailsAdapter);
detailsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return v;
}
public Dialog onCreateDialog(Bundle SavedInstanceState) {
//The rest of the code you will get when u search for dialogfragments

Assign Dialog box view to another view

I am creating a dialog and setting setContentView of a layout. And I am programmatically adding buttons, images to layout in dialog setContentView . Now how I can assign dialog box view to another view.
That is a layout is assigned to a a view like below
View getview=R.layout.tamil_alphabet_speak_word;
Similarly how can I assign the dialog box view to another view. Since I am adding all elements to the view "TamilAlphabets" programmatically the child are null it returns for the below code.
Alphbetdialog=new Dialog(TamilAlphabets.this);
Alphbetdialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Alphbetdialog.setContentView(R.layout.tamil_alphabetsdialog);
(adding elements to the layout "TamilAlphabets" code
..............
)
LayoutInflater inflator=(LayoutInflater)TamilAlphabets.this.getSystemService
(TamilAlphabets.this.LAYOUT_INFLATER_SERVICE);
View row=inflator.inflate(tamil_alphabetsdialog, Parent,false);
LinearLayout l1=(LinearLayout)row.findViewById(R.id.alphabetlayout1);
ViewGroup vg=(ViewGroup)l1;
vg.getChildCount();
So I need to assign the dialog box view to another view how do I do that.
I need something like this
View getview=<I need dialog box view>
You should avoid using Dialog class directly and instead use Dialog subclass's or DialogFragment
https://developer.android.com/guide/topics/ui/dialogs.html:
The Dialog class is the base class for dialogs, but you should avoid instantiating Dialog directly. Instead, use one of the following subclasses:
AlertDialog
A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.
DatePickerDialog or TimePickerDialog
A dialog with a pre-defined UI that allows the user to select a date or time.
In any case im guessing that what you want is a custom dialog and what is recomendaded is using the DialogFragment class in that case here is an example
Dialog fragment layout
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
Dialog class
public class DialogExampleFragment extends DialogFragment {
private static final String ARG_PARAM = "extra:PARAM";
private String mText;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle arguments = getArguments();
mText = arguments.getString(ARG_PARAM);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setTitle("title");
return dialog;
}
public static DialogExampleFragment newInstance(String message) {
Bundle args = new Bundle();
args.putSerializable(ARG_PARAM, message);
DialogExampleFragment fragment = new DialogExampleFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_dialog_example, container, false);
TextView t = (TextView) root.findViewById(R.id.textView1);
t.setText(mText);
return root;
}
}
To show as a dialog
DialogExampleFragment.newInstance("Message")
.show(getFragmentManager(), "dialog");
Note since a DialogFragment is a fragment it has de advantage of being able to be shown as a Dialog or as a regular fragment you can get all the information on the link i posted above

Displaying dialog box within fragment

I would like to set up a dialog box in my fragment class. I have done this before with normal activity class, but not with fragment.
I am following this answer HERE that someone has already answered, but I'm getting an error. Will that answer work within a fragment?
I am wanting to open the dialog on image click that I have set up already from an ImageView.
Any help would be great, thanks in advanace.
I would like the dialog code to go:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.image_detail_fragment,
container, false);
mImageView = (RecyclingImageView) v.findViewById(R.id.imageView);
mImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
/HERE
}
});
return v;
}
If you are using fragments then you should be using DialogFragments instead of the old Dialog box.
http://developer.android.com/reference/android/app/DialogFragment.html

Categories

Resources