Q:

how to implement Views in Fragment

I've used this sample code to create a sliding menu using navigation drawrer. 
I have now the pages of this menu that are Fragment. Now I need to put View elements in these pages like buttons textviews, etc. How do I do this? As I'm not in an Activity class, I cant put view elements in the Fragment. I've read that i have to create an activity that comunicates with the fragment and does the work. How is this done? can you please show me some sample code? Thanks

A:

Refer to the code below. It has initlialized Button and an ArrayAdapter.
Some things to remember with fragments :
In fragments, you have to use "getActivity()" instead of context.    
When writing "findViewById", you have to use instance of View as shown in the code.
This will help you start.
e.g.
public class FragmentExample extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View v = inflater.inflate(R.layout.fragment_layout, container,
                false);

        // String[] valuesD = getResources().getStringArray(
        // R.array.amtsDestination);
        // ArrayAdapter<String> destinationArray = new ArrayAdapter<String>(
        // this.getActivity(), android.R.layout.simple_spinner_item,
        // valuesD);
        // destinationArray
        // .setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        // destination.setAdapter(startArray);

        Button button = (Button) v.findViewById(R.id.buttonRoute);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // Toast.makeText(getActivity(), "Clicked", 6000).show();
                Intent intent = new Intent(getActivity(),
                        NewActivity.class);
                startActivity(intent);
            }
        });

        return v;
    }

}

