How to set current playing audio as ringtone when user click on setting icon?











up vote
0
down vote

favorite












I have written code by taking examples from some apps. I have added buttons to my app to play a ringtone.



I just want a code to set as ringtone when user click on settings icon. My settings button id is btn_setting1, btn_setting2, btn_setting3, and so on



Here's my java code






package com.techjapreet.shivshankarkiringtone;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ringtone_tab extends AppCompatActivity {

Button clk1;
Button clk2;
Button clk3;
Button clk4;
Button clk5;
Button clk6;

MediaPlayer mediaPlayer;

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


clk6 = (Button) findViewById(R.id.btn_play6);

clk5 = (Button) findViewById(R.id.btn_play5);

clk4 = (Button) findViewById(R.id.btn_play4);

clk3 = (Button) findViewById(R.id.btn_play3);

clk2 = (Button) findViewById(R.id.btn_play2);

clk1 = (Button) findViewById(R.id.btn_play1);

mediaPlayer = new MediaPlayer();

}


public void setBtn_play6(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_vandana);
mediaPlayer.start();

}

public void setBtn_play5(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_tandav_mantra);
mediaPlayer.start();

}



public void setBtn_play4(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv_om);
mediaPlayer.start();

}

public void setBtn_play3(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv);
mediaPlayer.start();

}



public void setBtn_play2(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_aaradhna);
mediaPlayer.start();


}


public void setBtn_play1(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shankar);
mediaPlayer.start();
}

private void stopPlayer(){
if(mediaPlayer != null && mediaPlayer.isPlaying())
{mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
}
}

}





and here's my layout.xml code file



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">


<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play1"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play1" />


<Button
android:id="@+id/btn_setting1"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_toStartOf="@+id/btn_play1"
android:layout_toLeftOf="@+id/btn_play1"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item1"
android:layout_alignBottom="@+id/txt_grid_item1"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="2dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:layout_toStartOf="@+id/btn_setting1"
android:layout_toLeftOf="@+id/btn_setting1"
android:layout_toEndOf="@+id/img_grid_item1"
android:layout_toRightOf="@+id/img_grid_item1"
android:gravity="center_vertical"
android:text="@string/textview"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

<RelativeLayout
android:id="@+id/RelativeLayout2"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play2"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play2"/>


<Button
android:id="@+id/btn_setting2"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_toStartOf="@+id/btn_play2"
android:layout_toLeftOf="@+id/btn_play2"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item2"
android:layout_alignBottom="@+id/txt_grid_item2"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo1"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_setting2"
android:layout_toLeftOf="@+id/btn_setting2"
android:layout_toEndOf="@+id/img_grid_item2"
android:layout_toRightOf="@+id/img_grid_item2"
android:gravity="center_vertical"
android:text="@string/textview1"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

<RelativeLayout
android:id="@+id/RelativeLayout3"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play3"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play3"/>


<Button
android:id="@+id/btn_setting3"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_toStartOf="@+id/btn_play3"
android:layout_toLeftOf="@+id/btn_play3"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item3"
android:layout_alignBottom="@+id/txt_grid_item3"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo2"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_setting3"
android:layout_toLeftOf="@+id/btn_setting3"
android:layout_toEndOf="@+id/img_grid_item3"
android:layout_toRightOf="@+id/img_grid_item3"
android:gravity="center_vertical"
android:text="@string/textview2"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

<RelativeLayout
android:id="@+id/RelativeLayout4"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play4"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play4"/>


<Button
android:id="@+id/btn_setting4"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:layout_toStartOf="@+id/btn_play4"
android:layout_toLeftOf="@+id/btn_play4"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item4"
android:layout_alignBottom="@+id/txt_grid_item4"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo3"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_setting4"
android:layout_toLeftOf="@+id/btn_setting4"
android:layout_toEndOf="@+id/img_grid_item4"
android:layout_toRightOf="@+id/img_grid_item4"
android:gravity="center_vertical"
android:text="@string/textview3"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

<RelativeLayout
android:id="@+id/RelativeLayout5"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play5"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play5"/>


<Button
android:id="@+id/btn_setting5"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:layout_toStartOf="@+id/btn_play5"
android:layout_toLeftOf="@+id/btn_play5"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item5"
android:layout_alignBottom="@+id/txt_grid_item5"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo4"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_setting5"
android:layout_toLeftOf="@+id/btn_setting5"
android:layout_toEndOf="@+id/img_grid_item5"
android:layout_toRightOf="@+id/img_grid_item5"
android:gravity="center_vertical"
android:text="@string/textview4"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

<RelativeLayout
android:id="@+id/RelativeLayout6"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play6"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play6"/>


<Button
android:id="@+id/btn_setting6"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:layout_toStartOf="@+id/btn_play6"
android:layout_toLeftOf="@+id/btn_play6"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item6"
android:layout_alignBottom="@+id/txt_grid_item6"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo5"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_setting6"
android:layout_toLeftOf="@+id/btn_setting6"
android:layout_toEndOf="@+id/img_grid_item6"
android:layout_toRightOf="@+id/img_grid_item6"
android:gravity="center_vertical"
android:text="@string/textview5"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

</LinearLayout>









