How to access multiple views in a fragment in android? - android

HomeFragment.java:
public class HomeFragment extends Fragment {
FirebaseAuth fAuth;
String userId;
Button logout;
FirebaseFirestore fStore;
EditText name,no,mail;
public HomeFragment() {
}
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
name=(EditText)view.findViewById(R.id.fname);
mail=(EditText)view.findViewById(R.id.emailid);
no=(EditText)view.findViewById(R.id.phone);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
I have my xml file(fragment_home.xml) which contains three TextView's which Iam unable to modify and my app crashes when trying to do so. I need to set my textview with the data from the firebase.
Please help me find a solution

Related

Child Fragment is not animated on EnterTransition

After changing to the Android Navigation Component I encountered some problems with migrating my Activity Transitions to the corresponding Fragment Transitions.
Specifically, it appears that the EnterTransition of a Fragment doesn't apply to it's Child Fragments.
I've set up a test activity that contains an OuterTestFragment, which in turn consists of a TextView and another fragment, InnerTestFragment.
A button in the activity then replaces the OuterTestFragment with a new OuterTestFragment to see if the contents transition smoothly.
Here are the relevant classes:
Activity:
public class PlaygroundActivity extends AppCompatActivity {
public static final ArrayList<String> strings = new ArrayList<>();
static {
strings.add("sgdgdgsdfggggggggsdgsdfgsd\nsdsdgsdfgds\ndfgsd\nsdsdgsdfgds\ndfgsd");
strings.add("sgdgdgsdfgggg\nggggsndfgsd\nsdsdgsdfgds\ndfgsd");
strings.add("sgdgsd\nsdsdgsdfgds\ndfgsd");
strings.add("sgdgdgsdfggfgds\ndfgsd");
strings.add("sgdgdgsdfggggggggsdg");
}
int count = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playground);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragmentContainerOuter, OuterTestFragment.getInstance("This is an inner text", "Outer text!"))
.commit();
}
public void replace(View v) {
getSupportFragmentManager().beginTransaction()
.setReorderingAllowed(true)
.addToBackStack("replaced")
.replace(R.id.fragmentContainerOuter, InnerTestFragment.getInstance(count++ >= 4 ? "Default" : strings.get(count)))
.commit();
}
}
OuterTestFragment:
public class OuterTestFragment extends TransitionedFragment {
private static final String ARG_OUTER_TEXT = "OuterTestFragment:outerText";
private static final String ARG_INNER_TEXT = "OuterTestFragment:innerText";
private String innerText;
private String outerText;
public static OuterTestFragment getInstance(String inner, String outer) {
OuterTestFragment outerFragment = new OuterTestFragment();
Bundle args = new Bundle();
args.putString(ARG_OUTER_TEXT, outer);
args.putString(ARG_INNER_TEXT, inner);
outerFragment.setArguments(args);
return outerFragment;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
innerText = getArguments().getString(ARG_INNER_TEXT);
outerText = getArguments().getString(ARG_OUTER_TEXT);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.test_fragment_outer, container, false);
((TextView) layout.findViewById(R.id.textOuter)).setText(outerText);
getFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, InnerTestFragment.getInstance(innerText))
.commit();
return layout;
}
}
TransitionedFragment:
public abstract class TransitionedFragment extends Fragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TransitionInflater inflater = TransitionInflater.from(getContext());
setEnterTransition(inflater.inflateTransition(R.transition.default_transition));
setReenterTransition(inflater.inflateTransition(R.transition.default_transition));
setSharedElementEnterTransition(inflater.inflateTransition(R.transition.scaled_img_clip_transition));
}
}
Inner Test Fragment:
public class InnerTestFragment extends Fragment {
private static final String ARG_TEXT = "InnerTestFragment:text";
private String text;
public static InnerTestFragment getInstance(String text) {
InnerTestFragment frgmt = new InnerTestFragment();
Bundle args = new Bundle();
args.putString(ARG_TEXT, text);
frgmt.setArguments(args);
return frgmt;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
text = getArguments().getString(ARG_TEXT);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.test_fragment_inner, container, false);
((TextView) layout.findViewById(R.id.text)).setText(text);
return layout;
}
}
Any help is much appreciated, as the Transition API drives me nuts....
You may show transitions with the setEnterTransition method.
Fragment fragment = InnerTestFragment.getInstance(innerText);
Fade enterFade = new Fade(); //can be any transition, SlideTransition or Inflated Transitions
enterFade.setDuration(300); //Duration of transition in ms
fragment.setEnterTransition(enterFade);
getFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, fragment)
.addToBackStack(fragment.getClass().getSimpleName())
.commit();
Try to add a background color to the child fragment...

