How to increase-decrease control change android device ringtone volume with seekbar using coding.
In this tutorial we are dynamically changing the ringtone volume with seekbar. So here is the complete step by step tutorial for Set Ringer volume in android using seekbar programmatically.
How to Set Ringer volume in android using seekbar programmatically.
Code for MainActivity.java file.
package com.setringervolume_android_examples.com; import android.app.Activity; import android.content.Context; import android.media.AudioManager; import android.os.Bundle; import android.widget.SeekBar; import android.widget.TextView; public class MainActivity extends Activity { SeekBar seekbar; TextView textview; AudioManager audioManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); seekbar = (SeekBar)findViewById(R.id.seekBar1); textview = (TextView)findViewById(R.id.textView1); audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); seekbar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)); seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { textview.setText("Ringer Volume : " + i); audioManager.setStreamVolume(AudioManager.STREAM_RING, i, 0); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); } }
Code for activity_main.xml layout file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.setringervolume_android_examples.com.MainActivity" > <SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/seekBar1" android:layout_centerHorizontal="true" android:layout_marginBottom="49dp" android:text="Ringer Volume Show Here" android:textAppearance="?android:attr/textAppearanceLarge" /> </RelativeLayout>
Screenshot: