Tabs do not update text inside each tab layout - java

The textView which in every tablayout could not be changed to some text.
No errors etc. happen. here is the code.
Each tab layout are in different files .xml.
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.coordinators_tab, null,false);
TextView txt = view.findViewById(R.id.textView4);
txt.setText("JUST WE CAN");
Please don't dislike this question if don't understand it!

Access the views only after the fragment is created.In oncreateview after view is inflated access the views.
public class CoordinatorsTab extends Fragment {
private static final String TAG = "CoordinatorsTab";
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.coordinators_tab, container, false);
//Access here
TextView txt = view.findViewById(R.id.textView4);
txt.setText("JUST WE CAN");
return View;
}
}

After you find a view you have to cast it like this
TextView txt = (TextView) view.findViewById(R.id.textView4);

Related

How to stop fragment reload everytime clicked?

I have a bottom navigation which is responsible for switching fragments. So whenever I click the button twice, the fragment reloads the content on it everytime
How to stop that? I want to reload the page by only scrolling upwards
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View Layout = inflater.inflate(R.layout.fragment_profile, container, false);
ConstraintLayout profile = (ConstraintLayout) Layout.findViewById(R.id.ll);
ImageView imageView = (ImageView) Layout.findViewById(R.id.imageView3);
imageView.setClipToOutline(true);
SharedPreferences sharedPref = getActivity().getSharedPreferences("INFORMATION", 0);
String USERNAME = sharedPref.getString("user","");
TextView username = (TextView) Layout.findViewById(R.id.username);
Typeface custom_font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto Thin.ttf");
TextView desc = (TextView) Layout.findViewById(R.id.desc);
desc.setTypeface(custom_font);
username.setTypeface(custom_font);
username.setText(USERNAME);
new PostClass(getContext()).execute();
new PostClass_2().execute();
new PostClass_3().execute();
return Layout;
}
This code above gets executed everytime the fragment is changed. I want to reload it by scrolling upwards like in chrome in a fragment. Any help is appreciated
I had this problem a long time ago :/ but I found a simple way to solve the problem :)
Use this to solve the problem :
// Necessary :)
private Context mContext;
private View MyView;
// Nullable
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
mContext=getActivity();
if(MyView==null){
// Layout View
MyView=LayoutInflater.from(mContext).inflate(R.layout.fragment_profile, null);
// Your views & Codes
ConstraintLayout profile = (ConstraintLayout) MyView.findViewById(R.id.ll);
ImageView imageView = (ImageView) MyView.findViewById(R.id.imageView3);
imageView.setClipToOutline(true);
SharedPreferences sharedPref = getActivity().getSharedPreferences("INFORMATION", 0);
String USERNAME = sharedPref.getString("user","");
TextView username = (TextView) MyView.findViewById(R.id.username);
Typeface custom_font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto Thin.ttf");
TextView desc = (TextView) MyView.findViewById(R.id.desc);
desc.setTypeface(custom_font);
username.setTypeface(custom_font);
username.setText(USERNAME);
new PostClass(getContext()).execute();
new PostClass_2().execute();
new PostClass_3().execute();
// Stop fragment reload [ When changing ]
} ViewGroup parent = (ViewGroup) MyView.getParent();
if (parent != null) {
parent.removeView(MyView);
}
return MyView;
}
}
Use this if you want to set a new id : MyView.findViewById(R.id.SetId)
I hope that helps !
Good luck

How to call a tabbed activity fragment from main activity?

