Android Tutorial Day 10

Learning Objective: Let’s check out few components like RadioButton, , WebView, Toast, Soft Keyboard, Progressbar, Implicit Intent, Explicit Intent, Popup Menu, Checkbox, Switch Button, etc.

RadioButton Example (Source)

package example.javatpoint.com.radiobutton;  
  
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;  
import android.widget.RadioButton;  
import android.widget.RadioGroup;  
import android.widget.Toast;  
  
public class MainActivity extends AppCompatActivity {  
    Button button;  
    RadioButton genderradioButton;  
    RadioGroup radioGroup;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        radioGroup=(RadioGroup)findViewById(R.id.radioGroup);  
    }  
    public void onclickbuttonMethod(View v){  
        int selectedId = radioGroup.getCheckedRadioButtonId();  
        genderradioButton = (RadioButton) findViewById(selectedId);  
        if(selectedId==-1){  
            Toast.makeText(MainActivity.this,"Nothing selected", Toast.LENGTH_SHORT).show();  
        }  
        else{  
            Toast.makeText(MainActivity.this,genderradioButton.getText(), Toast.LENGTH_SHORT).show();  
        }  
  
    }  
}  

XML Code for Radio Button

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical"  
    tools:context="example.javatpoint.com.radiobutton.MainActivity">  
  
    <TextView  
        android:id="@+id/textView1"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_marginTop="30dp"  
        android:gravity="center_horizontal"  
        android:textSize="22dp"  
        android:text="Single Radio Buttons" />  
  
  
  
    <!--   Default RadioButtons  -->  
  
    <RadioButton  
        android:id="@+id/radioButton1"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_gravity="center_horizontal"  
        android:text="Radio Button 1"  
        android:layout_marginTop="20dp"  
  
        android:textSize="20dp" />  
    <RadioButton  
        android:id="@+id/radioButton2"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="Radio Button 2"  
        android:layout_marginTop="10dp"  
  
        android:textSize="20dp" />  
  
  
    <View  
        android:layout_width="fill_parent"  
        android:layout_height="1dp"  
        android:layout_marginTop="20dp"  
        android:background="#B8B894" />  
  
    <TextView  
        android:id="@+id/textView2"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_marginTop="30dp"  
        android:gravity="center_horizontal"  
        android:textSize="22dp"  
        android:text="Radio button inside RadioGroup" />  
  
  
    <!--   Customized RadioButtons  -->  
  
  
    <RadioGroup  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:id="@+id/radioGroup">  
  
        <RadioButton  
            android:id="@+id/radioMale"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:text="  Male"  
            android:layout_marginTop="10dp"  
            android:checked="false"  
            android:textSize="20dp" />  
  
        <RadioButton  
            android:id="@+id/radioFemale"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:text="   Female"  
            android:layout_marginTop="20dp"  
            android:checked="false"  
  
            android:textSize="20dp" />  
    </RadioGroup>  
  
    <Button  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="Show Selected"  
        android:id="@+id/button"  
        android:onClick="onclickbuttonMethod"  
        android:layout_gravity="center_horizontal" />  
  
  
</LinearLayout>  

Webview Example

WebView mywebview = (WebView) findViewById(R.id.webView1);  
mywebview.loadUrl("http://www.adarshspatel.in/");  

Toast Example

Toast.makeText(getApplicationContext(), "Message", Toast.LENGTH_LONG).show();

How to close/hide the Android soft keyboard programmatically? (Source)

// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

Progressbar Example (Source)

ProgressDialog progressBar = new ProgressDialog(this);  
progressBar.setCancelable(true);//you can cancel it by pressing back button  
progressBar.setMessage("File downloading ...");  
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
progressBar.setProgress(0);//initially progress is 0  
progressBar.setMax(100);//sets the maximum value 100  
progressBar.show();//displays the progress bar  

Implicit Intent (Source)

Implicit Intent doesn’t specifiy the component. In such case, intent provides information of available components provided by the system that is to be invoked.

Intent intent=new Intent(Intent.ACTION_VIEW);  
intent.setData(Uri.parse("http://www.javatpoint.com"));  
startActivity(intent);  

Explicit Intent

Explicit Intent specifies the component. In such case, intent provides the external class to be invoked.

Intent i = new Intent(getApplicationContext(), ActivityTwo.class);  
startActivity(i);  

Popup Menu

