Convert String value to Boolean in Java Android with example

How to change string variable true,false value directly into boolean true-false in android and show using TextView.

In this tutorial we are simply converting string variable value which is true and false into boolean true false so app developer can use them just like as boolean values. So here is the complete step by step tutorial for Convert String value to Boolean in Java Android with example.

android-project-download-code-button

How to Convert String value to Boolean in Java Android with example.

Code for MainActivity.java file.

 package com.convertstringvaluetoboolean_android_examples.com;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends Activity {

 TextView textview1,textview2,textview3,textview4;
 
 //Initializing string variable.
 String value = "true" ;
 
 String value2 = "TRue" ;
 
 //Initializing Boolean variable.
 Boolean booleanData ;
 
 Boolean booleanData2 ;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 textview1 = (TextView)findViewById(R.id.textView1);
 textview2 = (TextView)findViewById(R.id.textView2);
 textview3 = (TextView)findViewById(R.id.textView3);
 textview4 = (TextView)findViewById(R.id.textView4);
 
 //Converting string variable to Boolean.
 booleanData = Boolean.valueOf(value);
 
 //Converting string variable to Boolean with ignore capital or small case.
 booleanData2 = value2.equalsIgnoreCase("true");
 
 
 //Showing string on TextView.
 textview1.setText("String = " + value);
 textview3.setText("String with Case Change = " + value2);
 
 //printing Boolean on screen.
 
 textview2.setText("Boolean = " + booleanData);
 textview4.setText("Boolean with ignore case = " + booleanData2);
 
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.convertstringvaluetoboolean_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="String"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

 <TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="29dp"
 android:text="Boolean"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

 <TextView
 android:id="@+id/textView3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView2"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="29dp"
 android:text="String-2"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

 <TextView
 android:id="@+id/textView4"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView3"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="30dp"
 android:text="Boolean with ignore case"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

</RelativeLayout>

Screenshot:

Convert String value to Boolean in Java Android with example

Click here to download Convert String value to Boolean in Java Android with example project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *