I am trying to add multiple buttons to my app so if you click one you automatically call a certain person, but now I'm stuck on the call-action, how can I make a call when there is being clicked on the button? I have the following code for my activity named telefoonnummers.java:
package com.example.rome;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;
public class Telefoonnummers extends Activity {
Button mHALbellen;
Button mWITbellen;
Button mWDGbellen;
Button mVlierbellen;
Button mHotelbellen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_telefoonnummers);
mHALbellen = (Button) findViewById(R.id.button1);
mWITbellen = (Button) findViewById(R.id.button3);
mWDGbellen = (Button) findViewById(R.id.button4);
mVlierbellen = (Button) findViewById(R.id.button5);
mHotelbellen = (Button) findViewById(R.id.button6);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_telefoonnummers, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void vanHalbellen(View view){
if (view == mHALbellen){
//WHICH CODE SHOULD BE HERE TO MAKE A PHONECALL WHEN THE BUTTON mHALBELLEN IS PRESSED??
}
}
}
Would you guys please help???
Add the following permission to your manifest:
<uses-permission android:name="android.permission.CALL_PHONE" />
And then execute this Intent when the button is clicked:
String uri = "tel: phone_number_here";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
You can also use Intent.ACTION_DIAL instead of Intent.ACTION_CALL. This shows the dialer with the number already entered, but allows the user to decide wether to actually make the call or not. ACTION_DIAL doesn't require the CALL_PHONE permission.
Related
Hi I'm quite new to Java could you please show where i've gone wrong would be much appreciated many thanks! I want to Link two java activty's with button when presed
package com.example.matt.androidui;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends Activity {
Called at the top of activty.
Code i'm having trouble with seems to be something up!
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener) {
public void onClick (View v){
Intent intent = new Intent (v.getContext(), MainActivity2Activity.class);
startActivityForResult(kntent, 0);
}
}
}
Button Code ends here!
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
// noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
If you want to start second activity by pressing button then simply do this,
Intent intent = new Intent (MainActivity.this, SecondActivity.class);
startActivity(intent);
#MattTheCodeMan
If you want to start a second activity by pressing a button then write this code:
Intent intent = new Intent (v.getContext(), MainActivity2Activity.class);
startActivityForResult(intent, 0);
If you are new in Android maybe is good start with the navigation edition in android studio, LOOK THIS LINK
I have one activity with 5 buttons. Each button should show up one activity (total 5 different activities). I tried many methods for multiple buttons but my app does not work. The first button of the activity starts its linked activity without any problem (btEtiologia and the activity is Etiology), but the second button still won't show up, and returns to the Main Activity (that is home layout). When I restart the app, I try with the second button, and third buttons and works, but the fourth button won´t show up and returns to the main activity. I have dowloaded the app on a samsung galaxy S2 and is the same than emulator. ¿Is anything wrong with my code?
Here is the java code of Inicio:
package com.example.protesis;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Inicio extends ActionBarActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inicio);
Button btInformacion = (Button) findViewById(R.id.btInformacion);
btInformacion.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(getBaseContext(), "Esta aplicación esta basada en el manejo de las infecciones de prótesis articulares de la IDSA", Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.btEtiologia).setOnClickListener(this);
findViewById(R.id.btAlgoritmo).setOnClickListener(this);
findViewById(R.id.btAntibioiv).setOnClickListener(this);
findViewById(R.id.btAntibioral).setOnClickListener(this);
findViewById(R.id.btRetirada).setOnClickListener(this);
}
public void onClick(View arg0){
// create a general intent
Intent intent = null;
// define an intent for all cases
switch(arg0.getId()){
case R.id.btEtiologia:
// Setting intent for first button
intent = new Intent(this,Etiology.class);
break;
case R.id.btAlgoritmo:
// Setting intent for second button
intent = new Intent(this,Algoritmo.class);
break;
case R.id.btAntibioiv:
// Setting intent for third button
intent = new Intent(this,Antibioiv.class);
break;
case R.id.btAntibioral:
// Setting intent for fourth button
intent = new Intent(this,Antibioral.class);
break;
case R.id.btRetirada:
// Setting intent for fourth button
intent = new Intent(this,Retirada.class);
break;
}
this.startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.inicio, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The main activity code is:
package com.example.protesis;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btInicio = (Button) findViewById(R.id.btInicio);
btInicio.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Inicio.class);
startActivityForResult(intent, 0);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
As per you code, your images are dragging the code to OutOfMemory exception. Though the images which you are using of 120 dpi, their size is double to 640*480. please change image sizes then the problem will be solved. I have changed the all the 5 image sizes and now i am able to navigate to all the screens by clicking the buttons. Make the images to around 640*480 in ldpi folder.
My app force closes on the onClick (goForIt). I'm just trying to set a content View but it forces closes eatch time I press my button. And also is it possible to make an app with only 1 activity ?
Could somebody tell me why please:
package com.XanderApps.techquizz;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void goForIt(View view) {
setContentView(R.layout.question_one);
}
public void onCheckboxClicked(View view) {
boolean checked = ((CheckBox) view).isChecked();
switch(view.getId()) {
case R.id.checkbox_meat:
if (checked)
setContentView(R.layout.choice_done);
break;
case R.id.checkbox_cheese:
if (checked)
System.exit(0);
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Without looking at your Logcat, my guess is that you are getting a 'NullPointerException' when you try to access your button.
Make sure you are 'instantiating' your button object using the findViewById syntax, before you are trying to use onClick().
As you haven't instantiated your goForIt inside onCreateView() of Activity. Thus it throws NPE(Null Pointer Exception).
Get your id of goForIt from your XML layout file.
Add
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
goForit=(Button)findViewById(R.id.goForItId);
}
And then add onClickListener on goForIt.
So, I've just started views and moving between activities. I thought I got what I was looking for. I fixed all my errors, but now when I test the application it crashes. I'm new to android and eclipse. So I'm not exactly sure what's happening. Here's my MainActivity.java:
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String info = intent.getStringExtra("info_key");
TextView text = (TextView) findViewById(R.id.nametext);
text.setText(info);
}
//opens a new activity.
public void openAddItem (View v){
Intent intent = new Intent (this, Additem.class);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
And here's my second activity; Additem.java:
package com.grocerylist;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class Additem extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_additem);
}
public void additem (View v){
EditText text = (EditText)findViewById(R.id.itemname);
String info = text.getText().toString();
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("info_key", info);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_additem, menu);
return true;
}
}
Thanks, and I'm positive the problem lies where I've placed my id's.
Your first problem I see is here
Intent intent = getIntent();
String info = intent.getStringExtra("info_key");
TextView text = (TextView) findViewById(R.id.nametext);
text.setText(info);
there is no Intent to "get" if this is your first Activity. That is used when you start an Activity with an Intent and send extras. So, naturally, info is null. There will be something there when you create it from your second Activity but not when you first run your app. Also, you may want to check into using startActivityForResult in your first Activity
Second, I don't see where you call openAddItem() in your first Activity. It may be from a Button but I don't see any Buttons
It looks like you are missing some key understandings of the fundamentals of the Android framework. I suggest you start with the Docs Here if you haven't been through them already. Good luck to you
Also, this
Intent intent = new Intent (this, Additem.class);
should be this
Intent intent = new Intent (MainActivity.this, Additem.class);
the first problem I also see was already mentioned by codeMagic.
The second thing: in case you just call your methods which start another Activity in onCreate(), you will get a pseudo-infinite number of those Activities in the stack and your app will crash.
Consider watching these tutorials, it may help you to get started
I'm making an app and when it launches it starts Mainactivity.java
Mainactivity.java opens a layout with 9 Imagebuttons.
How can I implement in my code in Mainactivity.java that once one is clicked it opens another activity (like telefoonnummers.java)?
Sorry for my bad English but I'm dutch and a non-native speaker.
I have this code in Mainactivity.java:
package com.example.rome;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Very clean as you see, but how can I add the implementation, would you guys please help???
My Imagebuttons are all just called imagebutton1, imagebuttton2 etc. btw.
After
setContentView(R.layout.activity_main);
add for each ImageButton:
findViewById(R.id.imagebutton1).setOnClickListener(this);
Make class implement OnClickListener
class MainActivity extends Activity implements View.OnClickListener {
and add this method:
#Override
public void onClick(View v){
switch(v.getId()){
case R.id.R.id.imagebutton1:
startActivity(new Intent(telefoonnummers.class));
break;
case R.id.R.id.imagebutton2:
startActivity(new Intent(telefoonnummers.class));
break;
//-- more cases --
}
}