<?xml version="1.0" encoding="utf-8"?>  
<menu xmlns:android="http://schemas.android.com/apk/res/android">  
    <item  
        android:id="@+id/one"  
        android:title="One" />  
    <item  
        android:id="@+id/two"  
        android:title="Two"/>  
    <item  
        android:id="@+id/three"  
        android:title="Three"/>  
</menu>  
  PopupMenu popup = new PopupMenu(MainActivity.this, button);  
                //Inflating the Popup using xml file  
                popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());  
  
                //registering popup with OnMenuItemClickListener  
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
                    public boolean onMenuItemClick(MenuItem item) {  
                        Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();  
                        return true;  
                    }  
                });  
  
                popup.show();//showing popup menu  

Checkbox Example (Source)

<?xml version="1.0" encoding="utf-8"?>  
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:app="http://schemas.android.com/apk/res-auto"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context="example.javatpoint.com.checkbox.MainActivity">  
  
  
    <CheckBox  
        android:id="@+id/checkBox"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginLeft="144dp"  
        android:layout_marginTop="68dp"  
        android:text="Pizza"  
        app:layout_constraintStart_toStartOf="parent"  
        app:layout_constraintTop_toTopOf="parent" />  
  
    <CheckBox  
        android:id="@+id/checkBox2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginLeft="144dp"  
        android:layout_marginTop="28dp"  
        android:text="Coffee"  
        app:layout_constraintStart_toStartOf="parent"  
        app:layout_constraintTop_toBottomOf="@+id/checkBox" />  
  
    <CheckBox  
        android:id="@+id/checkBox3"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginLeft="144dp"  
        android:layout_marginTop="28dp"  
        android:text="Burger"  
        app:layout_constraintStart_toStartOf="parent"  
        app:layout_constraintTop_toBottomOf="@+id/checkBox2" />  
  
    <Button  
        android:id="@+id/button"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginLeft="144dp"  
        android:layout_marginTop="184dp"  
        android:text="Order"  
        app:layout_constraintStart_toStartOf="parent"  
        app:layout_constraintTop_toBottomOf="@+id/checkBox3" />  
  
</android.support.constraint.ConstraintLayout>  
package example.javatpoint.com.checkbox;  
  
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;  
import android.widget.CheckBox;  
import android.widget.Toast;  
  
public class MainActivity extends AppCompatActivity {  
    CheckBox pizza,coffe,burger;  
    Button buttonOrder;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        addListenerOnButtonClick();  
    }  
    public void addListenerOnButtonClick(){  
        //Getting instance of CheckBoxes and Button from the activty_main.xml file  
        pizza=(CheckBox)findViewById(R.id.checkBox);  
        coffe=(CheckBox)findViewById(R.id.checkBox2);  
        burger=(CheckBox)findViewById(R.id.checkBox3);  
        buttonOrder=(Button)findViewById(R.id.button);  
  
        //Applying the Listener on the Button click  
        buttonOrder.setOnClickListener(new View.OnClickListener(){  
  
            @Override  
            public void onClick(View view) {  
                int totalamount=0;  
                StringBuilder result=new StringBuilder();  
                result.append("Selected Items:");  
                if(pizza.isChecked()){  
                    result.append("\nPizza 100Rs");  
                    totalamount+=100;  
                }  
                if(coffe.isChecked()){  
                    result.append("\nCoffe 50Rs");  
                    totalamount+=50;  
                }  
                if(burger.isChecked()){  
                    result.append("\nBurger 120Rs");  
                    totalamount+=120;  
                }  
                result.append("\nTotal: "+totalamount+"Rs");  
                //Displaying the message on the toast  
                Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();  
            }  
  
        });  
    }  
}  

Send SMS

//Getting intent and PendingIntent instance  
Intent intent=new Intent(getApplicationContext(),MainActivity.class);  
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);  
  
//Get the SmsManager instance and call the sendTextMessage method to send message                 
SmsManager sms=SmsManager.getDefault();  
sms.sendTextMessage("0000000000", null, "hello javatpoint", pi,null);  
<uses-permission android:name="android.permission.SEND_SMS"/>  

Broadcast Receiver (Source)

public class MyReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
      Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
   }
}
<application
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name"
   android:theme="@style/AppTheme" >
   <receiver android:name="MyReceiver">
   
      <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED">
         </action>
      </intent-filter>
   
   </receiver>
</application>

How do I pass data between Activities in Android application?

Check out answer on Stackoverflow

How to check if a service is running on Android?

Check out answer on Stackoverflow