Android Spinner to give text answer displayed - android

I currently have a spinner giving the same result as the spinner selection. For example if I selected the word "Blue" from the spinner drop down the word "Blue" would be displayed in a text box below the spinner.
How would I go about displaying a different text result than the spinner selection name. For example, if I select "Blue" from the spinner I get a different paragraph of text shown below in the text box, which corresponds to my selection?
Any help would be appreciated as I am new to this.

You need to set a listener on your spinner. Here is an example:
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String s = (String) mySpinner.getSelectedItem();
if (s.equals("Blue")){
myTextView.setText("GIANT PARAGRAPH");
}//else ifs and so on...
}
public void onNothingSelected(AdapterView<?> arg0) {}
});

Well all you need to do is set a conditional statement. Right now you're saying if blue is selected set the text for (yourtextviewname) to "blue". All you have to do is instead say, if blue is selected set the text to "whatever you want". Here is an example. This is not exact code, just an example of what you might want to try.
if (spinner.equals("blue")) {
TextView tv = (TextView) findViewById(R.id.textview1);
tv.setText("Whatever I want to type!");
}
You can also set the TextView by a String resource. the only difference is you would put
tv.setText(R.string.something);

Related

Moving cursor to the next field

How do I move the cursor to the next line? I have two spinners, then a title edit text box and then two edit text boxes in which I am setting the date using a date picker. When setting the date in the "To"-field, the cursor is moved back to the Title field. After selecting the date I want it move to the next field which is "Send By", but it keeps returning to the previous field which is title field. Can anybody help?
android:nextFocusDown="#+id/.."
android:nextFocusLeft="#+id/.."
android:nextFocusRight="#+id/.."
android:nextFocusUp="#+id/.."
or
Spinner spinner=(Spinner)findViewById(R.id.SpinnerGender);
spinner.setFocusable(true);
spinner.setFocusableInTouchMode(true);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(flag == false)
flag = true;
else
edtTitle.requestFocus();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
You can try to call requestFocus() on the corresponding EditText in the DatePicker.OnDateChangedListener method.
Kindly add this line to xml file Spinner/EditText Control..
android:imeOptions="actionNext"
or
android:imeOptions="actionNext"
android:singleLine="true"
Hope, this might help you..

Select text from group of views in listview

Is it somehow possible to achieve that?
In example: we have listView with 20 items, every item has some text. User want to select half of ending text from item 1. and the half of another item text (same behaviour like in webView or selectable textView). Did someone think about that feature? Where should I search the solution?
This topic will be updated when solution will be found.
ps. I know you will say "show us code first". I do not have it yet.
In your istview on item click listener code like this
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3)
{
// TODO Auto-generated method stub
TextView tv;
String text_from_tv, finalText;
tv= (TextView) view.findViewById(R.id.textview_id);
text_from_tv = tv.getText();
if(!firstPartSelected)
finalText += text_from_tv.substring(0,text_from_tv.length()/2);
else
finalText += text_from_tv.substring(text_from_tv.length()/2,text_from_tv.length());
//Save this finalText String to any string array and use those values
}
});
and that string array contains the second halfs of the selected word, if it helpful up vote the answer!!
In your list_item.xml for your list view. You will want to set,android:textIsSelectable="true" and make android:clickable="true" for that same item.
I won't give any code, just how I would do it :
boolean firstPartSelected false;
String finalText ="";
// ListView Definition
// OnItemClickListener
OnItemClick(...){
if(!firstPartSelected)
finalText += TextFromItem.substring(0,TextFromItem.length()/2)
else
finalText += TextFromItem.substring(TextFromItem.length()/2,TextFromItem.length())
}
This is not some real code, just an idea of how to implement it. Is that what you wanted ?
Try handle OnFocusChangeListener for your EditText, when focus will be change, color selected text in EditText using spans (Android: Coloring part of a string using TextView.setText()?). If you have large count of EditText you can use view.setTag(etNumber) and (Integer)view.getTag() for each EditText and use this information while concat output string in loop (look for more info Android - get children inside a View?)
P.S. EditText is inheritor of TextView, what yo can do with TextView you will can do with EditText

I have 200 textviews and i want to know which one was pressed then how to change the text

