ANDROID : adding call function when a button clicked - java

I have added a ListView inside a fragment, when clicking that listview i see single itemview. when i click the button in that single item view a call should go to that particular id in the listview. button is for call , when its clicked and automatic cal should go . my code is down , phone numbers are stored in side parse server. if anyone knows please help
btn = (Button) findViewById(R.id.button56) ;
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String phno="telephone";
Intent i=new Intent(Intent.ACTION_DIAL,Uri.parse(phno));
startActivity(i);
}
});
xml code
<Button
android:id="#+id/button56"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="TELE PHONE"
android:layout_weight="1"
android:background="#EFEFEF"/>

Try using,
Uri.parse("tel:" +number)
and I assume you have added the permission,
.CALL_PHONE
in your manifest.

Add to your manifest permission to make calls
<uses-permission android:name="android.permission.CALL_PHONE" />

Related

Button not working (android studio) [duplicate]

This question already has answers here:
How to start new activity on button click
(28 answers)
Closed 5 years ago.
My app runs well and shows no errors, but my button is not working ( is unClickable in device)
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button11=(Button)findViewById(R.id.button11);
button11.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
new Intent(MainActivity.this, Main2Activity.class);
}
});
}
}
and XML file :
<Button
android:id="#+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50px"
android:layout_marginTop="200dp"
android:background="#color/colorPrimary"
android:text="صفحه اصلی"
android:onClick="onClick"/>/>
On the real device when I click on this button nothing happens !
try this way
Intent i = new Intent(MainActivity.this, Main2Activity.class);
startActivity(i);
EDIT: I wanted to add a bit of an explanation to help
Hi MRAK,
First things first, you do NOT need to create an onClick listener. Remove all of that completely. Android studio is a beast and automatically does that for you using the XML file. When you set "onClick" in the XML file, it automatically calls the name of whatever method you put in there. You should change it so it is not also called "onClick." I would prefer to call it "startAcitivty2" or so on so you are not confused later. I stuck with your method name for now.
See below for corrected code:
public void onClick(View v){
// note in the below line i'm just using "this"
Intent myIntent = new Intent(this, Main2Activity.class)
// Secondly, you need to end the current activity
finish();
// Third, you need to start your new activity...
// Creating an Intent does not the activity alone
startActivity(myIntent);
}
Also, this has so many downvotes because this has been asked 1000+ times. Please use google or the search bar above before asking. Google will reroute you to stackoverflow anyway :)

Button working only on second click [duplicate]

This question already has answers here:
Android Button Works Only on the second Click
(2 answers)
Closed 7 years ago.
I have a simple, but annoying problem:
In my fragment, I have a button which should open another activity (intent) on click. However I have to click the button twice, and only the second time it's opening the activity.
Here is the xml layout of the button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go!"
android:onClick="login"
android:id="#+id/bt_SignIn"
android:layout_below="#+id/pass"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp" />
And here is the code for onClick:
public void login(View view){
bt_SignIn = (Button) findViewById(R.id.bt_SignIn);
bt_SignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Frontpage.class);
startActivity(i);
}
}
}
You're setting the onClick listener for the button twice. You're declaring the onClick in the XML so your method can just be.
public void login(View view){
Intent i = new Intent(getApplicationContext(), Frontpage.class);
startActivity(i);
}
Why are you defining Onclick listener for btn_signIn again? As you already define in your xml login method will call on button click.
Use only below code.
public void login(View view) {
Intent i = new Intent(getApplicationContext(), Frontpage.class);
startActivity(i);
}
That is because of thee fact that in the function defined in xml you are setting the listener for the button. Do it from either the JAVA code or from the xml code.
This shall suffice
public void login(View view) {
Intent i = new Intent(getApplicationContext(), Frontpage.class);
startActivity(i);
}
What was happening earlier was that on the first click the function defined in the xml was getting invoked which inturn was setting the listener for the button, and so on the next click your activity was getting triggered
You should avoid to use onClick directly in the XML, it's a bad idea.
Remove onClick from the XML:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go!"
android:id="#+id/bt_SignIn"
android:layout_below="#+id/pass"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp" />
put this code in the onCreate of your Activity:
bt_SignIn = (Button) findViewById(R.id.bt_SignIn);
bt_SignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Frontpage.class);
startActivity(i);
}
}
Change your login(View view) method as follows
public void login(View view){
Intent i = new Intent(getApplicationContext(), Frontpage.class);
startActivity(i);
}
Or much better put your code inside onCreate() method
bt_SignIn = (Button) findViewById(R.id.bt_SignIn);
bt_SignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Frontpage.class);
startActivity(i);
}
}
and remove onClick from your layout
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go!"
android:id="#+id/bt_SignIn"
android:layout_below="#+id/pass"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp" />

Ignore EditText setOnClickListener code to be able to edit text

