First, I click on Go to level one button in MainActivity to go to the LevelOne Activity from MainActivity, then I am going back to MainActivity and now here I need Go to level one button to be INVISIBLE.
I tried to accomplish this via SharedPreferences, but couldn't make it.
The code I posted below is working just fine, it just doesn't make my Go to level one button invisible when I go back to MainActivity.
I've done this so far:
MainActivity.java:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bLevelOne:
Intent i = new Intent(this, LevelOne.class);
startActivityForResult(i, 1);
break;
case R.id.bLevelTwo:
break;
case R.id.bLevelThree:
break;
case R.id.bLevelFour:
break;
case R.id.bLevelFive:
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
String result = data.getStringExtra("result");
if (result == "milos") {
level1.setVisibility(View.INVISIBLE);
}
}
}
}
LevelOne.java:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.bMenu:
Intent returnIntent = new Intent();
returnIntent.putExtra("result", "milos");
setResult(RESULT_OK, returnIntent);
finish();
break;
case R.id.bNextLevel:
break;
}
}
you have a problem over here.
if (result == "milos") {
level1.setVisibility(View.INVISIBLE);
}
it should rather be
if (result.equalsIgnoreCase("milos") {
level1.setVisibility(View.INVISIBLE);
}
Related
I am building an app that utilities speech to text, in my code everything seems fine till it gets to protected void onActivityResult method to handle the results, it generates an error saying onActivityResult is a variable and then if I delete the access modifier, it sees it as a method then it generates another error in the parameters saying identifier expected and token is missing
Any help would be greatly appreciated.
My code
private static final int SPEECH_REQUEST_CODE = 100;
// Create an intent that can start the Speech Recognizer activity
private void displaySpeechRecognizer() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// Start the activity, the intent will be populated with the speech text
startActivityForResult(intent, SPEECH_REQUEST_CODE);
}
// This callback is invoked when the Speech Recognizer returns.
// This is where you process the intent and extract the speech text from the intent.
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
List<String> results = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
Output.setText(results.get(0));
}
}
DO it like this:
private void displaySpeechRecognizer() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
And in onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput.setText(result.get(0));
}
break;
}
}
}
And in onCreate:
//Button you craete to start speech recognition
Button btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
// hide the action bar
//getActionBar().hide();
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displaySpeechRecognizer();
}
});
I want to make a speech to text application but output on a different text view. I tried to search the solution on the internet, but most of the answers they only output to 1 text view. I want to make it look like this:
Here is the image of my idea: https://i.imgur.com/M9y4Rcz.png
This is for study purposes. I hope someone can help me
TextView textview1, textview2;
Button btnVoice1, btnVoice2;
textview1= findViewById(R.id.textview1);
textview2= findViewById(R.id.textview2);
btnVoice1 = findViewById(R.id.btnVoice1);
btnVoice2 = findViewById(R.id.btnVoice2);
btnVoice1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startVoiceInput();
}
});
btnVoice2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startVoiceInput();
}
});
//Function
private void startVoiceInput(){
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say Something!");
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
textview1.setText(result.get(0));
// how to make textview2 output when btnVoice2 is click
}
break;
}
}
}
For Multiple onActivityResult for single Activity change your code like this:
int REQ_CODE_SPEECH_INPUT=100;
int REQ_CODE_SPEECH_INPUT_SECOND=101;
TextView textview1, textview2;
Button btnVoice1, btnVoice2;
textview1= findViewById(R.id.textview1);
textview2= findViewById(R.id.textview2);
btnVoice1 = findViewById(R.id.btnVoice1);
btnVoice2 = findViewById(R.id.btnVoice2);
btnVoice1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startVoiceInput(REQ_CODE_SPEECH_INPUT);
}
});
btnVoice2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startVoiceInput(REQ_CODE_SPEECH_INPUT_SECOND);
}
});
private void startVoiceInput(int code){
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say Something!");
try {
startActivityForResult(intent, code);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
textview1.setText(result.get(0));
}
break;
}
case REQ_CODE_SPEECH_INPUT_SECOND:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
textview2.setText(result.get(0));
}
break;
}
}
}
I'm following the answer https://stackoverflow.com/a/867828/129805 to add a contact picker to my app.
The problem is that onActivityResult is immediately invoked with the correct reqCode (PICK_CONTACT), but with aresultCode of 0 and a null for data.
It is not invoked again, when the user actually picks a contact.
The AndroidManifest gives this activity android:launchMode="singleInstance"> as I only ever want there to be one instance.
What have I done wrong?
MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
...
addContactButton = (Button) findViewById(R.id.addContactButton);
addContactButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
});
}
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult");
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// TODO Whatever you want to do with the selected contact name.
Log.d(TAG, "you chose " + name + ".");
}
}
break;
}
}
The singleInstance fires the callback immediately. You have more info in this link
I have code which should allow me to take value from calculator and use it further:
//-----------------This section creates the keypad functionality
for (int o = 0; o < keybuttons.length; o++) {
final int n = o;
keybuttons[o] = (Button) findViewById(data.keyIds[n]);
keybuttons[o].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
String tmp = texts[selectEdit].getText()
.toString();
switch (n) {
case 3:
texts[selectEdit].setText(tmp.substring(0, tmp.length() - 1));
break; //get cursor position and delete char
case 7:
{
// Create intent for RealCalc.
Intent intent2 = new Intent("uk.co.quarticsoftware.REALCALC");
double x = 0; // Set initial value (double).
if (!texts[selectEdit].getText()
.toString()
.equals("")) {
x = Double.valueOf(texts[selectEdit].getText()
.toString());
}
intent2.putExtra("X", x);
// Launch calculator
try {
startActivityForResult(intent2, 0);
} catch (ActivityNotFoundException e) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=uk.co.nickfines.RealCalc"));
try {
startActivity(intent);
} catch (ActivityNotFoundException f) {
// Google Play Store app not available.
}
}
break;
} //open Calculator
case 11:
{
if (!tmp.contains("E")) texts[selectEdit].setText(tmp + "" + keybuttons[n].getText());
break;
} //check for E if dont have do default case
case 15:
{
TL.setVisibility(View.GONE);
break;
} //simulate back button
default:
{
texts[selectEdit].setText(tmp + "" + keybuttons[n].getText());
//get cursor start and end and get entire String
// replace selected String with button text
//insert back
break;
}
} //end of switch
} //end of try
catch (ActivityNotFoundException e) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=uk.co.nickfines.RealCalc"));
// Calculator not installed
} //calculator.num=n;
catch (Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
EasyPhysActivity.error = sw.toString();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
// User pressed OK.
double value = data.getDoubleExtra("X", Double.NaN);
if (Double.isNaN(value)) {
// Calculation result was "Error".
} else {
// Calculation result ok.
}
} else {
// User pressed cancel or back button.
}
}
});
}
//----------------------------------------
But it doesn't like these three lines:
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
If I delete #Override it becomes better, but it still shows an error for
super.onActivityResult(requestCode, resultCode, data);
What is going wrong in here?
You cannot override onActivityResult inside OnClickListener because it does not exist in the base class. Move your onActivityResult code so that it is inside your Activity class, not the OnClickListner.
hey guys im trying to create a quiz, but im having trouble in the intent,after the user will clicked the button, it will automatically intent to another activity.. how can i do that?
please help guys...thanks in advance..
this is the code:
if(btn1.isClickable())
{
img1.setVisibility(View.VISIBLE);
img1.setImageResource(R.drawable.check);
img2.setVisibility(View.INVISIBLE);
img2.setImageResource(R.drawable.wrong);
img3.setVisibility(View.INVISIBLE);
img3.setImageResource(R.drawable.wrong);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Scorecount++;
Intent i = getIntent();
startActivity(i);
Scorecount = i.getIntExtra("score",0);
}
});
}
else if (btn2.isClickable())
{
img2.setVisibility(View.VISIBLE);
img2.setImageResource(R.drawable.wrong);
img1.setVisibility(View.INVISIBLE);
img1.setImageResource(R.drawable.wrong);
img3.setVisibility(View.INVISIBLE);
img3.setImageResource(R.drawable.wrong);
}
else if (btn3.isClickable())
{
img3.setVisibility(View.VISIBLE);
img3.setImageResource(R.drawable.wrong);
img2.setVisibility(View.INVISIBLE);
img2.setImageResource(R.drawable.wrong);
img1.setVisibility(View.INVISIBLE);
img1.setImageResource(R.drawable.wrong);
}
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = getIntent();
startActivity(i);
Scorecount = i.getIntExtra("score",0);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = getIntent();
startActivity(i);
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = getIntent();
startActivity(i);
}
});
}
}
first activity
Intent i= new Intent(CurrentActivity.this,
NextActivity.class);
i.putExtra("quiz", array[0]);
i.putExtra("pass_value","Pass value");
startActivity(i);
second activity
String getvalue= getIntent().getSerializableExtra("pass_value").toString();
I'm not sure that I fully understand what you want, but you can add one intent to another one because intent implements parceable.You can do:
intent.putExtra("INTENT_KEY", someOtherIntent);
and then:
intent = getIntent();
Intent someOtherIntent = (Intent)intent.getParcelableExtra("INTENT_KEY");
Does it help?
P.S. Your code is very strange... (I mean wrong and senseless sometimes)
In order to pass the parameters you create new intent and put a parameter map:
Intent myIntent = new Intent(this, NewActivityClassName.class);
myIntent.putExtra("firstKeyName","FirstKeyValue");
myIntent.putExtra("secondKeyName","SecondKeyValue");
startActivity(myIntent);
In order to get the parameters values you must call the get[type]Extra() on the same intent:
Intent myIntent= getIntent(); // gets the previously created intent
String firstKeyName = intent.getStringExtra("firstKeyName"); // will return "FirstKeyValue"
String firstKeyName = intent.getStringExtra("firstKeyName"); // will return "SecondKeyValue"
If your parameters are ints you would use getIntExtra() instead etc.
Now you can use your parameters like you normally would.