Passing Data from acitvity to fragment android studio - android

--This is my Fragment :--
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_save, container, false);
savedAlamat = view.findViewById(R.id.saved_alamat);
savedContac = view.findViewById(R.id.saved_kontak);
savedName = view.findViewById(R.id.saved_nama);
String malamat = getArguments().getString("mAlamat");
String mcontac = getArguments().getString("mContac");
String mname = getArguments().getString("mname");
savedAlamat.setText(malamat);
savedContac.setText(mcontac);
savedName.setText(mname);
return view;
}
//and This is main acivity that constarin framelayout :
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_save, container, false);
savedAlamat = view.findViewById(R.id.saved_alamat);
savedContac = view.findViewById(R.id.saved_kontak);
savedName = view.findViewById(R.id.saved_nama);
String malamat = getArguments().getString("mAlamat");
String mcontac = getArguments().getString("mContac");
String mname = getArguments().getString("mname");
savedAlamat.setText(malamat);
savedContac.setText(mcontac);
savedName.setText(mname);
return view;
}
--I don't understand why I keep get NotNULLobject, i want to make a passing data between that activity and fragment.

In Activity, write this code
Bundle bundle = new Bundle();
bundle.putString("mAlamat", "Data to send"); // set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);
In Fragment class, receive data like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("mAlamat");
return inflater.inflate(R.layout.fragment, container, false);
}
Courtesy

Related

How use a button inside a fragment?

I'm trying to use 4 fragments
[F1]
[F2][F3][F4]
F1 will have 3 buttons to change between F2,F3 and F4 but i don´t know how to use the buttons properties like in a normal Activity
That's what i have in my F1 code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_01, container, false);
}
Check this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_01, container, false);
// here you have the reference of your button
Button yourButton = (Button)view.findViewById(R.id.your_button_id);
return view;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_01, container, false);
Button firstButton = (Button)view.findViewById(R.id.firstbutton);
Button seconButton = (Button)view.findViewById(R.id.seconbutton);
Button thirdButton = (Button)view.findViewById(R.id.thirdbutton);
return view;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_one, container, false);
Button button = (Button) rootView.findViewById(R.id.buttonSayHi);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
onButtonClicked(v);
}
});
return rootView;
}
public void onButtonClicked(View view)
{
//do your stuff here..
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.frameLayoutFragmentContainer, new FragmentTwo(), "NewFragmentTag");
ft.commit();
ft.addToBackStack(null);
}
}
It's a simple way to reference a button or any widget in a fragment.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fragment_one, container, false);
btnOne = view.findViewById(R.id.btnOne);
return view;
You should get the view first, and then get the button from the view
like this:
View rootView = inflater.inflate(R.layout.fragment_01, container, false);
Button btn = (Button) rootView.findViewById(R.id.btn);
You should do some tutoriasl first, its not that hard.

how pass parameters between two fragment?

i must pass two parameters from fragment to another fragment. First parameter is type String and second parameter is type int. i want pass parameters with bundle but doesn't work.
FIRST FRAGMENT
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt("totale", totalAmount);
fragment.setArguments(bundle);
SECOND FRAGMENT
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
TextView titolo2 = (TextView) getView().findViewById(R.id.quantità2);
Bundle bundle=this.getArguments();
int myInt = bundle.getInt("totale", 0);
titolo2.setText(myInt);
return inflater.inflate(R.layout.fragment_two, container, false);
}
Change Second Fragment like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_two, container, false);
TextView titolo2 = rootView.findViewById(R.id.quantità2);
Bundle bundle=this.getArguments();
int myInt = bundle.getInt("totale", 0);
titolo2.setText(myInt + "");
return rootView;
}
In first fragment write this:
TwoFragment fragment = new TwoFragment ();
Bundle bundle = new Bundle();
bundle.putInt("totale", totalAmount);
fragment.setArguments(bundle);
Second fragmnent:
Bundle bundle=getArguments();
int myInt = bundle.getInt("totale", 0);
titolo2.setText(myInt);
Create a public class with two static members like this :
public class SomeClass {
public static String stringValue="";
public static int integerValue=0;
}
Then from your first fragment just set values like this :
SomeClass.stringValue="yourValue";
SomeClass.integerValue=yourIntegerValue;
You can use these variable from your second fragment where you want :
int a =SomeClass.integerValue;
String str=SomeClass.stringValue;
If both fragments belong to the same activity, better to use this approach:
In activity you should save references to fragments, and all comunications between this fragments should be through activity, let's name it BaseActivity. For example add to activity method which will pass params to second fragment:
void updateSecondFragment(int param1, String param2){
mFragment2.updateTitle(param2);
}
In second fragment:
void updateTitle(String title){
mTextView.setText(title);
}
And in first fragment:
BaseActivity mActivity;
#Override
void onAttach(Activity activity) {
if (activity instanceOf BaseActivity){
mActivity = (BaseActivity) activity;
}
}
private void updateSecondFragment(){
if (mActivity != null){
mActivity.updateSecondFragment(int param1, String param2);
}
}
You should do the work of second fragment as:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two, container, false);
}
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView titolo2 = view.findViewById(R.id.quantità2);
Bundle bundle=getArguments();
int myInt = bundle.getInt("totale", 0);
titolo2.setText(String.valueOf(myInt));
}

