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
Related
Whenever I use or implement onClickListner in Fragment, the application crashes. I have tried all the methods like implementing the fragment class using on click in XML but the application crashes all the time. Please help me with this. I don’t even write anything in the onClick override method and still, the application crashes.
Button btn_edit;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.multi_frag,container,false);
btn_edit=(Button)rootView.findViewById(R.id.edit);
btn_edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
return inflater.inflate(R.layout.activity_account,container,false);
}
The application is crashing because you are returning a different view and finding the button in a different view.
// In below line you are using R.layout.multi_frag
View rootView=inflater.inflate(R.layout.multi_frag,container,false);
btn_edit=(Button)rootView.findViewById(R.id.edit);
btn_edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
// And here, you are returning R.layout.activity_account.
return inflater.inflate(R.layout.activity_account,container,false);
You cannot use two layouts within one fragment. Check the right layout which contains the button and use that layout in both of the places.
am trying to make the text under my login button "no account yet? signup now" when clicked to send me to my RegisterFragment.so i added an OnClickListener for it like this in my LoginFragment.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView signup = (TextView) getView().findViewById(R.id.signup);
signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginFragment.this.startActivity(new Intent(getActivity(), RegisterFragment.class));
}
});
but when i run the code the app crashes on this line
TextView signup = (TextView) getView().findViewById(R.id.signup);
this is my fragment_login.xml file
<TextView android:id="#+id/signup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="No Account Yet? Signup_now"
android:gravity="center"
android:textSize="16dp"
android:clickable="true"/>
please guys i really need your help.
onCreateView() method is responsible for creating view, so you should first create it.
You can't directly use getView() inside onCreateView().
You need to inflate it first like
View view = inflater.inflate(R.layout.my_layout, container, false);
Then use
TextView signup = (TextView) view.findViewById(R.id.signup);
So full code should look like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_layout, container, false);
TextView signup = (TextView) view.findViewById(R.id.signup);
signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return view;
}
On side note, setting up click listener initializing TextView's etc should be done inside onViewCreated(). onCreateView() should just create & return the view.
like
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
TextView signup = (TextView) view.findViewById(R.id.signup);
// and so on...
}
I would recommend not setting a click listener into something you want to type into
You can't call getView before onCreateView has actually returned a proper View
findViewById in Fragment
RegisterFragment.class is not an Activity, you cannot startActivity for it.
Use the FragmentManager
getSupportFragmentManager().beginTransaction()
.replace(R.id.<someContainer>, RegisterFragment.class)
.commit();
Or you can see the documentation on Communication between Fragments to see how you might implement on onLogin() or onRegistrationSelected() action to swap out to the registration fragment or "post-login" main fragment.
by this answer
I can't understand where to put my onClickListener() - inside onCreateView() or inside onActivityCreated() , below codes describe it better:
CODE A: (Setting Button click listener inside onActivityCreated())
private FloatingActionButton bt;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do something.
}
});
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.first_frag, container, false);
bt = (FloatingActionButton) v.findViewById(R.id.fab);
return v;
}
CODE B: (Setting Button click listener inside onCreateView())
private FloatingActionButton bt;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.first_frag, container, false);
bt = (FloatingActionButton) v.findViewById(R.id.fab);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do something.
}
});
return v;
}
I may have not understood which code is better because of my poor English, anyway, thank you all :)
Both will have no effect as far as I know. Once the view is inflated you can put it anywhere either in onCreateView() or in onActivityCreated().
After all, for binding views and setting click listeners, onViewCreated() is a better candidate though, as it will be called immediately after onCreateView. It clearly suggests that your view has been inflated.
There is no specific reason or rule for it. Google itself doesn't care much about it. As a rule of thumb, you can put it anywhere you want once the view is inflated.
I would suggest putting the onClickListener it inside the onActivityCreated. And binding the button to the view inside onCreateView. Just like you have done the first time in your question.
To read more regarding the methods you can go through this post
Since onActivityCreated was deprecated in API level 28, it's probably wise to put it in onCreateView!
I'm new to android, I'm facing an issue! I created a custom Dialog fragment which displayed the image when I click on it. and the Dialog fragment appears with that image. when I click outside nothing happens. And when I click little more down it displays the below image. I have surfed and I tried any methods but couldn't get it right! I think the custom Dialog fragment is taking the whole view.I tried dismiss(), setCanceledOnTouchOutside(true),implemented DialogInterface.OnCancelListener but i'm not able to dismiss the dialog fragment. Kindly Help.
here is my code:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.frag_layout, container, false);
Bitmap bitImage=getArguments().getParcelable("BitImage");
ImageView imageView=(ImageView)view.findViewById(R.id.fragid);
imageView.setImageBitmap(bitImage);
return view;
}
#Override
public void onCancel(DialogInterface dialog) {
dialog.dismiss();
}
}
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.