This question already has answers here:
Dialing a phone call on click of textview in android
(6 answers)
Closed 8 years ago.
I am new to android and:
I have a TextView that can show a phone number.
I want to have that part of the text to be 'touchable' and attempt to make a phone call or something similar.
I have tried to implement implicit intent for when the user clicks the TextView but I can't make it work.
Is there any solution to this? Or a different approach?
Thanks.
First make your TextView clickable by adding below in your layout.xml
<TextView
...
...
android:clickable="true">
</TextView>
And in your java code inside OnClickListener of that particular TextView Start phone activty as
TextView tv=(TextView) findViewById(R.id.tv_contact);
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("tel://"+ tv.getText().toString().trim())));
}
});'
And if you want link on specific text on textview then this post may help you
Android textview with clickable phone number
Edit
As #Apoorv suggested,you may also use android:autoLink = "phone" as
In the xml file, add the below lines.
<TextView
....
android:autoLink = "phone"
/>
For more info see android:autoLink - Have a Clickable Phone Number link in a TextView, in XML
You should use built-in linkify. Read this tutorial, easy to understand: http://android.okhelp.cz/linkify-text-link-url-in-textview-text-android-example/
Related
I want to create a book in android. It's text come from database and every section of this text has a reference.I need to put clickable image inside text to show reference and when I click on image something like Toast which contain reference list will appear.
I didn't see something like that in typical app. any body have any suggestion to implement it ?
If I understand the question correctly, you will have a clickable image inside the activity. Next add a onClickListener to the image, inside of this display the toast with the reference.
ImageView img = (ImageView) findViewById(R.id.img);
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(this, "https://www.google.com/", Toast.LENGTH_SHORT).show();
});
}
If you have multiple clickable images then you might be better off implementing the interface View.OnClickListener and adding the onClick method.
I think the best way is to use Linkify class provided by Android Framework - see http://developer.android.com/reference/android/text/util/Linkify.html for details.
Also you can play with google sample - https://github.com/googlesamples/android-TextLinkify
Hello I'm new to android developing.
Is there a method in java that equals to #.gotFocus?
Is there in java an events list that I can watch and select like in c# visual studio?
I tried to do #.Focus or something similar but had no success.
I want to reproduce the following scheme:
1- EditText has a certain hint => "Enter a value"
2- The user clicks the edit text and the hint disappears => ""
3- The user fills a certain value => "certain value"
Thank's for helpers :)
Ron Yamin, If I understand your doubt correctly what you want is:
1- Have a field of text for the user to type words/numbers etc --> It is called EditText in android
2- Have an hint so the user knows what to type --> Eg. "Type your name"
3- And react to focus in some way.
The first one you will achieve either through XML or by code. If you have a main.xml in your layouts folder (assuming you are using eclipse/android studio to develop), you can use the interface to drag an edit text to the android screen.
The second one you will achieve still through the XML. If you right click on it, right side of the screen there will be a little window called Proprieties that you can change things like height and width and a hint. Type there your hint.
Finally the last one you need to go to your code in .java and get a reference of your edit text (findViewById).
Either through setOnClickListener or setOnFocusChangeListener.
More info you can checkout here:
http://developer.android.com/guide/topics/ui/controls/text.html
I have googled a tutorial you can check with more detailed information and step by step guide.
Hope it helps:
http://examples.javacodegeeks.com/android/core/widget/edittext/android-edittext-example/
It seems that you changed your question quite a bit, and my C# ignorance got the best of me.
It seems that what you really want is an EditText, the example text you are looking for is the hint.
You can set the hint in the xml file or by code with .setHint(string) method.
Here's where to start:http://developer.android.com/guide/topics/ui/controls/text.html
edit 3 - events in android are dealt with by using listeners. You can use an onClickListener to achieve what you want.
textView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(){
//dostuff
}
}
Assuming your textfield is an instance of EditText (which it probably should be), you can do the following:
textfield.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// this is where you would put your equivalent #.gotFocus logic
}
}
});
It's worth noting that the behavior you've described can be achieved by using textfield.setHint. The hint is text that is cleared automatically when the user selects the EditText. It's designed specifically for the case you describe, e.g. textfield.setHint("Enter a Value")
I'm not familiar with c# but I'm guessing you want event fired when edittext get focus. Try this
EditText txtEdit= (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// do the job here when edittext get focus
}
}
});
I am new to android, and I am working on a Quiz app with a set of questions, each question has a question(text) and 4 images(imageButton), when user select the correct image, then they go to next question. So from question to question, should I:
call setContentView() multiple times in one activity? (layout is same for all questions, only the question text and image changes)
or add multiple layout in xml, and at runtime, set visibility of each layout? (but by doing so, I received warnings of too many views in one xml file)
any other better suggestions?
Thanks for your reply!
My suggestion populate the question dynamically...
From your Perspective do like this
1.Take xml with Question text and 4 images.
2. Get the Ids of question text(TextView), and 4 imageview's.
3.When ever the event trigger(i.e next question) change the question text and Answer images..
Try this simple logic
int question=0;
ImageView answer1,answer2,answe3,answer4;
int answerids[]={R.id.answer1,R.id.answer1,R.id.answer1,R.id.answer1};
ImageView[] answerimages={answer1,answer2,answer3,answer4};
String questions[]={"Question1","Question2","...."};
int images[][]={{R.drawable.answer1,R.drawable.answer2,R.drawable.answer3,R.drawable.answer4},{second question drawables},{Third question drawables}...};
In onCreate do like this
TextView question=findViewById(R.id.question);
for(int i=0;i<answerids.length;i++)
{
answerimages[i]=(ImageView)findViewById(answerids[i]);
answerimages[i].setImageResource(images[question][i]);
answerimages[i].setOnclickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
question+=1;
nextqueston();
}
});
To populate next question text and images..
public void nextqueston()
{
question.setText(questions[queston]);
for(int i=0;i<answerids.length;i++)
{
answerimages[i].setImageResource(images[question][i]);
}
}
Just keep the same Activity open, when one question is done, set the question text on the TextView programmatically, same for the ImageViews. All in the same layout.
I'm trying to change my TextView text from the code.
This is what my xml looks like:
XML:
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal" />
And the code:
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Hello");
setContentView(tv1);
I'm getting an error on my device and the application stops.
I tried to show a TextView (not connected to an XML TextView) and it worked.
Your approach is incorrect. I think it will be Null Pointer Exception (next time post log cat)
You need to specify the layout you are using first, before finding views.
Java:
// Specify the layout you are using.
setContentView(R.layout.yourlayout);
// Load and use views afterwards
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Hello");
Kotlin:
// Specify the layout you are using.
setContentView(R.layout.yourlayout)
// Load and use views afterwards
val tv1: TextView = findViewById(R.id.textView1)
tv1.text = "Hello"
Click Here to study exactly you want to know
remove this.. setContentView(tv1);
I got the same problem. My app was stopping too.
Actually, I was writing the code outside of the function/method. So to fix this problem, these lines
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Hello");
must be inside a function/method. (can be user defined)
(I am new to android studio so I don't know the reason behind the problem but I only know how to fix this. Maybe this helps new ones despite this question being 8 years old.)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Android Problem calling TextView from second layout file
Hey guys the main layout xml file for my activity is R.layout.date_list_layout as it is used as follows
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.date_list_layout);
However I wish to set the text in a TextView from this same activity in another layout xml file R.layout.display_item
I know I would usually use the code below but this is not working as the R.id.currency TextView is not in the main R.layout.date_list_layout....
TextView currency = (TextView) findViewById(R.id.currency);
currency.setText(cur);
I know the next line of code is incorrect but is there a way to write a similar line of code to access the R.id.currency in the R.layout.display_item xml or can this not be done?
TextView currency = (TextView) findViewById(R.layout.display_item/R.id.currency);
currency.setText(cur);
Some help would really be appreciated as this has me boggled the last two days and I can't find any solution online
Thanks
A
I'm quite new to Android so I am not sure, but I think this should work:
TextView textView = (TextView)View.inflate(this, R.layout.display_item, null);
"this" would be your Activity.