[Q] How do I make a call through a long press context menu - Android Software Development

I orginally had it setup to make a call based on a button click, but now I want the context menu through a long press to do it.
The menu works fine as it finds it way to my call class but I can't figure out how to change it around for it to just start dialing as any android phone does. Can someone suggest what I should change the code below to.
HTML:
public class Dial extends Activity {
Button Btn2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.note_edit);
Btn2 = (Button) findViewById(R.id.dial);
Btn2.setOnClickListener(new OnClickListener () {
public void onClick(View v) {
EditText num=(EditText)findViewById(R.id.phone);
String number = "tel:" + num.getText().toString().trim();
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(number));
startActivity(callIntent);
}
});
}
}

take a look at these methods from View
http://developer.android.com/reference/android/view/View.html#setLongClickable(boolean)
http://developer.android.com/refere...ndroid.view.View.OnCreateContextMenuListener)
http://developer.android.com/refere...stener(android.view.View.OnLongClickListener)
should handle the many ways to make an item long clickable

Related

I need help making a longpress Context menu

Basiclly I need help making it so people can longpress on a button and choose to set as ringtone or notfication tone.
here is some of my code I use so far for the sound:
Code:
MediaPlayer mpButtonClick;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//set up the button sounds
mpButtonClick = MediaPlayer.create(this, R.raw.money);
Button bmoney = (Button) findViewById(R.id.money);
bmoney.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mpButtonClick.start();
}
});
View.setOnLongClickListener is all you need. As you should know Button extends TextView, which in turn extends View, so you can use that method on a Button too
martino2k6 said:
View.setOnLongClickListener is all you need. As you should know Button extends TextView, which in turn extends View, so you can use that method on a Button too
Click to expand...
Click to collapse
sorry but still kind of confused because I added the following before you mentioned this and got a force close:
Code:
Button money = (Button) findViewById(R.id.money);
Button registerForContextMenu;
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Action 1");
menu.add(0, v.getId(), 0, "Action 2");
}
Ok, so now after reading I do actually have a menu on long press like I wanted...the only problem is that it doesn't actually get the sound file and save it
I am wondering what did I do wrong now? Here is the code I used:
Code:
Button SoundButton1 = (Button) findViewById(R.id.money);
registerForContextMenu(SoundButton1);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Save as...");
menu.add(0, MENU_RINGTONE, 0, "Ringtone");
menu.add(0, MENU_NOTIFICATION, 0, "Notification");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Ringtone"){function1(item.getItemId());}
else if(item.getTitle()=="Notification"){function2(item.getItemId());}
else {return false;}
return true;
}
public void function1(int id){
Toast.makeText(this, "Ringtone saved", Toast.LENGTH_SHORT).show();
}
public void function2(int id){
Toast.makeText(this, "Notification saved", Toast.LENGTH_SHORT).show();
}
I hate to bump an old thread but I am trying to add notifications and ringtones to a soundboard, I was able to successfully use the example here:
http://stackoverflow.com/questions/...ode-only-setting-first-button-as-ringtone-etc
but I have only been able to get it to save the audio from the first button. Could anyone point out how to get it working for each sound rather than just the first?
I would not rely on "==" returning what you think you want....use .equals() instead.
And I would probably just call one method and pass in the item id and let that method handle your processing. function1 and function2 are redundant.
I wonder how exactly you thought that just showing a toast with the Ringtone id will save it in device settings. in function1() and function2() you must add handlers to get resource from the id, set that stream as the desired ringtone, notification.
vamp6x6x6x said:
Ok, so now after reading I do actually have a menu on long press like I wanted...the only problem is that it doesn't actually get the sound file and save it
I am wondering what did I do wrong now? Here is the code I used:
Code:
Button SoundButton1 = (Button) findViewById(R.id.money);
registerForContextMenu(SoundButton1);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Save as...");
menu.add(0, MENU_RINGTONE, 0, "Ringtone");
menu.add(0, MENU_NOTIFICATION, 0, "Notification");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Ringtone"){function1(item.getItemId());}
else if(item.getTitle()=="Notification"){function2(item.getItemId());}
else {return false;}
return true;
}
public void function1(int id){
Toast.makeText(this, "Ringtone saved", Toast.LENGTH_SHORT).show();
}
public void function2(int id){
Toast.makeText(this, "Notification saved", Toast.LENGTH_SHORT).show();
}
Click to expand...
Click to collapse

