How to add new Controls (Buttons, TextViews) to a Fragment dynamically - java

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 .

Related

i always get error on the button part and getActivity. its my first time using fragment in android

public class Supplier_Home extends Fragment {
private static final String TAG = "Supplier_Home";
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.s_1home, null);
view = inflater.inflate(R.layout.supply__home__po, container, false);
Button pOrders=(Button)view.findViewById(R.id.pOrders);
pOrders.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: Clicked login1.");
Intent intent = new Intent(getContext(), Login.class);
}
});
}
}
Do like, Create View object like this. Pass container object while inflating layout
View view;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.dashbord, container, false);
Button btn=(Button)view.findViewById(R.id.btn)
return view;
}
You are returning the complete view in the starting itself making the code below return unreachable.
You have given two layouts, If I am not wrong s_1home is layout of your activity where view pager is resting and supply__home__po is your fragment layout. Considering my assumptions are correct please change the code as follows
public class Supplier_Home extends Fragment {
private static final String TAG = "Supplier_Home";
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.supply__home__po, container, false);
Button pOrders=(Button) view.findViewById(R.id.pOrders);
pOrders.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: Clicked login1.");
Intent intent = new Intent(getContext(), Login.class);
}
});
return view;
}
}

How to SetText of One Activity/Fragment from another one

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?

Creating TextView in a View in the Navigation Drawer

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;

add button inside viewpager fragment and intent in another activity?

I'm trying to create an app which will display my menu and I use viewpager fragment for display and I want to add a button for every page of the viewpager which will intent to another activity can someone help me for this?
This is my viewpager:
public class menu_meat_viewer extends FragmentActivity{
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.meat_viewer);
initialisePaging();
}
private void initialisePaging(){
List<Fragment> fragments=new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, meat_adobo.class.getName()));
fragments.add(Fragment.instantiate(this, meat_bbqpork.class.getName()));
fragments.add(Fragment.instantiate(this, meat_tlshrmp.class.getName()));
mPagerAdapter=new menu_meat_viewer_adapter(this.getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager) findViewById(R.id.meat_view);
pager.setAdapter(mPagerAdapter);
}
}
and this is one of my page in view pager:
public class meat_adobo extends Fragment{
int price= 70;
String name="classic chicken adobo";
MDB mdb;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(container==null){
return null;
}
return(RelativeLayout) inflater.inflate(R.layout.meat_adobo,container,false);
}
}
Create a button in your fragment layout
and
Add this code to your fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.meat_adobo, container, false);
Button b=(Button) v.findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent in=new Intent(getActivity(),SecondActivity.class);
startActivity(in);
}
});
return v;
}
and add import android.view.View.OnClickListener;

How to get Button click from Fragments

I got a Fragment that need to take a buttonclick.
Problem is that the click it going to the MainActivity hosting all fragments and not to the Fragment itself.
I have tried Loging it and nothing shows in the logs when i click the button.
I will show u the entire onCreateView.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.server_settings, container, false);
final Button loginButton = (Button) view.findViewById(R.id.addServerSettingsbtn);
loginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
//were the magic won't happen.
}
});
return inflater.inflate(R.layout.server_settings, container,false);
}
Your problem (I believe) is that you're returning an instance of the inflated view that doesn't have the onClick handler assigned.
Try this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.server_settings, container, false);
final Button loginButton = (Button) view.findViewById(R.id.addServerSettingsbtn);
loginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
//were the magic won't happen.
}
});
return view;
}
you have to return view instead of inflate again server_setting. inflate returns everytime a new object

Categories