How to output text result in Android? - java

In an android website, I found an article about a widget similar to a drop-down list for selecting items. (Following is the link to the site; and it shows all the codes).
http://developer.android.com/resources/tutorials/views/hello-spinner.html
It uses the following code to display a message once you have selected a planet.
Toast.makeText(parent.getContext(), "Planet is Selected", Toast.LENGTH_LONG).show();
But this message "Planet is Selected" is only going to display for about 3 seconds and then it disappears. Can you please tell me how can I make the message stay on the screen for a longer time. Or how can I output the "Planet is Selected" message as a text layout in to the screen(So that it will stay on the screen permanently till I select another item from the list)? How can I use addView(tv) instead of setContentView(tv) Any help would be greatly appreciated.
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if (parent.getItemAtPosition(pos).toString().equals("Mars"))
{ TextView tv = new TextView(HelloSpinner.this);
tv.setText(parent.getItemAtPosition(pos).toString() + "Planet is Selected");
setContentView(tv); //How can I use addView(tv); here?
//Toast.makeText(parent.getContext(), "Planet Selected", Toast.LENGTH_LONG).show();
}
}
public void onNothingSelected(AdapterView parent)
{
// Do nothing.
} }

If you want it to stay permanently on the screen, why not use a TextView and set your value to that instead of a Toast.
If you have any problems with not being able to use TextView, ie undefined. You should take a look at the textview documentation, as it is very well described there.

public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
TextView tv = new TextView(this);
tv.setText(parent.getItemAtPosition(pos).toString());
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}

third parameter Toast.LENGTH_LONG is time. so you can set any integer value ( not sure second or millisecond) ,. then on specific event call toast.hide() ;
toast is good choice for show message for some times only . so use textView if possible
create TextView with activity context :
TextView tv = new TextView(ActrivityName.this)
else if not an activity
TextView tv = new TextView(parent.getContext())

Related

display the word in the selected spinner in the textview

I try to explain by showing as many examples as possible, I chose a word from the spinner, for example it is "cat", after I select it in the spinner i.e. after I click it should also appear in the textview.
Spinner click "cat"
TextView change "cat"
I again click Spinner "dog"
TextView change "dog"
now my not full code:
```Spinner sababspinner = findViewById(R.id.Sababspinner);
sababspinner.setOnItemClickListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}```
all spinner texts in string.xml
Make an ArrayList with your spinner dataset, then in the onItemSelected method write something like textView.setText(dataset.get(position)).

Unable to select items from the Spinner in Android Studio

I am new to Android Studio, so I don't know much about the functions. I am using a spinner which allows me to choose from different languages.
In my MainActivity class, I have the following code:
String lang="eng";
dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object language = parent.getItemAtPosition(pos);
TextView L = (TextView) findViewById(R.id.spinLang);
//for deciding tessdata file
if(language.toString().equals("Bengali"))
lang="ben";
if(language.toString().equals("English"))
lang="eng";
if(language.toString().equals("Gujarati"))
lang="guj";
if(language.toString().equals("Hindi"))
lang="hin";
if(language.toString().equals("Kannada"))
lang="kan";
if(language.toString().equals("Tamil"))
lang="tam";
L.setText("You selected " + language + " and " + lang);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
My text view shows the display depending on what language I choose from the spinner. However, I believe the change in variable <lang> is limited to only this method.
Outside this method, when I am working with <lang>, it gives the result for <lang="eng"> all of the time.
How can I make the change in <lang> outside this method? Thanks in advance.

Two spinners change text in TextView

