Showing posts with label seekbar. Show all posts
Wednesday, 10 June 2015
Set Ring volume in android
Posted by
devraj chavda,
on
06:39
In this android example i show you, how to set a ringing volume in android with seekbar by using audio manager.
CREATE NEW ANDROID PROJECT
Step 1: Write code into activity_main.xml
<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=".MainActivity" >
<TextView
android:id="@+id/tVVolume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="130dp"
android:text="Volume: " />
<SeekBar
android:id="@+id/sbVolume"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp" />
</RelativeLayout>
Step 2: Write code into MainActivity.java
package dev.androidapplink.volumecontrollapp;
import android.media.AudioManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
//define TextViews to show details of volume and brightness
private TextView tVVolume;
// define SeekBars to set volume and brightness
private SeekBar sbVolume;
private AudioManager audioManager;
int maxVolume = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Suggests an audio stream whose volume should be changed with
// hardware volume controls.
setVolumeControlStream(AudioManager.STREAM_RING);
initializeControls();
}
private void initializeControls() {
// get reference of the UI Controls
sbVolume = (SeekBar) findViewById(R.id.sbVolume);
tVVolume = (TextView) findViewById(R.id.tVVolume);
try {
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// set max progress according to volume
sbVolume.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_RING));
// get current volume
sbVolume.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_RING));
// Set the seek bar progress to 1
sbVolume.setKeyProgressIncrement(1);
// get max volume
maxVolume = sbVolume.getMax();
sbVolume.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
audioManager.setStreamVolume(AudioManager.STREAM_RING,
progress, 0);
// Calculate the volume percentage
float perc = (progress / (float) maxVolume) * 100;
// Set the volume percentage
tVVolume.setText("Volume: " + (int) perc + " %");
}
});
} catch (Exception e) {
}
}
}
NOTE: Give permission in AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
Step 3: Run Project:
CREATE NEW ANDROID PROJECT
Step 1: Write code into activity_main.xml
<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=".MainActivity" >
<TextView
android:id="@+id/tVVolume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="130dp"
android:text="Volume: " />
<SeekBar
android:id="@+id/sbVolume"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp" />
</RelativeLayout>
Step 2: Write code into MainActivity.java
package dev.androidapplink.volumecontrollapp;
import android.media.AudioManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
//define TextViews to show details of volume and brightness
private TextView tVVolume;
// define SeekBars to set volume and brightness
private SeekBar sbVolume;
private AudioManager audioManager;
int maxVolume = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Suggests an audio stream whose volume should be changed with
// hardware volume controls.
setVolumeControlStream(AudioManager.STREAM_RING);
initializeControls();
}
private void initializeControls() {
// get reference of the UI Controls
sbVolume = (SeekBar) findViewById(R.id.sbVolume);
tVVolume = (TextView) findViewById(R.id.tVVolume);
try {
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// set max progress according to volume
sbVolume.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_RING));
// get current volume
sbVolume.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_RING));
// Set the seek bar progress to 1
sbVolume.setKeyProgressIncrement(1);
// get max volume
maxVolume = sbVolume.getMax();
sbVolume.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
audioManager.setStreamVolume(AudioManager.STREAM_RING,
progress, 0);
// Calculate the volume percentage
float perc = (progress / (float) maxVolume) * 100;
// Set the volume percentage
tVVolume.setText("Volume: " + (int) perc + " %");
}
});
} catch (Exception e) {
}
}
}
NOTE: Give permission in AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
Step 3: Run Project:
Sunday, 31 May 2015
Adjust screen brightness in android
Posted by
devraj chavda,
on
04:42
In this tutorial i show you,how to adjust screen brightness programatically in android.In this example we learn get current screen brightness state and changing screen brightness.
CREATE NEW ANDROID PROJECT
Step 1: Write code into activity_main.xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Slide seekbar to change the brightness" />
<TextView
android:id="@+id/txtPercentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="0%"
android:textAppearance="?android:attr/textAppearanceLarge" />
<SeekBar
android:id="@+id/brightbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
</LinearLayout>
Step 2: Write code into MainActivity.java
package dev.Androidapplink.setscreenbrightnessapp;
import dev.Androidapplink.setscreenbrightnessapp.R;
import android.app.Activity;
import android.content.ContentResolver;
import android.os.Bundle;
import android.provider.Settings.System;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
//Seek bar object
private SeekBar brightbar;
//Variable to store brightness value
private int brightness;
//handle to the system's settings
private ContentResolver cResolver;
//Window object store a reference to the current window
private Window window;
TextView txtPerc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Instantiate seekbar object
brightbar = (SeekBar) findViewById(R.id.brightbar);
txtPerc = (TextView) findViewById(R.id.txtPercentage);
//Get the content resolver
cResolver = getContentResolver();
//Get the current window
window = getWindow();
//Set the seekbar range between 0 and 255
brightbar.setMax(255);
//Set the seek bar progress to 1
brightbar.setKeyProgressIncrement(1);
try {
//Get the current system brightness state
brightness = System.getInt(cResolver, System.SCREEN_BRIGHTNESS);
float perc = (brightness /(float)255)*100;
txtPerc.setText((int)perc + "%");
} catch (Exception e) {
// TODO: handle exception
//Throw an error case it couldn't be retrieved
Log.e("Error", "cannot access system brightness.");
e.printStackTrace();
}
//Set the progress of the seek bar based on the system's brightness
brightbar.setProgress(brightness);
//Register OnSeekBarChangeListener, so it can actually change values
brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
//Set the system brightness using the brightness variable value
System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
//Get the current window attributes
LayoutParams layoutpars = window.getAttributes();
//Set the brightness of this window
layoutpars.screenBrightness = brightness / (float)255;
//Apply attribute changes to this window
window.setAttributes(layoutpars);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
//Nothing handled here
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
//Set the minimal brightness level
if(progress<=20)
{
brightness=20;
}
else //brightness is greater than 20
{
//Set brightness variable based on the progress bar
brightness = progress;
}
//Calculate the brightness percentage
float perc = (brightness /(float)255)*100;
//Set the brightness percentage
txtPerc.setText((int)perc + "%");
}
});
}
//IT WORK ONLY,IF SCREEN BRIGHTNESS IS NOT SETED IN "AUTO BRIGHTNESS MODE".
}
CREATE NEW ANDROID PROJECT
Step 1: Write code into activity_main.xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Slide seekbar to change the brightness" />
<TextView
android:id="@+id/txtPercentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="0%"
android:textAppearance="?android:attr/textAppearanceLarge" />
<SeekBar
android:id="@+id/brightbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
</LinearLayout>
Step 2: Write code into MainActivity.java
package dev.Androidapplink.setscreenbrightnessapp;
import dev.Androidapplink.setscreenbrightnessapp.R;
import android.app.Activity;
import android.content.ContentResolver;
import android.os.Bundle;
import android.provider.Settings.System;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
//Seek bar object
private SeekBar brightbar;
//Variable to store brightness value
private int brightness;
//handle to the system's settings
private ContentResolver cResolver;
//Window object store a reference to the current window
private Window window;
TextView txtPerc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Instantiate seekbar object
brightbar = (SeekBar) findViewById(R.id.brightbar);
txtPerc = (TextView) findViewById(R.id.txtPercentage);
//Get the content resolver
cResolver = getContentResolver();
//Get the current window
window = getWindow();
//Set the seekbar range between 0 and 255
brightbar.setMax(255);
//Set the seek bar progress to 1
brightbar.setKeyProgressIncrement(1);
try {
//Get the current system brightness state
brightness = System.getInt(cResolver, System.SCREEN_BRIGHTNESS);
float perc = (brightness /(float)255)*100;
txtPerc.setText((int)perc + "%");
} catch (Exception e) {
// TODO: handle exception
//Throw an error case it couldn't be retrieved
Log.e("Error", "cannot access system brightness.");
e.printStackTrace();
}
//Set the progress of the seek bar based on the system's brightness
brightbar.setProgress(brightness);
//Register OnSeekBarChangeListener, so it can actually change values
brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
//Set the system brightness using the brightness variable value
System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
//Get the current window attributes
LayoutParams layoutpars = window.getAttributes();
//Set the brightness of this window
layoutpars.screenBrightness = brightness / (float)255;
//Apply attribute changes to this window
window.setAttributes(layoutpars);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
//Nothing handled here
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
//Set the minimal brightness level
if(progress<=20)
{
brightness=20;
}
else //brightness is greater than 20
{
//Set brightness variable based on the progress bar
brightness = progress;
}
//Calculate the brightness percentage
float perc = (brightness /(float)255)*100;
//Set the brightness percentage
txtPerc.setText((int)perc + "%");
}
});
}
//IT WORK ONLY,IF SCREEN BRIGHTNESS IS NOT SETED IN "AUTO BRIGHTNESS MODE".
}
NOTE: Give permission in AndroidManifest.xml
It allow to change setting.
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
Step 3: Run project and see Output:
Subscribe to:
Posts
(
Atom
)
check current state
(8)
android basic
(7)
Layout background
(6)
Network & Connectivity
(4)
Toast
(4)
XML
(4)
listview
(4)
Animation XML
(3)
Menu
(3)
web browser
(3)
Alert box
(2)
Calculator
(2)
Mobile data connection
(2)
Spinner
(2)
ToggleButton
(2)
Transparetn background
(2)
hello world
(2)
seekbar
(2)
set color
(2)
webview
(2)
wifi manager
(2)
Android interview Question
(1)
App uninstall
(1)
Battery
(1)
Bluetooth
(1)
CMD
(1)
CheckBox
(1)
Drag and Drop
(1)
Emi Loan
(1)
Flightmode or Airplane mode
(1)
Full screen
(1)
Maths
(1)
PopUp
(1)
SDcard
(1)
Sensor
(1)
Slider
(1)
Sound
(1)
Splash screen
(1)
Torch flash light
(1)
Transparent color
(1)
android architecture & fundamental
(1)
android folder and package
(1)
call
(1)
contact
(1)
converter
(1)
dialer
(1)
internet
(1)
screen brightness
(1)
sms
(1)
storage info
(1)
time
(1)
unite converter
(1)
| Follow me | Share |
|---|---|
| Follow @devraj205027 | Tweet |


