String Length Example


how to count string character length in android :

1) xml file:

<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:layout_gravity="center"
    android:background="#1ea076"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:gravity="center" >
        
        <TextView
             android:id="@+id/srtcharacter0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="String : "
            android:textColor="#fff"
            android:textSize="18sp" />

        <TextView
             android:id="@+id/srtcharacter2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           
            android:textColor="#fff"
            android:textSize="18sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/srtcharacter1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="String Length : "
            android:textColor="#fff"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/srtcharacter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#fff"
            android:textSize="18sp" />
    </LinearLayout>


</LinearLayout>


2) MainActivity:

package com.example.stringcharacter;

import com.example.stringcharecter.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

TextView txt1, txt2;

String s1 = "Parthiv MCA";

int strLength = s1.length();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txt1 = (TextView) findViewById(R.id.srtcharacter);
txt1.setText("" + s1.length());

txt2 = (TextView) findViewById(R.id.srtcharacter2);
txt2.setText("" + s1);
}


}


Output Screen:

                           



  Enjoy coding.......

No comments:

Post a Comment

back to top