1

I have a class in android that extends Fragment. And I need to create a button dynamically. I can't use new Button(this). Because I'm not extending activity. How do I do this?

public class Tab2Fragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {


    LinearLayout theLayout =  (LinearLayout) inflater.inflate(R.layout.tab2, container, false);

    Context mFragmentContext=getActivity().getApplicationContext(); 
    Button btn=new Button(mFragmentContext);
    btn.setText("Hello Button");
    RelativeLayout.LayoutParams paramsd = new RelativeLayout.LayoutParams(150,30);
    paramsd.height = 600;
    paramsd.width = 60;
    btn.setLayoutParams(paramsd);
    addContentView(btn,paramsd); 
4
  • pass the Activity or application context instead of "this". Commented Jul 16, 2012 at 15:53
  • I tried making a new activity and putting it in space of "this" but the app crashed. Where should i be passing the activity from? Sorry I'm kinda new to this. How do I know what the application context is? Commented Jul 16, 2012 at 16:04
  • where is your 'setContentView' ??? Commented Jul 16, 2012 at 16:23
  • its a fragment so I used this method LinearLayout theLayout = (LinearLayout) inflater.inflate(R.layout.tab2, container, false); I added it into my code Commented Jul 16, 2012 at 16:26

2 Answers 2

3

//try using this below code

sol1:

Button myButt=new Button(YourFragmentClass.this);

sol2:

Button myButt=new Button(getApplicationContext());

//you can also get context like this

 private Context mFragmentContext=getActivity().getApplicationContext(); 
Sign up to request clarification or add additional context in comments.

3 Comments

Ok I'm trying the private context method you posted, but how do i add the button to my view so it actually shows up in the app?
without posting your code do you want to do all your homework.
alright i edited it and added my code, the method addContentView worked on a class where Activity was extended but not on my fragment class
1
/* getActivity() will give you the Activity this fragment belongs
to, you can use this as 'Context' */

Button button = new Button(getActivity())

/* getView() will give you the root layout for this fragment 
(Relative, Linear or whatever you used in xml) and you must cast it
to a ViewGroup to access the getView() method*/

RelativeLayout.LayoutParams paramsd = new RelativeLayout.LayoutParams(150,30);
paramsd.height = 600;
paramsd.width = 60;

ViewGroup viewGroup = (ViewGroup) getView();
viewGroup.addView(button, paramsd);

See here for more info on getView(): Android add button to Fragment without id

Happy Coding...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.