I have a fragment that containd a EditText. Usally I get the address of the EditTExt by calling findViewById to get the object, then I call setText to chnage the text.
When I call findViewById, I get a error saying
"cannot resolve methed findViewById. I'm assuming the Fragment class does not support this methed.
HHow can I chnage the text?
My code
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
EditText editText;
editText = (TextView)(findViewById(R.id.textViewVol));
editText.setText("% Volumne");
}
Try this:
You have to pass the instance of view of the fragment that you instantiate in your onCreateView method:
private View mView;
private EditText editText;
#Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
if (mView != null) {
ViewGroup group = (ViewGroup) mView.getParent();
if (group != null) {
group.removeView(mView);
}
} else {
//Inflate layout for this fragment
mView = layoutInflater.inflate(R.layout.frgament_layout, container, false);
edittext = (TextView)(mView.findViewById(R.id.textViewVol));
editText.setText("% Volumne");
return mView;
}
Related
I have a Fragment Activity i need to set Text to the text view in that Activity,when i am using following code getting null pointer exception.
can any other approach is there to set data for text view .
public class Introduction extends Fragment {
TextView txt;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.activity_introduction, container, false);
txt=(TextView) v.findViewById(R.id.officeheader);
return v;
}
public void setText(String text){
txt.setText(text);
}
}
and in My Fragment Activity class :
frgManager=getSupportFragmentManager();
frgManager.beginTransaction()
. replace(R.id.fragmentoffice, introduction,"introduction")
.commit();
introduction.setText(" Office Coffee that will \n have everyone talking ");
set string in bundle
Bundle bundl = new Bundle();
bundl.putString("Text", text);
introduction.setArguments(bundl)
frgManager=getSupportFragmentManager();
frgManager.beginTransaction()
. replace(R.id.fragmentoffice, introduction,"introduction")
.commit();
get text from bundle
public class Introduction extends Fragment {
TextView txt;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
String str = getArguments().getString("Text");
View v= inflater.inflate(R.layout.activity_introduction, container, false);
txt=(TextView) v.findViewById(R.id.officeheader);
setText(str);
return v;
}
public void setText(String text){
txt.setText(text);
}
}
you can write introduction constructor ,which contain a parameter Type String ,
and on the onCreateView ,you can initialize the textivew and settext
I am getting a Null Pointer Exception when trying to add a textView programmatically to a fragment.
I think the context is coming back null but I could be wrong. The crash is happening at startMenu.addView(tv);
public class TabFragment1 extends Fragment {
View inflatedView;
Context context = null;
private Bundle mBundle;
private LinearLayout startMenull ;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
if (inflatedView != null) {
ViewGroup parent = (ViewGroup) inflatedView.getParent();
if (parent != null)
parent.removeView(inflatedView);
}
try {
inflatedView = inflater.inflate(R.layout.start_menu, container,
false);
context = getActivity().getApplicationContext();
//startMenu = (LinearLayout) getActivity().findViewById(R.id.start_menull);
startMenu = (LinearLayout) getLayoutInflater(mBundle).inflate(R.id.start_menull, null);
TextView tv = new TextView(context);
tv.setId(1);
tv.setText("Here is the text Box");
startMenu.addView(tv);
} catch (InflateException e) {
}
return inflatedView;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBundle = savedInstanceState;
}
}
Updated above code 9/16 3:48
Solved:
in the try/catch
inflatedView = inflater.inflate(R.layout.start_menu, container,
false);
context = getActivity().getApplicationContext();
startMenull =((LinearLayout) inflatedView.findViewById(R.id.start_menull));
TextView tv = new TextView(context);
tv.setId(1);
tv.setText("Here is the text Box");
startMenull.addView(tv);
context cant be null at that point, since he is using applicationContext.
but start Menu IS! null since onCreate is called before onCreateView. So you would have to inflate the View first, which you should do in onCreateView not onCreate.
Just Noticed you are inflating the View in onCreateView, but still you cant Access it in onCreate as it is called before onCreateView.
See Android Life Cycle. http://developer.android.com/images/fragment_lifecycle.png
public class TabFragment1 extends Fragment {
View inflatedView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
inflatedView = inflater.inflate(R.layout.start_menu, container,false);
LinearLayout startMenu = (LinearLayout) inflatedView.findViewById(R.id.start_menu);
TextView tv = new TextView(getActivity());
tv.setId(1);
tv.setText("Here is the text view");
startMenu.addView(tv);
return inflatedView;
}
}
If i wanted to do something like you are doing, this would b how i'd do it. Why would u check if container is null? or why would you assume that inflatedView is != null?
onCreate calls before onCreateView. Read about lifecycle.
I have a fragment with a control (a TextView) with id R.id.currentCycleIndicator and want to programmatically set its value from within the fragment.
Both
getActivity().findViewById(R.id.currentCycleIndicator) and
getView().findViewById(R.id.currentCycleIndicator)
return null.
How can I get reference to that TextView inside the fragment code?
Try getting its value in onCreateView() callBack function of your Fragment like below :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
TextView textView = (TextView) view.findViewById(R.id.currentCycleIndicator);
return view;
}
You can do that by overriding the onViewCreated function as below :
private TextView tv;
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tv = (TextView) view.findViewById(R.id.currentCycleIndicator);
}
Inflate your view inside onCreateView and refer to your TextView from there.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layoutwheretextviewis, container, false);
TextView text= (TextView) view.findViewById(R.id.currentCycleIndicator);
return view;
}
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;
}
I have a fragment with one editText
<EditText
android:id="#+id/textViewName"
/>
I want to change its text at run time .
How can i access edit Text so that i can change its text?
Inside onCreateView(), you can find out particular view by inflating your layout.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myFragmentLayout, container, false);
EditText editText = (EditText) view.findViewById(R.id.textViewName);
return view;
}
Or
you can use getView() if you don't want to inflate, it will return root view of the fragment.
EditText editText = (EditText) getView().findViewById(R.id.textViewName);
Let's Say you have a class EditFragment
public class EditFragment extends Fragment
{
private EditText edit;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myFragmentLayout, container, false);
edit = (EditText) view.findViewById(R.id.textViewName);
return view;
}
}
You can make a getter to access the EditText of this Fragment
public EditText getEditText()
{
return edit;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
EditText e = (EditText) view.findViewById(R.id.textViewName);
....
}