This post answers some of the common android interview questions and answers.
How to send SMS in Android?
You may send sms either using SmsManager or by invoking Built-in SMS application
Sending SMS using SmsManager API
SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage("", null, "< message body>", null, null);
SmsManager require, SMS_SEND permission in your android mainfeast.
Sending SMS by invoking Built-in SMS application
Intent sendIntent = new Intent(Intent.ACTION_VIEW); sendIntent.putExtra("sms_body", “"); sendIntent.setType("vnd.android-dir/mms-sms"); startActivity(sendIntent);
How to Make a call in Android?
Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:03777788")); startActivity(callIntent);
How to send Email in Android?
Android Intent is the handy way to send Email using your app.
String to = toEmail.getText().toString(); String subject = emailSubject.getText().toString(); String message = emailBody.getText().toString(); Intent email = new Intent(Intent.ACTION_SEND); email.putExtra(Intent.EXTRA_EMAIL, new String[] { to }); email.putExtra(Intent.EXTRA_SUBJECT, subject); email.putExtra(Intent.EXTRA_TEXT, message); // need this to prompts email client only email.setType("message/rfc822"); startActivity(Intent.createChooser(email, "Choose an Email client"));
How to integrating sharing in Android?
Android API made sharing feature handy. You don’t need to integrate the third party applications SDK’s like Facebook, twitter. Just a step to use share intent and it makes your application ready to share data to your social networks.
Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send."); sendIntent.setType("text/plain"); startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
How to open link in Android Browser?
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://stacktips.com")); startActivity(browserIntent);
How to Display HTML in Android TextView?
In android there is a lovely class android.text.HTML
that processes HTML strings into displayable styled text. Currently android doesn’t support all HTML tags.
Html.formHtml()
method takes an Html.TagHandler
and an Html.ImageGetter
as arguments as well as the text to parse. We can parse null as for the Html.TagHandler but you’d need to implement your own Html.ImageGetter as there isn’t a default implementation. The Html.ImageGetter needs to run synchronously and if you’re downloading images from the web you’ll probably want to do that asynchronously. But in my example I am using the images from resources to make my ImageGetter implementation simpler.
String htmlText = "<html><body>....</body></html>"; TextView htmlTextView = (TextView)findViewById(R.id.html_text); htmlTextView.setText(Html.fromHtml(htmlText, new ImageGetter(), null));
How to display Android Toast?
Toast is a notification message that pop up, display a certain amount of time, and automatically fades in and out, most people just use it for debugging purpose. Below is the code snippets to create a Toast message
//display in short period of time Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show(); //display in long period of time Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_LONG).show();
How to Pass data between Activities in android
We an pass values between the activities, by using Bundles. Use below code to send data from ActivityA
String name="StackTips"; String url="http://stacktips.com"; Intent intent=new Intent(ActivityA.this, ActivityB.class); intent.putExtra("name", name); intent.putExtra("url", url); startActivity(intent);
Now, you can retrieve the data in ActivityB using below code
Bundle bundle = new Bundle(); bundle = getIntent().getExtras(); String name = bundle.getString("name"); String url = bundle.getString("url");