I have been trying to add a textview dynamically to my fragment but it is causing the app to crash, the below code works in a non-fragment activity I just cant get it work in a fragment. At the moment my code uses just on textview, but it will eventually return many from a database, hence why I need it to be dynamically created.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View theView = inflater.inflate(R.layout.fragment_list_batches, container, false);
context=getActivity();
//Dynamically create Elements
LinearLayout.LayoutParams SVView = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout SV = (LinearLayout)getActivity().findViewById(R.id.listBatchesRelative);
TextView batchName = new TextView(context);
int i = 1;
batchName.setId(Integer.valueOf(i));
batchName.setText("Dynamic Input view");
batchName.setLayoutParams(SVView);
SV.addView(batchName);
return theView
View theView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
theView = inflater.inflate(R.layout.fragment_list_batches, container,false);
LinearLayout.LayoutParams SVView = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout SV = (LinearLayout)theView .findViewById(R.id.listBatchesRelative);
TextView batchName = new TextView(theView.getContext);
int i = 1;
batchName.setId(Integer.valueOf(i));
batchName.setText("Dynamic Input view");
batchName.setLayoutParams(SVView);
SV.addView(batchName);
return theView
Related
I want to change the text of the Fragment_B from the Fragment_A. How can I do this?
Something like this.
Fragment_A
ola = (TextView) v.findViewById(R.id.textView12);
ola2 = (TextView) (FRAGMENT_B).findViewById(R.id.textView2);
AsyncTask(ola,ola2).execute //this should set text on Fragment_A and Fragment_B
Fragment A:
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_second,container,false);
ola = (TextView) v.findViewById(R.id.textView12);
Asynctask(ola).execute
return v;
}
Fragment B:
public class Fragmento_B extends android.support.v4.app.Fragment{
public TextView ola2;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_third,container,false);
ola2 = (TextView) v.findViewById(R.id.textView2);
Asynctask(ola2).execute
return v;
}
}
What is the problem you are facing? Is it showing some sort of error or is it not changing the text as your or anything?
I'm new to Android and have a problem with the Navigation Drawer in Android Studio. I want to create dynamically a TextView in one of the Navigation Drawer's view. I cannot create a new TextView and I cannot search by id a TextView
public class StatoServer extends Fragment {
View myView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView tx = new TextView(this); //doesn't work this
tx.setText("text that change dynamically with a function");
container.addView(tx);
myView = inflater.inflate(statoserver, container, false);
return myView;
}
You don't assign any LayoutParams to your TextView, nor do you add it to the correct ViewGroup. It should be added to the View returned by onCreatView(). The following is tested and will work as an example of how to dynamically add a view:
public class OneFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.one_fragment, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
TextView textView = new TextView(getContext());
textView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
);
((ViewGroup)view).addView(textView);
textView.setText("Some Text");
super.onViewCreated(view, savedInstanceState);
}
}
Inside your onCreateView , here an example code on how you do it!
View v = new View(getActivity());
v = inflater.inflate(R.layout.page2, container, false);
View tv = v.findViewById(R.id.Ausgabe);
((TextView)tv).setText("TestText");
View pl = v.findViewById(R.id.PageLayout);
TextView Paper = new TextView(pl.getContext());
Paper.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Paper.setText("Inserted TestText");
((LinearLayout)pl).addView(Paper);
return v;
I am using fragments and i trying to build a list i am getting from the database. Everything seems to be fine except for the following piece of code
listadapter = new charitylist(this, getLayoutInflater());
I already set the layout inflater here
LayoutInflater lf = getActivity().getLayoutInflater();
But i don't have any idea how to use the layoutinflater lf int the listadapter.
This is the full code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LayoutInflater lf = getActivity().getLayoutInflater();
View view = lf.inflate(R.layout.activity_charity, container, false);
listView = (ListView) view.findViewById(R.id.charityList);
listadapter = new charitylist(this, getLayoutInflater());
listView.setAdapter(listadapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//String trackName = listadapter.getItem(position).optString("track_name", "");
}
});
setType = "favourite";
getlist(setUrl, setType);
return view;
}
I have no idea how to get this working. I tried several things to get this to work. But i need to initiate the adapter.
Thank for your help.
LayoutInflater inflater is already provided in onCreateView of Fragment. Why don't you use like this:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.activity_charity, container, false);
listView = (ListView) view.findViewById(R.id.charityList);
listadapter = new charitylist(getActivity(), inflater);
...
This is my Fragment class and I have a textView in xmlfile
How can I change the TextView by setText?
public class Frist extends Fragment {
TextView text;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_frist, container, false);
TextView t = (TextView) this.getView().findViewById(R.id.textView1);
t.setText("SOME TEXT");
return rootView;
}
}
Instead of
TextView t = (TextView) this.getView().findViewById(R.id.textView1);
You should be calling findViewById on the view you inflate:
TextView t = (TextView) rootView.findViewById(R.id.textView1);
Here's my fragment class:
public class FirstFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
EditText newText = new EditText(getActivity());
newText.setText("This is a fragment");
newText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
return inflater.inflate(R.layout.fragment_first, container, false);
}
}
I am trying to add newText to the fragment being created.
simply store the view that was returned from the inflater.inflate call , and add anything you want to it .
in the end , return the stored view .