Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Seekbar coding

I'm creating an android application for reading a document. In this app, I'm using seekbar to adjust the size of the font. I can't make that happen with the code. Could anyone assist me with the code.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:visibility="visible"
    tools:context="islamicdawahkuwait.allahyaar.SettingsActivity">

       <LinearLayout
           android:orientation="vertical"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="App Font Size :-"
            android:textSize="20dp"
            android:scrollbars="vertical"
            />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="36dp"/>

    <TextView
        android:id="@+id/progress"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="36dp"
        android:gravity="center"/>

    </LinearLayout>

</ScrollView>

I had added the code in another activity as mentioned below. But still I didn't get nothing. Please assist

    package islamicdawahkuwait.allahyaar;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;

public class C2Activity extends AppCompatActivity {

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

        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow1);

        SharedPreferences sharedPreferences = getSharedPreferences("SAMPLE_PREFERENCE", MODE_PRIVATE);
        int fontSize = sharedPreferences.getInt("FONT_SIZE", 14);

        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setTextSize((float) fontSize);


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case android.R.id.home:
                Intent intent = new Intent(this,ContentActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

I had applied the point 3 in Settings activity as mentioned below

package islamicdawahkuwait.allahyaar;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import org.w3c.dom.Text;

import static islamicdawahkuwait.allahyaar.R.id.seekBar;

public class SettingsActivity extends AppCompatActivity {
    private SeekBar seekBar;
    private TextView textProgress;
    private TextView textParagraph;

    private static final int progress = 14;

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;

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

        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow1);

        seekBar = (SeekBar) findViewById(R.id.seekBar);
        textProgress = (TextView) findViewById(R.id.progress);
        textParagraph = (TextView) findViewById(R.id.textParagraph);

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

        updateView(progress);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

                updateView(progress + i);

                editor.putInt("FONT_SIZE", progress + i);
                editor.commit();

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    public void updateView(int fontSize) {
        textProgress.setText(String.valueOf(fontSize));
        textParagraph.setTextSize((float) fontSize);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case android.R.id.home:
                Intent intent = new Intent(this,ContentActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}

I had multiple textviews in this activity. If I called each one like textViewOne.setTextSize((float) fontSize); textViewTwo.setTextSize((float) fontsize); I already had called the textView byid onCreate as below. Hence, I need to use btn.setTextSize((float) fontSize); ?? Is it correct. Please educate me. Thanks

package islamicdawahkuwait.allahyaar;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import static android.R.attr.id;

public class ContentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        TextView btn = (TextView) findViewById(R.id.tv1);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent i = new Intent(getApplicationContext(),Activity1.class);
                i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(i);
            }
        });

        TextView btn2 = (TextView) findViewById(R.id.tv2);
        btn2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent i = new Intent(getApplicationContext(),SolActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(i);
            }
        });

        TextView btn3 = (TextView) findViewById(R.id.tv3);
        btn3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent i = new Intent(getApplicationContext(),C2Activity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(i);
            }
        });

        TextView btn4 = (TextView) findViewById(R.id.tv4);
        btn4.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent i = new Intent(getApplicationContext(),C3Activity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(i);
            }
        });

        TextView btn5 = (TextView) findViewById(R.id.tv5);
        btn5.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent i = new Intent(getApplicationContext(),C4Activity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(i);
            }
        });

        TextView btn6 = (TextView) findViewById(R.id.tv6);
        btn6.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent i = new Intent(getApplicationContext(),C5Activity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(i);
            }
        });

        TextView btn7 = (TextView) findViewById(R.id.tv7);
        btn7.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent i = new Intent(getApplicationContext(),C6Activity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(i);
            }
        });

        TextView btn8 = (TextView) findViewById(R.id.tv8);
        btn8.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent i = new Intent(getApplicationContext(),C7Activity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(i);
            }
        });

        TextView btn9 = (TextView) findViewById(R.id.tv9);
        btn9.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent i = new Intent(getApplicationContext(),C8Activity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(i);
            }
        });

        TextView btn10 = (TextView) findViewById(R.id.tv10);
        btn10.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent i = new Intent(getApplicationContext(),C9Activity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(i);
            }
        });

        TextView btn11 = (TextView) findViewById(R.id.tv11);
        btn11.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent i = new Intent(getApplicationContext(),C10Activity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(i);
            }
        });

        TextView btn12 = (TextView) findViewById(R.id.tv12);
        btn12.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent i = new Intent(getApplicationContext(),C11Activity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(i);
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_content, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case R.id.action_settings:
                if (item.isChecked())
                    item.setChecked(false);
                else
                    item.setChecked(true);
                Intent i = new Intent(getApplicationContext(),SettingsActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(i);
                return true;

            case R.id.action_about:
                if (item.isChecked())
                    item.setChecked(false);
                else
                    item.setChecked(true);
                displayToast("  Allah Yaar\n\nVersion 1.0.0");
                return true;

            case R.id.action_exit:
                if(item.isChecked())
                    item.setChecked(false);
                else
                    item.setChecked(true);
                new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit")
                        .setMessage("Are you sure?")
                        .setPositiveButton("yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                Intent intent = new Intent(Intent.ACTION_MAIN);
                                intent.addCategory(Intent.CATEGORY_HOME);
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                                finish();
                            }
                        }).setNegativeButton("no", null).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    void displayToast(String message){
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit")
                .setMessage("Are you sure?")
                .setPositiveButton("yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Intent intent = new Intent(Intent.ACTION_MAIN);
                        intent.addCategory(Intent.CATEGORY_HOME);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        finish();
                    }
                }).setNegativeButton("no", null).show();
    }

}
like image 838
Rey Avatar asked Apr 16 '26 10:04

