Calling a phone number from TextView in Android - android

I am testing this little code snippet on a tablet. So i am not sure if it is not working because of that. Nothing happens when i click the number. No error message or log shown in LogCat. Please look at my implementation below.:
MainActivity.java
public void callPolice(View view){
TextView tv = (TextView) findViewById(R.id.policeno);
String val = tv.getText().toString();
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+val));
startActivity(intent);
Log.v("Calling", "Calling..... "+val);
}
activity_main.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="999"
android:textColor="#000000"
android:textSize="13pt"
android:textStyle="bold"
android:id="#+id/policeno"
android:layout_alignTop="#+id/textView2"
android:layout_alignRight="#+id/textView5"
android:layout_alignEnd="#+id/textView5"
android:onClick="callPolice"/>
AndroidManifest.xml
<uses-permission android:name = "andriod.permission.CALL_PHONE" />

Just put android:clickable="true" in textview.
<TextView
android:id="#+id/policeno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="87dp"
android:onClick="callPolice"
android:text="999"
android:textColor="#000000"
android:textSize="13pt"
android:clickable="true"
android:textStyle="bold" />

if you want to call you should use
Intent.setAction(Intent.ACTION_CALL);
If you are using fragments the xml onclick may not work. So try implementing onclick listner. Put the log before starting activity.

Related

XML ready, Java unsuccessful

I have a problem with this piece of code. I have the ids registered in my XML, but the code isn't working. The main is in red, the add_photo is in red, the targeturi and targetimage are all in red (Everything in red is in italic).
setContentView(R.layout.*main*);
Button buttonAddPhoto = (Button)findViewById(R.id.*add_photo*);
textTargetUri = (TextView)findViewById(R.id.*targeturi*);
targetImage = (ImageView)findViewById(R.id.*targetimage*);
XML:
<TextView
android:text= "Thot of The Day!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60sp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:id="#+id/text_shout_out"
/>
<Button
android:id="#+id/add_photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add Photo"
android:gravity="center"
android:layout_below="#+id/text_shout_out"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="131dp"
android:onClick="Image"/>
Since the name of your activity layout is activity_main, you need to do
setContentView(R.layout.activity_main);
You don't have TextViews with id of R.id.targeturi and R.id.targetimage anywhere in your layout file. You need to put them in your layout first.
Also, you need to declare your TextViews(unless you have declared them already before onCreate())
TextView textTargetUri = (TextView)findViewById(R.id.targeturi);
TextView targetImage = (ImageView)findViewById(R.id.targetimage);
I think you are having a problem because you didn't declare a type for your variables "textTargetUri" and "targetImage".

onClick method keeps telling me "Unfortunately,myapp has stopped"

I am using a startActivityForResult method to get a string from an activity,the activity has an editview and a button to return the inputted value of the editview,but when i click on the button,it keeps telling me "Unfortunately,myapp has stopped",dont know where the problem is coming from?
This is the line calling the activity
startActivityForResult(new Intent("com.dreama.trafic.SearchActivity"),request_Code);
and this is the xml file for the activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editSearch"
android:layout_centerHorizontal="true"
android:layout_marginBottom="60dp"
android:text="#string/searchLocation"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#f00" />
<EditText
android:id="#+id/editSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10" />
<Button
android:id="#+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:onClick="click"
android:text="#string/search" />
</RelativeLayout>
and this is the click method
public void click(View view) {
Intent data = new Intent();
EditText editSearch = (EditText) findViewById(R.id.editSearch);
data.setData(Uri.parse(editSearch.getText().toString()));
setResult(RESULT_OK, data);
finish();
}
Try this..
I guess you are getting ActivityNotFoundException
Change this..
startActivityForResult(new Intent("com.dreama.trafic.SearchActivity"),request_Code);
to
startActivityForResult(new Intent(currentActivity.this,SearchActivity.class),request_Code);
it also may be because you have not declared SearchActivity.java in your manifest file as
<activity android:name=".SearchActivity"/>

