javaandroidandroid-studiosharedpreferencescustom-adapter

Seekbar with textview in a listview


So i was try to use seekbar to get progress value in a custom adapter for the listview. everything was fine on the code, however when i try to launch it, the automaticcaly close and go to previous activity. can anyone know what is the problems in my code ?

i am using the sharedpreferences for other activity in the app in the future, so thats why im using it.

here the custom adapter code

public class PerrowAdapter extends ArrayAdapter<Perrow> {

    int textSize = 30;

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;


    private Context mContext;
    private int mResource;
    private ArrayList<Perrow> objects;

    public PerrowAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Perrow> objects) {
        super(context, resource, objects);
        this.mContext = context;
        this.mResource = resource;
        this.objects = objects;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        convertView = layoutInflater.inflate(mResource, parent, false);

        TextView tvArabic = convertView.findViewById(R.id.arabictext);
        TextView tvTransliteration = convertView.findViewById(R.id.transliteration);
        TextView tvTranslation = convertView.findViewById(R.id.translation);

        //seekbar

        int progress = sharedPreferences.getInt("progress", 0);
        
        SeekBar seekbar1 = convertView.findViewById(R.id.seekbar1);
        seekbar1.setProgress(progress);

        //settextsize
        tvArabic.setTextSize(textSize+seekbar1.getProgress()); // size 30sp

        Perrow perrow = objects.get(position);

        tvArabic.setText(perrow.getArabic());
        tvTransliteration.setText(perrow.getTransliteration());
        tvTranslation.setText(perrow.getTranslation());

        //seekbar

        seekbar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            int progressNew = 0;

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

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        return convertView;
    }
}

EDIT

here the error log

2021-02-27 23:05:26.130 3959-3959/? E/example.munaja: Unknown bits set in runtime_flags: 0x8000
2021-02-27 23:05:26.148 3959-3959/? E/libc: Access denied finding property "runtime.mmitest.isrunning"
2021-02-27 23:05:26.345 3959-3995/com.example.munajat E/AwareLog: AtomicFileUtils: readFileLines file not exist: android.util.AtomicFile@b449e48

and here is the activity java

public class fontsize extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{


    //SharedPreferences

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;


    ListView listView;


    //variablesmenudrawer

    DrawerLayout drawerLayout;
    NavigationView navigationView;
    Toolbar toolbar;




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

        //sharedpreferences

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


        //seekbar

        String[] arabic = getResources().getStringArray(R.array.fontsize);
        String[] transliteration = getResources().getStringArray(R.array.transliteration);
        String[] translation = getResources().getStringArray(R.array.translation);


        listView = findViewById(R.id.listView);

        //Create Data

        ArrayList<Perrow> list = new ArrayList<>();
        for(int i = 0; i < arabic.length; i++){
            list.add(new Perrow(arabic[i], transliteration[i], translation[i]));
        }



        ///customadapter

        PerrowAdapter perrowAdapter = new PerrowAdapter(this, R.layout.list_row, list);
        listView.setAdapter(perrowAdapter);


