I'm having some trouble with my EditText's on orientation change. For some reason they dont restore whatever was typed in them.
I have 2 classes. The main activity and the fragment which goes in the activity
Main activity:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(
setContentView(R.layout.
getFragmentManager().beginTransaction().replace(R.id.controlsBar, new AddItemFragment()).commit();
}
}
And the fragment:
public class AddItemFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_add_item, container, false);
EditText item = (EditText) view.findViewById(R.id.itemNameAdd);
EditText amount = (EditText) view.findViewById(R.id.itemAmount);
if (savedInstanceState != null) {
System.out.println(savedInstanceState.getString("item"));
item.setText(savedInstanceState.getString("item", ""));
amount.setText(savedInstanceState.getString("amount", ""));
}
//item.setText("SOME TEXT");
//amount.setText("SOME TEXT");
return view;
}
#Override
public void onSaveInstanceState(Bundle outState) {
EditText item = (EditText) getActivity().findViewById(R.id.itemNameAdd);
EditText amount = (EditText) getActivity().findViewById(R.id.itemAmount);
outState.putString("item", item.getText().toString());
outState.putString("amount", amount.getText().toString());
}
}
The funny thing is that the line "System.out.println(savedInstanceState.getString("item"));" prints out the correct word in the console.
And furthermore the outcommented lines where i set the text to "SOME TEXT" also works.
Its only when i set the text to savedInstanceState.getString("amount", ""), it wont work
Thank you
Try adding the line marked "<---- THIS LINE" to public void onSaveInstanceState(Bundle outState):
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); // <---- THIS LINE
EditText item = (EditText) getActivity().findViewById(R.id.itemNameAdd);
EditText amount = (EditText) getActivity().findViewById(R.id.itemAmount);
outState.putString("item", item.getText().toString());
outState.putString("amount", amount.getText().toString());
}
Also, look at using onActivityCreated, and check out the resources in my comment below.
Related
I have a class called "text". and then I have Dialog Fragment named "dialog" under this "text" class.
I am trying to make visible the text from the showText() which is located in the "text" class.
But it's getting visible. it's getting visible only from the dialog fragment.
Could anyone please help find out the issue.
private class text extends Activity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dialog dialogobj = new dialog();
dialogobj.show(mActivity.getFragmentManager(), "dialog");
dialogobj.showText();
}
public class dialog extends DialogFragment {
private TextView mText1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
setStyle(STYLE_NO_TITLE, android.R.style.Theme_Material_Light_Dialog);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View dialogView = inflater.inflate(R.layout.dummy_layout, container, false);
mText1 = (TextView) dialogView.findViewById(R.id.left_img_view);
}
private void showText() {
mText1.setVisibility(View.Visible);
}
}
}
You have declared the variable mText1 as textview, but when call the findViewById method you are casting with Imageview. Check if it is causing the problem.
I advise using a capital letter as the first letter of your classes.
I think you should try to call your dialog initialization in the onCreate method of the Activity:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Dialog dialog = new Dialog();
dialog.show(getFragmentManager(), "dialog");
dialog.showText();
}
In my ViewPager i have some fragments which i change some TextView texts dynamically, after switch between pages texts of TextViews clear, and i can't save and restore them from instance
this is my code and dont work
private String value1;
private String value2;
private String value3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//#formatter:off
context = getActivity().getBaseContext();
activity = getActivity();
//#formatter:on
}
#Override
public void onViewStateRestored(#Nullable Bundle inState) {
super.onViewStateRestored(inState);
if (inState != null) {
inState.getString("value1", value1);
inState.getString("value2", value2);
inState.getString("value3", value3);
sahmiyeh_value_1.setText(value1);
sahmiyeh_value_2.setText(value2);
sahmiyeh_value_3.setText(value3);
}
}
#Override
public void onSaveInstanceState(#Nullable Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("value1", sahmiyeh_value_1.getText().toString());
outState.putString("value2", sahmiyeh_value_2.getText().toString());
outState.putString("value3", sahmiyeh_value_3.getText().toString());
}
You can try freezesText
<TextView
...
android:freezesText="true" />
From documentation :
If set, the text view will include its current complete text inside of its frozen icicle in addition to meta-data such as the current cursor position. By default this is disabled; it can be useful when the contents of a text view is not stored in a persistent place such as a content provider
You didn't declare the string value.
value1, value2, value3 is empty. That's why there's no value on your TextView or EditText.
#Override
public void onViewStateRestored(#Nullable Bundle inState) {
super.onViewStateRestored(inState);
if (inState != null) {
value1 = inState.getString("value1");
value2 = inState.getString("value2");
value3 = inState.getString("value3");
sahmiyeh_value_1.setText(value1);
sahmiyeh_value_2.setText(value2);
sahmiyeh_value_3.setText(value3);
}
}
Or you can do it like this.
sahmiyeh_value_1.setText(inState.getString("value1"));
sahmiyeh_value_2.setText(inState.getString("value2"));
sahmiyeh_value_3.setText(inState.getString("value3"));
signup screen with of two different modules driver and customer
Hi everyone I am working in android from past one year but now I am creating an app in which am using fragments instead of activity even after serching from so many sites and blogs am unable to implement what I want.
My requirement is as you see on the image two radio button one for customer and one for driver so when user click on any one of options then signup screen appears which is on fragment and one button is there at bottom which is on activity so I want how to get all edittext value on button click so that i can send these values to the server. My main problem is getting the values of edittext fields on button click.
So any help will be appriciated.
thanks in advance
Try like this
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.your_fragment, container, false);
this.initViews(rootView);
selectProfessionsTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
whta_you_want_on_button_click();
}
});
}
private void initViews(View view) {
//define your edittext like FirstNameEditText = (EditText) view.findViewById(R.id.FirstNameEditText);
}
now define the whta_you_want_on_button_click() method
in your activity, put below code in your button's click event.
try {
Fragment rg = getSupportFragmentManager().findFragmentByTag("YourFragmentTag");
if (rg != null) {
EditText et = (EditText) ((YourFragment) rg).getView().findViewById(R.id.edittext);
String etValue = et.getText().toString();
}
} catch (Exception e) {
}
Your context will change from getApplicationContext() to getActivity.getApplicationContext() when you are referencing a fragment instead of an activity
You have to create your edittext in this fashion
While assigning the layout you need to assign in this way.
View view = inflater.inflate(R.layout.fragmentlayout,container,false);
And while assigning edittext you need to take it as below
EditText edt = (EditText) view.findViewById(R.id.yourid);
String str = edt.getText().toString();
So on button click you can get the edittext string easily from onClick method with above code.
You can use static keyword make all edittext static so you will get all edittext values in activity on button click.
in your fragment suppose MyFragment.java
public class MyFragment extends Fragment{
View rootView;
public static EditText edt;
public MyFragment() {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_main, container, false);
init();
return rootView;
}
protected void init(){
edt = (EditText) rootView.findViewById(R.id.edt);
}
}
in your activity suppose MainActivity.java
public class MainActivity extends Activity{
Button start;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewByid(R.id.button);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try{
String value = MyFragment.edt.getText().toString().trim();
Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
}catch(Exception e){}
}
});
}
}
My app uses one activity and many fragments, the activity which extends AppCompatActivity inflates an xml file which has a LinearLayout among
other ViewGroups, the purpose of this LinearLayout is to hold 3 buttons not all are visible at start.
In code, I change the view of LinearLayout and
the view and texts of some of its buttons depending on the action taken in the current fragment. However their state is not maintained when the
activity is reCreated after phone home key press.
Saving visibility and text of each button every time one gets chnaged in SharedPreferences is too misssy, so I tried the below code but failed.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mMainButtons_LL != null) {
mMainButtons_LL = (LinearLayout) savedInstanceState.getSerializable("mainButtons");
} else {
mMainButtons_LL = (LinearLayout) findViewById(R.id.main_buttons_LL);
}
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("mainButtons", (Serializable) mMainButtons_LL);
}
Change your code to something like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.id.your_layout); // << replace your layout id here
mMainButtons_LL = (LinearLayout) findViewById(R.id.main_buttons_LL);
if (savedInstanceState != null) {
mMainButtons_LL.setVisibility(savedInstanceState.getInt("visibility"));
mMainButtons_LL.setText(savedInstanceState.getString("text");
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("visibility", mMainButtons_LL.getVisibility());
outState.putString("text", mMainButtons_LL.getText.toString());
}
I have this error nullpointerexception, when i do getText().toString() from EditTex:
public class SendMessActivity extends SherlockFragmentActivity {
private EditText tEmail;
private Button sendButton;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_mess_layout);
tEmail = (EditText)findViewById(R.id.editEmailTo);
sendButton = (Button)findViewById(R.id.btn_sendmess);
endButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//String textEmail = tEmail.getText().toString(); //nullpointerexception
Editable textEmail1Editable = tEmail.getText(); //nullpointerexception
String textEmail = textEmail1Editable.toString()
Log.d(DEBUGTAG, "SENDING START:::::::: " + textEmail);
}
});
}}
Please, tell me how to do it
UPDATE Q
David, thank you, for your surmise,the problem was really in the my tangled Layouts
I had all 4 levels of nesting LinearLayouts.
After I left the simplified scheme and only 2 levels I have, all began to work
You need to call setContentView(your_layout.xml) so it knows what layout to use. Without setting the layout, all of your calls to findViewById(...) that try to find the views in your layout will return null.
public class SendMessActivity extends SherlockFragmentActivity {
private EditText tEmail;
private Button sendButton;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(your_layout.xml); //set your activity layout here.
tEmail = (EditText)findViewById(R.id.editEmailTo);
sendButton = (Button)findViewById(R.id.btn_sendmess);
endButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String textEmail = tEmail.getText().toString(); //nullpointerexception
Log.d(DEBUGTAG, "SENDING START:::::::: " + textEmail);
}
});
}}
If you have no assigned text then I would assume that the EditText retrieval of the text would be a null entry and you are attempting to make a string of that null entry.