Starting new Activity from a Fragment with a button - java

I'm new to Java and android and I'm having some issues.
I'm using navigation drawer activity with each menu item linked to a fragment. From the fragment, I'm trying to setup another menu using buttons that link to more Activities.
Here is my fragment that I want to link to more activities:
package com.notimportant
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
/**
* A simple {#link Fragment} subclass.
*/
public class About extends Fragment {
public About() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_about, container, false);
}
Button button = (Button) rootView.findViewById(R.id.buttonDevs);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), DevsActivity.class);
startActivity(intent);
}
});
}
I'm getting these errors:
Error:(35, 30) error: <identifier> expected
Error:(35, 31) error: illegal start of type
Error:(35, 34) error: ')' expected
Error:(35, 39) error: ';' expected
Error:(35, 40) error: invalid method declaration; return type required
Error:(37, 28) error: ';' expected
Error:(37, 38) error: ';' expected
Error:(41, 6) error: illegal start of type
rootView is highlighted red "Cannot resolve symbol rootView"
setOnClickListener is highlighted red "Cannot resolve symbol
setOnClickListener"
#Override is highlighted red... "Annotations are not allowed here"
view is highlighted red "Cannot resolve symbol view"
My button id is buttonDevs. The activity I want to go to is called DevsActivity.
What am I doing wrong?

You have to put Button inside rootView.
Change this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_about, container, false);
}
Button button = (Button) rootView.findViewById(R.id.buttonDevs);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), DevsActivity.class);
startActivity(intent);
}
});
to this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_about, container, false);
Button button = (Button) rootView.findViewById(R.id.buttonDevs);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), DevsActivity.class);
startActivity(intent);
}
});
return rootView;
}

#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_about, container, false);
Button button = (Button) view.findViewById(R.id.buttonDevs);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), DevsActivity.class);
startActivity(intent);
}
});
return view;
}

Change this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_about, container, false);
}
Button button = (Button) rootView.findViewById(R.id.buttonDevs);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), DevsActivity.class);
startActivity(intent);
}
});
on this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_about, container, false);
Button button = (Button) rootView.findViewById(R.id.buttonDevs);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), DevsActivity.class);
startActivity(intent);
}
});
return rootView;
}

Put your button inside onCreateView() like this -
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_about, container, false);
Button button = (Button) view.findViewById(R.id.buttonDevs);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), DevsActivity.class);
startActivity(intent);
}
});
return view;
}

your code must be inside the onViewCreated() method since you cant interact with the Views from onCreateView() method
#Override
public void onViewCreated(View rootView, #Nullable Bundle savedInstanceState) {
Button button = (Button) rootView.findViewById(R.id.buttonDevs);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), DevsActivity.class);
startActivity(intent);
}
});
}
good luck

Related

Intent from fragment in drawer to LoginActivity

Hi I am trying to intent from a fragment in a drawer when I click on the button to the LoginActivity,but it's not working
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_logout, container, false);
btn=view.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().startActivity(new Intent(getContext(),LoginActivity.class));
}
});
return view;
}
what shall I do?

Need help getting a Toast to appear on a Tab Fragment

I'm trying to get a Toast to appear in a tabbed fragment when an EditText is left empty.
Here is my MainActivity's onCreate:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
}
My Tabbed Fragment works fine with this code:
public class SignupFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_signup, container, false);
}
}
But it doesn't work when creating the Toast like this:
public class SignupFragment extends Fragment {
EditText textFillCheck;
Button submitCheck;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
submitCheck = (Button) submitCheck.findViewById(R.id.signupBtn);
textFillCheck = (EditText) textFillCheck.findViewById(R.id.signupFirstName);
submitCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (TextUtils.isEmpty(textFillCheck.getText().toString())) {
Intent intent = new Intent(getActivity(), SignupFragment.class);
startActivity(intent);
Toast.makeText(getActivity(), "Please fill in all fields", Toast.LENGTH_SHORT).show();
}
else{Toast.makeText(getActivity(), textFillCheck.getText().toString(), Toast.LENGTH_LONG).show();
}
}
});
return inflater.inflate(R.layout.fragment_signup, container, false);
}
}
You're not accessing the views of your Fragment's layout so the layout you're returning in onCreateView() doesn't have your logic attached to it. You should set up your Fragment to look something like this:
public class SignupFragment extends Fragment {
EditText textFillCheck;
Button submitCheck;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
// Store a reference to your Fragment's inflated layout
View root = inflater.inflate(R.layout.fragment_signup, container, false);
// Use the reference to access your Fragment's views
submitCheck = (Button) root.findViewById(R.id.signupBtn);
textFillCheck = (EditText) root.findViewById(R.id.signupFirstName);
submitCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(TextUtils.isEmpty(textFillCheck.getText().toString())) {
Toast.makeText(getActivity(), "Please fill in all fields", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getActivity(), textFillCheck.getText().toString(), Toast.LENGTH_LONG).show();
}
}
});
// Return the inflated layout
return root;
}
}
I am amazed why your code is not having any error like VIEW DOES NOT EXIST or NULL POINTER EXCEPTION because
First, you change the activity before showing the Toast secondly you are inflating the layout after the initialization of views and not getting the context properly, Right way of doing what you are trying to achieve is using OnViewCreated() like this
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
submitCheck = (Button) view.findViewById(R.id.signupBtn);
textFillCheck = (EditText) view.findViewById(R.id.signupFirstName);
submitCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (TextUtils.isEmpty(textFillCheck.getText().toString())) {
Toast.makeText(getActivity(), "Please fill in all fields",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), SignupFragment.class);
startActivity(intent);
}
else{
Toast.makeText(getActivity(), textFillCheck.getText().toString(),
Toast.LENGTH_LONG).show();
}
}
});
}