share|improve this question
























  • Please add code for btn_setting1 ,2 ,3 ,4 ,5 and 6 to set as ringtone when user click on settings button
    – Jaspreet singh
    Nov 20 at 15:29















up vote
0
down vote

favorite












I have written code by taking examples from some apps. I have added buttons to my app to play a ringtone.



I just want a code to set as ringtone when user click on settings icon. My settings button id is btn_setting1, btn_setting2, btn_setting3, and so on



Here's my java code






package com.techjapreet.shivshankarkiringtone;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ringtone_tab extends AppCompatActivity {

Button clk1;
Button clk2;
Button clk3;
Button clk4;
Button clk5;
Button clk6;

MediaPlayer mediaPlayer;

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


clk6 = (Button) findViewById(R.id.btn_play6);

clk5 = (Button) findViewById(R.id.btn_play5);

clk4 = (Button) findViewById(R.id.btn_play4);

clk3 = (Button) findViewById(R.id.btn_play3);

clk2 = (Button) findViewById(R.id.btn_play2);

clk1 = (Button) findViewById(R.id.btn_play1);

mediaPlayer = new MediaPlayer();

}


public void setBtn_play6(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_vandana);
mediaPlayer.start();

}

public void setBtn_play5(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_tandav_mantra);
mediaPlayer.start();

}



public void setBtn_play4(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv_om);
mediaPlayer.start();

}

public void setBtn_play3(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv);
mediaPlayer.start();

}



public void setBtn_play2(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_aaradhna);
mediaPlayer.start();


}


public void setBtn_play1(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shankar);
mediaPlayer.start();
}

private void stopPlayer(){
if(mediaPlayer != null && mediaPlayer.isPlaying())
{mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
}
}

}





and here's my layout.xml code file



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">


<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play1"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play1" />


<Button
android:id="@+id/btn_setting1"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_toStartOf="@+id/btn_play1"
android:layout_toLeftOf="@+id/btn_play1"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item1"
android:layout_alignBottom="@+id/txt_grid_item1"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="2dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:layout_toStartOf="@+id/btn_setting1"
android:layout_toLeftOf="@+id/btn_setting1"
android:layout_toEndOf="@+id/img_grid_item1"
android:layout_toRightOf="@+id/img_grid_item1"
android:gravity="center_vertical"
android:text="@string/textview"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

<RelativeLayout
android:id="@+id/RelativeLayout2"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play2"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play2"/>


<Button
android:id="@+id/btn_setting2"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_toStartOf="@+id/btn_play2"
android:layout_toLeftOf="@+id/btn_play2"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item2"
android:layout_alignBottom="@+id/txt_grid_item2"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo1"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_setting2"
android:layout_toLeftOf="@+id/btn_setting2"
android:layout_toEndOf="@+id/img_grid_item2"
android:layout_toRightOf="@+id/img_grid_item2"
android:gravity="center_vertical"
android:text="@string/textview1"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

<RelativeLayout
android:id="@+id/RelativeLayout3"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play3"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play3"/>


<Button
android:id="@+id/btn_setting3"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_toStartOf="@+id/btn_play3"
android:layout_toLeftOf="@+id/btn_play3"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item3"
android:layout_alignBottom="@+id/txt_grid_item3"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo2"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_setting3"
android:layout_toLeftOf="@+id/btn_setting3"
android:layout_toEndOf="@+id/img_grid_item3"
android:layout_toRightOf="@+id/img_grid_item3"
android:gravity="center_vertical"
android:text="@string/textview2"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

<RelativeLayout
android:id="@+id/RelativeLayout4"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play4"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play4"/>


<Button
android:id="@+id/btn_setting4"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:layout_toStartOf="@+id/btn_play4"
android:layout_toLeftOf="@+id/btn_play4"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item4"
android:layout_alignBottom="@+id/txt_grid_item4"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo3"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_setting4"
android:layout_toLeftOf="@+id/btn_setting4"
android:layout_toEndOf="@+id/img_grid_item4"
android:layout_toRightOf="@+id/img_grid_item4"
android:gravity="center_vertical"
android:text="@string/textview3"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

<RelativeLayout
android:id="@+id/RelativeLayout5"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play5"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play5"/>


<Button
android:id="@+id/btn_setting5"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:layout_toStartOf="@+id/btn_play5"
android:layout_toLeftOf="@+id/btn_play5"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item5"
android:layout_alignBottom="@+id/txt_grid_item5"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo4"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_setting5"
android:layout_toLeftOf="@+id/btn_setting5"
android:layout_toEndOf="@+id/img_grid_item5"
android:layout_toRightOf="@+id/img_grid_item5"
android:gravity="center_vertical"
android:text="@string/textview4"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

<RelativeLayout
android:id="@+id/RelativeLayout6"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play6"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play6"/>


<Button
android:id="@+id/btn_setting6"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:layout_toStartOf="@+id/btn_play6"
android:layout_toLeftOf="@+id/btn_play6"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item6"
android:layout_alignBottom="@+id/txt_grid_item6"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo5"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_setting6"
android:layout_toLeftOf="@+id/btn_setting6"
android:layout_toEndOf="@+id/img_grid_item6"
android:layout_toRightOf="@+id/img_grid_item6"
android:gravity="center_vertical"
android:text="@string/textview5"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

