How to create a button inside a Fragment? - android

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.

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

Navigation menu fragment import media player

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();
}
});
}
}

Tabs and fragments

I am using the library com.roughike:bottom-bar:1.2.1 to make an Android app using bottom tabs. The problem is that in his github, there is no actual example on how tabs work with fragments. I have tried to work my way around it but to no success. Here is the code below.
public class MainActivity extends AppCompatActivity {
private CoordinatorLayout coordinatorLayout;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
coordinatorLayout = (CoordinatorLayout) findViewById(R.id.three_buttons_activity);
BottomBar bottomBar = BottomBar.attach(this, savedInstanceState);
bottomBar.setItemsFromMenu(R.menu.three_buttons_menu, new OnMenuTabSelectedListener() {
#Override
public void onMenuItemSelected(int itemId) {
switch (itemId) {
case R.id.recent_item:
//Snackbar.make(coordinatorLayout, "Recent Item Selected", Snackbar.LENGTH_LONG).show();
break;
case R.id.favorite_item:
//Snackbar.make(coordinatorLayout, "Favorite Item Selected", Snackbar.LENGTH_LONG).show();
break;
case R.id.location_item:
//Snackbar.make(coordinatorLayout, "Location Item Selected", Snackbar.LENGTH_LONG).show();
break;
}
}
});
bottomBar.setActiveTabColor("#FFFFFF");
}
}
I wanted to have for example if I choose "recent item" it will go to recent item tab and do whatever on that tab but it doesn't happen to me.
If any of you have a good suggestion and example on how to do it right, I will really appreciate it..
Thanks
I used this bottom bar over a year ago. this is how i added the fragments.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three_buttons);
// ************* Setting Up The Bottom Bar ************ //
BottomBar bottomBar = BottomBar.attach(this, savedInstanceState);
bottomBar.noTabletGoodness();
// *********** Adding Item Fragments to the bottom Bar ************ //
bottomBar.setFragmentItems(getSupportFragmentManager(), R.id.fragmentContainer,
new BottomBarFragment(ProfileFragment.newInstance("Content For Profile"), R.drawable.ic_profile, "Profile"),
new BottomBarFragment(MessagesFragment.newInstance(), R.drawable.ic_chat, "Messages"));
// bottomBar.setActiveTabColor("#009688");
bottomBar.setActiveTabColor(getResources().getColor(R.color.blue));
}
And one of the Fragment Class:
public class ProfileFragment extends Fragment {
public static ProfileFragment newInstance(String text) {
Bundle args = new Bundle();
args.putString(STARTING_TEXT, text);
ProfileFragment homeFragment = new ProfileFragment();
homeFragment.setArguments(args);
return homeFragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.layout_profile_fragment, container, false);
// do your stuff
return rootView;
}

Button click listener in a fragment

i know this question has been asked thousands of times, but i have this code and i have no idea it doesn't work. What i want to do is having a button in a fragment that when it is clicked, it has to show a toast.
public class MyFragment1 extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_my_fragment1, container, false);
Button bot = (Button)rootView.findViewById(R.id.button3);
bot.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "button clicked", Toast.LENGTH_LONG);
}
});
return rootView;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
With this code literally nothing happens, it doesn't even show an error message. I've tried different solutions but none of them have solved the problem
You forgot to show the Toast, So kindly show the Toast using show() like
Toast.makeText(getActivity(), "button clicked", Toast.LENGTH_LONG).show();
There is actually no error, that is why you are not getting any error messages in your Logcat. Only thing is that you are not showing the Toast
See the docs for more info.

Fragment implements OnClickListener

I've got an application that I'm modernizing. One step of this process is changing to a Fragment based layout (using the Fragments from the support library).
I converted my Activities into Fragments, and got the layout working nicely (using a ViewPager, cool stuff!)
I was having my Activities implement OnClickListener for all of my button-pressing needs. I have the new Fragment incarnations doing the same thing of course, but it looks like "onClick" is never getting hit. Is there something special about Fragments that prevents them from working this way?
Just do one this
public class fragmentOne extends Fragment implements OnClickListener {
Button myButton;
#Override
public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState) {
View myView = inflater.inflate(R.layout.fragment_1, container, false);
myButton = (Button) myView.findViewById(R.id.myButton);
myButton.setOnClickListener(this);
return myView;
}
#Override
public void onClick(View v) {
// implements your things
}
}
very simple
I will Focus to use the OnClick action for global access, You have to do like this is your project, Must Implement the View.OnClickListener, then Override the Method
OnClick(), In OnCreateView() have to do like this button_submit.setOnClickListener(this); for the Views you need,
Please see the below code for Clear Answer,Thankyou.
public class New_Project extends Fragment implements View.OnClickListener{
private View mView;
private Button button_submit;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_newproject, container,false);
button_submit=(Button)mView.findViewById(R.id.button_submit);
button_submit.setOnClickListener(this);
return mView;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_submit:
//do your stuff
break;
}
}
}
I want to comment on Abhijit Chakra answer but it seems that I need to have 50 reps for that. For those who are wondering if you can't use Abhijit's answer, it is because of:
public void OnClick(View v) {
// implements your things
}
You need to make sure that it is onClick, NOT OnClick. Thankfully Android Studio internal error message come to rescue.
view.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
switch (v.getId()) {
case R.id.imgView1:
Toast.makeText(getActivity(), "Long pressing", Toast.LENGTH_SHORT).show();
updateImage();
break;
case R.id.imgView2:
Toast.makeText(getActivity(), "Long pressing", Toast.LENGTH_SHORT).show();
updateImage();
break;
case R.id.imgView3:
Toast.makeText(getActivity(), "Long pressing", Toast.LENGTH_SHORT).show();
updateImage();
break;
default:
break;
}

Categories

Resources