I have a problem... have been thinking about it for a while now and been looking on line and still haven't come up with a clear explanation...
I have a number of textviews and have set onClickListeners to each of them.. and when the user clicks on one of them I want them to have the ability to change the text to another set of string array options which I have created progammatically. When the user selects an option the text should change to the option they choose. (I.e. TextView was A now it is B. hope this makes sense.. anyway... )
The current solution was to set a OnClickListener to every TextView and when someone pressed it an individual dialog showed. But I found that if I do this the code would be so long it would take an eternity to code so am hoping someone has a more elegant way of coding such a long process =(
So I guess my question would be... 1) is there a way I can find out which text view was pressed and then change the text of that TextView being pressed within a single method? to save me having to code 1000 alert dialogs...
http://i.stack.imgur.com/LRJGz.png
I would advise you to use a grid view.
You can see which textview was pressed like this:
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
//get id
switch (v.getId()) {
case R.id.textView1: ...
}
});
One of the ways to do what you want is to use the text view setTag() and getTag() methods.
On init of a text view use the setTag() to set some value to identify the view.
In the on click event use the getTag() on the view argument to know which view was clicked.
I would suggest holding the textviews in an array, like so:
TextView[] textViewArray = new TextView[textViewCount];
Then using a for loop assign each one a tag of integer - it's position
textViewArray.setTag(i)
And add an onClickListener to each one, again using a for loop:
textviewArray[i].setOnClickListener(etc...)
Then when one is clicked, you can use get the position of view that was clicked. This will require a custom method inside of your:
textviewArray.setOnClickListener(new customOnClickListener())
Where your customOnClickListner is like this:
private class customOnClickListener implements CompoundButton.{
public void OnClick(View view){
int position = (Integer) view.getTag()
///Do more code here - your processing
}
}
Hope that makes sense :)
For your for loops, you could use for(i = 0, i
Use set id for all text, where set the id positive integer(distinct), and then have one on view click listener(set it all) where u catch all text view clicks(downcast view with textview) and in side it put a switch case where you handle clicks on which text view is clicked.
You have to set "onClickListner" on all of of your textview.
For Saving some length of code i would suggest you create a function of your dialogbox, and give some int parameter to it, which would be directly called by the clickListener of textview,
Like ,
int i=0;
......
textView1 = (TextView)findViewById(R.id.yourtextview1);
textView2 = (TextView)findViewById(R.id.yourtextview2);
......
......
// and so on, for your all textviews
#Override
public void onClick(View view) {
if (view.equals(textView1)) {
i = 1;
CustomDialog(i);
}
//Similarly for all your textViews..
..........
Make A function CustomDialog Like
public void CustomDialog(int i){
if(i==1){
//Do something
}
}

how to change the font color of an item in a gridview (android)

I have a gridview of letters for a wordsearch and I'm trying to get it so when you click on a letter it changes colour (which is working fine). But I also want to be able to change the font color back if the word is not correct.
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Toast.makeText(TodaysPuzzleClass.this, "" + position, Toast.LENGTH_SHORT).show();
((TextView) v).setTextColor(Color.BLUE);
}
This is the code so when I click on an item it changes color.
Using the coordinates from my toast is it possble to change the items color that way?
looking aroung i need to use the postion if the item then changes its color, but how do i do that?
Try this one,
((TextView) v).setTextColor(Color.parseColor("#000000"));

setHintTextColor() in EditText

I have View in which there are two text boxes, and the user can select text color from another view on the same screen (through dialog box).
So when the user changes color via dialog box, I am changing color of EditText text and its hint. But when there is some text is available in EditText after that user selects other color, then that text is coming in that color. But if I remove all that text then the color of HintText is that of the previous color.
For example, currently if I have red color in text box and the user selects green color so text is there in green color. But if I remove that text then hint text are coming in red even if I change hint color in code. This problem only comes when there is some text there. if it is blank and hint text is there then problem is not coming.
Simply add this in your layout for the EditText :
android:textColorHint="#FFFFFF"
Use this to change the hint color. -
editText.setHintTextColor(getResources().getColor(R.color.white));
Solution for your problem -
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3){
//do something
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
//do something
}
#Override
public void afterTextChanged(Editable arg0) {
if(arg0.toString().length() <= 0) //check if length is equal to zero
tv.setHintTextColor(getResources().getColor(R.color.white));
}
});
Default Colors:
android:textColorHint="#android:color/holo_blue_dark"
For Color code:
android:textColorHint="#33b5e5"
Inside Layout Xml File We can Change Color of Hint.....
android:textColorHint="#android:color/*****"
you can replace * with color or color code.
Seems that EditText apply the hintTextColor only if the text is empty. So simple solution will be like this
Editable text = mEditText.getText();
mEditText.setText(null);
mEditText.setHintTextColor(color);
mEditText.setText(text);
If you have multiple fields, you can extend the EditText and write a method which executes this logic and use that method instead.
Programmatically in Java - At least API v14+
exampleEditText.setHintTextColor(getResources().getColor(R.color.your_color));
This is like default hint color, worked for me:
editText.setHintTextColor(Color.GRAY);
You could call editText.invalidate() after you reset the hint color. That could resolve your issue. Actually the SDK update the color in the same way.

Categories

Resources