Rey


1 Answers

1. Update your activity_settings XML as below:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:visibility="visible">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:text="App Font Size :-"
            android:textSize="16sp"
            android:scrollbars="vertical" />

        <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp" />

        <TextView
            android:id="@+id/progress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="0"
            android:textSize="36sp"
            android:gravity="center_horizontal" />

        <TextView
            android:id="@+id/textParagraph"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="36dp"
            android:text="This is a sample text."/>

    </LinearLayout>
</ScrollView>

2. In your SettingsActivity, add SeekBarChangeListener to SeekBar and update TextView when progress value changed.

public class SettingsActivity extends AppCompatActivity {

    private SeekBar seekBar;
    private TextView textProgress;
    private TextView textParagraph;

    private static final int progress = 14;  // Default font-size 14sp

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

        seekBar = (SeekBar) findViewById(R.id.seekBar);
        textProgress = (TextView) findViewById(R.id.progress);
        textParagraph = (TextView) findViewById(R.id.textParagraph);

        updateView(progress);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

                updateView(progress + i);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        }); 
    }

    public void updateView(int fontSize) {
        textProgress.setText(String.valueOf(fontSize));
        textParagraph.setTextSize((float) fontSize);
    }
}

OUTPUT:

enter image description here

3. If you want to store FontSize value into SharedPreference for later use from other Activity or Fragment, then try this to store value:

public class SettingsActivity extends AppCompatActivity {

    private SeekBar seekBar;
    private TextView textProgress;
    private TextView textParagraph;

    private static final int progress = 14;  // Default font-size 14sp

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;

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

        seekBar = (SeekBar) findViewById(R.id.seekBar);
        textProgress = (TextView) findViewById(R.id.progress);
        textParagraph = (TextView) findViewById(R.id.textParagraph);

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

        updateView(progress);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

                updateView(progress + i);

                // Store font-size in preference
                editor.putInt("FONT_SIZE", progress + i);
                editor.commit();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        }); 
    }

    public void updateView(int fontSize) {
        textProgress.setText(String.valueOf(fontSize));
        textParagraph.setTextSize((float) fontSize);
    }
}

4. From C2Activity, Get FontSize value from SharedPreference and change TextView FontSize by using textView.setTextSize() method.

public class C2Activity extends AppCompatActivity {

    TextView textView;
    SharedPreferences sharedPreferences;

    int fontSize;

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

        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow1);

        textView = (TextView) findViewById(R.id.textView);

        sharedPreferences = getSharedPreferences("SAMPLE_PREFERENCE", MODE_PRIVATE);

    }

    @Override
    protected void onResume() {
        super.onResume();

        // Get font-size from preference        
        fontSize = sharedPreferences.getInt("FONT_SIZE", 14);

        // Change font-size
        textView.setTextSize((float) fontSize);
    }
}

Hope this will help~

like image 170
Ferdous Ahamed Avatar answered Apr 19 '26 00:04

Ferdous Ahamed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!