</LinearLayout>









share|improve this question
























  • Please add code for btn_setting1 ,2 ,3 ,4 ,5 and 6 to set as ringtone when user click on settings button
    – Jaspreet singh
    Nov 20 at 15:29













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have written code by taking examples from some apps. I have added buttons to my app to play a ringtone.



I just want a code to set as ringtone when user click on settings icon. My settings button id is btn_setting1, btn_setting2, btn_setting3, and so on



Here's my java code






package com.techjapreet.shivshankarkiringtone;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ringtone_tab extends AppCompatActivity {

Button clk1;
Button clk2;
Button clk3;
Button clk4;
Button clk5;
Button clk6;

MediaPlayer mediaPlayer;

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


clk6 = (Button) findViewById(R.id.btn_play6);

clk5 = (Button) findViewById(R.id.btn_play5);

clk4 = (Button) findViewById(R.id.btn_play4);

clk3 = (Button) findViewById(R.id.btn_play3);

clk2 = (Button) findViewById(R.id.btn_play2);

clk1 = (Button) findViewById(R.id.btn_play1);

mediaPlayer = new MediaPlayer();

}


public void setBtn_play6(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_vandana);
mediaPlayer.start();

}

public void setBtn_play5(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_tandav_mantra);
mediaPlayer.start();

}



public void setBtn_play4(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv_om);
mediaPlayer.start();

}

public void setBtn_play3(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv);
mediaPlayer.start();

}



public void setBtn_play2(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_aaradhna);
mediaPlayer.start();


}


public void setBtn_play1(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shankar);
mediaPlayer.start();
}

private void stopPlayer(){
if(mediaPlayer != null && mediaPlayer.isPlaying())
{mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
}
}

}





and here's my layout.xml code file



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">


<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play1"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play1" />


<Button
android:id="@+id/btn_setting1"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_toStartOf="@+id/btn_play1"
android:layout_toLeftOf="@+id/btn_play1"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item1"
android:layout_alignBottom="@+id/txt_grid_item1"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="2dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:layout_toStartOf="@+id/btn_setting1"
android:layout_toLeftOf="@+id/btn_setting1"
android:layout_toEndOf="@+id/img_grid_item1"
android:layout_toRightOf="@+id/img_grid_item1"
android:gravity="center_vertical"
android:text="@string/textview"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

<RelativeLayout
android:id="@+id/RelativeLayout2"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play2"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play2"/>


<Button
android:id="@+id/btn_setting2"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_toStartOf="@+id/btn_play2"
android:layout_toLeftOf="@+id/btn_play2"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item2"
android:layout_alignBottom="@+id/txt_grid_item2"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo1"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_setting2"
android:layout_toLeftOf="@+id/btn_setting2"
android:layout_toEndOf="@+id/img_grid_item2"
android:layout_toRightOf="@+id/img_grid_item2"
android:gravity="center_vertical"
android:text="@string/textview1"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

<RelativeLayout
android:id="@+id/RelativeLayout3"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play3"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play3"/>


<Button
android:id="@+id/btn_setting3"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_toStartOf="@+id/btn_play3"
android:layout_toLeftOf="@+id/btn_play3"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item3"
android:layout_alignBottom="@+id/txt_grid_item3"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo2"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_setting3"
android:layout_toLeftOf="@+id/btn_setting3"
android:layout_toEndOf="@+id/img_grid_item3"
android:layout_toRightOf="@+id/img_grid_item3"
android:gravity="center_vertical"
android:text="@string/textview2"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

<RelativeLayout
android:id="@+id/RelativeLayout4"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play4"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play4"/>


<Button
android:id="@+id/btn_setting4"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:layout_toStartOf="@+id/btn_play4"
android:layout_toLeftOf="@+id/btn_play4"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item4"
android:layout_alignBottom="@+id/txt_grid_item4"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo3"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_setting4"
android:layout_toLeftOf="@+id/btn_setting4"
android:layout_toEndOf="@+id/img_grid_item4"
android:layout_toRightOf="@+id/img_grid_item4"
android:gravity="center_vertical"
android:text="@string/textview3"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

<RelativeLayout
android:id="@+id/RelativeLayout5"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play5"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play5"/>


<Button
android:id="@+id/btn_setting5"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:layout_toStartOf="@+id/btn_play5"
android:layout_toLeftOf="@+id/btn_play5"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item5"
android:layout_alignBottom="@+id/txt_grid_item5"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo4"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_setting5"
android:layout_toLeftOf="@+id/btn_setting5"
android:layout_toEndOf="@+id/img_grid_item5"
android:layout_toRightOf="@+id/img_grid_item5"
android:gravity="center_vertical"
android:text="@string/textview4"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

<RelativeLayout
android:id="@+id/RelativeLayout6"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play6"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play6"/>


<Button
android:id="@+id/btn_setting6"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:layout_toStartOf="@+id/btn_play6"
android:layout_toLeftOf="@+id/btn_play6"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item6"
android:layout_alignBottom="@+id/txt_grid_item6"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo5"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_setting6"
android:layout_toLeftOf="@+id/btn_setting6"
android:layout_toEndOf="@+id/img_grid_item6"
android:layout_toRightOf="@+id/img_grid_item6"
android:gravity="center_vertical"
android:text="@string/textview5"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