        //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 from run terminal i got this

V/AudioManager: querySoundEffectsEnabled...
I/HwViewRootImpl: removeInvalidNode all the node in jank list is out of time
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@6c623a
V/ActivityThread: callActivityOnCreate
D/ActivityThread: add activity client record, r= ActivityRecord{50930bb token=android.os.BinderProxy@6c623a {com.example.munajat/com.example.munajat.fontsize}} token= android.os.BinderProxy@6c623a
D/HiTouch_PressGestureDetector: onAttached, package=com.example.munajat, windowType=1, mHiTouchRestricted=false
I/BlockMonitor: dispatchingThrewException In MainThread
D/AndroidRuntime: Shutting down VM
I/QarthLog: [PatchStore] createDisableExceptionQarthFile
    [PatchStore] create disable file for com.example.munajat uid is 10425
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.munajat, PID: 9485
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SeekBar.setProgress(int)' on a null object reference
        at com.example.munajat.PerrowAdapter.getView(PerrowAdapter.java:57)
        at android.widget.AbsListView.obtainView(AbsListView.java:2494)
        at android.widget.HwAbsListView.obtainView(HwAbsListView.java:1139)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1446)
        at android.widget.ListView.onMeasure(ListView.java:1352)
        at android.view.View.measure(View.java:24742)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:735)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
        at android.view.View.measure(View.java:24742)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:735)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
        at android.view.View.measure(View.java:24742)
        at androidx.constraintlayout.widget.ConstraintLayout$Measurer.measure(ConstraintLayout.java:792)
        at androidx.constraintlayout.solver.widgets.ConstraintWidgetContainer.measure(ConstraintWidgetContainer.java:583)
        at androidx.constraintlayout.solver.widgets.analyzer.Direct.verticalSolvingPass(Direct.java:355)
        at androidx.constraintlayout.solver.widgets.analyzer.Direct.solveVerticalMatchConstraint(Direct.java:636)
        at androidx.constraintlayout.solver.widgets.analyzer.Direct.verticalSolvingPass(Direct.java:407)
        at androidx.constraintlayout.solver.widgets.analyzer.Direct.verticalSolvingPass(Direct.java:446)
        at androidx.constraintlayout.solver.widgets.analyzer.Direct.solvingPass(Direct.java:178)
        at androidx.constraintlayout.solver.widgets.ConstraintWidgetContainer.layout(ConstraintWidgetContainer.java:642)
        at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.solveLinearSystem(BasicMeasure.java:159)
        at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.solverMeasure(BasicMeasure.java:290)
        at androidx.constraintlayout.solver.widgets.ConstraintWidgetContainer.measure(ConstraintWidgetContainer.java:119)
        at androidx.constraintlayout.widget.ConstraintLayout.resolveSystem(ConstraintLayout.java:1578)
        at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1690)
        at android.view.View.measure(View.java:24742)
        at androidx.drawerlayout.widget.DrawerLayout.onMeasure(DrawerLayout.java:1119)
        at android.view.View.measure(View.java:24742)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6903)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:146)
        at android.view.View.measure(View.java:24742)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6903)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1562)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:849)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:728)
        at android.view.View.measure(View.java:24742)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6903)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:24742)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6903)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1562)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:849)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:728)
        at android.view.View.measure(View.java:24742)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6903)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:896)
        at android.view.View.measure(View.java:24742)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3211)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1974)
E/AndroidRuntime:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2295)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1857)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8089)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1057)
        at android.view.Choreographer.doCallbacks(Choreographer.java:875)
        at android.view.Choreographer.doFrame(Choreographer.java:776)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1042)
        at android.os.Handler.handleCallback(Handler.java:888)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8178)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
I/Process: Sending signal. PID: 9485 SIG: 9


and the main activity i use to launch the font size activity

public class maintext extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

    int textSize = 30;

    TextView textView;

    //sharedpreferencesvariables

    SharedPreferences prefs;
    SharedPreferences.Editor editor;

    //variablesmenudrawer

    DrawerLayout drawerLayout;
    NavigationView navigationView;
    Toolbar toolbar;


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

        // definingtextview

        textView = findViewById(R.id.maintext);

        //sharedpreferencescall

        prefs = getSharedPreferences("fontsize", MODE_PRIVATE);

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

        //settextsizeaccordingseekbar

        textView.setTextSize(textSize+progress);


        //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(maintext.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(maintext.this, fontsize.class);
                startActivity(intent1);
                finish();

        }

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

Solution

  • Try this:

           PerrowAdapter perrowAdapter = new PerrowAdapter(getApplicationContext(), R.layout.list_row, list);
        listView.setAdapter(perrowAdapter);
    

    Add break keyword:

        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();
                break;
            case R.id.nav_fontsize:
               break;
        }
    

    Doing this should work:

        SeekBar seekbar1 = (SeekBar)convertView.findViewById(R.id.seekbar1);//typecast the seekbar
    

    First of all try this on your onprogresschanged:

            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(progress!=null){
                textSize = textSize + (progress - progressNew);
                progressNew = progress;
                tvArabic.setTextSize(textSize);
                }
            }
    
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
    
            }
    
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            editor.putInt("progress", progress);
                editor.apply();
    
            }
        });
       
    

    The problem with your code is that you have added NavigationItemselected in your fontsize activity instead of adding it in your mainActivity. After doing this you need to send an intent to your fontsize activity and your home activity in your NavigationItemselected.

    Edit

       private Context mContext;
    private int mResource;
    private ArrayList<Perrow> objects;
     TextView tvArabic;
        TextView tvTransliteration;
        TextView tvTranslation;
    public PerrowAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Perrow> objects, TextView tvArabic,
        TextView tvTransliteration,
        TextView tvTranslation) {
        super(context, resource, objects);
        this.mContext = context;
        this.mResource = resource;
        this.objects = objects;
        this.tvArabic=tvArabic;
        this.tvTransliteration=tvTransliteration;
       this.tvTranslation=tvTranslation;
    }