Search code examples
javaandroidsharedpreferences

Retrieving font size from SharedPreferences to other activity


i am trying to retrieve the font size from the sharedpreferences from other activity. i already add the getPreferences method however it doesnt work. please explain to me i love knowledges.

as on the code, i succesfully changing the textsize using seekbar, and save it to sharedpreferences, however, in trying to retrieve the font size on other activity, it doesnt work.

here is my 1st activity java


import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import android.content.Intent;

import android.content.SharedPreferences;
import android.os.Bundle;

import android.preference.PreferenceManager;
import android.view.MenuItem;
import android.widget.SeekBar;
import android.widget.TextView;

import com.google.android.material.navigation.NavigationView;

public class fontsize extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

    //textsize

    int textSize = 30;

    //textView


    TextView textViewFont, textViewSize;

    //SharedPreferences

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;




    //variablesmenudrawer

    DrawerLayout drawerLayout;
    NavigationView navigationView;
    Toolbar toolbar;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fontsize);



        sharedPreferences = getSharedPreferences("fontsize", MODE_PRIVATE);
        editor = sharedPreferences.edit();

        int progress = sharedPreferences.getInt("progress", 0);


        //definingtextview

        textViewFont = findViewById(R.id.textViewFont);
        textViewSize = findViewById(R.id.textViewSize);




        //seekbar



        SeekBar seekBar = findViewById(R.id.seekbar);

        seekBar.setProgress(progress);

        textViewFont.setTextSize(textSize+seekBar.getProgress()); // size 30sp

        textViewSize.setText(seekBar.getProgress() + "/" + seekBar.getMax()); // 0/30


        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            int progressNew = 0;

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                textSize = textSize + (progress - progressNew);
                progressNew = progress;
                textViewFont.setTextSize(textSize);
                editor.putInt("progress", progress);
                editor.apply();



            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {


            }


            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

                textViewSize.setText(seekBar.getProgress() + "/" + seekBar.getMax()); // 0/30


            }
        });

        //definingmenudrawer

        drawerLayout = findViewById(R.id.drawer_layout4);
        navigationView = findViewById(R.id.nav_view4);
        toolbar = findViewById(R.id.toolbar4);


        //toolbar

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        //toolbar.setNavigationIcon(R.drawable.ic_toolbar);
        toolbar.setTitle("");
        toolbar.setSubtitle("");
        //toolbar.setLogo(R.drawable.ic_toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_baseline_dehaze_24);

        //navdrawermenu

        navigationView.bringToFront();
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        navigationView.setNavigationItemSelectedListener(this);
        navigationView.setCheckedItem(R.id.nav_home);

    }

    @Override
    public void onBackPressed() {

        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        }
        else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

        switch (menuItem.getItemId()) {


            case R.id.nav_home:
                Intent intent = new Intent(fontsize.this, home.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            case R.id.nav_fontsize:
        }

        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

}

and my second activity java

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Display;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.google.android.material.navigation.NavigationView;

public class duatawassul extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

    TextView textView;

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;


    //variablesmenudrawer

    DrawerLayout drawerLayout;
    NavigationView navigationView;
    Toolbar toolbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_duatawassul);

        sharedPreferences = getSharedPreferences("fontsize", MODE_PRIVATE);




        textView = findViewById(R.id.duatawassultext);




        //definingmenudrawer

        drawerLayout = findViewById(R.id.drawer_layout1);
        navigationView = findViewById(R.id.nav_view1);
        toolbar = findViewById(R.id.toolbar1);


        //toolbar

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        //toolbar.setNavigationIcon(R.drawable.ic_toolbar);
        toolbar.setTitle("");
        toolbar.setSubtitle("");
        //toolbar.setLogo(R.drawable.ic_toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_baseline_dehaze_24);

        //navdrawermenu

        navigationView.bringToFront();
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        navigationView.setNavigationItemSelectedListener(this);
        navigationView.setCheckedItem(R.id.nav_home);

    }


    @Override
    public void onBackPressed() {

        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        }
        else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

        switch (menuItem.getItemId()) {


            case R.id.nav_home:
                Intent intent = new Intent(duatawassul.this, home.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            case R.id.nav_fontsize:
                Intent intent1 = new Intent(duatawassul.this, fontsize.class);
                startActivity(intent1);
                finish();

        }

        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }
}


Solution

  • in your on create method in the second activity add the following which appears to be missing in your original code

       prefs = getSharedPreferences("fontsize", MODE_PRIVATE);
       //this line is nowhere found in your second activity
       int progress=prefs.getInt("progress",0);