Emm Jay
Emm Jay

Reputation: 380

Android : Store text in string

I have an activity in which a toast returns the other part of sentence if we put the first part of sentence correctly. I want to get the other part (twinkle twinkle) from the Android resources, but I don't know how.

 final EditText     hetext=(EditText)findViewById(R.id.Text1);        
        Button btn=(Button)findViewById(R.id.Button1);

        btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
        {

        final String edittext= hetext.getText().toString();
                    if(edittext.trim().equals("twinkle twinkle"))
                    {


 // toast ( little star )

                }  

                else
                {


//toast ( the word is not matching) 

                }

Upvotes: 2

Views: 495

Answers (4)

user3664554
user3664554

Reputation:

You can try this instead to load string from resources,

String hello = getResources().getString(R.string.hello);

Upvotes: 1

Daan Olislagers
Daan Olislagers

Reputation: 3253

We misunderstood your question. The awser you are looking for is:

String mystring = getResources().getString(R.string.some_string);

getResources() is a method of the Context class. If you are inside a Activity or a Service (which extend Context) you can use it like in this snippet.

Also note that the whole language dependency can be taken care of by the android framework. Simply create different folders for each language. If english is your default language, just put the english strings into res/values/strings.xml. Then create a new folder values-ru and put the russian strings with identical names into res/values-ru/strings.xml. From this point on android selects the correct one depending on the device locale for you, either when you call getString() or when referencing strings in XML via @string/mystring. The ones from res/values/strings.xml are the fallback ones, if you don't have a folder covering the users locale, this one will be used as default values.

Upvotes: 2

Sethu
Sethu

Reputation: 428

try{
final String edittext= hetext.getText().toString();
//consider.for example you put below string in a edittext.      
String se="sethu is a good boy";

if(se.trim().contains("is"))
{
 se+="twinkle";
 System.out.print(se);
}
}catch(Exception e)
{
e.printStackTrace();
}

Upvotes: 0

RMachnik
RMachnik

Reputation: 3684

Try to change you condition to sth weaker if(edittext.trim().contains("twinkle twinkle")) or make sure that you string is't capitalized or sth, and try to use debugger.

Upvotes: 0

Related Questions