How to go to another activity in Fragments | Android Studio

Need to create a button to go to a new activity in Fragments. I tried with the following code but its not working, This is my code,
public class UserFragment extends Fragment {
Button mTextuserinfo;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_user,container,false);
mTextuserinfo = (Button) findViewById(R.id.userinfo_user);
mTextuserinfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(UserFragment.this, userinformation.class);
startActivity(i);
}
});
return view;
}
}
Use this in your onClick.
Intent i = new Intent(getActivity(), userinformation.class);
startActivity(i);
Also define your button like this:
mTextuserinfo = view.findViewById(R.id.userinfo_user);

Resume fragment when switching back from a another fragment in android(Java)

I heve read all the answers related this.
I have 2 buttons, both opens different fragment.
when I open a fragment, do something with it, open another fragment and come back to the first fragment then I see all the changes I made are not applied, because fragment restarted.
Please help me..
Activity
package com.example.canvastag;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.canvastag.fragments.firstFrag;
import com.example.canvastag.fragments.secondFrag;
public class asking extends AppCompatActivity {
firstFrag frag1;
secondFrag frag2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_asking);
Button button = findViewById(R.id.frag1button);
Button button2 = findViewById(R.id.frag2button);
frag1 = new firstFrag();
frag2 = new secondFrag();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, frag1, "").commit();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, frag2, "").commit();
}
});
}
}
Fragment 1
public class firstFrag extends Fragment {
View view;
TextView textView;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view= inflater.inflate(R.layout.fragment_first, container, false);
textView=view.findViewById(R.id.textFrag);
Button button = view.findViewById(R.id.buttonAdd);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText("button clicked");
}
});
return view;
}
}
Fragment 2-
public class secondFrag extends Fragment {
View view;
TextView textView;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view= inflater.inflate(R.layout.fragment_second, container, false);
textView=view.findViewById(R.id.textFrag);
Button button = view.findViewById(R.id.buttonAdd);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText("button seocnd clicked");
}
});
return view;
}
}
I want to resume fragment when I come back to it, not restart.
OUTPUT-
Thank you so much...
Instead of:
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, frag1, "").commit();
Try using:
getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer, frag1, "").commit();
Note:
You can add this to the fragment transaction: addToBackStack(null) so your previous state will be added to the backstack allowing you to go back with the back button.

HI.I have an error with findViewById

public class HomeFragment extends BasePageFragment {
private Unbinder unbinder;
// #BindView(R.id.fab)
// FloatingActionButton mFab;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_homepage, container, false);
ImageButton ImageButton = (ImageButton) FindViewById (R.id.imageButton);
ImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("https://play.google.com/store/apps/details?id=com.mowmo.minimale");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
});
}
Your problem starts here:
return inflater.inflate(R.layout.fragment_homepage, container, false);
None of the code following this statement will ever execute since you've already returned the inflated View. Change it to this:
View rootview = inflater.inflate(R.layout.fragment_homepage, container, false);
ImageButton ImageButton = (ImageButton) rootview.findViewById (R.id.imageButton);
ImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("https://play.google.com/store/apps/details?id=com.mowmo.minimale");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
});
return rootview;
This inflates the view and allows you to access the child views on the inflated view (using the findViewById method) before returning the view.

Categories

Resources