Switching to and from layouts using buttons

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button display = (Button) findViewById(R.id.button);
TextView tv = (TextView) findViewById(R.id.textView);
display.setOnClickListener( new View.OnClickListener(){
public void onClick(View p1)
{
setContentView(R.layout.main2);
// TODO: Implement this method
}
});
Button ba = (Button) findViewById(R.id.buttonBac);
ba.setOnClickListener(new View.OnClickListener(){
public void onClick(View p1)
{
setContentView(R.layout.main);
// TODO: Implement this method
}
});
}
}
Anyway, the first button works and when clicked, displays main2 makin 2 whoum d have a textview and a button that switches back 2 main but when the onclick listener whTever is aplyed to the code it wont even disllag the first layout and simply crashes... hitting the wall again
Focusedrelaxaation87 said:
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button display = (Button) findViewById(R.id.button);
TextView tv = (TextView) findViewById(R.id.textView);
display.setOnClickListener( new View.OnClickListener(){
public void onClick(View p1)
{
setContentView(R.layout.main2);
// TODO: Implement this method
}
});
Button ba = (Button) findViewById(R.id.buttonBac);
ba.setOnClickListener(new View.OnClickListener(){
public void onClick(View p1)
{
setContentView(R.layout.main);
// TODO: Implement this method
}
});
}
}
Anyway, the first button works and when clicked, displays main2 makin 2 whoum d have a textview and a button that switches back 2 main but when the onclick listener whTever is aplyed to the code it wont even disllag the first layout and simply crashes... hitting the wall again
Click to expand...
Click to collapse
You can't do a second setContentView call in an activity. What you can do is define two Layouts within a LinearLayout in the XML, set the first one to "android:visibility="gone"", the second one to "visible" and switch between them onClick
Code:
[user=439709]@override[/user]
public void onClick(View v) {
switch (v.getId()){
case R.id.btnOne:
mLayoutFirst.setVisibility(View.GONE);
mLayoutSecond.setVisibility(View.VISIBLE);
break;
case (R.id.btnTwo):
mLayoutFirst.setVisibility(View.VISIBLE);
mLayoutSecond.setVisibility(View.GONE);
break;
}
}
Zatta said:
You can't do a second setContentView call in an activity. What you can do is define two Layouts within a LinearLayout in the XML, set the first one to "android:visibility="gone"", the second one to "visible" and switch between them onClick
Click to expand...
Click to collapse
Alternatively you could use a ViewSwitcher and call it's .next() and .previous() methods. This would probably be necessary when you have more than 2 views.
octobclrnts said:
Alternatively you could use a ViewSwitcher and call it's .next() and .previous() methods. This would probably be necessary when you have more than 2 views.
Click to expand...
Click to collapse
Thanks, never heard of before. I use that method for hiding/unhiding complete ViewGroups, Buttons and what not but I'll look into that, might be more easy.
Try naming the activity other than the "main" attribute, that is only for the first or "parent" activity. You need it to be a child activity.
Sent from my HUAWEI-M835 using xda app-developers app
Cool, learned something new

multiple buttons to send predefined SMS