</LinearLayout>









share|improve this question















I have written code by taking examples from some apps. I have added buttons to my app to play a ringtone.



I just want a code to set as ringtone when user click on settings icon. My settings button id is btn_setting1, btn_setting2, btn_setting3, and so on



Here's my java code






package com.techjapreet.shivshankarkiringtone;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ringtone_tab extends AppCompatActivity {

Button clk1;
Button clk2;
Button clk3;
Button clk4;
Button clk5;
Button clk6;

MediaPlayer mediaPlayer;

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


clk6 = (Button) findViewById(R.id.btn_play6);

clk5 = (Button) findViewById(R.id.btn_play5);

clk4 = (Button) findViewById(R.id.btn_play4);

clk3 = (Button) findViewById(R.id.btn_play3);

clk2 = (Button) findViewById(R.id.btn_play2);

clk1 = (Button) findViewById(R.id.btn_play1);

mediaPlayer = new MediaPlayer();

}


public void setBtn_play6(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_vandana);
mediaPlayer.start();

}

public void setBtn_play5(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_tandav_mantra);
mediaPlayer.start();

}



public void setBtn_play4(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv_om);
mediaPlayer.start();

}

public void setBtn_play3(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv);
mediaPlayer.start();

}



public void setBtn_play2(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_aaradhna);
mediaPlayer.start();


}


public void setBtn_play1(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shankar);
mediaPlayer.start();
}

private void stopPlayer(){
if(mediaPlayer != null && mediaPlayer.isPlaying())
{mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
}
}

}





and here's my layout.xml code file



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">


<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play1"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play1" />


<Button
android:id="@+id/btn_setting1"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_toStartOf="@+id/btn_play1"
android:layout_toLeftOf="@+id/btn_play1"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item1"
android:layout_alignBottom="@+id/txt_grid_item1"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="2dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:layout_toStartOf="@+id/btn_setting1"
android:layout_toLeftOf="@+id/btn_setting1"
android:layout_toEndOf="@+id/img_grid_item1"
android:layout_toRightOf="@+id/img_grid_item1"
android:gravity="center_vertical"
android:text="@string/textview"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

<RelativeLayout
android:id="@+id/RelativeLayout2"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play2"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play2"/>


<Button
android:id="@+id/btn_setting2"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_toStartOf="@+id/btn_play2"
android:layout_toLeftOf="@+id/btn_play2"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item2"
android:layout_alignBottom="@+id/txt_grid_item2"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo1"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_setting2"
android:layout_toLeftOf="@+id/btn_setting2"
android:layout_toEndOf="@+id/img_grid_item2"
android:layout_toRightOf="@+id/img_grid_item2"
android:gravity="center_vertical"
android:text="@string/textview1"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

<RelativeLayout
android:id="@+id/RelativeLayout3"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play3"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play3"/>


<Button
android:id="@+id/btn_setting3"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_toStartOf="@+id/btn_play3"
android:layout_toLeftOf="@+id/btn_play3"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item3"
android:layout_alignBottom="@+id/txt_grid_item3"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo2"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_setting3"
android:layout_toLeftOf="@+id/btn_setting3"
android:layout_toEndOf="@+id/img_grid_item3"
android:layout_toRightOf="@+id/img_grid_item3"
android:gravity="center_vertical"
android:text="@string/textview2"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

<RelativeLayout
android:id="@+id/RelativeLayout4"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play4"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play4"/>


<Button
android:id="@+id/btn_setting4"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:layout_toStartOf="@+id/btn_play4"
android:layout_toLeftOf="@+id/btn_play4"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item4"
android:layout_alignBottom="@+id/txt_grid_item4"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo3"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_setting4"
android:layout_toLeftOf="@+id/btn_setting4"
android:layout_toEndOf="@+id/img_grid_item4"
android:layout_toRightOf="@+id/img_grid_item4"
android:gravity="center_vertical"
android:text="@string/textview3"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

<RelativeLayout
android:id="@+id/RelativeLayout5"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play5"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play5"/>


<Button
android:id="@+id/btn_setting5"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:layout_toStartOf="@+id/btn_play5"
android:layout_toLeftOf="@+id/btn_play5"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item5"
android:layout_alignBottom="@+id/txt_grid_item5"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo4"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_setting5"
android:layout_toLeftOf="@+id/btn_setting5"
android:layout_toEndOf="@+id/img_grid_item5"
android:layout_toRightOf="@+id/img_grid_item5"
android:gravity="center_vertical"
android:text="@string/textview4"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

<RelativeLayout
android:id="@+id/RelativeLayout6"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">

<Button
android:id="@+id/btn_play6"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play6"/>


<Button
android:id="@+id/btn_setting6"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:layout_toStartOf="@+id/btn_play6"
android:layout_toLeftOf="@+id/btn_play6"
android:background="@drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_setting" />

<ImageView
android:id="@+id/img_grid_item6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txt_grid_item6"
android:layout_alignBottom="@+id/txt_grid_item6"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/todo5"
android:gravity="center_vertical"
android:src="@drawable/ic_logo"
android:visibility="visible" />

