B4A Library [Custom View] Speak Button

This class implements a "speak button":

SS-2013-06-12_11.21.17.png


It makes it very simple to add a speak button to an EditText.

You should add the button with the designer. Add a CustomView and set its type to SpeakButton.

In your code you should set the SpeakButton TargetEditText to the relevant EditText.

The code in the above example:
Code:
Sub Globals
   Dim EditText1 As EditText
   Dim EditText2 As EditText
   Dim Speak1 As SpeakButton
   Dim Speak2 As SpeakButton
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   Speak1.TargetEditText = EditText1
   Speak2.TargetEditText = EditText2
End Sub

This class depends on the Phone library and Reflection library.
 

Attachments

Hi Erel,
I am a beginner. I like your source. How do I add the CustomView at run-time? Sorry for my English.
 
Hi Erel,
I wrote:

Sub Globals
Dim sf As StringFunctions
Dim EditText1 As EditText
Dim Speak1 As SpeakButton
End Sub

Sub Activity_Create(FirstTime As Boolean)
Dim pnl1 As Panel
pnl1.Initialize("pnl1")
Activity.AddView(pnl1,300dip,100dip,50dip,50dip)
EditText1.Initialize("edittext1")
Activity.AddView(EditText1,50dip,100dip,230dip,50dip)
Speak1.Initialize("Speak1","" )
Speak1.DesignerCreateView(pnl1,"","")
Speak1.TargetEditText = EditText1
End Sub

but it does not work. There is an error:

Speak1.DesignerCreateView(pnl1,\
javac 1.7.0_45
src\b4a\example\main.java:325: error: inconvertible types
mostCurrent._speak1._designercreateview(_pnl1,(anywheresoftware.b4a.objects.LabelWrapper) anywheresoftware.b4a.AbsObjectWrapper.ConvertToWrapper(new anywheresoftware.b4a.objects.LabelWrapper(), (android.widget.TextView)("")),(anywheresoftware.b4a.objects.collections.Map) anywheresoftware.b4a.AbsObjectWrapper.ConvertToWrapper(new anywheresoftware.b4a.objects.collections.Map(), (anywheresoftware.b4a.objects.collections.Map.MyMap)("")));
 
Thank you. I am a beginner. I thought "" and Null was the same thing. Now it works. Thanks to Erel and also to Klaus.
 
I put everything in a sub and it does not work:
Sub Dialogo2(i As Int) As Int
Dim cd As CustomDialog2
Dim pnl As Panel
pnl.Initialize("pnl")
Dim pnl1 As Panel
pnl1.Initialize("pnl1")
pnl1.Color=Colors.White
EditText1.Initialize("")
cd.AddView(pnl, 77%x, 60%y)
pnl.AddView(EditText1, 80dip, 50dip, 120dip, 50dip)
pnl.AddView(pnl1, 200dip, 50dip, 50dip, 50dip)
Speak1.Initialize("Speak1",Null )
Speak1.DesignerCreateView(pnl1,Null,Null)
Speak1.TargetEditText = EditText1
ret = cd.Show("Immetti Stazione", "Yes", "Successiva", "Maybe", Main.Bmp)
Return ret
End Sub

Help me :-(
 
I needed two speakbuttons which behave in a different way - one like this and the other by adding the text to the already written text.
I made the following changes:
1. Made Private variables for the TargetModule and Eventname so they can be used throughout the class.
Code:
Public Sub Initialize (TargetModule As Object, EventName As String)
    Module = TargetModule
    Ename = EventName
    vr.Initialize("vr")
End Sub
2. Changed the Result sub to:
Code:
Private Sub vr_Result (Success As Boolean, Texts As List)
If Success AND Texts.Size > 0 Then
    If SubExists(Module,Ename & "_Result") Then
        CallSub2(Module,Ename & "_Result",Texts.Get(0))
    End If
End If
End Sub
3. Deleted SetTargetEditText sub
4. Made two subs in the main module using the names of the EditText views:
Code:
Public Sub saytopic_Result(text As String)
topicet.text =  text
End Sub
Public Sub saynote_Result(text As String)
noteet.Text = noteet.Text & text & CRLF
End Sub
Thats it, it works.
 
My problem is that as soon as I speak the dialog box closes. The dialog box closes without me confirm.
 
I tried and VoiceRecognition does not work with the dialog box, is not the fault of this class but its VoiceRecognition. I will use the panels.
Thanks.
 
I'm keep getting "An error has occurred in sub:speakbutton_vr_result(java line:138)
java.lang.RunTimeException:Object should first be initialized(EditText) Continue? when I tried on other module than Main module. I have tried several method of initialized but not successful. Look like it only work on Main module. Thanks.
 
I'm keep getting "An error has occurred in sub:speakbutton_vr_result(java line:138)
java.lang.RunTimeException:Object should first be initialized(EditText) Continue? when I tried on other module than Main module. I have tried several method of initialized but not successful. Look like it only work on Main module. Thanks.


Check this link:
This should fix the Object should first be initialized(EditText) I had the same issue.

http://www.b4x.com/android/forum/th...and-your-app-will-crash-unless.31862/#content
 
Can you upload your project (File - Export as zip)?
Erel, the project is too big to export. But I have solved the problem with addition panel just for the SpeakButton to sit on top and create spk1.DesignerCreteView(pnlSPK1, Null, Null) in the module. Thanks.
 
To add a speakbutton by code add the following subs to the class:
Code:
Public Sub CodeCreateView
    btn.Initialize("btn")
    btn.Background = LoadResourceDrawable(17301668) 'R.drawable.ic_btn_speak_now
    btn.Enabled = vr.IsSupported
End Sub

Public Sub AsView As View
Return btn
End Sub

Then in the calling module add the view by code, using [button].AsView with the name of the button.
 
To add a speakbutton by code add the following subs to the class:
Code:
Public Sub CodeCreateView
    btn.Initialize("btn")
    btn.Background = LoadResourceDrawable(17301668) 'R.drawable.ic_btn_speak_now
    btn.Enabled = vr.IsSupported
End Sub

Public Sub AsView As View
Return btn
End Sub

Then in the calling module add the view by code, using [button].AsView with the name of the button.
Derez:
Can you show me example? Not sure if I understand you completely. Thanks
 
Add the two subs above to speakbutton module.
In the calling module I have a edittext named et and a speakbutton named vbtn, declared in globals.
In initializing the button do:
Code:
vbtn.Initialize(Me,"vbtn")
vbtn.CodeCreateView
vbtn.TargetEditText = et
Activity.AddView(vbtn.asview, 87%x,2%y,12%x,8%y)

Of course, the button can be added to a panel instead of activity.
 
Back
Top