I have selected a tabbed activity template with "Action bar tabs" in android studio.
Then i had created three fragments (One.java, Two.java, Three.java) one for each of the tabs.
I have the code related to the cursors in the second fragment.The thing is when i run the application the code in the second fragment is not being executed.
My question is will the fragment (Two.java) will be called implicitly or it is needed to call by creating an instance (if yes, how?).
code snippet for MainActivity.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(getArguments().getInt(ARG_SECTION_NUMBER)==1){
View rootView = inflater.inflate(R.layout.fragment_one, container, false);
return rootView;
}
else if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) {
View rootView = inflater.inflate(R.layout.fragment_two, container, false);
return rootView;
} else {
View rootView = inflater.inflate(R.layout.fragment_three, container, false);
return rootView;
}
}
code snippet for Two.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
logReader1 = new LogReader(getContext());
db = logReader1.getReadableDatabase();
Cursor logCursor = db.rawQuery("SELECT rowid _id,name,Time_Used,app_Image FROM logs ORDER BY Time_Used DESC", null);
LogCursorAdapter logAdapter = new LogCursorAdapter((MainActivity) getContext(), logCursor);
ListView log_Items = (ListView) container.findViewById(R.id.list_item);
log_Items.setAdapter((logAdapter));
return log_Items;
}
From what I can tell you aren't actually creating a new fragment. You're inflating the layout that you're intending to use as the layout for the fragment two.
Something like https://developer.android.com/training/implementing-navigation/lateral.html should get you pointed in the right direction. Notice how they create and return a fragment in the DemoCollectionPagerAdapter.

Unfortunately, app has stopped,how can I make this work onCreateView?

Previously, I had a similar issue, app crashes when trying to access (or findByID), it turned out, I've put the textview in the wrong method, Now I have only one method, and I am working with a fragment, the method onCreateView and my app keeps crashing:
public class InfoFragment extends Fragment {
public InfoFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView n = (TextView) getView().findViewById(R.id.nama);
n.setText("Some String");
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_info, container, false);
}
}
Originally, I had the line looking like this :
TextView n = (TextView) findViewById(R.id.nama);
However,the compiler gave me an error saying it can't find the method findViewById, so some research showed that I should add getView() , I did, and the error disappeared, but now the app is crashing, any ideas?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_info, container, false);
TextView n = (TextView) view.findViewById(R.id.nama);
n.setText("Some String");
return view;
}
You don't have a view yet in that part of code. Inflate it first and use after.
Inflate your layout first, then you can use it to find the TextView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_info, container, false);
TextView n = (TextView) rootView.findViewById(R.id.nama);
n.setText("Some String");
return rootView;
}

Find internal views of a dynamically created fragment

I have dynamically added a fragment to another fragment.
I need to find a few views within that fragment.
How do I achieve that?
here is my code:
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_educational, container, false);
if (view != null)
{
ButterKnife.inject(this, view);
}
DisposableEducationalFragment disposableEducationalFragment = new DisposableEducationalFragment();
getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer,disposableEducationalFragment).addToBackStack(null).commit();
I want to find these views within that Dynamic fragment:
howItWorks = (RelativeLayout) fragmentView.findViewById(R.id.HowItWorks);
howWeHelp = (RelativeLayout) fragmentView.findViewById(R.id.HowWeHelp);
knowYourBloodPressure = (RelativeLayout) fragmentView.findViewById(R.id.KnowYourBloodPressure);
learnAboutBirthControl = (RelativeLayout) fragmentView.findViewById(R.id.LearnAboutBirthControl);
So, save View view into a temp variable and call it later.
tmpView = view;
and somewhere later:
howItWorks = (RelativeLayout) tempView.findViewById(R.id.HowItWorks);
or , may be, just try it:
howItWorks = (RelativeLayout) getActivity().findViewById(R.id.HowItWorks);

Adding view to fragment viewgroup

I want to update fragment view hierarchy on some event (button press, etc)
Here is my code of fragment:
public class MyFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_categories_preview, container, false);
}
private void updateMainView()
{
LayoutInflater inflater = (LayoutInflater) this.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout container = (LinearLayout) this.getView().findViewById(R.id.images_preview_container);
container.removeAllViews();
int layoutId = R.layout.list_preview;
int cellLayoutId = R.layout.list_image_view;
resultView = (ListView) inflater.inflate(layoutId, null);
resultView.setAdapter(new ImagesListAdapter(inflater, cellLayoutId, this.imagesList));
resultView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
container.addView(resultView);
}
// Other functions
// ...
}
Function updateMainView() called when pressing button. View isn't appearing in fragment.
I tryed to add empty colored view:
View test = new View(this.getActivity());
test.setBackgroundColor(0xFFFF0000);
test.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
container.addView(test);
No result.
What I am doing wrong?

Categories