How to implement button function in fragments in Android studio - android

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

Related

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

Trouble adding Logout Button to Tab Fragment Class?

I recently made a simple app that allows users to register information, login, and logout. I want to increase the complexity of this app by adding a tabbed activity that the user sees when they login, and having the third tab contain a TextView that will log them out.
Here is what I did previously to log out in my Main Activity before adding tabs:
public class MainActivity extends AppCompatActivity{
private Button bLogout;
private Session session;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
session = new Session(this);
if(!session.loggedin()){
logout();
}
bLogout = (Button) findViewById(R.id.bLogout);
bLogout.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
logout();
}
});
}
private void logout(){
session.setLoggedin(false);
finish();
startActivity(new Intent(MainActivity.this, LoginActivity.class));
}
}
Now, I want to transfer this same concept to my Tab3 Fragment Class, but I keep getting errors. Here is the Tab3 class without errors:
public class Tab3User extends Fragment{
private TextView tvLogout;
private Session session;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab3User, container, false);
tvLogout = (TextView) rootView.findViewById(R.id.tvLogout);
return rootView;
}
}
The errors happen when I try to create a new session using this as Context, as well as in the 'startActivity' method in my logout function when I try to use 'Tab3User.this'. The onClickListener seems to be working, but I am very new to android dev so I'm sure I'm just making a mistake. Here is my attempt to add in everything:
public class Tab3User extends Fragment{
private TextView tvLogout;
private Session session;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab3User, container, false);
tvLogout = (TextView) rootView.findViewById(R.id.tvLogout);
return rootView;
//error here under the "this"
session = new Session(this);
if(!session.loggedin()){
logout();
}
tvLogout.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
logout();
}
});
}
private void logout(){
session.setLoggedin(false);
finish();
startActivity(new Intent(Tab3User.this, LoginActivity.class));
}
}
Thanks for any and all help. I've been looking online but many answers to questions like this are very ambiguous so I decided to post.
Inside a fragment you need to use getContext() / getActivity().
session = new Session(getContext());

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

Open a new activity with a button from a static fragment?

I have an Activity (main) with four fragments. I want to add one button to the third fragment that opens another Activity (secondary) with three fragments. When I press back I want to return to the main Activity.
I have searched for an answer, but I can't find one that works. My fragments' codes are inside the main activity, and the fragments are static because otherwise the app Force Closes when rotating to landscape mode.
I took the button code from here:
http://developer.android.com/reference/android/widget/Button.html
public class fragmentFour extends Fragment {
Intent intent = new Intent(getActivity(), musikteori_ackord.class);
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_layout1, container, false);
}
final Button button = (Button) findViewById(R.id.buttonAckord);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
}
Errors:
- Cannot resolve symbol 'setOnClickListener'
- Cannot resolve symbol 'v'
So just add a Button in the third fragment and onClick of that button, start an Intent that opens up the new desired Activity containing the other three fragments.
[EDIT]
Try this:
public class fragmentFour extends Fragment {
Intent intent;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_layout1, container, false);
intent = new Intent(getActivity(), musikteori_ackord.class);
final Button button = (Button) root.findViewById(R.id.buttonAckord);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
return root;
}
}

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