Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting activity into fragment

This is a simple code to play a sound on click off a button, this code was initially written in Activity but now i want to change it to Fragments.

errors

1) The method setContentView(int) is undefined for the type Rajathmusic.

2) The method create(Context, int) in the type MediaPlayer is not applicable for the arguments (Rajathmusic, int).

3)The method findViewById(int) is undefined for the type Rajathmusic.

I am just starting off with android development, any help would be appreciated!

public class Rajathmusic extends Fragment {

private static final String TAG = "MyActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Log.v(TAG, "Initializing sounds...");

    final MediaPlayer mp = MediaPlayer.create(this, R.raw.rajath);

    Button play_button = (Button)this.findViewById(R.id.button3);

    play_button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.v(TAG, "Playing sound...");
            mp.start();
        }
    });
    Log.v(TAG, "Sounds initialized.");
}}
like image 465
Rajath Avatar asked Jan 18 '14 14:01

Rajath


People also ask

Is an activity a fragment?

Activity is an application component that gives a user interface where the user can interact. The fragment is only part of an activity, it basically contributes its UI to that activity. Fragment is dependent on activity. It can't exist independently.

Can we navigate from activity to fragment?

If you want to go back from Activity to Fragment. This is very simple just override onBackPressed() in your activity and call onBackPressed where you want.

Is fragment the child of an activity?

A Fragment is not an Activity.


3 Answers

  1. Fragment has a method called onCreateView(LayoutInflater, ViewGroup, Bundle). Override it, inflate using the layout and return the view.
  2. Since create method expects a Context, pass it using getActivity()
  3. findViewById(int) can be called as getView().findViewById(R.id.button3)

Here is a sample code:

public class Rajathmusic extends Fragment {

    private static final String TAG = "MyActivity";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_main, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Log.v(TAG, "Initializing sounds...");

        final MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.rajath);

        View v = getView();

        Button play_button = (Button) v.findViewById(R.id.button3);

        play_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.v(TAG, "Playing sound...");
                mp.start();
            }
        });
        Log.v(TAG, "Sounds initialized.");
    }

}

Read more about Fragment lifecycle here to know why I've put the code in onActivityCreated and not onCreate

like image 154
Ganesh Bhambarkar Avatar answered Oct 03 '22 04:10

Ganesh Bhambarkar


In fragment onCreate method is usually written as-

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

And Use-

final MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.rajath);
Button play_button = (Button) view.findViewById(R.id.button3);

If you want to read more about fragments. Check this link.

like image 23
Kanwaljit Singh Avatar answered Oct 03 '22 04:10

Kanwaljit Singh


Use this code in overriden method OnActivityCreated(...) and instead of this use getActivity(), since new fragment is not activity any more.

like image 30
Draško Avatar answered Oct 03 '22 02:10

Draško