I want to retrieve the button's text/label programmatically. the button's text/label is a string associated with a specific id of a specific view. I attempted to do so using the below posted code, but the toast displays false
javaCode:
protected String getBtnCurrentText() {
// TODO Auto-generated method stub
String btnText = getResources().getString(R.id.fixNowBtnID);
Toast.makeText(getApplicationContext(), btnText, Toast.LENGTH_LONG).show();
return btnText;
}
xml:
<Button
android:id="#+id/fixNowBtnID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:enabled="false"
android:text="#string/fixNowBtnLabel"
android:src="#drawable/gpsfix">
</Button>
string_file:
<string name="fixNowBtnLabel">Fix Now</string>
Retrieve button text using getText() method as below
Button btn = (Button) findViewById(R.id.fixNowBtnID);
String btnText = btn.getText();
instead as
String btnText = getResources().getString(R.id.fixNowBtnID);
OR
If you want to retrieve the text from strings.xml file then use pass string resource id R.string.fixNowBtnLabel instead of button id R.id.fixNowBtnID through getString() method
String btnText = getResources().getString(R.string.fixNowBtnLabel);
btnName.getText() is simply correct solution!
Related
I have one textview and one button ,i coud change textview text with below code :
final Textview c_tv_matn;
Button c_btn_dokme;
c_btn_dokme = (button) findviewbyid(R.id.btn1);
c_tv_matn = (Textview) findviewbyid(R.id.txt1);
c_btn_dokme.setonclickListener(new OnclickListener() {
#Override
public void onClick(View v) {
c_tv_matn.SetText("this is second text");
});
But i wanna change text from String.xml and make Next Button Like this ;
"matn_1","matn_2"matn_3"matn_4...
STRING.XML
<string name="matn_0">Hello world!</string>
<string name="matn_1">You are hero john</string>
<string name="matn_2">you can change this world</string>
<string name="matn_3">You are so clever</string>
cAN YOU HELP ME TO GET RES FROM STRING AND CHANG TEXTVIEW TEXT WITH NUMBERS?
cAN YOU HELP ME TO GET RES FROM STRING AND CHANG TEXTVIEW TEXT WITH NUMBERS?
You can use either getString(int) or getText(int) to retrieve a string. getText(int) retains any rich text styling applied to the string.
But in this case you'll use getString(int) that returns the string value associated with a particular resource ID. It will be stripped of any styled text information.
Sometimes you'll need a context like on this case that you want to do it inside a Button then you can get the context from your View, or if you have a global context in your Activity/Fragment you can use it also.
Example
If I did not misunderstood, what you want is to put the text from Strings.xml to TextView, so you can do it like this :
final Textview c_tv_matn;
Button c_btn_dokme;
int textNumber = 1;
c_btn_dokme = (button) findviewbyid(R.id.btn1);
c_tv_matn = (Textview) findviewbyid(R.id.txt1);
c_btn_dokme.setonclickListener(new OnclickListener() {
#Override
public void onClick(View v) {
switch(textNumber){
case 1:
c_tv_matn.setText(v.getContext().getString(R.string.matn_1))
textNumber++;
break;
case 2:
c_tv_matn.setText(v.getContext().getString(R.string.matn_2))
textNumber++;
break;
case 3:
c_tv_matn.setText(v.getContext().getString(R.string.matn_3))
textNumber++;
break;
default:
textNumber = 1;
c_tv_matn.setText(v.getContext().getString(R.string.matn_1)
break;
});
To get the String from string.xml you need a Context.
An Activity is a Context so, if you are in an Activity you can just call getString(R.string.<the name in string.xml>) to retrieve the String you need.
For example getString(R.string.matn_0).
Then it can be applied to your needs:
c_tv_matn.SetText(getString(R.string.matn_0));
If you are not in an Activity then you need to get hold of a Context, probably passing it in as a parameter to the constructor of the class (and store it as an attribute) or as a parameter to the method that will do the setting of the text.
Create an array in an Android XML file
you need an array in string.xml. For reference please have look on below link
https://www.homeandlearn.co.uk/android/grid_view_array.html
I'm new in android world and I have a problem, well, I'm making a project very simple it is about an activity where i have a button and an EditText. The button has an event onClick in XML.
My problem is it: I need the button value and send this value to
EditText but my button don't have a id. Help me I don't know how manipulate a element if it dont have a id.
XML Code:
<View
android:layout_height="#dimen/cell_height"
android:background="#color/red"/>
<Button
android:layout_marginLeft="#dimen/button_margin"
android:layout_gravity="center_vertical"
android:text="#string/hex_red"
android:textColor="#color/red"
android:onClick="copy"/>`
Java code:
public void copy(View boton){
EditText txtSelected = (EditText)this.findViewById(R.id.txtColor)
String color = boton; <-- here need the button value
txtSelected.setText(color);
}
I need your help, Thanks
you can say boton.getText().toString()
Modify your copy() function like this:
public void copy(View boton) {
EditText txtSelected = (EditText)this.findViewById(R.id.txtColor)
Button btn = (Button) boton; // << key point.
String color = btn.getText().toString();
txtSelected.setText(color);
}`
public void onClickBtn(View view) {
EditText txtSelected = (EditText)this.findViewById(R.id.txtColor)
Button btn = (Button)(view);
String value = (String) btn.getText();
txtSelected.setText(value);
txtSelected.setSelection(value.length()); // cursor will be at the end of the text
}
Hell i am trying to compare textview value with button how can i do it?
i tried this way but not working.
button xml - android:tag = "1"
Java
Button btn = (Button) findViewById(id);
string valu = btn.getTag();
string txt = textView.getText();
if(txt.equals(valu))
{
// do what you want
}
try this:
textView.getText().toString();
getText() returns a CharSequence not a String.
Because of that your equals must be false.
Simply use this.
if (b.getTag().toString().equalsIgnoreCase(tv.getText().toString())) {
Toast.makeText(getApplicationContext(), "HI", 9000).show();
}
I have text view and I want to set-text as link to direct the site but
the problem is that I cannot click:
Grades = (TextView) findViewById(R.id.textView9);
Grades.setText(Html.fromHtml(course.getString(TAG_Grade)));
Grades.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
WebView webView; webView.setWebViewClient(new WebViewClient()
{
}webView.loadUrl(course.getString(TAG_Grade)); });
And the xml:
<TextView
android:id="#+id/textView9"
android:layout_marginLeft="110dp"
android:layout_marginTop="365dp"
android:textColor="#000"
android:textSize="14sp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:autoLink="web"
android:onClick="onClick"
/>
Knowing that course.getString(TAG_Grade) will get the url from db but it does not work
What is the problem?
You are trying to Linkify it after the view is clicked, removed the Linkify from within the onClick.
Your going about it the wrong way.
Try this:
String userCanSeeThis = "Your Website Name";
String url = course.getString(TAG_Grade);
TextView grades = (TextView) findViewById(R.id.textView9);
grades.setText(userCanSeeThis);
addLinks(Grades, userCanSeeThis, url);
Using this helper method:
/**
* #param textView
* textView who's text you want to change
* #param linkThis
* a regex of what text to turn into a link
* #param toThis
* the url you want to send them to
*/
public static void addLinks(TextView textView, String linkThis, String toThis) {
Pattern pattern = Pattern.compile(linkThis);
String scheme = toThis;
android.text.util.Linkify.addLinks(textView, pattern, scheme, new MatchFilter() {
#Override
public boolean acceptMatch(CharSequence s, int start, int end) {
return true;
}
}, new TransformFilter() {
#Override
public String transformUrl(Matcher match, String url) {
return "";
}
});
}
Also if you set the onClickListener in your code with grades.setOnClickListener then your don't need android:onClick="" in your XML
Used this android:clickable="true"
<TextView
android:id="#+id/textView9"
android:layout_marginLeft="110dp"
android:layout_marginTop="365dp"
android:textColor="#000"
android:textSize="14sp"
android:clickable="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:autoLink="web"
android:onClick="onClick"
/>
Instead of using Linkify i would prefer the below line:
Html.fromHtml(course.getString(TAG_Grade));
TextView myWebSite = new TextView(this);
myWebSite .setText("http://www.google.com/");
Linkify.addLinks(myWebSite , Linkify.WEB_URLS);
You have received enough answers regarding Linkify, but there is one more subtile error crawling in your code:
You are mistaking the attribute his android:onClick with the method onClick of an View.onClickListener:
The attribute android:onClick works as followed:
Name of the method in this View's context to invoke when the view is
clicked. This name must correspond to a public method that takes
exactly one parameter of type View. For instance, if you specify
android:onClick="sayHello", you must declare a public void
sayHello(View v) method of your context (typically, your Activity).
As for the onClick-method provided by the View.onClickListener-interface:
view.setOnClickListener(...)
Register a callback to be invoked when this view is
clicked. If this view is not clickable, it becomes clickable.
Which will allow you to override the function :
public abstract void onClick (View v)
which is called when a view has been clicked.
I want to get the user input for the EditText view and display it on the screen through TextView when the Button is clicked. I also, want to know what modifications can be done on the string.xml file to do this.
I didn't get the second question, maybe you can elaborate...but for your first query.
String content = edtEditText.getText().toString(); //gets you the contents of edit text
tvTextView.setText(content); //displays it in a textview..
I'm just beginner to help you for getting edittext value to textview. Try out this code -
EditText edit = (EditText)findViewById(R.id.editext1);
TextView tview = (TextView)findViewById(R.id.textview1);
String result = edit.getText().toString();
tview.setText(result);
This will get the text which is in EditText Hope this helps you.
EditText ein=(EditText)findViewById(R.id.edittext1);
TextView t=new TextView(this);
t.setText("Your Text is="+ein.getText());
setContentView(t);
bb.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
String s1=tt.getText().toString();
tv.setText(s1);
}
}
);
First get the text from edit text view
edittext.getText().toString()
and Store the obtained text in a string, say value.
value = edittext.getText().toString()
Then set value as the text for textview.
textview.setText(value)
yesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
eiteText=(EditText)findViewById(R.id.nameET);
String result=eiteText.getText().toString();
Log.d("TAG",result);
}
});
Easiest way to get text from the user:
EditText Variable1 = findViewById(R.id.enter_name);
String Variable2 = Variable1.getText().toString();
in "String.xml" you can notice any String or value you want to use, here are two examples:
<string name="app_name">My Calculator App
</string>
<color name="color_menu_home">#ffcccccc</color>
Used for the layout.xml: android:text="#string/app_name"
The advantage: you can use them as often you want, you only need to link them in your Layout-xml, and you can change the String-Content easily in the strings.xml, without searching in your source-code for the right position.
Important for changing language, you only need to replace the strings.xml - file
Use the following code when clicked on the button :
String value = edittext.getText().toString().trim(); //get text from editText
textView.setText(value); //setText in a textview
Hope to be useful to you.
Try this->
EditText text = (EditText) findViewById(R.id.text_input);
Editable name = text.getText();
Editable is the return data type of getText() method it will handle both
string and integer values
First get the value from edit text in a String variable
String value = edttxt.getText().toString();
Then set that value to textView
txtview.setText(value);
Where edttxt refers to edit text field in XML file
and txtview refers to textfield in XML file to show the value