How to get fragment index from within itself?

I have 3 fragments that are generated from the same class file. These three fragments exist within a ViewPager and a FragmentStatePagerAdapter.
They are in sliding tabs. They are created once and never dismissed or deleted or go out of memory.
The app works fine so far, but I need to figure out how to make the fragments have knowledge of their own index (1,2, or 3).
Is there some code like:
int myIndex = summonTheAllKnowingThing.getWhatFragmentThisIsPlease();
That I can add somewhere in the following code?
public class BlankFragment extends Fragment implements View.OnClickListener {
private boolean isDone = false;
EditText persons_name;
Spinner disc1_spinner;
Spinner disc2_spinner;
Spinner disc3_spinner;
Spinner fee1_spinner;
Spinner fee2_spinner;
EditText custom_disc;
EditText custom_fee;
Button doneButton;
public BlankFragment() {
// Required empty public constructor
}
public static BlankFragment newInstance() {
BlankFragment fragment = new BlankFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_blank, container, false);
doneButton = (Button) v.findViewById(R.id.doneButton);
doneButton.setOnClickListener(this);
return v;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// getView().findViewById(R.id.
persons_name = (EditText) getView().findViewById(R.id.NAME);
disc1_spinner = (Spinner) getView().findViewById(R.id.DISC1);
disc2_spinner = (Spinner) getView().findViewById(R.id.DISC2);
disc3_spinner = (Spinner) getView().findViewById(R.id.DISC3);
custom_disc = (EditText) getView().findViewById(R.id.customDISC);
custom_fee = (EditText) getView().findViewById(R.id.customFEE);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.doneButton:
if MainActivity.is1Done
doneButton.setBackgroundColor(Color.RED);
MainActivity.name1 = persons_name.getText().toString();
break;
}
}
}
Best approach is to pass the index while creating the fragment in pager adapter as in
#Override
public Fragment getItem(int position) {
return BlankFragment.newInstance(position);
}
and BlankFragment would be
public class BlankFragment extends Fragment implements View.OnClickListener {
private int myIndex; //<-- access it for getting its index
//.. some other variables
public BlankFragment() {
// Required empty public constructor
}
public static BlankFragment newInstance(int indexInPager) {
BlankFragment fragment = new BlankFragment();
Bundle bundle = new Bundle();
bundle.putInt("index",indexInPager);
fragment.setArguments(bundle);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
myIndex = getArguments().getInt("index");
View v = inflater.inflate(R.layout.fragment_blank, container, false);
doneButton = (Button) v.findViewById(R.id.doneButton);
doneButton.setOnClickListener(this);
return v;
}
//.. some other code
}

Fragments in ViewPager is not displayer after popBackStack

I have problem: A Fragment is not reattached to its hosting ViewPager after returning from another fragment by using popBackStack.
One Activity hosting a Fragment whose layout holds a ViewPager. The ViewPager is populated by a FragmentStateViewPagerAdapter. The Fragment hosted inside the pager can open sub page lists, containing a new set of pages. All works fine as long as the back button is not pressed. As soon as the user closes one of the sub Page the previous Page is recreated, but without the Page that was displayed previously (Empty page with whiteblank page). I have searching but still not resolve my problem.
Full Code of TestProject can be found on Github
MainActivity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val ft = supportFragmentManager.beginTransaction()
ft.add(R.id.frameLayout, HomeFragment.newInstance())
ft.commit()
}
}
HomeFragment:
public class HomeFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
TabLayout tabLayout = v.findViewById(R.id.tabLayout);
ViewPager viewPager = v.findViewById(R.id.viewPager);
VPAdapter adapter = new VPAdapter(getFragmentManager());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
return v;
}
public static Fragment newInstance() {
return new HomeFragment();
}
}
VPAdapter:
public class VPAdapter extends FragmentStatePagerAdapter{
public VPAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return TabFragment.newInstance(position);
}
#Override
public CharSequence getPageTitle(int position) {
return "TAB " + position;
}
#Override
public int getCount() {
return 2;
}
}
TabFragment:
public class TabFragment extends Fragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("TAB", "onCreate");
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
Log.e("TAB", "onCreateView");
return inflater.inflate(R.layout.fragment_tab, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Bundle args = getArguments();
if (args != null) {
int tabIndex = args.getInt("tab_index", 0);
TextView tv = view.findViewById(R.id.textView);
tv.setText(String.format(Locale.getDefault(), "TAB FRAGMENT INDEX %d", tabIndex));
}
Button button = view.findViewById(R.id.button);
button.setText("Open Next Fragment");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frameLayout, LastFragment.newInstance());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
}
#Override
public void onDestroy() {
super.onDestroy();
Log.e("TAB", "onDestroy");
}
public static Fragment newInstance(int index) {
Bundle args = new Bundle();
args.putInt("tab_index", index);
TabFragment tab = new TabFragment();
tab.setArguments(args);
return tab;
}
}
LastFragment:
public class LastFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_last, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getFragmentManager().popBackStack();
}
});
}
public static Fragment newInstance() {
return new LastFragment();
}
}
You have to Use getChildFragmentManager() For Viewpager to adapt Popback Feature.
VPAdapter adapter = new VPAdapter(getChildFragmentManager());
You Can Refer this Documentation on ChildFragmentManager()

