Navigation menu fragment import media player - android

I just made a navigation drawer having some fragments. I want to put some music on one fragment using MediaPlayer so that when button is click it play music but when i add raw resource file androidstudio do red and shows error cannot resolve method.Can anyone help me how to import media player in Menu1 fragment.
public class Menu1 extends Fragment {
MediaPlayer mp;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_menu1, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("Happy B'day Dear");
mp=MediaPlayer.create(this,R.raw.hpbday);
Button button=(Button)findViewById(R.id.btnplay);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mp.start();
}
});
}
}

Related

How to implement button function in fragments in Android studio

FeedFragment.java
public class FeedFragment extends Fragment {
Button bt_scan;
private Object Button;
public FeedFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_feed, container, false);
bt_scan = (Button) v.findViewById(R.id.bt_scan);
bt_scan.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent MainIntent = new Intent(getActivity(), MainActivityQR.class);
startActivity(MainIntent);
/* if you want to finish the first activity then just call
finish(); */
}
});
return v;
}
}
I already tried but the camera for scanning QR Code wont come out
The use of the camera require a permission request and a declaration in the AndroidManifest file

How to create a button inside a Fragment?

I am creating a fragment where if I click on the button it should show a Toast message
This is my code:
public class Aboutus extends Fragment implements View.OnClickListener{
Button ps,fb;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.about_layout, container, false);
ps = (Button) view.findViewById(R.id.ps_btn);
fb = (Button) view.findViewById(R.id.fb_btn);
try {
ps.setOnClickListener(this);
}catch (ActivityNotFoundException exception) {
}
//fb.setOnClickListener(this);
return view;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.fb_btn:
Toast.makeText(getActivity(), "This is my Toast message!", Toast.LENGTH_LONG).show();
break;
case R.id.ps_btn:
Toast.makeText(getActivity(), "This is my Toast message!", Toast.LENGTH_LONG).show();
break;
}
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("ABOUT US");
}
}
My app stops working when I open the fragment.
It does't seem like having issue with button. There may be a problem with implementing fragment inside activity. Once Watchout Fragment implementation. Thanks.

Implement a button in a fragment for tabbed activity after click should goto new activity

This is the code for the fragment. Please help me place a button and onClick it should go to the respective activity
class boards extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.boards, container, false);
}
}
Inside onViewCreated method:
View view = inflater.inflate(R.layout.boards, container, false);
Button button = (Button) view.findViewById(R.id.buttonId);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
// do something
startActivity(new Intent(getActivity(), YourActivity.class));
}
});
return view;
you need to implement a call back interface
interface code :
public interface FragmentCallback {
void changeActivity();
}
inside fragment:
FragmentCallback mListener;
void setListener(FragmentCallback listener){
mListener=listener;
}
inside activity:
public class Activity extends AppCompatActivity implements FragmentCallback{
Boards fragment=new Boards(); //class name shoud be capital i.e Boards
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
fragment.setListener(this);
}
#Override
void changeActivity(){
Intent intent =new Intent(this,newActivity.class);
startActivity(intent);
}
}
use the below code to change activity from fragment :
if(mListener!=null)
mListener.changeActivity()
below code will do the task

android - Start new activity from fragment, apk built but crashes on device

I'm trying to start new activity from fragment and here is my code:
public class HomePage extends Fragment {
View rootview;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.homepage_layout, container, false);
Button newPage = (Button)rootview.findViewById(R.id.view_our_products);
newPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ProductsOverview.class);
startActivity(intent);
}
});
return rootview;
}
}
There are no errors reflected by Android studio. But after I build the apk and click on this button it crashes and says "Unfortunately Sample App has stopped".
Your ProductsOverview.class is an fragment not an Activity so make that as a Activity by ProductOverview extends AppCompatActivity.
Hope this will help you

Button in tab fragment

I was following tutorials on how to make Tabs using fragments.
each tab has this in the java files:
public class rdfri extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
return (LinearLayout)inflater.inflate(R.layout.rdfri, container, false);
}
}
I would like to try and get a imageButton in the fragment. I figured image bottons work with this code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rdmon);
ImageButton rainbowbook = (ImageButton) findViewById(R.id.imageButton1);
rainbowbook.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = Intent(rdmon.this, RainbowBookActivity.class);
rdmon.this.startActivity(myIntent);
}
});
}
So how would I go about getting the button code in the fragment code?
Thanks.
Put the button code in the overridden onActivityCreated method in your fragment class.
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ImageButton rainbowbook = (ImageButton) findViewById(R.id.imageButton1);
rainbowbook.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = Intent(rdmon.this, RainbowBookActivity.class);
rdmon.this.startActivity(myIntent);
}
});
}
This assumes of course that your imagebutton is contained in your fragment layout.

Categories

Resources