android - Populate AlertDialog With Values -
i new programming , started android development out of passion , second application.
i looked android tutorials online , trying this: 1. record button in mainaactivity 2. on-click opens dialog box gives options record. 3. dialog box has 4 buttons (start, stop, playback, stop playback).
what working: 1. on click shows dialog box, no options in it. 2. record works, when played different activity.
i believe have missed something, can help?
//mainactivity.java
import android.app.alertdialog; import android.content.context; import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.support.v7.app.actionbaractivity; import android.view.layoutinflater; import android.support.v4.app.dialogfragment; import android.view.view; public class mainactivity extends fragmentactivity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } public void recordclick(view v){ recorddialog dialog = new recorddialog(); dialog.show(getfragmentmanager(),"record_dialog"); } }
//dialog.java
package com.example.record; import android.app.alertdialog; import android.app.dialog; import android.app.dialogfragment; import android.content.dialoginterface; import android.media.mediaplayer; import android.media.mediarecorder; import android.os.bundle; import android.os.environment; import android.support.v7.app.actionbaractivity; import android.view.layoutinflater; import android.view.view; import java.io.file; import java.io.ioexception; public class recorddialog extends dialogfragment{ view v; private mediaplayer mediaplayer; private mediarecorder mediarecorder; private string store_file; /* @override public dialog oncreatedialog(bundle savedinstancestate) { layoutinflater inflator = getactivity().getlayoutinflater(); v = inflator.inflate(r.layout.customdialog, null); alertdialog.builder builder = new alertdialog.builder(getactivity()); builder.setview(v).setpositivebutton("done", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialoginterface, int i) { } }).setnegativebutton("cancel", new dialoginterface.onclicklistener(){ @override public void onclick(dialoginterface dialoginterface, int i) { } }); return builder.create(); } */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); layoutinflater inflator = getactivity().getlayoutinflater(); v = inflator.inflate(r.layout.customdialog, null); alertdialog.builder builder = new alertdialog.builder(getactivity()); store_file= environment.getexternalstoragedirectory()+"/audiorecorder.3gpp"; } public void onbuttonclick(view v){ switch (v.getid()){ case r.id.startbtn: try{ startrecording(); } catch(exception e){ e.printstacktrace(); }break; case r.id.stopbtn: try{ stoprecording(); } catch(exception e){ e.printstacktrace(); }break; case r.id.playbackbtn: try{ startplayback(); } catch(exception e){ e.printstacktrace(); }break; case r.id.stopplayingbtn: try{ stopplayback(); } catch(exception e){ e.printstacktrace(); }break; } } private void startrecording() throws ioexception { discardmediarecorder(); file storefile= new file(store_file); if(storefile.exists()) storefile.delete(); mediarecorder = new mediarecorder(); mediarecorder.setaudiosource(mediarecorder.audiosource.mic); mediarecorder.setoutputformat(mediarecorder.outputformat.three_gpp); mediarecorder.setaudioencoder(mediarecorder.audioencoder.aac); mediarecorder.setoutputfile(store_file); mediarecorder.prepare(); mediarecorder.start(); } private void discardmediarecorder(){ if (mediarecorder != null) mediarecorder.release(); } private void stoprecording() { if (mediarecorder != null) mediarecorder.stop(); } private void startplayback() throws ioexception { discardmediaplayer(); mediaplayer = new mediaplayer(); mediaplayer.setdatasource(store_file); mediaplayer.prepare(); mediaplayer.start(); } private void discardmediaplayer() { if (mediaplayer != null) { try{ mediaplayer.release(); }catch (exception e) { e.printstacktrace(); } } } private void stopplayback() { if (mediaplayer != null) mediaplayer.stop(); } }
//dialog.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:clickable="true"> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/record" android:id="@+id/imageview" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start" android:id="@+id/startbtn" android:layout_marginleft="67dp" android:layout_marginstart="67dp" android:onclick="onbuttonclick" android:layout_below="@+id/imageview" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_margintop="70dp" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="stop" android:id="@+id/stopbtn" android:layout_marginleft="59dp" android:layout_marginstart="59dp" android:layout_aligntop="@+id/startbtn" android:layout_torightof="@+id/startbtn" android:layout_toendof="@+id/startbtn" android:onclick="onbuttonclick" android:clickable="true" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="playblack" android:id="@+id/playbackbtn" android:onclick="onbuttonclick" android:clickable="true" android:layout_centervertical="true" android:layout_alignleft="@+id/startbtn" android:layout_alignstart="@+id/startbtn" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="stopplaying" android:id="@+id/stopplayingbtn" android:onclick="onbuttonclick" android:nestedscrollingenabled="true" android:clickable="true" android:layout_centervertical="true" android:layout_alignleft="@+id/stopbtn" android:layout_alignstart="@+id/stopbtn" /> </relativelayout>
Comments
Post a Comment