public class AndroidTranslate extends Activity {
EditText MyInputText;
Button MyTranslateButton;
TextView MyOutputText;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyInputText = (EditText)findViewById(R.id.InputText);
MyTranslateButton = (Button)findViewById(R.id.TranslateButton);
MyOutputText = (TextView)findViewById(R.id.OutputText);
MyTranslateButton.setOnClickListener(MyTranslateButtonOnClickListener);
}
private Button.OnClickListener MyTranslateButtonOnClickListener
= new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String InputString;
String OutputString = null;
InputString = MyInputText.getText().toString();
try {
GoogleAPI.setHttpReferrer("http:\\www.google.com");
GoogleAPI.setKey(" API KEY");
OutputString = Translate.DEFAULT.execute(InputString,Language.ENGLISH, Language.HINDI);
} catch (Exception ex) {
ex.printStackTrace();
OutputString = "Error";
}
Typeface customF = Typeface.createFromAsset(getAssets(), "akshar.ttf");
//final TextView textV = (TextView) findViewById(...);
MyOutputText.setTypeface(customF);
MyOutputText.setText(OutputString);
}
};
}
this code is running but not showing proper output like
if my input is "aap kaise ho" it gives output = "आप कैसे हो "
BUT
wen i give input only "a" or "abc" then output should be "अ" or "अबक" but it is not showing output like this. for "a" it shows "एक"
can ane one please help to solve this issue
Thanks
It doesn't seem to be a Android or technical problem but rather the way google translate 'a':
http://translate.google.com/?hl=nl&tab=wT#en|hi|a
I think you are using translation API while trying to achieve transliteration in your code.
It won't show "अ" for "a" because in hindi "a" means "एक". The only way is to achieve this is multiple translation of a word but Right now there is no multiple translation support for a word in google translate api. You can achieve this by a web service which is currently used by google translate in web interface.
http://translate.google.com/translate_a/t?client=t&text=a&hl=en&sl=en&tl=hi&multires=1&otf=2&pc=0&sc=1
You may not receive desired output from this.
Related
I have quite a simple application. However, after I clik on the button, app crashes. Tried to debug it and the problem seems to be in first 3 row of the onClick method. Once I tried to get there values manually, not via those edit boxes, everything went smoothly. Any ideas please?
public class MainActivity extends AppCompatActivity {
EditText editText_pocetKM;
EditText editText_spotreba;
EditText editText_cenaPHM;
TextView textView_spotrebaO;
TextView textView_cenaO;
DecimalFormat df = new DecimalFormat("0.00##");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText_pocetKM = (EditText) findViewById(R.id.editText1_pocetKM);
editText_spotreba = (EditText) findViewById(R.id.editText_Spotreba);
editText_cenaPHM = (EditText) findViewById(R.id.editText1_cenaPHM);
textView_spotrebaO = (TextView) findViewById(R.id.textView_spotrebaO);
textView_cenaO = (TextView) findViewById(R.id.textView_cenaO);
}
public void onClick(View v) {
Double pocetKm = Double.parseDouble(editText_pocetKM.getText().toString());
Double spotreba = Double.parseDouble(editText_spotreba.getText().toString());
Double cenaPHM = Double.parseDouble(editText_cenaPHM.getText().toString());
Double spotrebaO = spotreba * pocetKm / 100;
Double cenaO = spotrebaO * cenaPHM;
textView_cenaO.setText("Cena za spotřebované palivo bude "+ df.format(cenaO) + " Kč");
textView_spotrebaO.setText("Celkem bude spotřebováno "+ df.format(spotrebaO) + " litrů paliva");
}
}
You didn't provide the logcat of your crash report. If the logcat was provided we could be certain of your exact problem. But anyway, as you've got rid of your crash by removing the first three lines of your onClick function, I suppose you're setting invalid inputs in your EditText.
You're parsing the text entered in the EditText to double which will fail if the input is not a valid double string. For example, it'll parse 11.01 fine when it'll throw an exception while parsing Hello.
So to check if the application is crashing for a parsing error, you might consider surrounding them with a try/catch block like this.
try {
Double pocetKm = Double.parseDouble(editText_pocetKM.getText().toString());
Double spotreba = Double.parseDouble(editText_spotreba.getText().toString());
Double cenaPHM = Double.parseDouble(editText_cenaPHM.getText().toString());
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "Parsing error", Toast.LENGTH_LONG).show();
}
I am new to Android Studio and just trying to learn new ways to code and I'm stuck right now. I would like to use a String in a findViewById for example.
public void BDate0(String BId, String BHistoryClass) {
bdatum0 = (Button) findViewById(R.id.BId);
bdatum0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent BDatum0 = new Intent(bethistory.this, BHistoryClass.class);
startActivity(BDatum0);
}
});
}
and I would like to call the BDate0 with the strings for example,
BDate0(BId1, BHistory1);
I have 10 buttons and the activity they start would be different every day.
Sorry for bad englihs it's not my native language and thank you for help in advance.
You can use this code to replace the findViewById(..). This should make it possible to use a string as identifier:
String BId = "button1"; // for example
int id = getResources().getIdentifier(BId, "id", getPackageName());
// finds R.id.button1
bdatum0 = (Button) findViewById(id);
I've been working on an Android program for a bit and I'm going crazy here... I'm trying to let the user enter a few numbers that would be put into the middle of a URL.
I need the code to read from a text box.
Here's what I've got so far:
public void browser(View view)
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.website.com/(',R.id.isbn'));
startActivity(browserIntent);
}
Doing this just adds the (',R.id.isbn')); to the URL.
How do I fix this?
Thanks!
The answer Anand posted is a simple solution to your problem. If you want the ISBN to be in the middle of the URL, you can just concatenate it like this:
EditText t = (EditText) findViewById(R.id.isbn);
String isbn = t.getText().toString();
Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.website.com/" + isbn + "/more-text-here"));
startActivity(browserIntent);
Read more about concatenating strings here:
http://www.javatpoint.com/string-concatenation-in-java
Supposing that you are talking about Android EditText field, you could read it's value using editText.getText().toString(). This is an example on how to use it in your application:
#Override
public void onCreate(Bundle savedInstanceState) {
//... init code cutted
Button button = (Button)findViewById(R.id.button);
EditText editText = (EditText)findViewById(R.id.isbn);
button.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
String isbnText = editText.getText().toString());
// other code to open the new activity
}
});
}
Try it in this way.
EditText t= (EditText)findViewById(R.id.isbn);
String st= t.getText().toString();
Uri.parse("http://www.website.com/"+st);
Let your URL be like String url ="http://www.website.com/%1s/somevalue/%2s/etc+%3s";
Call String.format(url,1,2,3);
Hope this will solve your problem
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm doing a word shuffle app on android studio for a class project. I need help understanding how I can get the users input and match it to the correct String answer. I tried a few approaches and have fallen short. I tried using an if(word.equals(userAnswer)) statement but having a hard time understanding it. How can I write the if statement for text input/output to match my answer in android studio?
(Optional question) Also is public void OnClick(View v) a good approach or should I go with something else?
Any help will be greatly appreciated. Thanks in advance!
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private EditText userAnswer;
private TextView answerOutput;
private TextView scrambledWord;
public void OnClick(View v){
scrambledWord = (TextView) findViewById(R.id.scrambledWord);
userAnswer = (EditText) findViewById(R.id.answerInput);
answerOutput = (TextView) findViewById(R.id.answerOutput);
Button button = (Button) v;
String word = "Animals"; // scan for word
ArrayList<Character> chars = new ArrayList<Character>(word.length()); // gets array with length of word
for ( char c : word.toCharArray() ) {
chars.add(c);
}
Collections.shuffle(chars); //shuffles the characters
char[] shuffled = new char[chars.size()];
for ( int i = 0; i < shuffled.length; i++ ) {
shuffled[i] = chars.get(i);
}
String shuffledWord = new String(shuffled);
if (word.equals(userAnswer)){
answerOutput.setText("Correct!!");
} else {
answerOutput.setText("Sorry try again.");
}
}
This will allow you to determine if they are the same
if(word.equalsIgnoreCase(userAnswer.getText().toString())) {
answerOutput.setText("Correct");
}
However, generally speaking you have a much larger problem, unless it's in code somewhere that you aren't showing us.
Somewhere in your activity onCreate/onStart you want to initialize your button with whatever view it might be.
Button checkAnswer = (Button) findViewById(//whatever your id is)
Then you want to set the onClick listener of the button. With the approach that you are using, it would end up needing two things. First this
public class MainActivity extends Activity implements View.OnClickListener {
Then in you need to set the onClick listener to your Button, probably in OnCreate.
checkAnswer.setOnClickListener(this);
Then your onClick would look something like
#Override
public void onClick(View v) {
if (word.equals(userAnswer)){
answerOutput.setText("Correct!!");
}
else {
answerOutput.setText("Sorry try again.");
}
}
The logic for scrambling the word etc, probably wouldn't be in onClick here.
Also, if you have multiple things you want to set click listeners for you would do something like this
#Override
public void onClick(View v) {
switch(v.getId()) {
case(R.id.//whatever): {
//dosomething
break;
}
}
}
Where you can multiple cases for all of the views that you have set the MainActivity to handle.
Edit: Since you updated your code
public class MainActivity extends Activity implements View.OnClickListener {
private EditText userAnswer;
private TextView answerOutput;
private TextView scrambledWord;
private String word;
private String shuffledWord;
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrambledWord = (TextView) findViewById(R.id.scrambledWord);
userAnswer = (EditText) findViewById(R.id.answerInput);
answerOutput = (TextView) findViewById(R.id.answerOutput);
createWord();
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
private void createWord() {
word = "Animals";
ArrayList<Character> chars = new ArrayList<Character>(word.length()); // gets array with length of word
for ( char c : word.toCharArray() ) {
chars.add(c);
}
Collections.shuffle(chars); //shuffles the characters
char[] shuffled = new char[chars.size()];
for ( int i = 0; i < shuffled.length; i++ ) {
shuffled[i] = chars.get(i);
}
shuffledWord = new String(shuffled);
shuffledText.setText(shuffledWord);
}
#Override
public void OnClick(View v){
if (word.equalsIgnoreCase(userAnswer.getText().toString())){
answerOutput.setText("Correct!!");
} else {
answerOutput.setText("Sorry try again.");
}
}
Did you set the onClickListener of the button to your MainActivity?
Your MainActivity should implement OnClickListener too
You need to use userAnswer.getText() to get the answer. Your userAnswer variable currently is of type EditText, which means a check to see if word.equals(userAnswer) will always return false, as they are of different types. Instead, try word.equals(userAnswer.getText()) to check if their answer equals the original word. To check if their answer equals the scrambled word, use shuffledWord.equals(userAnswer.getText()).
My code seems to run but it does not display the result any result on text view am guessing is because of the way I set my code. The code is below Somebody please help me. Thanks
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
Button button = (Button) findViewById(R.id.but);
input = (EditText) findViewById(R.id.editTextj);
display = (TextView) findViewById(R.id.textView8);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CCActivity3 fs = new CCActivity3();
fs.fileReader();
}// button
});// button end
}
public void fileReader() {
try {
InputStream is=this.getResources().openRawResource(R.raw.file);
BufferedReader bc = new BufferedReader(new InputStreamReader(is));
String cLine;
String inputText = "";
List<String> test2 = new ArrayList<String>();
// read file line by line
while ((cLine = bc.readLine()) != null) {
inputText = inputText + cLine + "\n";
}
s = input.getText().toString();
test = CCActivity3.getPermutation(s);//Permutation method
test2.retainAll(test);//intersection
String fg = "";
for (String s2 : test2) {
fg += s2 + "\n";
}
display.setText(fg);
bc.close();
} catch (Exception e) {// catch any errors if necessary
display.setText(e.getMessage());
}
}
If you check the resource line am very sure am not getting that right and also the formating of the code I believe they just scattered . Hint the file.txt on the res/raw path has more than 100,000 strings/words, could this be the cause.Thanks Again
Regarding your code:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CCActivity3 fs = new CCActivity3();
fs.fileReader();
}// button
});// button end
Is CCActivity3 derived from an Activity?
If so, then you shouldn't construct it this way.
You can't instantiate your own Activity class. Only the Android framework can do that.
What seems to be happening here is that inside fileReader(), you are calling getResources() on an Activity whose Context has not yet been properly initialized by onCreate().
In any case, one thing you can do is call fileReader() method directly without instantiating CCActivity3, like so:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
fileReader();
}// button
});// button end
test = CCActivity3.getPermutation(s);//Permutation method
test2.retainAll(test);//intersection
Above codes can get the right return?