<TextView
android:id="@+id/txt_grid_item6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_setting6"
android:layout_toLeftOf="@+id/btn_setting6"
android:layout_toEndOf="@+id/img_grid_item6"
android:layout_toRightOf="@+id/img_grid_item6"
android:gravity="center_vertical"
android:text="@string/textview5"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />


</RelativeLayout>

</LinearLayout>





package com.techjapreet.shivshankarkiringtone;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ringtone_tab extends AppCompatActivity {

Button clk1;
Button clk2;
Button clk3;
Button clk4;
Button clk5;
Button clk6;

MediaPlayer mediaPlayer;

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


clk6 = (Button) findViewById(R.id.btn_play6);

clk5 = (Button) findViewById(R.id.btn_play5);

clk4 = (Button) findViewById(R.id.btn_play4);

clk3 = (Button) findViewById(R.id.btn_play3);

clk2 = (Button) findViewById(R.id.btn_play2);

clk1 = (Button) findViewById(R.id.btn_play1);

mediaPlayer = new MediaPlayer();

}


public void setBtn_play6(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_vandana);
mediaPlayer.start();

}

public void setBtn_play5(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_tandav_mantra);
mediaPlayer.start();

}



public void setBtn_play4(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv_om);
mediaPlayer.start();

}

public void setBtn_play3(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv);
mediaPlayer.start();

}



public void setBtn_play2(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_aaradhna);
mediaPlayer.start();


}


public void setBtn_play1(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shankar);
mediaPlayer.start();
}

private void stopPlayer(){
if(mediaPlayer != null && mediaPlayer.isPlaying())
{mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
}
}

}





package com.techjapreet.shivshankarkiringtone;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ringtone_tab extends AppCompatActivity {

Button clk1;
Button clk2;
Button clk3;
Button clk4;
Button clk5;
Button clk6;

MediaPlayer mediaPlayer;

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


clk6 = (Button) findViewById(R.id.btn_play6);

clk5 = (Button) findViewById(R.id.btn_play5);

clk4 = (Button) findViewById(R.id.btn_play4);

clk3 = (Button) findViewById(R.id.btn_play3);

clk2 = (Button) findViewById(R.id.btn_play2);

clk1 = (Button) findViewById(R.id.btn_play1);

mediaPlayer = new MediaPlayer();

}


public void setBtn_play6(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_vandana);
mediaPlayer.start();

}

public void setBtn_play5(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_tandav_mantra);
mediaPlayer.start();

}



public void setBtn_play4(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv_om);
mediaPlayer.start();

}

public void setBtn_play3(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv);
mediaPlayer.start();

}



public void setBtn_play2(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_aaradhna);
mediaPlayer.start();


}


public void setBtn_play1(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shankar);
mediaPlayer.start();
}

private void stopPlayer(){
if(mediaPlayer != null && mediaPlayer.isPlaying())
{mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
}
}

}






android






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 12:06

























asked Nov 20 at 13:46









Jaspreet singh

85




85












  • Please add code for btn_setting1 ,2 ,3 ,4 ,5 and 6 to set as ringtone when user click on settings button
    – Jaspreet singh
    Nov 20 at 15:29


















  • Please add code for btn_setting1 ,2 ,3 ,4 ,5 and 6 to set as ringtone when user click on settings button
    – Jaspreet singh
    Nov 20 at 15:29
















Please add code for btn_setting1 ,2 ,3 ,4 ,5 and 6 to set as ringtone when user click on settings button
– Jaspreet singh
Nov 20 at 15:29




Please add code for btn_setting1 ,2 ,3 ,4 ,5 and 6 to set as ringtone when user click on settings button
– Jaspreet singh
Nov 20 at 15:29












3 Answers
3






active

oldest

votes

















up vote
0
down vote













First copy selected ringtone into device using below code :-



 private void CopyAssets() {
AssetManager assetManager = getAssets();
String files = null;
try {
files = assetManager.list("Files");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}

for(String filename : files) {
System.out.println("File name => "+filename);
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("Files/"+filename); // if files resides inside the "Files" directory itself
out = new FileOutputStream(Environment.getExternalStorageDirectory().toString() +"/" + filename);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(Exception e) {
Log.e("tag", e.getMessage());
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}


After copying set ringtone using below code:-



 ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, ringFile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "ring");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.MediaColumns.SIZE, rindFile.length());
values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);

Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringFile.getAbsolutePath());
Uri newUri = mCr.insert(uri, values);


try {
RingtoneManager.setActualDefaultRingtoneUri(getContext(), RingtoneManager.TYPE_RINGTONE, newUri);
} catch (Throwable t) {

}


where ringFile is your file path of your ringtone.






share|improve this answer





















  • but where is the settings button i mentioned above, on which user clicks and it will set it as ringtone
    – Jaspreet singh
    Nov 20 at 14:19










  • You need to create button like this Button btnSetting = (Button) this.findViewById(R.id.btn_setting); and then set on click listener.
    – Anand Jain
    Nov 20 at 14:23










  • i have added Button setring1 = (Button) this.findViewById(R.id.btn_setting1);. You can see java code above. please tell me where to add te rest of the code
    – Jaspreet singh
    Nov 20 at 14:32










  • i have added onlicklistener method but it is in red colour all over
    – Jaspreet singh
    Nov 20 at 14:50










  • please edit my code file
    – Jaspreet singh
    Nov 20 at 14:50


