Change layout_bellow attribute field to another field programmatically

Given the following code:
<RelativeLayout
android:id="#+id/rel"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textPersonalInfoEmpty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"
android:hint="Edit to set your personal info"
android:textSize="16sp"
android:visibility="gone" />
<EditText
android:id="#+id/editName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="15dp"
android:hint="Name"
android:visibility="gone"
android:textSize="16sp" />
<Button
android:id="#+id/insertGameId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editName"
android:layout_marginTop="20dp"
android:text="Insert" />
</RelativeLayout>
The TextView and the EditText are visible depending the user interaction and both will never be visible at the same time. One per time only!
The problem is the third component(Button) need to change the attribute android:layout_below depending on which component is visible.
I need to this programmatically.
Any help?
Use Relativelayout for your textview and edittext. And relativelayout's height:wrap-content
Then give an id to your relativelayout
and then add this line your xml file for your button
<Button
android:layout_below="#id/RealtiveLayoutid" />
Thats all
First, use a ToggleButton instead of Button. It can have all the attributes that you assigned to it.
Next add this to the ToggleButton:
<ToggleButton
....
android:textOn="#string/ifTextView"
android:textOff="#string/ifTextBox"
android:onClick="onToggleClicked"
/>
Then this for the activity
public void onToggleClicked(View v) {
boolean on = ((ToggleButton) v).isChecked();
if(on) {
theTextView.setVisibility(View.VISIBLE);
theTextBox.setVisibility(View.INVISIBLE);
} else {
theTextBox.setVisibility(View.VISIBLE);
theTextView.setVisibility(View.INVISIBLE);
}
}
Remember to import the required classes.

I want text view as a clickable link

<TextView
android:id="#+id/link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Forget password?"
android:autoLink="all"
android:linksClickable="true"
android:textColor="#color/lgreen"
android:textStyle="italic"
/>
Android is highlighting the links in the TextView, but they do not respond to clicks. Can someone tell me how can i do that as a clickable and move to another link.
i tried more times by seeing examples. but i can't. Can some one explain me clearly how to change text view as a clickable link.
All tested and working 100%
below is a complete example
Solution: android:autoLink="web"
Sample Xml:
<TextView
android:id="#+id/txtLostpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:autoLink="email"
android:gravity="center"
android:padding="20px"
android:text="#string/lostpassword"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/txtLostpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:autoLink="web"
android:gravity="center"
android:padding="20px"
android:text="#string/defaultpassword"
android:textAppearance="?android:attr/textAppearanceSmall" />
String in string.xml
<string name="lostpassword">If you lost your password please contact support#cleverfinger.com.au</string>
<string name="defaultpassword">User Guide http://www.cleverfinger.com.au/user-guide/</string>
You can add click listener to TextView and start websearch:
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view)
{
Intent browser= new Intent(Intent.ACTION_VIEW, Uri.parse(PASTE_YOUR_URL_HERE));
startActivity(browser);
}
});
and xml looks like:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/link_string"
android:textSize="14dp"
android:autoLink="web" />
In the string file put this code
<string name="link">'<a href="http://www.www.com" >ForGet Password</a>'</string>
and in the XML file
android:text="#string/link"
this is the most easy and understandable code
TextView link = (TextView) findViewById(R.id.hyper);
String linkText = "This is my website <a href='http://programondaspot.blogspot.com'>Programondaspot</a> .";
link.setText(Html.fromHtml(linkText));
link.setMovementMethod(LinkMovementMethod.getInstance());
Check out this post!
HAPPY CODING!
In your onCreate() method put this code:
TextView clickableTextLink = (TextView)findViewById(R.id.your_textview_id); clickableTextLink.setMovementMethod(LinkMovementMethod.getInstance());
I tried a number of solutions. But this worked perfectly for me.
this is the EDITED answer
Hi you can try by replacing this in you layout.xml file
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="visit the site at www.google.com from here"
android:autoLink="all"
android:clickable="true"
android:linksClickable="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
I have tried it and it will surely work. and treats "www.google.com" as web link
Just add onClick tag to your TextView which equals the defined function. Just as below
<TextView
android:id="#+id/link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Forget password?"
android:autoLink="all"
android:linksClickable="true"
android:textColor="#color/lgreen"
android:textStyle="italic"
android:onClick="callFunction"/>
Here is simple implementation tested up to Android N
String termsText = "By registering, you are agree to our";
String termsLink = " <a href=https://www.yourdomain.com/terms-conditions.html >Terms of Service</a>";
String privacyLink = " and our <a href=https://www.yourdomain.com/privacy-policy.html >Privacy Policy</a>";
String allText = termsText + termsLink + privacyLink;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
((TextView) findViewById(R.id.text_terms_conditions)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.text_terms_conditions)).setText(Html.fromHtml(allText, Html.FROM_HTML_MODE_LEGACY));
} else {
((TextView) findViewById(R.id.text_terms_conditions)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.text_terms_conditions)).setText(Html.fromHtml(allText));
}

