I created a new project (Tabbed Activity).
This is default code for onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format,getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
but then I made some changes to the code:
.
.
textView.setText(getString(R.string.section_format,getArguments().getInt(ARG_SECTION_NUMBER)));
Toast.makeText(getContext(), getString(R.string.section_format,getArguments().getInt(ARG_SECTION_NUMBER)), Toast.LENGTH_SHORT).show();
return rootView;
.
.
But the Toast value is different from what textview displayed. So the question is, how can I get the exact(accurate) value of section number?
Edit 1: Toast displayed two times when activity first started.
Edit 2: Screenshot added.
AFAIK,text from TextView might change faster. Whereas, the toast will continue showing until its time is up. It take approximately about 5s for the toast to change. So, you might think the section is not the same.
My suggestion is to test using two TextViews.
Hope this helps :)
Related
I am using fragments in my dynamic application, where the User.java file contains the values and TabbedActivity.java file contains three fragments. I want to set the text to TextView in ProfileFrgament.java. So, I created a TextView in fragment_profile.xml and referenced it from TabbedActivity.java file with the following code
name = findViewById(R.id.name);
//getting the current user
User user = SharedPrefManager.getInstance(this).getUser();
//setting values to textviews
name.setText(user.getUsername());
It does not shows any compilation error, but after opening TabbedActivity.java, the app stops with NullPointerException at line name.setText(user.getUsername()); How to solve this issue?
This is the code of ProfileFragment.java file
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
name = getActivity().findViewById(R.id.name);
//getting the current user
User user = SharedPrefManager.getInstance(getActivity()).getUser();
//setting values to textviews
name.setText(user.getUsername());
return inflater.inflate(R.layout.fragment_profile, container, false);
}
Your TextView is in fragment_profile.xml and you trying to find that TextView in TabbedActivity.java, so NullPointerException is occured
Put this code in ProfileFrgament.java
Change your code with this one
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState)
{
final View rootView = inflater.inflate(R.layout.fragment_profile, container, false);
name = (TextView) rootView.findViewById(R.id.name);
User user = SharedPrefManager.getInstance(getActivity()).getUser();
name.setText(user.getUsername());
return rootView;
}
You need to do this in the fragment's java file than in the activity's java file.
Now for finding view in fragment.
Replace this code :
name = findViewById(R.id.name);
to :
name = getActivity().findViewById(R.id.name);
Rest of the code can be written as it is already.
Also put this type of operation in onViewCreated method. Putting this code in onCreateView may crash the app.
Here is the screenshot of the code, I am new to programming and this app I am making is my school project, no experience working with Android studio before( a bit of Java but not too much)
http://prntscr.com/i465km
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
So my question is, why is this section_format error and app is giving me two answers how to fix it: create string value or create field with the same name(section_format)?
setText() method displays the string inside it. Like this
textView.setText("Sample Text");
if you are displaying a message from a method it should return a string value
Like this
private string getString(param1 , param2){
//do something with parameters
return string;
}
Now you can get the message from the method like this:
textView.setText(getString(param1, param2))
i have try a lot of example or tutorial on tablayout, all work fine
(https://www.simplifiedcoding.net/android-tablayout-example-using-viewpager-fragments/, http://www.androidbegin.com/tutorial/implementing-fragment-tabs-in-android/)
but now i encounter 1 problem, which is i dont know how to set/change the TextView/ImageView while onCreate/onLoad
so far, what i able to do is, write all my code under onTabSelected, but this is not my solution because i cant show empty or static values on the 1st tab, then have to wait until click or slide again then only load the real data.
i'm sure it have a way or solution for my problem, can anyone share it to me (code/website)
or in order to have this Tab feature with viewpager contain recyclerview, Tablayout is not the right way to code, perhaps it have another more correct way to code.
Do these changes inside of onCreateView of fragment that you want
Example:
//Our class extending fragment
public class Tab1 extends Fragment {
//Overriden method onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Change R.layout.tab1 in you classes
View view = inflater.inflate(R.layout.tab1, container, false);
TextView textView = (TextView) view.findViewById(R.id.you_textview_id);
textView .setText("Your text to show");
//Returning the layout file after inflating
return view;
}
}
The TextView need to be previous declared in tab.xml that you're inflating, in our example, tab1.
I have a some fragment in my PageViewer.
In the main fragment, I would like to show a component ( TextView or imageView) if there is no connection.
In the code below, I can reach my textview, but I cannot get them disapperead.
public class MainFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.main, container, false);
// RelativeLayout mainLayout = (RelativeLayout)findViewById(R.layout.main);
TextView TxtIsNotConnected = (TextView) layout.findViewById(R.id.isNotConnected);
TextView TxtIsConnected = (TextView) layout.findViewById(R.id.isConnected);
// String text = TxtIsNotConnected.getText().toString(); // This is a test which works, return the text o my textview.
boolean isConnected = ConnectivityUtils.isConnected(getActivity()); // This Works fine
if (!isConnected) TxtIsNotConnected.setVisibility(View.VISIBLE); // NOT WORKING
else TxtIsConnected.setVisibility(View.VISIBLE); // NOT WORKING
return inflater.inflate(R.layout.main, container, false);
}
How should I do???
To make your TextView visible
yourTextView.setVisibility(View.VISIBLE);
To make Invisible
yourTextView.setVisibility(View.GONE);
From your code it seems that you have to keep one view visible and another invisible. So, please try this
if (!isConnected) {
TxtIsNotConnected.setVisibility(View.VISIBLE);
TxtIsConnected.setVisibility(View.GONE);
} else {
TxtIsConnected.setVisibility(View.VISIBLE);
TxtIsNotConnected.setVisibility(View.GONE);
}
Hope it helps...
Edit :
Well, your code is correct about visibility but you have made a silly mistake that's why it seems that your code is not working. Look at the first line and last line of your onCreateView method. You have inflated your R.layout.main in View object called "layout". You have set your actions within that layout. Finally you have returned a new instance of that view. So, your previous codes became useless. So, your return statement will be...
return layout ;
It should fix the problem.
Seems like a simple question to me, but I can't figure it out. I have this code inside my Fragment:
myTextView.setText("Test");
However, this code brakes my app and I am unsure why. Perhaps this is a conflict with Views, but I still don't understand why it wont work. Any suggestions?
How did you set the content?
Edited my answer :) try this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.xmlName,
container, false);
TextView tv= (TextView) **view**.findViewById(R.id.textViewName);
tv.setText("yourText");
return view;
}
here is a tutorial for fragments : fragments tutorial