up vote
0
down vote













Define Media player within the onCreate method like this:



protected void onCreate(Bundle savedInstanceState){
Button clk1;
Button clk2;
Button clk3;
Button clk4;
Button clk5;
Button clk6;

MediaPlayer mediaPlayer;


Also replace this



mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv_om); wih



mediaPlayer = MediaPlayer.create(this, R.raw.shiv_shiv_om);





share|improve this answer




























    up vote
    0
    down vote













    You are not implementing onClickListener. That is when button is actually pressed.



      public void setBtn_play3.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    mediaPlayer = MediaPlayer.create(this,
    R.raw.shiv_shiv);
    mediaPlayer.start();
    }





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394427%2fhow-to-set-current-playing-audio-as-ringtone-when-user-click-on-setting-icon%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      First copy selected ringtone into device using below code :-



       private void CopyAssets() {
      AssetManager assetManager = getAssets();
      String files = null;
      try {
      files = assetManager.list("Files");
      } catch (IOException e) {
      Log.e("tag", e.getMessage());
      }

      for(String filename : files) {
      System.out.println("File name => "+filename);
      InputStream in = null;
      OutputStream out = null;
      try {
      in = assetManager.open("Files/"+filename); // if files resides inside the "Files" directory itself
      out = new FileOutputStream(Environment.getExternalStorageDirectory().toString() +"/" + filename);
      copyFile(in, out);
      in.close();
      in = null;
      out.flush();
      out.close();
      out = null;
      } catch(Exception e) {
      Log.e("tag", e.getMessage());
      }
      }
      }
      private void copyFile(InputStream in, OutputStream out) throws IOException {
      byte buffer = new byte[1024];
      int read;
      while((read = in.read(buffer)) != -1){
      out.write(buffer, 0, read);
      }
      }


      After copying set ringtone using below code:-



       ContentValues values = new ContentValues();
      values.put(MediaStore.MediaColumns.DATA, ringFile.getAbsolutePath());
      values.put(MediaStore.MediaColumns.TITLE, "ring");
      values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
      values.put(MediaStore.MediaColumns.SIZE, rindFile.length());
      values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
      values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
      values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
      values.put(MediaStore.Audio.Media.IS_ALARM, true);
      values.put(MediaStore.Audio.Media.IS_MUSIC, false);

      Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringFile.getAbsolutePath());
      Uri newUri = mCr.insert(uri, values);


      try {
      RingtoneManager.setActualDefaultRingtoneUri(getContext(), RingtoneManager.TYPE_RINGTONE, newUri);
      } catch (Throwable t) {

      }


      where ringFile is your file path of your ringtone.






      share|improve this answer





















      • but where is the settings button i mentioned above, on which user clicks and it will set it as ringtone
        – Jaspreet singh
        Nov 20 at 14:19










      • You need to create button like this Button btnSetting = (Button) this.findViewById(R.id.btn_setting); and then set on click listener.
        – Anand Jain
        Nov 20 at 14:23










      • i have added Button setring1 = (Button) this.findViewById(R.id.btn_setting1);. You can see java code above. please tell me where to add te rest of the code
        – Jaspreet singh
        Nov 20 at 14:32










      • i have added onlicklistener method but it is in red colour all over
        – Jaspreet singh
        Nov 20 at 14:50










      • please edit my code file
        – Jaspreet singh
        Nov 20 at 14:50















      up vote
      0
      down vote













      First copy selected ringtone into device using below code :-



       private void CopyAssets() {
      AssetManager assetManager = getAssets();
      String files = null;
      try {
      files = assetManager.list("Files");
      } catch (IOException e) {
      Log.e("tag", e.getMessage());
      }

      for(String filename : files) {
      System.out.println("File name => "+filename);
      InputStream in = null;
      OutputStream out = null;
      try {
      in = assetManager.open("Files/"+filename); // if files resides inside the "Files" directory itself
      out = new FileOutputStream(Environment.getExternalStorageDirectory().toString() +"/" + filename);
      copyFile(in, out);
      in.close();
      in = null;
      out.flush();
      out.close();
      out = null;
      } catch(Exception e) {
      Log.e("tag", e.getMessage());
      }
      }
      }
      private void copyFile(InputStream in, OutputStream out) throws IOException {
      byte buffer = new byte[1024];
      int read;
      while((read = in.read(buffer)) != -1){
      out.write(buffer, 0, read);
      }
      }


      After copying set ringtone using below code:-



       ContentValues values = new ContentValues();
      values.put(MediaStore.MediaColumns.DATA, ringFile.getAbsolutePath());
      values.put(MediaStore.MediaColumns.TITLE, "ring");
      values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
      values.put(MediaStore.MediaColumns.SIZE, rindFile.length());
      values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
      values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
      values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
      values.put(MediaStore.Audio.Media.IS_ALARM, true);
      values.put(MediaStore.Audio.Media.IS_MUSIC, false);

      Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringFile.getAbsolutePath());
      Uri newUri = mCr.insert(uri, values);


      try {
      RingtoneManager.setActualDefaultRingtoneUri(getContext(), RingtoneManager.TYPE_RINGTONE, newUri);
      } catch (Throwable t) {

      }


      where ringFile is your file path of your ringtone.






      share|improve this answer





















      • but where is the settings button i mentioned above, on which user clicks and it will set it as ringtone
        – Jaspreet singh
        Nov 20 at 14:19










      • You need to create button like this Button btnSetting = (Button) this.findViewById(R.id.btn_setting); and then set on click listener.
        – Anand Jain
        Nov 20 at 14:23










      • i have added Button setring1 = (Button) this.findViewById(R.id.btn_setting1);. You can see java code above. please tell me where to add te rest of the code
        – Jaspreet singh
        Nov 20 at 14:32










      • i have added onlicklistener method but it is in red colour all over
        – Jaspreet singh
        Nov 20 at 14:50










      • please edit my code file
        – Jaspreet singh
        Nov 20 at 14:50













      up vote
      0
      down vote










      up vote
      0
      down vote









      First copy selected ringtone into device using below code :-



       private void CopyAssets() {
      AssetManager assetManager = getAssets();
      String files = null;
      try {
      files = assetManager.list("Files");
      } catch (IOException e) {
      Log.e("tag", e.getMessage());
      }

      for(String filename : files) {
      System.out.println("File name => "+filename);
      InputStream in = null;
      OutputStream out = null;
      try {
      in = assetManager.open("Files/"+filename); // if files resides inside the "Files" directory itself
      out = new FileOutputStream(Environment.getExternalStorageDirectory().toString() +"/" + filename);
      copyFile(in, out);
      in.close();
      in = null;
      out.flush();
      out.close();
      out = null;
      } catch(Exception e) {
      Log.e("tag", e.getMessage());
      }
      }
      }
      private void copyFile(InputStream in, OutputStream out) throws IOException {
      byte buffer = new byte[1024];
      int read;
      while((read = in.read(buffer)) != -1){
      out.write(buffer, 0, read);
      }
      }


      After copying set ringtone using below code:-



       ContentValues values = new ContentValues();
      values.put(MediaStore.MediaColumns.DATA, ringFile.getAbsolutePath());
      values.put(MediaStore.MediaColumns.TITLE, "ring");
      values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
      values.put(MediaStore.MediaColumns.SIZE, rindFile.length());
      values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
      values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
      values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
      values.put(MediaStore.Audio.Media.IS_ALARM, true);
      values.put(MediaStore.Audio.Media.IS_MUSIC, false);

      Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringFile.getAbsolutePath());
      Uri newUri = mCr.insert(uri, values);


      try {
      RingtoneManager.setActualDefaultRingtoneUri(getContext(), RingtoneManager.TYPE_RINGTONE, newUri);
      } catch (Throwable t) {

      }


      where ringFile is your file path of your ringtone.






      share|improve this answer












      First copy selected ringtone into device using below code :-



       private void CopyAssets() {
      AssetManager assetManager = getAssets();
      String files = null;
      try {
      files = assetManager.list("Files");
      } catch (IOException e) {
      Log.e("tag", e.getMessage());
      }

      for(String filename : files) {
      System.out.println("File name => "+filename);
      InputStream in = null;
      OutputStream out = null;
      try {
      in = assetManager.open("Files/"+filename); // if files resides inside the "Files" directory itself
      out = new FileOutputStream(Environment.getExternalStorageDirectory().toString() +"/" + filename);
      copyFile(in, out);
      in.close();
      in = null;
      out.flush();
      out.close();
      out = null;
      } catch(Exception e) {
      Log.e("tag", e.getMessage());
      }
      }
      }
      private void copyFile(InputStream in, OutputStream out) throws IOException {
      byte buffer = new byte[1024];
      int read;
      while((read = in.read(buffer)) != -1){
      out.write(buffer, 0, read);
      }
      }


      After copying set ringtone using below code:-



       ContentValues values = new ContentValues();
      values.put(MediaStore.MediaColumns.DATA, ringFile.getAbsolutePath());
      values.put(MediaStore.MediaColumns.TITLE, "ring");
      values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
      values.put(MediaStore.MediaColumns.SIZE, rindFile.length());
      values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
      values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
      values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
      values.put(MediaStore.Audio.Media.IS_ALARM, true);
      values.put(MediaStore.Audio.Media.IS_MUSIC, false);

      Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringFile.getAbsolutePath());
      Uri newUri = mCr.insert(uri, values);


      try {
      RingtoneManager.setActualDefaultRingtoneUri(getContext(), RingtoneManager.TYPE_RINGTONE, newUri);
      } catch (Throwable t) {

      }


      where ringFile is your file path of your ringtone.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 20 at 14:15









      Anand Jain

      92352655




      92352655












      • but where is the settings button i mentioned above, on which user clicks and it will set it as ringtone
        – Jaspreet singh
        Nov 20 at 14:19










      • You need to create button like this Button btnSetting = (Button) this.findViewById(R.id.btn_setting); and then set on click listener.
        – Anand Jain
        Nov 20 at 14:23










      • i have added Button setring1 = (Button) this.findViewById(R.id.btn_setting1);. You can see java code above. please tell me where to add te rest of the code
        – Jaspreet singh
        Nov 20 at 14:32










      • i have added onlicklistener method but it is in red colour all over
        – Jaspreet singh
        Nov 20 at 14:50










      • please edit my code file
        – Jaspreet singh
        Nov 20 at 14:50


















      • but where is the settings button i mentioned above, on which user clicks and it will set it as ringtone
        – Jaspreet singh
        Nov 20 at 14:19










      • You need to create button like this Button btnSetting = (Button) this.findViewById(R.id.btn_setting); and then set on click listener.
        – Anand Jain
        Nov 20 at 14:23










      • i have added Button setring1 = (Button) this.findViewById(R.id.btn_setting1);. You can see java code above. please tell me where to add te rest of the code
        – Jaspreet singh
        Nov 20 at 14:32










      • i have added onlicklistener method but it is in red colour all over
        – Jaspreet singh
        Nov 20 at 14:50










      • please edit my code file
        – Jaspreet singh
        Nov 20 at 14:50
















      but where is the settings button i mentioned above, on which user clicks and it will set it as ringtone
      – Jaspreet singh
      Nov 20 at 14:19




      but where is the settings button i mentioned above, on which user clicks and it will set it as ringtone
      – Jaspreet singh
      Nov 20 at 14:19












      You need to create button like this Button btnSetting = (Button) this.findViewById(R.id.btn_setting); and then set on click listener.
      – Anand Jain
      Nov 20 at 14:23




      You need to create button like this Button btnSetting = (Button) this.findViewById(R.id.btn_setting); and then set on click listener.
      – Anand Jain
      Nov 20 at 14:23












      i have added Button setring1 = (Button) this.findViewById(R.id.btn_setting1);. You can see java code above. please tell me where to add te rest of the code
      – Jaspreet singh
      Nov 20 at 14:32




      i have added Button setring1 = (Button) this.findViewById(R.id.btn_setting1);. You can see java code above. please tell me where to add te rest of the code
      – Jaspreet singh
      Nov 20 at 14:32












      i have added onlicklistener method but it is in red colour all over
      – Jaspreet singh
      Nov 20 at 14:50




      i have added onlicklistener method but it is in red colour all over
      – Jaspreet singh
      Nov 20 at 14:50












      please edit my code file
      – Jaspreet singh
      Nov 20 at 14:50




      please edit my code file
      – Jaspreet singh
      Nov 20 at 14:50












      up vote
      0
      down vote













      Define Media player within the onCreate method like this:



      protected void onCreate(Bundle savedInstanceState){
      Button clk1;
      Button clk2;
      Button clk3;
      Button clk4;
      Button clk5;
      Button clk6;

      MediaPlayer mediaPlayer;


      Also replace this



      mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv_om); wih



      mediaPlayer = MediaPlayer.create(this, R.raw.shiv_shiv_om);





      share|improve this answer

























        up vote
        0
        down vote













        Define Media player within the onCreate method like this:



        protected void onCreate(Bundle savedInstanceState){
        Button clk1;
        Button clk2;
        Button clk3;
        Button clk4;
        Button clk5;
        Button clk6;

        MediaPlayer mediaPlayer;


        Also replace this



        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv_om); wih



        mediaPlayer = MediaPlayer.create(this, R.raw.shiv_shiv_om);





        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          Define Media player within the onCreate method like this:



          protected void onCreate(Bundle savedInstanceState){
          Button clk1;
          Button clk2;
          Button clk3;
          Button clk4;
          Button clk5;
          Button clk6;

          MediaPlayer mediaPlayer;


          Also replace this



          mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv_om); wih



          mediaPlayer = MediaPlayer.create(this, R.raw.shiv_shiv_om);





          share|improve this answer












          Define Media player within the onCreate method like this:



          protected void onCreate(Bundle savedInstanceState){
          Button clk1;
          Button clk2;
          Button clk3;
          Button clk4;
          Button clk5;
          Button clk6;

          MediaPlayer mediaPlayer;


          Also replace this



          mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv_om); wih



          mediaPlayer = MediaPlayer.create(this, R.raw.shiv_shiv_om);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 14:15









          Adeel Malik

          1




          1






















              up vote
              0
              down vote













              You are not implementing onClickListener. That is when button is actually pressed.



                public void setBtn_play3.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
              mediaPlayer = MediaPlayer.create(this,
              R.raw.shiv_shiv);
              mediaPlayer.start();
              }





              share|improve this answer

























                up vote
                0
                down vote













                You are not implementing onClickListener. That is when button is actually pressed.



                  public void setBtn_play3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                mediaPlayer = MediaPlayer.create(this,
                R.raw.shiv_shiv);
                mediaPlayer.start();
                }





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  You are not implementing onClickListener. That is when button is actually pressed.



                    public void setBtn_play3.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                  mediaPlayer = MediaPlayer.create(this,
                  R.raw.shiv_shiv);
                  mediaPlayer.start();
                  }





                  share|improve this answer












                  You are not implementing onClickListener. That is when button is actually pressed.



                    public void setBtn_play3.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                  mediaPlayer = MediaPlayer.create(this,
                  R.raw.shiv_shiv);
                  mediaPlayer.start();
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 at 14:33









                  Adeel Malik

                  1




                  1






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394427%2fhow-to-set-current-playing-audio-as-ringtone-when-user-click-on-setting-icon%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Wiesbaden

                      Marschland

                      Dieringhausen