I follow a youtube tutorial to write an application. this codes is a part of my recyclerViewAdapter
vHolder.item_contact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView dialog_name_tv = (TextView) myDialog.findViewById(R.id.dialog_name_id);
TextView dialog_phone_tv = (TextView) myDialog.findViewById(R.id.dialog_phone_id);
ImageView dialog_contact_img = (ImageView) myDialog.findViewById(R.id.dialog_img);
TextView dialog_detail_tv = (TextView) myDialog.findViewById(R.id.dialog_detail);
dialog_name_tv.setText(mData.get(vHolder.getAdapterPosition()).getName());
dialog_phone_tv.setText(mData.get(vHolder.getAdapterPosition()).getPhone());
dialog_contact_img.setImageResource(mData.get(vHolder.getAdapterPosition()).getPhoto());
dialog_detail_tv.setText(mData.get(vHolder.getAdapterPosition()).getDetail());
Toast.makeText(mContext, "Test Click" + String.valueOf(vHolder.getAdapterPosition()), Toast.LENGTH_SHORT).show();
myDialog.show();
Button dialog_add_tv = (Button) myDialog.findViewById(R.id.dialog_add);
dialog_add_tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText dialog_quantity_tv = (EditText) myDialog.findViewById(R.id.dialog_quantity);
quantity = dialog_quantity_tv.getText().toString();
name = mData.get(vHolder.getAdapterPosition()).getName()+"\n";
price = mData.get(vHolder.getAdapterPosition()).getPhone();
Intent i = new
Intent(getActivity(),FragmentFav.class);
Bundle bundle = new Bundle();
bundle.putString("choices",choices);
bundle.putDouble("price",price);
i.putExtras(bundle);
startActivity(i);
}
});
}
});
return vHolder;
}
I want to Clicking the dialog_add_tv Button to pass the data to the fragment.class
public class FragmentFav extends Fragment {
View v;
public FragmentFav() {
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.call_fragment, container, false);
return v;
}
}
I try many times by using intent still happen error.
Using intent you can't open fragment, use below code for open fragment and also pass data to fragment
// define a framelayout in activity xml
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_container"></FrameLayout>
// then write below code on the click of dialog_add_tv button
Bundle bundle = new Bundle();
bundle.putString("choices",choices);
bundle.putDouble("price",price);
FragmentFav frgFav=new FragmentFav();
frgFav.setArguments(bundle);
FragmentTransaction fragmentTrasaction=getFragmentManager().beginTransaction();
fragmentTrasaction.add(R.id.fragment_container,frgFav,"frg");
fragmentTrasaction.commit();
// then get data in fragment like this
String choice=getArguments().getString("choices");
String price=getArguments().getString("price");
Here are the step you can follow and implement what you need
Step 1:
Create an interface between adapter and fragment
Step 2:
Implement an interface in Adapter like this:
vHolder.item_contact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mProfileItemClickListener.itemContact(v);
}
// aka Pass the whole view through an interface
Step 3:
In an interface, class add this method
void itemContact(View mView);
Step 4:
Implement an interface in Fragment:
Where you will obtain an override method and perform your operation there
#Override
public void itemContact(ImageView mProfileImageView) {
Your code here
}
You can handle callback properly while passing data between the views.
Related
I'm trying to get a Toast to appear in a tabbed fragment when an EditText is left empty.
Here is my MainActivity's onCreate:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
}
My Tabbed Fragment works fine with this code:
public class SignupFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_signup, container, false);
}
}
But it doesn't work when creating the Toast like this:
public class SignupFragment extends Fragment {
EditText textFillCheck;
Button submitCheck;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
submitCheck = (Button) submitCheck.findViewById(R.id.signupBtn);
textFillCheck = (EditText) textFillCheck.findViewById(R.id.signupFirstName);
submitCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (TextUtils.isEmpty(textFillCheck.getText().toString())) {
Intent intent = new Intent(getActivity(), SignupFragment.class);
startActivity(intent);
Toast.makeText(getActivity(), "Please fill in all fields", Toast.LENGTH_SHORT).show();
}
else{Toast.makeText(getActivity(), textFillCheck.getText().toString(), Toast.LENGTH_LONG).show();
}
}
});
return inflater.inflate(R.layout.fragment_signup, container, false);
}
}
You're not accessing the views of your Fragment's layout so the layout you're returning in onCreateView() doesn't have your logic attached to it. You should set up your Fragment to look something like this:
public class SignupFragment extends Fragment {
EditText textFillCheck;
Button submitCheck;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
// Store a reference to your Fragment's inflated layout
View root = inflater.inflate(R.layout.fragment_signup, container, false);
// Use the reference to access your Fragment's views
submitCheck = (Button) root.findViewById(R.id.signupBtn);
textFillCheck = (EditText) root.findViewById(R.id.signupFirstName);
submitCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(TextUtils.isEmpty(textFillCheck.getText().toString())) {
Toast.makeText(getActivity(), "Please fill in all fields", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getActivity(), textFillCheck.getText().toString(), Toast.LENGTH_LONG).show();
}
}
});
// Return the inflated layout
return root;
}
}
I am amazed why your code is not having any error like VIEW DOES NOT EXIST or NULL POINTER EXCEPTION because
First, you change the activity before showing the Toast secondly you are inflating the layout after the initialization of views and not getting the context properly, Right way of doing what you are trying to achieve is using OnViewCreated() like this
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
submitCheck = (Button) view.findViewById(R.id.signupBtn);
textFillCheck = (EditText) view.findViewById(R.id.signupFirstName);
submitCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (TextUtils.isEmpty(textFillCheck.getText().toString())) {
Toast.makeText(getActivity(), "Please fill in all fields",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), SignupFragment.class);
startActivity(intent);
}
else{
Toast.makeText(getActivity(), textFillCheck.getText().toString(),
Toast.LENGTH_LONG).show();
}
}
});
}
Need to create a button to go to a new activity in Fragments. I tried with the following code but its not working, This is my code,
public class UserFragment extends Fragment {
Button mTextuserinfo;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_user,container,false);
mTextuserinfo = (Button) findViewById(R.id.userinfo_user);
mTextuserinfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(UserFragment.this, userinformation.class);
startActivity(i);
}
});
return view;
}
}
Use this in your onClick.
Intent i = new Intent(getActivity(), userinformation.class);
startActivity(i);
Also define your button like this:
mTextuserinfo = view.findViewById(R.id.userinfo_user);
Hello I want to move to another activity from my fragment. I ve tried almost every solution which I found, but after I press button my app is instantly closed. I am new in java, and i guess i need some help.
public class GalleryFragment extends Fragment {
private GalleryViewModel galleryViewModel;
Intent intent;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_gallery, container, false);
intent = new Intent(GalleryFragment.this.getActivity(), HomeFragment.class);
final Button btn = (Button) rootView.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(intent);
}
});
galleryViewModel =
ViewModelProviders.of(this).get(GalleryViewModel.class);
View root = inflater.inflate(R.layout.fragment_gallery, container, false);
final TextView textView = root.findViewById(R.id.text_gallery);
galleryViewModel.getText().observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});
return root;
}
}
I'm assuming from this line:
intent = new Intent(GalleryFragment.this.getActivity(), HomeFragment.class);
That you're trying to launch a Fragment, not an Activity. In order to transition to another Fragment, replace your Intent with:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getFragmentManager()
.beginTransaction()
.replace(...) //Or add, depends on your use case
.commit();
}
});
I have a problem with sending data from fragment to fragment. I have DialogFragment named fmonday, it is Viewpager's fragment.
I call other DialogFragment, named AlertDFragment to add some data to my fragment. I can read the data from spinner, it's working good.
Now I need to send one variable, type string, from AlertDFragment to fmonday.
Here is the code of fmonday:
public class fmonday extends DialogFragment implements LoaderManager.LoaderCallbacks<Cursor> {
DB db;
Button button12;
DialogFragment dlg1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dlg1 = new AlertDFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fmonday, container, false);
button12 = (Button) rootView.findViewById(R.id.button12);
button12.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
dlg1.show(getFragmentManager(), "dlg1");
}
});
return rootView;
}
And code of AlertDFragment:
public class AlertDFragment extends DialogFragment {
Spinner spin;
DB db;
String string1;
Button button13;
private String namestr;
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
LayoutInflater li = LayoutInflater.from(getActivity());
View fdfS = li.inflate(R.layout.fdf, null);
adb.setView(fdfS);
spin=(Spinner)fdfS.findViewById(R.id.spinner);
db = new DB(getActivity());
db.open();
spin.setOnItemSelectedListener(new OnSpinnerItemClicked());
loadSpinnerData();
button13 = (Button) fdfS.findViewById(R.id.button13);
button13.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getActivity().getApplicationContext(), "Clicked : " +
string1, Toast.LENGTH_LONG).show();
getDialog().dismiss();
}
});
return adb.create();
}
I can't find the way to send this string1 variable to my fmonday DialogFragment, because simple intent doesn't work with non-activity things. Also read some advices about bundle, but couldn't find out how to work with it.
Thanks
UPDATE
fmonday:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dlg1 = new AlertDFragment();
string1 = getArguments().getString("latitude");
}
AlertDFragment:
public class AlertDFragment extends DialogFragment {
Spinner spin;
DB db;
String string1;
Button button13;
private String namestr;
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
LayoutInflater li = LayoutInflater.from(getActivity());
View fdfS = li.inflate(R.layout.fdf, null);
adb.setView(fdfS);
spin=(Spinner)fdfS.findViewById(R.id.spinner);
db = new DB(getActivity());
db.open();
spin.setOnItemSelectedListener(new OnSpinnerItemClicked());
loadSpinnerData();
button13 = (Button) fdfS.findViewById(R.id.button13);
button13.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getActivity().getApplicationContext(), "Clicked : " +
string1, Toast.LENGTH_LONG).show();
Bundle bundle = new Bundle();
bundle.putString("latitude", string1);
fmonday alertdfragment= new fmonday();
alertdfragment.setArguments(bundle);
getDialog().dismiss();
}
});
return adb.create();
}
To Set Data:
Bundle bundle = new Bundle();
bundle.putString("latitude", latitude);
MapFragment mapFragment = new MapFragment();
mapFragment.setArguments(bundle);
To Get Data
String latitude = getArguments().getString("latitude")
You can crate a setter to your second dialogFragment ex:
public void setSmth(String value)
{
this.myData = value;
}
, than on your first dialog
DialogFragment dlg1 = new AlertDFragment();
dlg1.setSmth(your data here);
before showing the second one;
fragment1:
public class fragment1 extends Fragment implements View.OnClickListener {
ImageButton but, but1, but2;
ImageView view;
#Override << this one
public View OnCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment1, null);
but = (ImageButton) myView.findViewById(R.id.imageButton11);
but.setOnClickListener(this);
but1 = (ImageButton) myView.findViewById(R.id.imageButton1);
but1.setOnClickListener(this);
but2 = (ImageButton) myView.findViewById(R.id.imageButton2);
but2.setOnClickListener(this);
return myView;
}
#Override
public void onClick(View v) {
main xxx = (main)getActivity();
switch (v.getId()) {
case R.id.imageButton11:
xxx.str="but1";
break;
..
}}
main:
Fragment frag1 = new fragment1();
fr1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fTrans = getFragmentManager().beginTransaction();
fTrans.replace(R.id.frameLayout2, frag1);
fTrans.addToBackStack(null);
fTrans.commit();
}
});
method does not override method from its superclass in fragment1(first override)
but without "implemets View.OnClickListener" it works
Spelling mistake. Name the method onCreateView instead of OnCreateView.
I am not familiar with the android sdk but I think you need something like this
Fragment frag1 = new fragment1(){
#Override
public void onClick(View view) {
fTrans = getFragmentManager().beginTransaction();
fTrans.replace(R.id.frameLayout2, frag1);
fTrans.addToBackStack(null);
fTrans.commit();
}
};
return your inflated view
public View onCreateView(.....) {
...
...
return InputFragmentView;
}
and change OnCreateView to onCreateView.
you can tap ctrl+o to get it ready ^^
In my case I was not extending it from Fragment. Make sure you extend your fragment from Fragment class.
public class fragment1 extends Fragment implements View.OnClickListener {
#Override
/// public View onCreateView.......
}