How to activate Text-To-Speech when clicked on TextView? - android

the question is as above.
scenario : i programmatically created table rows with text view in it. i wanted to allow text-to-speech when i clicked on the textview. there is some reason for not using listview. i tried to use button for easier usage, however the button that i created is always out of the dimension that gave. so, i wanted to use textview to activate the TTS.
how do i do that ?
i tried using
tv.setOnClickListener(new OnClickListener()
{
public void OnClick(View v)
{
String speech = list.get(i).toString();
tts.speak(speech,TextToSpeech.QUEUE_FLUSH,null);
}
});
i uses for-loop for it, so that it will create a table row for every data collected. the problem is, it requested the "i" to be final. and when i made it final, i cant use i++.
please help. thanks alot =)

Try to declare your int i; in your class. I believe this will help you to avoid of using final modifier.
public class MyActivity extends Activity {
int i;
public void onCreate(Bundle savedInstanceState) {
...
tv.setOnClickListener(new OnClickListener()
{
public void OnClick(View v){
String speech = list.get(i).toString();
tts.speak(speech,TextToSpeech.QUEUE_FLUSH,null);
}
});
...
}
}
or put this code String speech = list.get(i).toString();
tts.speak(speech,TextToSpeech.QUEUE_FLUSH,null); to another method. for example:
tv.setOnClickListener(new OnClickListener()
{
public void OnClick(View v){
speak();
}
});
__
public void speak(){
String speech = list.get(i).toString();
tts.speak(speech,TextToSpeech.QUEUE_FLUSH,null);
}

Related

Intent GridLayout to 4 Activities

I have GridLayout with a CardView inside. I want to create an Intent to 4 different activities. I only can execute one Intent. I do not know if I should use Else If or case. Thanks for your help. Here is my code.
GridLayout mainGrid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
mainGrid = (GridLayout)findViewById(R.id.grid);
setSingleEvent(mainGrid);
}
private void setSingleEvent(GridLayout mainGrid) {
for (int i=0;i<mainGrid.getChildCount();1++)
{
CardView cardView = (CardView)mainGrid.getChildAt(i);
final int final1= i;
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i=new Intent(DashboardActivity.this,MapsActivity.class);
startActivity(i);
}
});
}
}
I think your problem comes from a lack of understanding how the order of execution of the program works. If you insert a cycle for and then you put inside an anonymous class as it is the case of View.onViewClickListener is normal that you will have only one result, because you break the for cycle.
The way to go should be to not use a for cycle, but assigning different Explicit Intents depending by what you want to obtain.
EDIT. On the base of your use case you need to trigger the Intent from the Adapter. Please see here and mainly here, basically you need to use the Android SDK functionality that tells you which card has been clicked mRecyclerView.getChildLayoutPosition(view); then depending from your needs you may (or not) pass a switch case for educational purposes, although possibly is not the most elegant and efficient way to solve.
If you want a different Activity to be started depending on what CardView is clicked try something like the following:
private void setSingleEvent(GridLayout mainGrid) {
mainGrid.getChildAt(0).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(i);
}
});
mainGrid.getChildAt(1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(i);
}
});
//do this for all 4 cardViews.
}
In short: set all Intents separately.

Android Quiz App and how to code for multiple correct answers

I have written a quiz app for students preparing for an English exam.Some of the quiz asks the users to enter the correct answer into an edittext box and their answers are checked against the list of correct answers in a string. Some questions could have two possible answers ( I'd talk to my boss / I would talk to my boss) How do I code for two possible answers.
You need to compare the values that your students are entering with preset and saved values in your codes, Use string1.equals(string2)piece of code, maybe put it in a if and else line and ask what to do if true and what to do if wrong, something like this maybe:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv=(TextView)findViewById(R.id.tv1);
final EditText ed =(EditText)findViewById(R.id.ed1);
final Button btn = (Button)findViewById(R.id.btn1);
final String argue1="I'd talk to my boss";
final String argue2="I would talk to my boss";
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String saver= ed.getText().toString();
if (saver.equals(argue1)){
tv.setText("true");
} else if (saver.equals(argue2)){
tv.setText("true");
}else {
tv.setText("wrong");
}
}
});
}}
I think I didn't quite get what you mean in your comment, but if you mean something like this:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String saver= ed.getText().toString();
if (saver.equals(argue1)|saver.equals(argue2)){
tv.setText("true");
}else {
tv.setText("wrong");
}
}
});
Yes, It also works like this, I just checked that out for you...

Im trying to figure out how to access an image from the android default gallery and email it

