Update textview in fragment - android

This might be a very simple problem, but I can't seem to get it to work.
I'm trying to update a textview in a Fragment on "OnCreateView"
My code looks like this
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_display_product, container, false);
txtAge = (TextView)v.findViewById(R.id.txtAge);
txtCountry = (TextView)v.findViewById(R.id.txtCountry);
txtName = (TextView)v.findViewById(R.id.txtName);
txtAge.setText("Hallo world");
return inflater.inflate(R.layout.fragment_display_product, container,false);
}
The text in textview txtAge never get set and I can't seem to find a reason.
I'm new to android so sorry if this is a stupid question.
/Birger

Do it like this-
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_display_product, null);
txtAge = (TextView)v.findViewById(R.id.txtAge);
txtCountry = (TextView)v.findViewById(R.id.txtCountry);
txtName = (TextView)v.findViewById(R.id.txtName);
txtAge.setText("Hallo world");
return v;
}
Return the same view which you have inflated already.

Related

setImageResource not working

I'm about to set an ImageView with setImageResource, but it's not working. nothing error too. and the logcat seems fine.
Here is my code :
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list_request, container, false);
setHasOptionsMenu(true);
View anotherView = inflater.inflate(R.layout.style_fragment_list_request,container,false);
imgView = (CircleImageView)anotherView.findViewById(R.id.listPhoto);
imgView.setImageResource(R.drawable.pastel_red);
}
Considering that I called and declare the ImageView from another layout. Is at problem if I inflate 2 layouts just for set image for another layout ?
Anything wrong here?
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list_request, container, false);
setHasOptionsMenu(true);
return view;
}
public void onActivityCreated(){
ViewGroup layout = (ViewGroup) getView().findById(R.id.layout_another_view); // need layout for anotherView in fragment_item_list_request.xml
View anotherView = inflater.inflate(R.layout.style_fragment_list_request,container,false);
imgView = (CircleImageView)anotherView.findViewById(R.id.listPhoto);
imgView.setImageResource(R.drawable.pastel_red);
layout.addView(anotherView);
}
What if you do this instead:
View anotherView = inflater.inflate(R.layout.style_fragment_list_request,null,false);
The statement return anotherView; is missing in onCreateView().

use font in Fragment

I tend to use the font fragment and fragment have control over
Why the following code produces an error?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rooo = inflater.inflate(R.layout.din2, container, false);
TextView tv1=(TextView) rooo.findViewById(R.id.tv1);
Typeface yaghut=Typeface.createFromAsset(getActivity().getAssets(), "font/BKOODB.ttf");
tv1.setTypeface(yaghut);
return rooo;
}
}
this error
onCreateView is supposed only to return the view that represent the fragment. Other operations on the View should be performed in another callback, onViewCreated
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.din2, container, false);
}
#Override
public void onViewCreated (View view, Bundle savedInstanceState) {
TextView tv1=(TextView) view.findViewById(R.id.tv1);
Typeface yaghut=Typeface.createFromAsset(getActivity().getAssets(), "font/BKOODB.ttf");
tv1.setTypeface(yaghut);
}
you need change :
TextView tv1=(TextView) r.findViewById(R.id.tv1);
to
TextView tv1=(TextView) rooo.findViewById(R.id.tv1);
because your TextView with tv1 id's must be on din2 layout.
You are creating a view named rooo but you are returning a view named roo...now replace all roo with rooo as below...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rooo = inflater.inflate(R.layout.din2, container, false);
TextView tv1=(TextView) rooo.findViewById(R.id.tv1);
Typeface yaghut=Typeface.createFromAsset(getActivity().getAssets(), "font/BKOODB.ttf");
tv1.setTypeface(yaghut);
return rooo;
}
}

How to access a UI element in a fragment?

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;
}

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;
}

child already has a parent error showing?

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfragment, container,false);
ImageView tv = (ImageView ) v.findViewById(R.id.imageView);
tv.setImageResource(R.drawable.h1);
return tv;
}
why am i getting the runtime error?
Change your above code as below:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfragment, container,false);
ImageView tv = (ImageView ) v.findViewById(R.id.imageView);
tv.setImageResource(R.drawable.h1);
return v;
}
The Reason you are getting RuntimeError is you are returning ImageView (tv) for onCreateView which is view for Fragment. But ImageView is a child View of myfragment layout so It cannot be set as View to Fragment so you are getting Runtime Error.

Categories

Resources