Button does not respond to onClick event

Hi everyone I ve been searching this a lot but I couldn't find an answer that would work in my case. In my layout i have a listview and outside the list and more specific above i have an imageview an editText which work as a search and a custom Button!The problem is that when i press the button does not start the Activity which i have put in th Intent.Why is this happening?I cannot find the error and so far i had never problem with button events.
Here is my xml:
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/imageView1"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:clickable="false"
android:layout_below="#+id/autoCompleteTextView1" >
</ListView>
</LinearLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="LIST OF PATIENTS"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#33B5E5" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="90dp"
android:layout_height="85dp"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_below="#+id/textView1"
android:padding="2dp"
android:src="#drawable/search_user" />
<LinearLayout
android:id="#+id/linear"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignBottom="#+id/autoCompleteTextView1"
android:layout_alignParentRight="true"
android:orientation="vertical" >
<Button
android:id="#+id/add"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_above="#+id/linearLayout1"
android:layout_alignParentRight="true"
android:background="#drawable/buttonadd"
android:onClick="button_click"
android:src="#drawable/add" />
</LinearLayout>
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView1"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_toLeftOf="#+id/linear"
android:ems="10"
android:hint="Search patient" />
the button is the right on the top next to autocomplete.In the .java doc i have right this code as far as concerned the button:
In the onCreate
addp=(Button) findViewById(R.id.add);
addp.setOnClickListener(this);
In th onClick
case R.id.add:
Intent add= new Intent(sqlviewpatients.this,afprognosis.class);
startActivity(add);
break;
I ve been doing the same thing to others button and they working only this one does not respond to onClick events.Please tell me what I am doing wrong?
thanks in advance
This is because your ids do not match:
addp=(Button) findViewById(R.id.add);
and:
<Button
android:id="#+id/addpatient"
Change the java to:
addp=(Button) findViewById(R.id.addpatient);
EDIT: If you are defining your onClick within the XML then you should not need to set an onClickListener within the java code. Just make the code:
Intent add= new Intent(sqlviewpatients.this,afprognosis.class);
startActivity(add);
within a method that is called button_click.
EDIT after Code update
Okay, after reading the code you just posted: sqlviewpatients:
what does customwindow3 look like? Does it inherit from ListActivity or what?
This class only implements onClickListener and so a bunch of code won't get executed, including onCreate unless of course, customwindow3 does
Original
1) your button's id is addpatient not add which is the clause if you switch statement
2) have you looked into doing this:
public void button_click(View v)
{
Intent .....
}
Also, from the snippet you posted this:
addp=(Button) findViewById(R.id.add);
addp.setOnClickListener(this);
shouldn't actually run (you'll get a null pointer exception) since there is no element with that id (unless of course you've defined that somewhere else and didn't show us).
First of all thanks everyone for your answers and your time!I appreciate it
Second i found where the error was. the onClick method should have an #overide above

Categories

Resources