i have an error in my rootView

#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
et=(EditText)rootView.findViewById(R.id.et1);
String str=et.getText().toString();
Fragment frag=new Fragment();
Bundle bundle1=new Bundle();
bundle1.putString("name", str);
frag.setArguments(bundle1);
getFragmentManager().beginTransaction().add(R.id.container_body, frag).commit();
return inflater.inflate(R.layout.fragment_currentmovie, container, false);
}
}
it is displaying rootView cannot be resolved,i need to add edit text et to string str so that using bundle i can pass it to the next fragment
Try this Add inflater then return.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_currentmovie, container, false);
et=(EditText)rootView.findViewById(R.id.et1);
String str=et.getText().toString();
Fragment frag=new Fragment();
Bundle bundle1=new Bundle();
bundle1.putString("name", str);
frag.setArguments(bundle1);
getFragmentManager().beginTransaction().add(R.id.container_body, frag).commit();
return rootView ;
}
}
You need to inflate it first:
View rootView = inflater.inflate(R.layout.fragment_currentmovie, container, false);
...
return rootview;

How to get an id of a View inside a Fragment

Hi I am trying to get an id of some View in a Fragment from a string, however it doesn't seem to work... Is there a special case for fragments?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View singleplayerView = inflater.inflate(R.layout.singleplayer_tab, container, false);
setupMap();
return singleplayerView;
}
private void setupRank() {
TextView view = (TextView) getByStringId("rank0");
view.setText("hello");
}
private final View getByStringId(final String id) {
return findViewById(getResources().getIdentifier(id, "id", getActivi
ty().getPackageName()));
}
So I am trying to set the textView field rank0 in the layout that is used for my fragment... but right now it gets a NUllPointer because it couldnt find the id
Thank you
you can define your Layout In your onCreateView() inside Fragments like this:
View V = new View(getActivity());
V = inflater.inflate(R.layout.fragment_layout,container, false);
Then
TextView SomeField = (TextView)V.findViewById(R.id.TxtSomeField);
Inside fragment, add getActivity before findViewById:
getActivity().findViewById(...);
Try this,
String id;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extra = getArguments();
id = extra.getString("id", "");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_calendar_details, container, false);
Button b = (Button) view.findViewById(id);
return view;
}

How to transfer value from one fragment to another fragment

Example
I do not understand this example.
How to apply in my program
I try to write In my program
General use that I can.
But replaced fragment.
I do not know how to use.
Pass the value of the object.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.templistmune, container, false);
FragmentManager DS2 = getFragmentManager();
FragmentTransaction DSE2 = DS2.beginTransaction();
Fragment DF2 = DS2.findFragmentById(R.id.frameLayout4);
if (DF2 == null) {
String title = "Fragment A";
templistview2 usermune = new templistview2(title);
DSE2.add(R.id.frameLayout4, usermune);
DSE2.addToBackStack(null);
DSE2.commit();
/////////////////////////////////////////////////////////
Bundle bundle = new Bundle();
String SAS="50";
bundle.putString("ST", SAS);
DF2.setArguments(bundle);
////////////////////////////////////////////////
}
The pick value the object
templistview2.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.templistmune2, container, false);
////////////////////////////////////////////////////////////////////////////////////
Bundle bundle = this.getArguments();
String myST = bundle.getString("ST", SAS);
///////////////////Error SAS cannot be resolved to a variable /////////////////
return view;
}
You're only adding adding the Bundle when DF2 is null... so your program will either crash with a NullPointerException somewhere along the line or run but not have the Bundle passed.
So place
Bundle bundle = new Bundle();
String SAS="50";
bundle.putString("ST", SAS);
DF2.setArguments(bundle);
outside the if structure.

Categories

Resources