I have an EditText as a text field to accept a telephone number amongst other personal details on an activity.
On activity launch the EditText is disabled until the user selects edit to edit the information. It is then enabled and allows the user to type a number in it.
Once the user clicks to save the data the EditText is made unfocusable using
txtPhone.setFocusable(false);
It then uses an onClickListener to allow the user to click it and call the number it contains.
When the user clicks edit to edit the number, I am setting
txtPhone.setFocusable(true);
To allow the EditText to receive focus however it is still seems to be using the onClickListener and won't allow the text to be edited giving the warning in log cat:
TextView does not support text selection. Action mode canceled.
Code for the EditText listener:
txtPhone.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if(txtPhone.isFocusable()==false)
{
if(txtPhone.getText().length() > 1)
{
// Call number
// Using Toast to show listeners working first
Toast.makeText(getApplicationContext(), "Calling... " + txtPhone.getText().toString(), Toast.LENGTH_LONG).show();
}
}
}
});
The EditText xml:
<EditText
android:id="#+id/txtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:enabled="false"
android:hint="Telephone..."
android:inputType="phone"
android:maxLength="11" />
How can I make it so that the text is editable like on first launch and ignore the listener but when not focusable call the number, I don't seem to be able to get it to work as desired
I wasn't able to reproduce the warning you were getting, but wasn't able to get the Toast to appear on pressing the textbox when not focused. I did try the following to replicate what you're trying to achieve. Hopefully I understood you correctly.
EditText
Instead of using enabled="false" I used focusable="false". This will have a similar behaviour to enabled being false, but allows the onClickListener of txtPhone to fire.
<EditText
android:id="#+id/txtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:focusable="false"
android:hint="Telephone..."
android:inputType="phone"
android:maxLength="11" />
Listeners
Since you didn't show code for the buttons, I used a button called btnEdit to switch between focuses. Instead of txtPhone.setFocusable(true); I used txtPhone.setFocusableInTouchMode(true); to properly get the edit text to be editable.
btnEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (txtPhone.isFocusable()) {
txtPhone.setFocusable(false);
} else {
txtPhone.setFocusableInTouchMode(true);
}
}
});
txtPhone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (txtPhone.isFocusable() == false) {
if (txtPhone.getText().length() > 1) {
// Call number
// Using Toast to show listeners working first
Toast.makeText(MainActivity.this, "Calling... " + txtPhone.getText().toString(), Toast.LENGTH_LONG).show();
}
}
}
});
So when I first start the activity, txtPhone is not editable. When I click the edit button, txtPhone becomes editable and I am able to enter numbers. Lastly, I click the edit button again to disable txtPhone and when I press on it, the Toast appears.

Onclick listener throwing NullPointerException

I've setup a button and I'm trying to display a toast when the user clicks on it. Here's my Java code -
file = (Button) findViewById(R.id.file);
file.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Display the file chooser dialog
//showChooser();
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)", Toast.LENGTH_LONG).show();
}
});
Here's my XML code to setup the button -
<Button
android:id="#+id/file"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/plt"
android:text="File" />
This throws a NullPointerException on the line file.setOnClickListener(new OnClickListener() { . What am I doing wrong?
Are you initializing the Button inside the onCreate() method of your Activity?
If so, please check if you are calling
setContentView(R.id.yourlayoutfile);
before initializing the Button with findViewById(R.id.file);
Your error occurs because your Button "file" is null, which means that findViewById(...) didn't find any View with that id. The reason therefore can either be that there is no such ID in the inflated layout, or that you didn't call setContentView(...)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayoutfile);
// initialize your button here
}
try to clean project
project-->clean-->choose your project-->ok,
then run again.
if you still facing the same problem you can use another way to set click action
in your XML
<Button
android:id="#+id/file"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/plt"
<!--added line-->
android:onClick="anyName"
android:text="File" />
and then in you activity remove the initialization of the button and click listner too
and make the code looks like that
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.id.yourlayoutfile);
}
public void anyName(View v){
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();
}
Hope this help.
If there is a Null pointer exception on this line:
file.setOnClickListener(new OnClickListener()
then it mean your file object is null
Make sure you initialize your file object before adding a listener to it.

Android Programming: Calling function with different input using onClick

I am new to android programming and so please pardon if the question looks stupid.
I am creating a Calculator in Android and for the user interface I have many buttons (around 20 for 10 digits, and various operation). Now there is a string expression that I calculate once the user presses the button "=". However if he presses any other button then the input is updated. Say he presses "1" then input =1; then he presses 2 then input becomes "12" and so on.
So I need to call the same function whenever various buttons are pressed, but the input to the function is different.
I can go-by this by making n different functions, one for each button but that is not very scalable.
So how can I go about it?
The current xml file is:
<Button
android:id="#+id/Button01"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/Button03"
android:layout_alignBottom="#+id/Button03"
android:layout_toRightOf="#+id/Button03"
android:onClick="UpdateExpression_/"
android:text="/" />
<Button
android:id="#+id/Button02"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/Button01"
android:layout_alignBottom="#+id/Button01"
android:layout_toRightOf="#+id/Button01"
android:onClick="UpdateExpression_X"
android:text="x" />
I need to update to android:onClick="UpdateExpression" and mention some input to this function call.
Thank you.
You will need a central onClick method, let's call it updateExpression(View v) Also a note about your code: method names should start with a lowercase letter, it's a Java naming convention.
android:onClick="updateExpression"
Now the implementation:
public void updateExpression (View v)
{
switch (v.getId())
{
case R.id.button1:
//do stuff here
break;
case R.id.button2:
//do other stuff here
break;
}
}
The reason you need v.getId() is because you're checking the id of the button then doing something if it's that particular id. This logic is needed since all your buttons will implement that same method.
Here is the implementation using code....
In the onClick function, compare the v.getId() to each of your Android layout ids, like R.id.button1 R.id.button2 etc...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Here are some buttons defined in the XML layout as button1, button2, etc...
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(myListener);
Button button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(myListener);
Button button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(myListener);
}
//Create an anonymous implementation of OnClickListener
private OnClickListener myListener = new OnClickListener() {
public void onClick(View v) {
Log.d(logtag,"onClick() called");
Toast.makeText(MainActivity.this, "The " + v.getId() + " button was clicked.", Toast.LENGTH_SHORT).show();
}
};

Categories

Resources