Hi guys i am new to programming. I am trying to have multiple button to send different predefined SMS to predefined number. I am not sure how to have multiple setOnClickListener(new OnClickListener() as the 2nd setOnClickListener(new OnClickListener() gave me error.
public class SendSMSActivity extends Activity {
Button buttonSend;
Button buttonSend2;
@override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonSend = (Button) findViewById(R.id.buttonSend);
buttonSend2 = (Button) findViewById(R.id.buttonSend2);
buttonSend.setOnClickListener(new OnClickListener() {
buttonSend2.setOnClickListener(new OnClickListener() {
@override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonSend:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "abc");
sendIntent.putExtra("address", "9909990");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
break;
case R.id.buttonSend2:
Intent sendIntent1 = new Intent(Intent.ACTION_VIEW);
sendIntent1.putExtra("sms_body", "def");
sendIntent1.putExtra("address", "012345678");
sendIntent1.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent1);
break;
}
}
});
});
}
}
@stewypost
You cant write statements anywhere inside an anonymous inner class anyways ignoring the poor syntax
To do this first declare your
OnClickListner listner = (View v) ->
{
// your code
};
then call
button1.setOnClickListener(listner);
button2.setOnClickListener(listner);
Sent from my GT-S5302 using Tapatalk 2

changing from a window to another

Hi Im just starting to learn android code and I dont understand why this doesnt work. Just for as a test I wanted to create 2 pages (or 1 page and 1 popup page but i thought 2 pages would be easier to make), and a way to get from one to the other, which I thought could be solved easly with a button on each page which takes me to the other page.
So I started with this:
HTML:
final ImageButton button = (ImageButton) findViewById(R.id.imageButton);
button.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
setContentView(R.layout.layout2);
} });
Basicually I start in a layout called activity_my which has a button and when I press it I get to the next page called layout2. I thought I could just make a similiar function to get back to my original page:
HTML:
final Button button2 = (Button) findViewById(R.id.button);
button2.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
setContentView(R.layout.activity_my);
}
});
But now I get error for whatever reason
Hi,
Basically in android, a 'page' is an Activity (class where you attach a XML layout).
So you must create 2 activity with 2 layout.
To switch activity use this foloowing code :
HTML:
Intent intent = new Intent(this, YourSecondActivity.class);
startActivity(intent);

[Q] Open another screen on button click

Hello, I'm extremely new to both android application developing, and this forum.
I am trying to open another screen ( activity ) when a button ( readyButton ) on the splash screen is pressed. I've tried at least ten different times with different tutorials to no avail, this is my current code which didn't work, and instead forces the app to crash.
Code:
public class MainActivity extends Activity {
// Called when the activity is first created.
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
OnClickListener listener = new OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
Intent intent = new Intent("SecondActivity");
startActivity(intent);
}
};
Button btn = (Button) findViewById(R.id.readybutton);
btn.setOnClickListener(listener);
}
}
Please help.
The button's name is 'readybutton'
the second activity name is 'SecondActivity'
also, where am I supposed to put this code into the java class? Here is how it is currently set up:
Code:
package com.unchat;
import android.app.Activity;
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.view.View.OnClickListener;
// Default Items
public class FirstActivity extends Activity {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
/** New button code start */
public class MainActivity extends Activity {
// Called when the activity is first created.
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
OnClickListener listener = new OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
Intent intent = new Intent("SecondActivity");
startActivity(intent);
}
};
Button btn = (Button) findViewById(R.id.readybutton);
btn.setOnClickListener(listener);
}
}
/** new button code end */
[user=439709]@override[/user]
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
[user=439709]@override[/user]
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);
}
}
// End of Default Items
incorrectly announces intent
Try like this.
Code:
Intent intent = new Intent(this, SecondActivity.class) ;
startActivity(intent);
and check whether your Listener
1. you need to use the full name of your activity, including the package name.
2. you need to declare the activity in your AndroidManifest.xml file before calling it.
rhmsoft said:
2. you need to declare the activity in your AndroidManifest.xml file before calling it.
Click to expand...
Click to collapse
Unless he want's to run an activity that's not his, like opening the contact list, but I think you're right in assuming he's looking to launch a second one of his own activities.
bornander said:
Unless he want's to run an activity that's not his, like opening the contact list, but I think you're right in assuming he's looking to launch a second one of his own activities.
Click to expand...
Click to collapse
I thought you had to declare all your activities in the manifest?
Log
Post the error log please

Categories

Resources