How do I call a number ending with '#' sign using android dialer?
My code is as follows,
EditText firstNumber;
Button btnAdd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main23);
Toast.makeText(Main23Activity.this,
"Type in your Pin Number", Toast.LENGTH_LONG).show();
btnAdd = (Button)findViewById(R.id.button12);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
numberSign = "#";
EditText et = (EditText) findViewById(R.id.editText6);
String text= et.getEditableText().toString();
Toast.makeText(Main23Activity.this,
"Adding Money to your account has never been this easy", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:*221*" + text + numberSign ));
startActivity(intent);
} catch (Exception e) {
}
}
});
}
}
This is how you call a number ending with '#' using android dialer. You need to encode and then parse.
Try the below code,
String text = et.getText().toString();
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + Uri.encode("*221*" + text + "#")));
startActivity(intent);
Using this link you can get unicode and hex values
for example 36 can be stored in strings.xml as
<string name="abcd">36</string>
Related
I'm trying to read a different .txt file (in a raw folder) in a second activity TextView depending on what button is pressed in MainActivity (the previous activity), but it doesn't work. I'm using the .putextras method and here is my code of the MainActivity:
ImageButton but1=(ImageButton) findViewById(R.id.imageButton2);
but1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent int1=new Intent(MainActivity.this,SecondActivity.class);
int1.putExtra("Thistext", "textnumberone");
startActivity(int1);
finish();
}
});
ImageButton but2(ImageButton) findViewById(R.id.imageButton3);
but2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent int2=new Intent(MainActivity.this,SecondActivity.class);
int2.putExtra("Thistext", "textnumbertwo");
startActivity(int2);
finish();
}
});
Here is my code of the SecondActivity with the Bundle..
Bundle extradata = getIntent().getExtras();
TextView tv = (TextView)findViewById(R.id.firsttextView);
vitautori.setText(extradata.getString("Thistext"));
if (extradata.equals("textnumberone")) {
String texttxt = "";
StringBuffer sbuffer = new StringBuffer();
InputStream is = this.getResources().openRawResource(R.raw.file);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
while ((texttxt = reader.readLine()) !=null){
sbuffer.append(texttxt + "n");
}
tv.setText(sbuffer);
is.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
You are currently comparing the Bundle data to a string in
if (extradata.equals("textnumberone"))
This will not work, you have to extract the String data first. Try this:
Bundle extradata = getIntent().getExtras();
String textString = extradata.getString("Thistext");
if (textString.equals("textnumberone")) {
One other thing:
You set the String from the Bundle to a (I suppose) TextView in
vitautori.setText(extradata.getString("Thistext"));
but I don't see the initialization of vitautori anywhere. So make sure the it is initialized or this will crash.
When creating the Intent (both places, of course), I suggest you try to set the type to text:
int1.setType("text/plain");
See if this helps.
*UPDATED*still haveing issues please if you see any errors please let me know, i would like to put my logcat in but i cant access it due to the errors .......I'm having issues with my login and registration mechanism. I'm probably doing something wrong, but when i try to test out the login my app crashes with the message "...has suddenly stopped." msg. Also, my textview that goes to my registration class isnt responding. Can some please help me?
I know there is probably a lot of errors, but be kind I'm new to this :)
If anyone is generous or just bored and wanted to personally help me out with issues please leave your email
Below is the code for my login class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setting default screen to login.xml
setContentView(R.layout.login);
TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
// Listening to register new account link
registerScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), SignUp.class);
startActivity(i);
// create a instance of SQLite Database
loginDataBaseAdapter = new LoginDataBaseAdapter(this);
loginDataBaseAdapter = loginDataBaseAdapter.open();
// Get The Reference Of Buttons
btnSignIn = (Button) findViewById(R.id.btnLogin);
}
// Methods to handleClick Event of Sign In Button
public void signIn(View V) {
try{
final Dialog dialog = new Dialog(LoginScreen.this);
dialog.setContentView(R.layout.login);
dialog.setTitle("Login");
// get the References of views
final EditText loginUsername = (EditText) dialog
.findViewById(R.id.liUsername);
final EditText loginPassword = (EditText) dialog
.findViewById(R.id.liPassword);
Button btnSignIn = (Button) dialog.findViewById(R.id.btnLogin);
}catch(Exception e){
Log.e("tag", e.getMessage());
}
// Set On ClickListener
btnSignIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// get The User name and Password
String username = loginUsername.getText().toString();
String password = loginPassword.getText().toString();
// fetch the Password form database for respective user name
String storedPassword = loginDataBaseAdapter
.getSingleEntry(username);
// check if the Stored password matches with Password entered by
// user
if (password.equals(storedPassword)) {
Toast.makeText(LoginScreen.this,
"Congrats: Login Successful", Toast.LENGTH_LONG)
.show();
dialog.dismiss();
} else {
Toast.makeText(LoginScreen.this,
"User Name or Password does not match",
Toast.LENGTH_LONG).show();
dialog.show();
}
}
#Override
public void startActivity(Intent intent) {
// TODO Auto-generated method stub
try{
super.startActivity(intent);
Intent mainpage = new Intent(LoginScreen.this, MainPage.class);
startActivity(mainpage);
finish();
}catch(Exception e){
Log.e("tag", e.getMessage());
}
}
#Override
protected void onDestroy() {
try{
super.onDestroy();
// Close The Database
loginDataBaseAdapter.close();
}catch(Exception e){
Log.e("onDestroy - Error", e.getMessage());
}
MY registration class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set View to register.xml
setContentView(R.layout.signup);
TextView loginScreen = (TextView) findViewById(R.id.link_to_login);
// Listening to Login Screen link
loginScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Closing registration screen
// Switching to Login Screen/closing register screen
finish();
// get Instance of Database Adapter
loginDataBaseAdapter = new LoginDataBaseAdapter(this);
loginDataBaseAdapter = loginDataBaseAdapter.open();
// Get References of Views
reg_fullname = (EditText) findViewById(R.id.reg_fullname);
reg_username = (EditText) findViewById(R.id.reg_username);
reg_email = (EditText) findViewById(R.id.reg_email);
reg_password = (EditText) findViewById(R.id.reg_password);
reg_confirmpassword = (EditText) findViewById(R.id.reg_confirmpassword);
btnRegister = (Button) findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String fullname = reg_fullname.getText().toString();
String username = reg_username.getText().toString();
String password = reg_password.getText().toString();
String email = reg_email.getText().toString();
String confirmPassword = reg_confirmpassword.getText()
.toString();
// check if any of the fields are vacant
if (username.equals("") || password.equals("")
|| confirmPassword.equals("")) {
Toast.makeText(getApplicationContext(), "Field Vaccant",
Toast.LENGTH_LONG).show();
return;
}
// check if both password matches
if (!password.equals(confirmPassword)) {
Toast.makeText(getApplicationContext(),
"Password does not match", Toast.LENGTH_LONG)
.show();
return;
} else {
// Save the Data in Database
loginDataBaseAdapter.insertEntry(username, password);
Toast.makeText(getApplicationContext(),
"Account Successfully Created ", Toast.LENGTH_LONG)
.show();
}
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
try{
super.onDestroy();
loginDataBaseAdapter.close();
}catch(Exception e){
Log.e("onDestroy - Error", e.getMessage());
}}
The best way to know your code errors it's, put all your code in a block try-catch in all functions..
try{
//your code here
}catch(Exception e){
Log.e("tag", e.getMessage());
}
in Logcat will be apears in red color the errors of your code, "tag" it's a way to differentiate the erros like:
#Override
protected void onDestroy() {
try{
super.onDestroy();
loginDataBaseAdapter.close();
}catch(Exception e){
Log.e("onDestroy - Error", e.getMessage());
}
}
I hope this helps...
Below is the code that i used to open an app on the google paly.
final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (Exception e) {
Log.d(tag,"Message ="+e);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName)));
}
I have provided the hyperlink at the last of the text of the textview.how i can open an app on the google paly when user click on the hyperlink.
Use the below code in xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="<market_url>"
android:id="#+id/openPlaystore"
android:autoLink="all"
android:linksClickable="true">
</TextView>
OR
Add OnClickListener to the textview
TextView tv = (TextView)findViewById(R.id.openPlaystore);
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (Exception e) {
Log.d(tag,"Message ="+e);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName)));
}
}
});
I have an app where I do the same... I use this code:
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
TextView tv = (TextView)v;
if(! (tv.getSelectionStart()==-1 && tv.getSelectionEnd()==-1))
// Fired only when you touch the part of the text that is hyperlinked
// do something
}
Note that this works ONLY if you already successfully have your TextView display the hyperlink. Also, you usually don't need any code, since a hyperlink is resolved automatically by the TextView, but this code allows you to do additional processing.
I'm developing an app which will do multiple method in a single input. For example calculating square circumference and area, I give only one EditText and two button. But when I run the app, if I give an input and click the area button it won't do the calculation until I click the circumference button. And same goes if I change the input. Here is the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.square);
etSide = (EditText) findViewById(R.id.etSquare);
tvResult = (TextView) findViewById(R.id.tvSquare);
Button btnCir = (Button) findViewById(R.id.btnSqrCir);
btnCir.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
countCir();
}
});
Button btnArea = (Button) findViewById(R.id.btnSqrArea);
btnArea.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
countArea();
}
});
}
private void countArea() {
try {
side = etSide.getText().toString();
s = parseInt(side);
area = s * s;
tvResult.setText("Area = " + cir);
} catch (NumberFormatException ex){
Toast.makeText(getApplicationContext(), "Oops, you seem haven't enter the side length", Toast.LENGTH_LONG).show();
}
}
private void countCir() {
try {
side = etSide.getText().toString();
s = parseInt(side);
cir = 4 * s;
tvResult.setText("Circumference = " + area);
} catch (NumberFormatException ex){
Toast.makeText(getApplicationContext(), "Oops, you seem haven't enter the side length", Toast.LENGTH_LONG).show();
}
}
Any better idea? Really need help...
It looks like you have your variables backwards. For example:
private void countArea() {
try {
side = etSide.getText().toString();
s = parseInt(side);
area = s * s;
tvResult.setText("Area = " + cir); // <-- here cir doesn't have a value until you click the circumference button
} catch (NumberFormatException ex){
Toast.makeText(getApplicationContext(), "Oops, you seem haven't enter the side length", Toast.LENGTH_LONG).show();
}
}
So your TextView would display ""Area = ""
It looks to me like you want
tvResult.setText("Area = " + cir);
to be
tvResult.setText("Area = " + area);
Let me know if I'm not understanding you correctly
Note:
For your Toast you should use this or YourActivityName.this for Context instead of getApplicationContext()
One other suggestion I might make since your onClick()s only call a method, to make it simpler you could use one listener like this
public void onCreate(...)
{
...
btnCir.setOnClickListener(this);
btnArea.setOnClickListener(this);
...
}
public void onClick(View v)
{
switch(v.getId()) // get the id of the Button clicked
{
case (R.id.btnSqrArea): // call appropriate method
countArea();
break;
case (R.id.btnSqrCir):
countCir();
break;
}
}
You would just have to remember to add implements OnClickListener to your class definition. That's just a preference but worth mentioning.
How do I make a phone call when I get number from edittext? I try this but it does not work. I have a problem with passing variable number to Uri.parse() must be inside void the gettext?
public String string;
public String number;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText txtcallnumber;
txtcallnumber = (EditText)findViewById(R.id.callnumber);
string = txtcallnumber.getText().toString().trim();//There no work call
number = "tel:" + string;
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
call();
}
});
}
public void call() {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
//callIntent.setData(Uri.parse("tel:xxxxxxx")); //This work
string = txtcallnumber.getText().toString().trim();
number = "tel:" + string;//There work call
callIntent.setData(Uri.parse(number));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
Log.e("helloandroid dialing example", "Call failed");
}
You need to add Permission inside in your manifest file ::
<uses-permission android:name="android.permission.CALL_PHONE">
Inside your call() method try to use these Intent::
number=edittext.getText().trim();
no="tel:"+number;
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(no)));
in onCreate method there is no need for :
string = txtcallnumber.getText().toString().trim();
number = "tel:" + string;
because onCreate is pretty much used for setting up the display and there will be no input by the user before the screen is set up but in onCall method after entering the text and pressing the button then it wouldd...you know.
Plus you should have this to check for nothing entered:
if(string!=null){
try{try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
string = txtcallnumber.getText().toString().trim();
number = "tel:" + string;
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(number)));
} catch (ActivityNotFoundException activityException) {
Log.e("helloandroid dialing example", "Call failed");
}}else{
Log.d("helloandroid dialing example","nothing entered");}