I've figured out how to access the default gallery, and I've figured out how to use the email function for text but I wanna email images in my gallery, how do I do this? I was going to try and use the code bellow but an error keeps poping up for "getTempUri", what am I overlooking, I appreciate any help on this. Thanks
public void onClick(View v) {
Intent photosendbutton = new Intent(Intent.ACTION_SEND);
photosendbutton.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
photosendbutton.putExtra(Intent.EXTRA_EMAIL, new String[]{"johnnywalker#hotmail.com"});
photosendbutton.setType("image/*");
Uri parse = getTempUri("/mnt/sdcard/yourfolder");
photosendbutton.putExtra(MediaStore.EXTRA_OUTPUT, parse);
startActivity(Intent.createChooser(photosendbutton, "Send mail..."));
This is because you're trying to access a method from within an onclicklistener. The method getTempUri is defined outside the onClickListener, but you probably don't see it because the onClickListener is declared inline like this:
class YourContainingClass{
public Uri getTempUri(String){
//process string
}
public void someOtherMethod(){
button.setOnClickListener(new OnCLickListener(){
public void onClick(View v) {
//this is where you're trying to access getTempUri,
//from within the new OnClickListener instance
}
});
}
}
You can solve this by accessing getTempUri like this, if your containing class is an Activity:
((YourContainingClass)v.getContext()).getTempUri()
otherwise you can let your containing class implement onClickListener like this:
class YourContainingClass implements OnClickListener
{
public void someMethod(){
button.setOnClickListener(this);
}
public void OnClick(View v){
//put your code here, here you have access to getTepUri
this.getTempUri();
}
public Uri getTempUri(String someString){
//your logic
}
}
I realize this may be hard to follow, but try it out and feel free to ask questions

How do you Override onClick in an Android ImageButton derived class

I've seen this asked a thousand times in a thousand different ways, but still can't get it to work...
I created a class which I've derived from ImageButton. I want to define my "on-click" behavior in the class.
I know I can do something inside my Activity's onCreate like:
myButton b;
b = (myButton)findViewById(R.drawable.mybutton);
b.setOnClickListener(new View.OnClickListener() {
...etc...
but I want to define the code where it should be, in the derived class.
I first thought I could define it as:
#Override public void onClick(View v) {
...but I get an error saying that I can't use "#Override" here because "onClick" isn't in the superclass. (When trying to remove "#Override", it just builds and runs, but never gets called). I've also tried:
#Override public void onClickListener(View v) {
...and several variants of "implements onClickListener" and "implements OnClickListener" to no avail.
This should be fairly simple - any ideas??
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
derivedClassFunction(v);
}
});
public void derivedClassFunction(View v) {
/* code...*/
}
Another way:
public class DerivedClass extends ImageButton implements View.OnClickListener {
/*code...*/
b.setOnClickListener(this);
/*code...*/
#Override
public void onClick(View v) {
/*code...*/
}
}
This is because there actually is no method onClick() in views. The work is done in the onUpKey() in the View class.
However, if you want to listen to clicking events in the subclass, this could be done very easily. You can either create an inner class which implements View.OnClickLister and use it to listen to events or even simpler, implement the interface in your class and set it as a listener during construction. The latter will look like this:
class YourClass extends ImageButton implements View.OnClickListener {
public YourClass() {
setOnClickListener(this);
}
#Override
public void onClick(View v) {
//Your code
}
}
LAS_VEGAS has already posted how the first variant with the inner class may look like.

Working with buttons in android

Alright, so i've been making great progress on the app i'm trying to create, but most of the tutorials that i've been learning from only showcase the wondrous feature of having only one active widget inside the application at a time...
The thing is, my application requires 2 or more buttons and that's the part i'm partially stuck at. My code implements a "SetWordsBtn" shown below (everything else is declared),
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
SetWordsBtn=(Button)findViewById(R.id.SetWordsBtn);
SetWordsBtn.setOnClickListener(this);
}
which implements a onClick() like this:
public void onClick(View view) {
startWords();
}
but what if i have another button that deletes the words such as "DelWordsBtn"? I was thinking i could declare both buttons simultaneously like this:
SetWordsBtn=(Button)findViewById(R.id.SetWordsBtn);
DelWordsBtn=(Button)findViewById(R.id.DelWordsBtn);
SetWordsBtn.setOnClickListener(this);
DelWordsBtn.setOnClickListener(this);
but what about the onClick() method? Does it automatically apply itself to both the buttons when i do this?
How am i able to declare a seperate onClick from each other so it both does different stuff when i click on either one of them?
I was thinking the answer could be something like this, but i dunno :
//Declarations
SetWordsBtn=(Button)findViewById(R.id.SetWordsBtn);
DelWordsBtn=(Button)findViewById(R.id.DelWordsBtn);
SetWordsBtn.setOnClickListener(setWordsView);
DelWordsBtn.setOnClickListener(delWordsView);
//onClick Functions
public void onClick(View setWordsView) {
startWords();
}
public void onClick(View delWordsView) {
deleteWords();
}
So it would actually link the startWords() function to the SetWordsBtn, and deleteWords() to DelWordsBtn...
Any clear cut explanation/form of help would be appreciated. Thanks in advance guys. :)
The typical convention is to just switch off of the ID of the View that is clicked. For example:
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.SetWordsBtn:
startWords();
break;
case R.id.DelWordsBtn:
deleteWords();
break;
}
}
};
int[] ids = { R.id.SetWordsBtn, R.id.DelWordsBtn };
for(int i : ids) ((Button)findViewById(i)).setOnClickListener(listener);
You can alternatively set up anonymous inner class(es) that listen, instead of having your Activity itself be the listener that implements OnClickListener. Example from the Android Button javadoc:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
http://developer.android.com/reference/android/widget/Button.html
P.S. start your local variable names, and method names, with lower case letters -- upper case is for class names.
Where you suggested:
public void onClick(View setWordsView) {
startWords();
}
public void onClick(View delWordsView) {
deleteWords();
}
If you think about it, there is no difference in the two method declarations and you would get a build error (method signatures are the same, even though the method parameter, View, has a different name).
If I understand your question correctly then the answer given by kcoppock is correct. You also could define an Anonymous Class
Drag and drop button on graghiclayout.xml
...>right click the button -->choose other properties....>choose inherited from view ---->click on click ....name it callme.
That will be shows like this:
xml file
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="76dp"
android:layout_y="58dp"
android:onClick="callme"
android:text="Button" />
Run once your project:
Open src --->activity .java
----->, do the coding like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
but=(Button)findViewById(R.id.button1);
}
public void callme(View v)
{
//Do somthing
}

Categories

Resources