Awais Ahmed
Awais Ahmed

Reputation: 25

Want to change Activity to Fragment

I want to change my activity to fragment. my code is given below. help would be appreciated. I have spend two days without any success. I have modified a lot my above code but unable to solve this. Last five lines are confusing me too much. I have also used a MediaPlayerPool class which i think is not creating problem.

package com.example.soundpoolexample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {
private MediaPlayerPool mediaPlayerPool;

Button button;
Button button2;

class b1 implements OnTouchListener {
    b1() {
    }

    public boolean onTouch(View view, MotionEvent motionEvent) {
        view.setPressed(true);
        if (motionEvent.getAction() == 0) {
            MainActivity.this.mediaPlayerPool.play("android.resource://com.example.soundpoolexample/raw/a1");
            return true;
        } else if (motionEvent.getAction() != 1 && motionEvent.getAction() != 3) {
            return false;
        } else {
            MainActivity.this.mediaPlayerPool.stop("android.resource://com.example.soundpoolexample/raw/a1");
            return false;
        }
    }
}

class b2 implements OnTouchListener {
    b2() {
    }

    public boolean onTouch(View view, MotionEvent motionEvent) {
        view.setPressed(true);
        if (motionEvent.getAction() == 0) {
            MainActivity.this.mediaPlayerPool.play("android.resource://com.example.soundpoolexample/raw/a1");
            return true;
        } else if (motionEvent.getAction() != 1 && motionEvent.getAction() != 3) {
            return false;
        } else {
            MainActivity.this.mediaPlayerPool.stop("android.resource://com.example.soundpoolexample/raw/a1");
            return false;
        }
    }
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView((int) R.layout.activity_main);

    this.button = (Button) findViewById(R.id.button);
    this.button2 = (Button) findViewById(R.id.button2);
    this.mediaPlayerPool = new MediaPlayerPool(this);
    this.button.setOnTouchListener(new b1());
    this.button2.setOnTouchListener(new b2());

}

}

Upvotes: 0

Views: 1285

Answers (1)

Hyun I Kim
Hyun I Kim

Reputation: 589

1. Create Fragment layout

Create a new layout file.

In your case it will be a copy of current activity_main.xml file.

In my case, I'll just use red background.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#f00">

</FrameLayout>

2. Create Fragment class

Create a new Java Class file.

Extend Fragment from android.support.v4.app.Fragment and override onCreateView().

public class MainFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }
}

3. Inflate layout

Inside onCreateView(), inflate and return the layout from 1. Create Fragment layout.

return inflater.inflate(R.layout.fragment_layout, container, false);

4. Move implementations from MainActivity to MainFragment

Move oncreate() in MainActivity implementation to onViewCreated() in MainFragment.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    this.button = (Button) findViewById(R.id.button);
    this.button2 = (Button) findViewById(R.id.button2);
    this.mediaPlayerPool = new MediaPlayerPool(this);
    this.button.setOnTouchListener(new b1());
    this.button2.setOnTouchListener(new b2());
}

You should leave onCreate() in MainActivty as follows.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_java);
}

Move everything else as is.

5. Use MainFragment in MainActivity

In activity_main.xml, use MainFragment with <fragment> tag.

<FrameLayout ... >

<fragment
        android:id="@+id/main_fragment"
        android:name="com.example.blockrecyclerview.MainFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</FrameLayout>

It results in following screen.

Result Image

Upvotes: 3

Related Questions