This is my first time posting so bare with please.
I'm trying to set the text of a textView item in the navigation drawer fragment from another activity.
I've tried:
public class SomeActivity{
...
...
NavigationDrawerFragment.textViewName.setText("words");
}
public class SomeActivity{
...
...
NavigationDrawerFragment frag = (NavigationDrawerFragment )
getFragmentManager().findFragmentByTag("NavigationDrawerFragment");
frag.GetTextFromActivityMethod("words");
}
and I get a null pointer exception and class not loaded (NavigationDrawerFragment) error in debugger.
Try SharedPreference, here is a sample for your case:
public class SomeActivity{
NavigationDrawerFragment.textViewName.setText("words");
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
editor.putString("WORDS", b);
editor.commit();
}
public class SomeActivity {
NavigationDrawerFragment frag = (NavigationDrawerFragment)
getFragmentManager().findFragmentByTag("NavigationDrawerFragment");
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
String words = pref.getString("WORDS", null);
textView.setText
}
From main activity you can set the fragment drawer's text view value. I have set it and it works well!
In navigation Drawer Bar
public TextView test_text;
test_text = (TextView) viewtype.findviewbyid(R.id.xyz);
In the main activity
String ABC = preference.getString(key);
test_text.setText(ABC);
Related
I have MainActivity where i define viewpager and i also define button on toolbar.
I want when i click on button it save the viewpager page position with title in another activity in listview.
Provide complete example.
Try saving position of viepager in a shared preference.
save when going away from activity
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("viepager_position", postion);
editor.apply();
retrieve when user comes back to activity
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
int position = prefs.getInt("viepager_position",default_position);
I need to get the tab title when I first open my app. I can do it when the user changes the tab and save it in shared pref by using onTabSelected but if the user does not change the tab, I don't know how to access the title. Im using this:
tabLayout.addOnTabSelectedListener(
new ViewPagerOnTabSelectedListener(myViewPager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
String dataTab = tab.getText().toString();
SharedPreferences.Editor editor = getSharedPreferences("PREFERENCIAS", MODE_PRIVATE).edit();
editor.putString("DATA", dataTab);
editor.apply();
}
});
You can do that after setting up the TabLayout by using
tabLayout.getTabAt(0).getText()
You can use below code to get the title of the selected tab.
int selectedTabIndex = tabLayout.getSelectedTabPosition();
TabLayout.Tab tab = tabLayout.getTabAt(selectedTabIndex);
String tabTitle = tab.getText();
You can use next method for get tab title:
private String getTabText(Integer tabPosition){
TabLayout.Tab tab = tabLayout.getTabAt(tabPosition);
return tab.getText().toString();
}
I'm trying to show a fragment only on the first time the application is launched. What I'm trying works to show the fragment but it always shows, I only want it to show on first launch.
I'm replacing fragments from a method called displayView, in a navigation drawer drawerlistener (this may be my problem), im trying to use a boolean and shared preferences in my method. Any ideas as to why this isn't working
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
firstTimeFrag();
}
}
private boolean firstTimeFrag(){
SharedPreferences pref = getPreferences(MODE_PRIVATE);
boolean ranBefore = pref.getBoolean("RanBefore",false);
if(!ranBefore){
SharedPreferences.Editor editor = pref.edit();
editor.commit();
Fragment fr = new ifFirstTimeFrag();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, fr);
fragmentTransaction.commit();
}else{
displayView(0);
}
return ranBefore;
}
You are not saving anything ^^
SharedPreferences.Editor editor = pref.edit();
editor.commit();
Use this:
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("RanBefore", true);
editor.commit();
I want to show the value of preferences in a custom preference screen. For this I need to get the values, in this case strings, within the preference fragment. In non fragments (activities) I get the values with e.g. final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); and get the string with String phonenumber = prefs.getString("preference_name", null); but in the preference fragment getDefaultSharedPreferences is not applicable for preference fragment.
Any idea how to solve this?
Here's my code snippet:
public class PreferencesFragment extends PreferenceFragment implements
OnSharedPreferenceChangeListener {
TextView tvusername, tvphonenumber;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
final SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
// entering preference phonenumber in text view
String phonenumber = prefs.getString("phonenumber", null);
;
tvphonenumber.setText(phonenumber);
// entering preference username in text view
String username = prefs.getString("username", null);
;
tvusername.setText(username);
}
In onActivityCreated (It's the time when the activity is created) of your fragment class, you do
Context hostActivity = getActivity();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(hostActivity);
That's how you access the hostActivity from the attached fragment.
In my application, datas are loading while the splash screen is displayed. When it's done, the user can enter new data that are displayed in a fragment and insert in the database. Moreover, new data entered by the user allows to compute a variable that is also displayed in a custom view (in the fragment) and insert in the database. So, outside the splash screen, I'm using only db.insert().
I have several fragments and I want to recover my datas when I swith from one fragment to another without call the database with db.get()
Now, when I switch to another fragment (using navigation drawer) and go back to the previous one, my data are no more displayed...
Indeed, I want to limit call to the database when the app is running.
UPDATE
A little update to be more explicit :)
I have an Activity handling a Navigation Drawer. For the moment, I switch between 2 fragments (Frag1 & Frag2).
In the first fragment I have a custom view (named for example CustomView1) and some TextView (TextView1 & TextView2).
Through an item in action bar, TextView1 can be updated by the user. The value set for TextView1 is also insert in a database. Automatically, TextView2 is updated with the previous value of TextView1 as below :
/**
* Listener to see if TextView1 change
**/
TextView1.addTextChangedListener(new TextWatcher() {
String X;
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
X = charSequence.toString();
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void afterTextChanged(Editable editable) {
TextView2.setText(X);
}
});
When I switch to Frag2 and back to Frag1, TextView1 & TextView2 are empty and nothing is display.
I tried to use onSaveInstanceState like this :
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putFloat("mTextView1", mTextView1);
savedInstanceState.putFloat("mTextView2", mTextView2);
}
and in onCreateView :
if(savedInstanceState != null) {
// Restore last state
mTextView1 = savedInstanceState.getFloat("mTextView1");
mTextView2= savedInstanceState.getFloat("mTextView2");
}
My savedInstanceState is always returning null...
This is how I switch between fragment :
private void selectItem(int position) {
// update the main content by replacing fragments
switch(position){
case 0:
Fragment1 Frag1 = new Fragment1();
FragmentTransaction fragTransaction = getSupportFragmentManager().beginTransaction();
fragTransaction.replace(R.id.content_frame,Frag1,"TAG1");
fragTransaction.commit();
break;
case 1:
Fragment2 Frag2 = new Fragment2 ();
FragmentTransaction fragTransaction1 = getSupportFragmentManager().beginTransaction();
fragTransaction1.replace(R.id.content_frame,Frag2,"TAG2");
fragTransaction1.commit();
break;
}
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mSection[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
Solution
I am using SharedPreference during user action to store data I want (moreover, these data are insert in the database)
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putFloat("mKey1", value1);
editor.commit();
In onCreate, I get data I stored previously :
sharedPref = this.getActivity().getPreferences(Context.MODE_PRIVATE);
if(sharedPref!=null) {
variable1 = sharedPref.getFloat("mKey1",DEFAULT_VALUE);
}
TextView1.setText(String.valueOf(variable1));
As TextView1 is updated, TextView2 is update too with the listener taking the previous value of TextView1.
I solved the issue by using SharedPreference