I'm trying to make this android app and I have some problems. I want to pick a card number in my left spinner, and in the right spinner is my dealers hand, and I pick a card number there aswell.(see Image below)
I can change my cards and the image updates no problem. But when I pick ex 17 and 2, how do I change the text in my Textview? I have a TextView, but cleared the text.
I tried this, but it doesn't work
if(plHand.equals("8") && dlHand.equals("2") || dlHand.equals("3")){
Toast.makeText(this, "IT WORKS!", Toast.LENGTH_LONG).show();
}
I made a Toast just to check if something happens. But nothing happens. But how do I do it? plHand and dlHand is a String Array. I also tried this
if(sp1.getSelectedItem().toString().equals("17") && (sp2).getSelectedItem().toString().equals("Ace"){
Toast.makeText(MainActivity.this, "IT WORKS!", Toast.LENGTH_LONG).show();
}
Hope you guys know what to do!
EDIT: added more code
final String[] plHand = getResources().getStringArray(R.array.yourHand_array);
final String[] dlHand = getResources().getStringArray(R.array.dealerHand_array);
final Spinner sp1 = (Spinner) findViewById(R.id.spinner1);
final Spinner sp2 = (Spinner) findViewById(R.id.spinner2);
The text I want to change is in a String Array aswell, but I don't have to use the String Array, if there is an easier way!
EDIT!!: It works now! Full code is here! http://pastebin.com/g3M2wbtL
Just attach the same OnItemSelectedListener to both spinners like the following:
AdapterView.OnItemSelectedListener listener = new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String plHandText = sp1.getSelectedItem().toString();
String dlHandText = sp2.getSelectedItem().toString();
if ("17".equals(plHandText) && "2".equals(dlHandText)){
Toast.makeText(MyActivity.this, "It works!", Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
sp1.setOnItemSelectedListener(listener);
sp2.setOnItemSelectedListener(listener);

Android gridview change background by position

I have Gridview with images. above the image I have text(hello) and I can to show images(without text) to my gridview .now I want if I click gridview's 5th element I want to hide 5th image's text.meybe problem solution is gridview.setOnItemClickListener but I do not know how I can do this
I wrote some code witch can to show toast message by position, but I do not know how I can to hide text by position
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getApplicationContext(), "Cliked" + position,
Toast.LENGTH_SHORT).show();
}
});
You could try setVisibility along with an array to sort through the GUI identifiers:
int textId = this.getResources().getIdentifier(position, "id", this.getPackageName());
TextView picText = (TextView) findViewById (R.id.TextID);
picText.setVisibility(TextView.INVISIBLE);
Or something like that. INVISIBLE makes it invisible, but take up the same space - GONE will make the space disappear too - so depending on what you want.
That should at least get you in the right direction.
For more: http://developer.android.com/reference/android/view/View.html#setVisibility(int)
First Find the textView that you want INVISIBLE.
Then set it INVISIBLE.
By below Code
((TextView) v).setVisibility(TextView.INVISIBLE);
if above Line dont work use below
TextView textview=(TextView)v.findViewById(R.id.id_of_textview);
textview.setVisibility(TextView.INVISIBLE);

Android onClickListener strange behavior

i'm trying to set a child click listener on my ExpandedListView, and i'm getting some strange results:
theList.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, int childPosition, long id) {
Log.i(LOG_TAG, "clicked");
ExpandableListAdapter adapter = parent.getExpandableListAdapter();
View v = (View)adapter.getChild(groupPosition, childPosition);
Toast toast = Toast.makeText(getApplicationContext(),
"item: " + new Integer(groupPosition).toString() + ":" + new Integer(childPosition).toString(),
Toast.LENGTH_SHORT);
toast.show();
if (!((CheckedTextView)view.findViewById(R.id.check)).isChecked()){
((CheckedTextView)view.findViewById(R.id.check)).setChecked(true);
} else {
((CheckedTextView)view.findViewById(R.id.check)).setChecked(false);
}
return true;
}
});
it sort of works. when i check one of the items, however, another, additional item will also be selected, seemingly at random. am i not doin this correctly?
Solved the issue myself. i ended up extending the adapter class and overriding getChildView and getGroupView and added a list of booleans to the adapter to keep up with the checkmarks. probably not the most sophisticated solution, but works perfectly.

Categories

Resources