I'd like to create editable textfield in fragment, which after close or stop of app would be saved. But there's something wrong in line with return notatki; I already have this:
public class DetailFragment2 extends Fragment {
private EditText notatki;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#SuppressLint("SimpleDateFormat")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details2, parent, false);
notatki = (EditText) view.findViewById(R.id.editText1);
SharedPreferences settings = this.getActivity().getSharedPreferences("PREFS", 0);
notatki.setText(settings.getString("value", ""));
return view;
}
#Override
public void onStop( ){
super.onStop();
if(notatki.getText() != null) {
SharedPreferences settings = this.getActivity().getSharedPreferences("PREFS", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("value", notatki.getText().toString());
editor.commit();
}
}
}
When I change return notatki; to return view; it works until the stop of app, when I wanted to save content of editText but it isn't saving anything.
Change to
private EditText notatki;
#SuppressLint("SimpleDateFormat")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details2, parent, false);
notatki = (EditText) view.findViewById(R.id.editText1);
SharedPreferences settings = this.getActivity().getSharedPreferences("PREFS", 0);
notatki.setText(settings.getString("value", ""));
return view; // return view here instead of notaki
}
You have already declared EditText as a class member
private EditText notatki;
So just initialize it in onCreateView.
Change
EditText notatki = (EditText) view.findViewById(R.id.editText1);
to
notatki = (EditText) view.findViewById(R.id.editText1);
because if you declare it again in your onCreateView it will shadow the global one and you will get it null in onStop
Try This....
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details2, parent, false);
EditText notatki = (EditText) view.findViewById(R.id.editText1);
SharedPreferences settings = this.getActivity().getSharedPreferences("PREFS", 0);
notatki.setText(settings.getString("value", ""));
return view;
}
You are doing it right but the problem is that you should return the parent view from onCreateView and not the EditText instance notatki
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details2, parent, false);
....
return view;
}
private EditText etext;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.secondfragment,container,false);
etext = (EditText) getView().findViewById(R.id.editText4);
return v;
Related
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//Animation animation = AnimationUtils.loadAnimation(this.getContext(), R.anim.fade);
sectionlabeltextview = (TextView) rootView.findViewById(R.id.section_label);
sectionlabeltextview.setText(getArguments().getString("text"));
sectionlabeltextview.setTextColor(Color.BLACK);
sectionlabeltextview.setTextSize(25);
sectionlabel_text = (String) sectionlabeltextview.getText();
Toast.makeText(getContext(),sectionlabel_text,Toast.LENGTH_SHORT).show();
Log.e("sectionlabel_text",sectionlabel_text);
SharedPreferences sharedPref=getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPref.edit();
editor.putString("catch_text",sectionlabel_text);
editor.commit();
//sectionlabeltextview.startAnimation(animation);
//sectionlabeltextview.setGravity();
return rootView;
}
Here the values of sectionlabel_text and toast message are getting differed, though both are in the same function.Please try to give the solution?
I am very new to android and I am trying to make CardView by programmatically, I am using Fragment. By setting an OnClick on the button I am able to create cards in my layout. The issue is after closing the activity cards are not showing.
How can I save the cards, so after closing the activity cards should be there?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle("Okhlee");
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.hawker_jobs_fragment_layout, container, false);
context = getContext();
button = (Button) view.findViewById(R.id.hawker_job_add_card_view_button);
button.setOnClickListener(this);
relativeLayout = (RelativeLayout) view.findViewById(R.id.hawker_job_relative_layout);
return view;
}
#Override
public void onClick(View v) {
createCardView();
}
/*Adding cardview programmatically function */
private void createCardView() {
CardView addedCardView = new CardView(context);
layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, height);
addedCardView.setLayoutParams(layoutParams);
addedCardView.setPadding(10 ,10, 10, 10);
addedCardView.setRadius(15);
addedCardView.setCardBackgroundColor(Color.BLUE);
addedCardView.setMaxCardElevation(30);
addedCardView.setMaxCardElevation(6);
relativeLayout.addView(addedCardView);
}
Sorry for my bad English.
You can use SharedPreferences in order to know whether you should display the card.
Here's a sample code using your original one -
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.hawker_jobs_fragment_layout, container, false);
context = getContext();
button = (Button) view.findViewById(R.id.hawker_job_add_card_view_button);
button.setOnClickListener(this);
relativeLayout = (RelativeLayout) view.findViewById(R.id.hawker_job_relative_layout);
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
boolean shouldDisplayCard = sharedPref.getBoolean("should_display_card", false);
if(shouldDisplayCard)
createCardView();
return view;
}
#Override
public void onClick(View v) {
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("should_display_card", true);
editor.apply();
createCardView();
}
Hi I am new to fragments and I am working with this activity.I populated an edit text but I want to highlight the borders when the user focus on one of the edit text.I created the method focus and I am calling it,but it is not working.
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class Fragment_Info extends Fragment {
EditText CompanyName;
EditText CompanyOwner;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_fragment__info, container, false);
CompanyName = (EditText) view.findViewById(R.id.CompName);
CompanyOwner = (EditText) view.findViewById(R.id.CompOwner);
focus(CompanyName);
focus(CompanyOwner);
//Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_fragment__info, container, false);
}
public void focus(View view){
view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
v.setBackgroundResource(R.drawable.focus_border_style);
else
v.setBackgroundResource(R.color.colorPrimary);
}
});
}
}
You are applying focus on a view and returning a different view from the method.
Change
return inflater.inflate(R.layout.activity_fragment__info, container, false);
to
return view;
Change
return inflater.inflate(R.layout.activity_fragment__info, container, false);
to
return view;
Hi I am trying to get an id of some View in a Fragment from a string, however it doesn't seem to work... Is there a special case for fragments?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View singleplayerView = inflater.inflate(R.layout.singleplayer_tab, container, false);
setupMap();
return singleplayerView;
}
private void setupRank() {
TextView view = (TextView) getByStringId("rank0");
view.setText("hello");
}
private final View getByStringId(final String id) {
return findViewById(getResources().getIdentifier(id, "id", getActivi
ty().getPackageName()));
}
So I am trying to set the textView field rank0 in the layout that is used for my fragment... but right now it gets a NUllPointer because it couldnt find the id
Thank you
you can define your Layout In your onCreateView() inside Fragments like this:
View V = new View(getActivity());
V = inflater.inflate(R.layout.fragment_layout,container, false);
Then
TextView SomeField = (TextView)V.findViewById(R.id.TxtSomeField);
Inside fragment, add getActivity before findViewById:
getActivity().findViewById(...);
Try this,
String id;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extra = getArguments();
id = extra.getString("id", "");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_calendar_details, container, false);
Button b = (Button) view.findViewById(id);
return view;
}
I have a fragment with one editText
<EditText
android:id="#+id/textViewName"
/>
I want to change its text at run time .
How can i access edit Text so that i can change its text?
Inside onCreateView(), you can find out particular view by inflating your layout.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myFragmentLayout, container, false);
EditText editText = (EditText) view.findViewById(R.id.textViewName);
return view;
}
Or
you can use getView() if you don't want to inflate, it will return root view of the fragment.
EditText editText = (EditText) getView().findViewById(R.id.textViewName);
Let's Say you have a class EditFragment
public class EditFragment extends Fragment
{
private EditText edit;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myFragmentLayout, container, false);
edit = (EditText) view.findViewById(R.id.textViewName);
return view;
}
}
You can make a getter to access the EditText of this Fragment
public EditText getEditText()
{
return edit;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
EditText e = (EditText) view.findViewById(R.id.textViewName);
....
}