Hello I am creating an application in Android using Tab Layout. I am not good with fragments and I cant get it to work, the application displays the right layout but button and stuff like that is unresponsive. Here is my code (I cant make the first class "FragmentActivity" because the caller wont accept that
public class ebookC extends android.support.v4.app.Fragment {
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout_ebook, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
public static class ebookr extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_ebook);
Button button= (Button) findViewById(R.id.nextbt);
final TextView textView = (TextView) findViewById(R.id.pageCount);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText("Works????");
}
});
}
}
}
I'm guessing that the activity ebookr is actually not used. I'm also guessing that the fragment does get used, but you expect the OnClickListener to work.
Here is what I assume you are trying to do:
public class ebookC extends android.support.v4.app.Fragment {
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.layout_ebook, container, false);
Button button= (Button) v.findViewById(R.id.nextbt);
final TextView textView = (TextView) v.findViewById(R.id.pageCount);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText("Works????");
}
});
return v;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
Related
I am a beginner in android development and I want to navigate to an activity from a fragment when an image in ImageView is clicked. My Java class does not let me declare variables to link to my XML file. Also, I am not sure as to in which java class I should be declaring my variables.
I have also tried writing the same code in my MainActivity but it still shows an error.
public class HomeFragment extends Fragment{
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable
ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home,container,false);
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
edSheeranSongs = (ImageView)findViewById(R.id.ed_sheeran);
justinBieberSongs = (ImageView)findViewById(R.id.justin_bieber);
shawnMendesSongs = (ImageView)findViewById(R.id.shawn_mendes);
edSheeranSongs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),SheeranSongs.class);
startActivity(intent);
}
});
I expect to go to another activity which I have already created called SheeranSongs, But I get an error message when I try to declare anything in a fragment
You should inflate the view in onCreateView of the fragment and initilize views with the created custom view. Override onAttach to get the context of the activivty to which the fragment is attached to.
public class HomeFragment extends Fragment{
private Context context;
#Override
public void onAttach(Context context) {
super.onAttach(context);
this.context=context;
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
edSheeranSongs = view.findViewById(R.id.ed_sheeran);
justinBieberSongs = view.findViewById(R.id.justin_bieber);
shawnMendesSongs = view.findViewById(R.id.shawn_mendes);
edSheeranSongs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, SheeranSongs.class);
startActivity(intent);
}
});
return view;
}
}
You should call findViewById() from the inflated view within onCreateView of the Fragment. Exapmle:
public class HomeFragment extends Fragment{
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
edSheeranSongs = view.findViewById(R.id.ed_sheeran);
justinBieberSongs = view.findViewById(R.id.justin_bieber);
shawnMendesSongs = view.findViewById(R.id.shawn_mendes);
edSheeranSongs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(requireContext(), SheeranSongs.class);
startActivity(intent);
}
});
return view;
}
}
When working with fragment you can't call findviewbyid() in onCreate() as you do in Activity since the fragment is created in onCreate() but the user interface isn't drawn yet (More about fragment).
You can do it in onCreateView() or onViewCreated(). From my perspective, I do it in onViewCreated() so it looks a bit cleaner.
public class HomeFragment extends Fragment{
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home,container,false);
}
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ImageView edSheeranSongs = view.findViewById(R.id.ed_sheeran);
ImageView justinBieberSongs = view.findViewById(R.id.justin_bieber);
ImageView shawnMendesSongs = view.findViewById(R.id.shawn_mendes);
edSheeranSongs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(requireActivity(), SheeranSongs.class);
startActivity(intent);
}
});
}
Code :
public class CustomerDetails extends Fragment {
EditText CustomerCode,FullName,AddressLine1,AddressLine2,MobileNo,EmailId,CustomerType;
Button Submit;
IonExchangeDataBasehelper ionexchangedatabase;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//super.onCreate(savedInstanceState);
// View v =inflater.inflate(R.layout.fragment_customer_details,container,false);
View v =getActivity().getLayoutInflater().inflate(R.layout.fragment_customer_details,null);
CustomerCode=(EditText)v.findViewById(R.id.CustomerCode);
FullName=(EditText)v.findViewById(R.id.FullName);
AddressLine1=(EditText)v.findViewById(R.id.AddressLine1);
AddressLine2=(EditText)v.findViewById(R.id.AddressLine2);
MobileNo=(EditText)v.findViewById(R.id.MobileNo);
EmailId=(EditText)v.findViewById(R.id.EmailId);
CustomerType=(EditText)v.findViewById(R.id.CustomerType);
Submit=(Button)v.findViewById(R.id.Submit);
Submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String abc = CustomerCode.getText().toString();
Toast.makeText(getActivity(),"hhh :" ,Toast.LENGTH_SHORT).show();
// database();
}
});
return v;
}
Cleaned your code; try it and say if you still get errors
public class CustomerDetails extends Fragment {
EditText customerCode,fullName,addressLine1,addressLine2,mobileNo,emailId,customerType;
Button submit;
IonExchangeDataBasehelper ionexchangedatabase;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_customer_details, container,
false);
// get the reference of views
customerCode=(EditText)v.findViewById(R.id.CustomerCode);
fullName=(EditText)v.findViewById(R.id.FullName);
addressLine1=(EditText)v.findViewById(R.id.AddressLine1);
addressLine2=(EditText)v.findViewById(R.id.AddressLine2);
mobileNo=(EditText)v.findViewById(R.id.MobileNo);
emailId=(EditText)v.findViewById(R.id.EmailId);
customerType=(EditText)v.findViewById(R.id.CustomerType);
submit=(Button)v.findViewById(R.id.Submit);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String abc = customerCode.getText().toString();
Toast.makeText(getActivity(), abc ,Toast.LENGTH_SHORT).show();
// database();
}
});
return v;
}
I want to change the background color of a fragment. But when I click on the button nothing happens.
In my main activity layout XML file I imported the fragment.
Here is my code:
public class Top_Fragment extends Fragment implements View.OnClickListener {
Button button;
LinearLayout mLinearLayout;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.top_fragment, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
button = (Button) getActivity().findViewById(R.id.button);
mLinearLayout = (LinearLayout) getActivity().findViewById(R.id.layout);
}
#Override
public void onClick(View v) {
mLinearLayout.setBackgroundColor(Color.parseColor("#ffffbb33"));
}
}
Your code is perfectly but you need to add button.setOnClickListener(this);
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
button = (Button)getActivity().findViewById(R.id.button);
mLinearLayout = (LinearLayout)getActivity().findViewById(R.id.layout);
button.setOnClickListener(this); }
Replace
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.top_fragment,container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
button = (Button)getActivity().findViewById(R.id.button);
mLinearLayout = (LinearLayout)getActivity().findViewById(R.id.layout);
}
with
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.top_fragment,container, false)
button = (Button)fragmentView.findViewById(R.id.button);
button.setOnClickListener(this);
mLinearLayout = (LinearLayout)fragmentView.findViewById(R.id.layout);
return fragmentView;
}
I trying out Fragment and have a problem with OnClickListener. Anyone can help?
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frag_one frag1=new frag_one();
FragmentManager manager=getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.add(R.id.top,frag1,"");
transaction.commit();
}
}
frag_one.java
public class frag_one extends Fragment{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_frag_one,container,false);
}
public class calculator extends AppCompatActivity implements View.OnClickListener {
TextView get_result;
EditText get_num1;
EditText get_num2;
Button get_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frag_one);
get_result=(TextView)findViewById(R.id.result);
get_num1=(EditText) findViewById(R.id.num1);
get_num2=(EditText) findViewById(R.id.num2);
get_button=(Button)findViewById(R.id.submit);
get_button.setOnClickListener(this);
}
public void onClick(View v) {
int num1=Integer.parseInt((get_num1.getText().toString()));
int num2=Integer.parseInt((get_num2.getText().toString()));
int sum=num1+num2;
get_result.setText(Integer.toString(sum));
}
}
}
When I run on normal activity without using Fragment. When i click the button, the calculation works. But when i move to Fragment, its not working. Please help! thx
You made a second Activity that uses the layout of the Fragment instead of actually placing the logic into the Fragment class....
public class frag_one extends Fragment implements View.OnClickListener {
TextView get_result;
EditText get_num1;
EditText get_num2;
Button get_button;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_frag_one,container,false);
get_result=(TextView)rootView.findViewById(R.id.result);
get_num1=(EditText)rootView.findViewById(R.id.num1);
get_num2=(EditText) rootView.findViewById(R.id.num2);
get_button=(Button)rootView.findViewById(R.id.submit);
get_button.setOnClickListener(this);
return rootView;
}
public void onClick(View v) {
// TODO: Check v.getId() == R.id.submit
int num1=Integer.parseInt((get_num1.getText().toString()));
int num2=Integer.parseInt((get_num2.getText().toString()));
int sum=num1+num2;
get_result.setText(Integer.toString(sum));
}
}
you not referring any view here, hope this will help you
public class ABC extends Fragment implements OnClickListener {
View view;
Context context;
Button btnClick;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment, container, false);
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
context = activity;
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btnClick = (Button) view.findViewById(R.id.btnClick);
btnClick.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnClick:
break;
default:
break;
}
}
}
I have two fragments in my mainactivity, from the first one, the user clicks on a button and send through my listener a char to the second fragment, depends of that char a textview in the second fragment must print a text. But in my metho onActivityCreated my textview is always null.
This is my code:
FragmentOne
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btnChefcito = (Button)getView().findViewById(R.id.btnChefcito);
btnChefcita = (Button)getView().findViewById(R.id.btnChefcita);
btnChefcito.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeListener.elegirSexo(sexo);
replaceFragment();
}
});
btnChefcita.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sexo="f";
welcomeListener.elegirSexo(sexo);
replaceFragment();
}
});
}
This is my second fragment
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
tviBienvenida = (TextView)getView().findViewById(R.id.tviBienvenida);
}
public void getSex(String sex){
if(sex.equals("m")){
tviBienvenida.setText(bienvenido);
}else if(sex.equals("f")){
tviBienvenida.setText(bienvenida);
}
}
Instead of doing in onActivityCreated Do This in onCreateView:
private static View viewHolder;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
viewHolder = inflater.inflate(R.layout.YourFragmentLayout, container, false);
btnChefcito = (Button) viewHolder.findViewById(R.id.YourButtonId);
btnChefcito.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeListener.elegirSexo(sexo);
replaceFragment();
}
});
return viewHolder;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.your_fragment_id, null, false);
btnChefcito = (Button)view.findViewById(R.id.btnChefcito);
btnChefcita = (Button)view.findViewById(R.id.btnChefcita);
btnChefcito.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeListener.elegirSexo(sexo);
replaceFragment();
}
});
btnChefcita.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sexo="f";
welcomeListener.elegirSexo(sexo);
replaceFragment();
}
});
return view;
}
the problem is public void onActivityCreated(Bundle savedInstanceState) not create any view in fragment so replace all code in this method
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
sorry for my English.
On Android you have to declare a global variable.
tviBienvenida in you class, and on create view
tviBienvenida = (TextView)getView().findViewById(R.id.tviBienvenida);
The variable tviBienvenida will always have the value, and any change in tviBienvenida will also be refected to your textview.
Implement view in onCreateView method and repleace your stuff there, for example:
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_layout, container, false);
TextView textView = view.findViewById(R.id.yourTextView);
//your code
return view;
}