pass data to Fragment

I have an Activity and a simple fragment ,I try to use Bundle to pass data to the fragment, but the bundle is always null.
I can't figure out what is the problem?!
this is my Activity class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentA fragmentA =new FragmentA();
Bundle bundle=new Bundle();
bundle.putString("name","Ahmed Ezzat");
fragmentA.setArguments(bundle);
}
public void showFragment(View view) {
FragmentA fragmentA=new FragmentA();
FragmentManager manager=getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.add(R.id.dumper,fragmentA,"fragment A");
transaction.commit();
}
}
and this is my fragment class
public class FragmentA extends Fragment {
TextView textView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_a,container,false);
textView= (TextView) view.findViewById(R.id.tv_fragment);
Bundle bundle = this.getArguments();
String name = bundle.getString("name");
if (name!=null){
textView.setText(name);}
return view;
}
}
any help?
You never called setArguments in the showFragment method.
The Fragment in the onCreate method is never used.
You may try using the following code
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showFragment(View view) {
FragmentA fragmentA=new FragmentA();
Bundle bundle=new Bundle();
bundle.putString("name","Ahmed Ezzat");
fragmentA.setArguments(bundle);
getSupportFragmentManager()
.beginTransaction()
.add(R.id.dumper,fragmentA,"fragment A")
.commit();
}
}
You can pass data to the fragment when building the fragment, then passing it to the transaction.
public class FragmentA extends Fragment {
TextView textView;
String message;
public FragmentA(String message){
this.message = message;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_a,container,false);
textView= (TextView) view.findViewById(R.id.tv_fragment);
textView.setText(message);
return view;
}
And when attaching, you just pass it in the constructor.

Send data to multiple fragments using Serialization

I'm new here so please bear with me. I'm trying to send some data to multiple fragments from MainActivity using Serialization method in POJO. In MainActivity I open a new fragment(FragOne) and I'm able to get data but when I open a new fragment(FragTwo) from FragOne the app crashes with NullPointerException. I searched how to send data using serialized POJO to more than one fragment but there were no results. So, if anyone can help me solve this, I would be grateful. Thanks.
public class FragTwo extends Fragment{
TextView name, position;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragtwo, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
name = (TextView)view.findViewById(R.id.name);
position = (TextView)view.findViewById(R.id.pos);
Spa spa = (Spa)getArguments().getSerializable("spa");
name.setText(spa.getName());
position.setText(spa.getPosition());
}
}
public class FragOne extends Fragment{
TextView name, position;
Button btn;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragone, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
name = (TextView)view.findViewById(R.id.name);
position = (TextView)view.findViewById(R.id.pos);
btn = (Button)view.findViewById(R.id.change);
Spa spa = (Spa)getArguments().getSerializable("spa");
name.setText(spa.getName());
position.setText(spa.getPosition());
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragTwo two = new FragTwo();
((MainActivity)getActivity()).replaceFragment(two);
}
});
}
}
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void replaceFragment(Fragment fragment){
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.replace(R.id.frame, fragment).addToBackStack(null);
t.commit();
}
public void SerializeMethod(View view){
Spa spa = new Spa();
spa.setName("Ricky");
spa.setPosition("Android Dev");
Bundle mBundle = new Bundle();
mBundle.putSerializable("spa", spa);
FragOne one = new FragOne();
one.setArguments(mBundle);
replaceFragment(one);
}
}
public class Spa implements Serializable{
private String name, position;
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
You must set arguments while switching from fragment one to fragment two
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bundle mBundle = new Bundle();
mBundle.putSerializable("spa", spa);
FragTwo two = new FragTwo();
two.setArguments(mBundle);
((MainActivity)getActivity()).replaceFragment(two);
}
});

Categories

Resources