How to show the name of the widget on a TextView? - android

Let me explain:
I need to show the name of any building block either it is imagebutton, edittext in my textview field depending upon which of above written will be hover over by the user.
So that my textview could behave like some dynamic display plate.
Any help will highly be appreciated.
mrana..

So something that you can do. Since there it no "setText" for imageviews, you can do something like
String name = "imageview";
imageView.setTag(name);
Then in your onFocusedChangedListener call the following method
void displayInTextView(View selectedView) {
String viewName = (String) selectedView.getTag();
mDisplayText.setText(viewName);
}

Since this is a touch device, "hovering" will not be possible. One solution is to show the name on when long-press. See this solution https://stackoverflow.com/a/4433441/1227692
EDI: Thanks Frank and mrana for pointing out. I agree and take back my comment.

Related

Xamarin: Setting text for a TextView programmatically

This is probably a mistake or lack of comprehension on my part, but I am quite confused right now. I'm trying to set a TextView in my Xamarin Android application programmatically. Here's my code:
TextView currentCharacterName =
FindViewById(Resource.Id.characterName);
currentCharacterName.SetText("test");
Unfortunately, this does not work, as I get the error "Argument 1: cannot convert from 'string' to 'int'". After reading in the available methods for SetText, I noticed the method I'm trying to call demands a ResId. I don't really understand why I would need a ResId to modify the text of a TextView.
I tried searching on Google for answers, and I came across this answer from 2014 that had the exact same problem as I do. The solution was to use the Text() method instead to set the TextView. Unfortunately, when I try this solution, I get the error "Non-invocable member 'TextView.Text' cannot be used like a method". When I try to check the Text method description, I see "string TextView {get/set} To be added."
Does this mean there's no implementation yet to set the text of a TextView? I am really reluctant to believe this, as it baffles me that such a big framework like Xamarin wouldn't even have get/set functions for something as simple as setting the text of TextView. I feel like there's a very simple solution for my problem, but I can't seem to find it.
TextView.SetText(X) allows you to set the text from a Resource id:
currentCharacterName.SetText(Resources.Id.MyString);
You are looking for the Text property:
currentCharacterName.Text = "test";
Xamarin: TextView class
Android.Widget.TextView.Text Property
Syntax:
public String Text { get; set; }
Test this code:
TextView currentCharacterName = FindViewById<TextView>(Resource.Id.characterName);
currentCharacterName.Text = "Your Text";

LInkify block onclick event

I have some TextView objects that I have onclick listeners assigned to. The onclick listeners work fine unless I run Linkify.addLinks on the TextView objects, at which point the onclick event never happens. This happens regardless of whether anything in the TextView is actually linkified. Is there a way to have both events happen, or at least be able to predict whether anything is or would be linkified in the text?
I don't know if you found an answer or not as this question is rather old, but I just found it while looking for another problem (not exactly similar) and I can help (a bit): there is an easy way to "detect" whether Linkify modifies the text: just compares it with the original. Something like:
String originalText = textView.getText().toString();
Linkify.addLinks(textView, Linkify.ALL);
String linkifiedText = textView.getText().toString();
if (originalText.equals(linkifiedText)) {
// linkify did not do anything...
}
Linkify.addLinks() returns a boolean which indicates if it found links in the text or not. There is no need to do a before/after comparison like Guillaume suggested.

Setting the score on textview

I am trying to set the score in a game but it isn't doing anything at the moment.
I have declared a score variable:
static int score = 0;
When an answer is shown in another textview as "Correct" I want the score to increment and show this in another textfield which will be for the score.
So far I have tried this:
public void score(){
check.getText();
if(check.equals("Correct")){
score++;
Score.setText(String.valueOf(score));
}
check is a Textfield which shows Correct or Incorrect. Score is another textfield.
and then I put the method score() into an onclick, but this doesn't update the textfield.
I would appreciate any advice on this.
Thanks
Edit:
check.equals("Correct")
should be
check.getText().equals("Correct")
You don't heed the check.getText() method call that you have by itself. And you need to use this for your if statement
if(check.getText().toString().equals("correct))
getText returns the object type Editable which is not a String. If you are getting a force close please edit you question to include the stack trace from the LogCat output.

TextView.setText() followed by TextView.invalidate() doesn't update text in view

I have no idea why this doesn't work. The TextView is defined from an tag in the view. The base TextView doesn't have text set and I want to set it in the View on display.
I have tried placing the below in onCreate and onStart but it doesn't seem to work. The last two lines are just for debugging. I can verify that the header does get the text. The thing is, the TextView doesn't actually get updated. Any ideas?
TextView header=(TextView) findViewById(R.id.acheader);
header.setText(R.string.accounts);
header.invalidate();
header=(TextView) findViewById(R.id.acheader);
String blah=(String) header.getText();
Try again removing the text in 4th line
header=(TextView) findViewById(R.id.acheader);
header.invalidate() is not needed.
Instead of String blah = (String) header.getText() try
String blah = heager.getText().toString();
And why are you verifying a "setText()" on text view using code? Why can't you check the
actual output?
The above code might not work the way you are trying to use it, because the redraw of text view is handled by the framework and generally it tries to group item updates (Dirty rectangles to be specific) and update them all at once. It may do it well after your function exits, Try to validate visually, thats the best way.

Resource ID question

may I know is it possible I use the resource id but I didnt set the Content View in the activity?
for example: abc.xml have one TextView id = "R.id.Text". Can I use the use TextView name = (TextView)findViewById(R.id.Text); method in the activity which was setContentView(R.layout.def);?
P/S: sorry about my bad english, hope you guys understand what I'm talking about.
Thanks
Short answer: no. The code setContentView(R.layout.def); loads only the views from def.xml, not abc.xml. You have to use another method to create the TextView or include it in def.xml.
For another method to use for separate XML files, check out http://developerlife.com/tutorials/?p=303

Categories

Resources