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;
}
Related
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 have several fragments here and there's this one fragment where I want to save its state when I get back to it. I tried doing it in a testfragment where, when I click the button, the textview text will change to "test1"(The default text of the textview is "New Text"). So the textview changes the text, but when I move into another fragment and get back to it (HomeFragment), the textview is going to its default state text ("New Text"). So I think the savedInstanceState is not working because if it does, it should have "test1" when I get back to the fragment. Here's my code:
public class HomeFragment extends Fragment {
public Button button;
public TextView txt_test;
public String data_test;
public HomeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
return view;
}
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
if(savedInstanceState == null){
}
else{
data_test = savedInstanceState.getString("txt_test");
txt_test = (TextView) getActivity().findViewById(R.id.textView2);
txt_test.setText(data_test);
}
button = (Button) getActivity().findViewById(R.id.button3);
txt_test = (TextView) getActivity().findViewById(R.id.textView2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_test.setText("test1");
}
});
}
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString("txt_test",data_test);
}
}
public class HomeFragment extends Fragment {
Button button;
TextView txt_test;
String data_test;
public HomeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
txt_test = (TextView) view.findViewById(R.id.textView2);
button = (Button) view.findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_test.setText("test1");
}
});
if(savedInstanceState == null) {
// do nothing
} else {
data_test = savedInstanceState.getString("txt_test");
txt_test.setText(data_test);
}
return view;
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString("txt_test",data_test);
}
}
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);
}
}
i wanna open the browser from my fragment
but when i start my app it crashes
what is wrong ?
public class C_spider extends Fragment {
Button btn_off2;
public C_spider() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_cspider, container, false);
btn_off2 = (Button)view.findViewById(R.id.btn_off2);
btn_off2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://www.google.com"));
getActivity().startActivity(i);
}
});
return view;
}
}
try putting your codes in the onActivityCreated()
public class C_spider extends Fragment {
Button btn_off2;
public C_spider() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_cspider, container, false);
return view;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btn_off2 = (Button)getActivity().findViewById(R.id.btn_off2);
btn_off2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://www.google.com"));
getActivity().startActivity(i);
}
});
}
}