I have two fragment which i want to pass data from one fragment to another,the bundle has value from first fragment when sending but not receiving in another fragment.Here is the code:
First Fragment :
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.customer_one,container,false);
edemail = v.findViewById(R.id.ed_email);
edpass = v.findViewById(R.id.ed_pass);
btn = v.findViewById(R.id.btn_next);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
email = edemail.getText().toString().trim();
password = edpass.getText().toString().trim();
Customer_Frag2 cf2 = new Customer_Frag2();
Bundle bundle = new Bundle();
bundle.putString("Email",email);
bundle.putString("Password",password);
cf2.setArguments(bundle);
Log.d("Test2",bundle.toString());
}
});
return v;
}
Second Fragment:
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable
ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.customer_two,container,false);
Bundle bundle = getArguments();
if(bundle!=null){
email = bundle.getString("Email");
pass = bundle.getString("Password");
}
Log.d("Test",email+pass);
return v;
}
Related
So I haven't seen any question like this and I have a problem I don't know how to start the other class from the fragment. I have been trying all day and I still can't find the answer. First of all how do I start my imageadapter class and how the hell can I write additional methods into the class and then execute them in the fragment class?
public class ImageFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_image, container, false);
GridView gridView = (GridView)getView().findViewById(R.id.gridView);
gridView.setAdapter(new imageadapter(getContext()));
}
}
but why does this then not work ?
public class ImageFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_image, container, false);
}
#Override
public void onViewCreated(#NonNull final View view, #Nullable Bundle savedInstanceState) {}
GridView gridView = (GridView)getView().findViewById(R.id.gridView);
gridView.setAdapter(new imageadapter(this));
}
I still get errors by the words this and the setAdapter.
All you need to do is add an onViewCreated method where you can do call your methods and other stuff. Here's my code.
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_upcoming, container, false);
}
#Override
public void onViewCreated(#NonNull final View view, #Nullable Bundle savedInstanceState) {
//you can call your methods and find your views in here
setHasOptionsMenu(true);
final FloatingActionButton fab = view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), CreateEditAppointmentActivity.class);
startActivityForResult(intent, RC_ADD_CLIENT);
}
});
coordinatorLayout = view.findViewById(R.id.upcomingLayout);
}
I want to be able to click a button on my settings fragment, which will change an image on another fragment called home fragment. My app keeps crashing once i click the button and im not sure why.
Settings Fragment :
public class SettingsFragment extends Fragment {
public Button button;
public ImageView Torso;
public ImageView ShopGreenTorso;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
button = (Button) getView().findViewById(R.id.button1);
Torso = (ImageView) getView().findViewById(R.id.Torso);
ShopGreenTorso= (ImageView) getView().findViewById(R.id.ShopGreenTorso);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Purchased", Toast.LENGTH_SHORT).show();
Intent i = new Intent(SettingsFragment.this.getActivity(), AvatarFragment.class);
//i.putExtra("resID", R.drawable.torso2green);
Bundle b= new Bundle();
b.putInt("resID", R.drawable.torso2green);
SettingsFragment.this.setArguments(b);
//setArguments(bundle);
startActivity(i);
}
});
}
Avatar Fragment:
public class AvatarFragment extends Fragment {
public ImageView IV;
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_avatar, container, false);
// init image view
IV = (ImageView) rootView.findViewById( R.id.Torso );
// getting extras
//Bundle bundle = getActivity().getIntent().getExtras();
// Bundle bundle=getArguments();
Bundle b=getArguments();
if(b!= null)
{
int resid = b.getInt("resID");
IV.setBackgroundResource(resid);
}
return rootView;
}
}
I believe the error is coming from IV.setImageResource(resid) but im not entirely sure, any help would be much appreciated.
It's hard to tell without seeing the exception, but it might be because Bundle mappings are case-sensitive.
Your SettingsFragment uses "ResId", but your HomeFragment is expecting "resID". If no mapping is found in the Bundle, you'll be given the default value of 0 for resID. It's possible that you're crashing when calling IV.setImageResource(0);.
Your image implementation in code is wrong, you need to init your image view like this :
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_avatar, container, false);
// init image view
IV = (ImageView) rootView.findViewById( R.id.Torso );
// getting extras
Bundle bundle = getActivity().getIntent().getExtras();
if(bundle!= null)
{
int resid = bundle.getInt("resId");
IV.setImageResource(resid);
}
return rootView;
}
Don't pass value from Fragment to Fragment directly.
You should use setArguments from SettingsFragment and getArguments in HomeFragment
public class select_fragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_select, null);
}
private void button_parking(){
Intent myIntent = new Intent(f, parking.class);
startActivity(myIntent);
}
}
try this...
public class select_fragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
Button your_button = (Button) getActivity.findViewById(R.id.your_id_button)
your_button.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
button_parking();
}
});
return inflater.inflate(R.layout.fragment_select, null);
}
private void button_parking(){
Intent myIntent = new Intent(getActivity(), parking.class);
startActivity(myIntent);
}
}
You haven't bind the view to your fragment so the click button can't work. You need to bind the view with findViewById(). Usually you need to do the binding by overriding onViewCreated() something like this:
public class select_fragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_select, null);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// bind the view here.
Button button = findViewById(R.id.your_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//call button method here
button_parking();
}
});
}
private void button_parking() {
Intent myIntent = new Intent(f, parking.class);
startActivity(myIntent);
}
}
Butterknife is working well when i am using it on Activities, but not working when I am trying to use it in a fragment :
public class SettingsFragment extends Fragment {
private static final String TAG = "SettingsFragment";
#BindView(R.id.settings_email) TextView _settingsMail;
#BindView(R.id.settings_password) TextView _passwordMail;
#BindView(R.id.settings_token) TextView _tokenMail;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
//returning our layout file
final View view = inflater.inflate(R.layout.fragment_3_settings, container, false);
ButterKnife.bind(this, view);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getContext());
String email = prefs.getString("email", null);
String passwd = prefs.getString("password", null);
String token = prefs.getString("token", null);
_settingsMail.setText(email);
_passwordMail.setText(passwd);
_tokenMail.setText(token);
return inflater.inflate(R.layout.fragment_3_settings, container, false);
}
You have to return the view that you already have inflated, not inflate another view again.
Instead of:
return inflater.inflate(...);
Do this:
return view;
Try waiting until you know the view has been fully constructed before altering the layout.
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
//returning our layout file
final View view = inflater.inflate(R.layout.fragment_3_settings, container, false);
ButterKnife.bind(this, view);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getContext());
String email = prefs.getString("email", null);
String passwd = prefs.getString("password", null);
String token = prefs.getString("token", null);
_settingsMail.setText(email);
_passwordMail.setText(passwd);
_tokenMail.setText(token);
}
why nobody uses unbinder?
according to documentation , if you work with fragments, you have to use unbinder
private Unbinder unbinder;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate...;
unbinder = ButterKnife.bind(this, view);
return view;
}
and
#Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
<ImageView
android:layout_marginTop="#dimen/padding_top_8dp
android:layout_centerHorizontal="true"
android:tint="#color/colorPrimary"
android:padding="#dimen/padding_top_8dp"
android:src="#drawable/ic_scan_qr_code"
android:id="#+id/qr__code_icon_iv"
android:background="#drawable/circle_shape_white"
android:layout_width="40dp"
android:layout_height="40dp" />
remove #id/qr__code_icon_iv
add #id/qr_code_icon_iv
This work for me:
...
#BindView(R.id.text_input)
TextView text_input;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
ButterKnife.bind(this, view);
return view;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
text_input.setText("Hello Lima, PerĂº");
...
Can't get this working, why am I getting "requestFeature() must be called before adding content"?
this is a Fragment.
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getActivity().requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
final View rootView = inflater.inflate(R.layout.expand, container, false);
getActivity().setContentView(R.layout.expand);
getActivity().getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
ExpandList = (ExpandableListView) rootView.findViewById(R.id.exp_list);
PD = new ProgressDialog(getActivity());
PD.setMessage("Loading.....");
PD.setCancelable(false);
makejsonobjreq();
return rootView;
}