Encode and Decode data in android

XML File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="match_parent"    
    android:layout_height="match_parent"    
    android:orientation="vertical"    
    android:weightSum="1">

    <RelativeLayout        
        android:layout_width="match_parent"        
        android:layout_height="match_parent"        
        android:layout_weight=".5"        
        android:orientation="vertical">

        <TextView            
          android:layout_width="match_parent"            
          android:layout_height="wrap_content"            
          android:gravity="center"            
          android:text="@string/Encode"            
          android:textColor="@android:color/holo_blue_dark"            
          android:textSize="@dimen/twenty" />

        <EditText            
          android:id="@+id/editEncode"            
          android:layout_width="match_parent"            
          android:layout_height="wrap_content"            
          android:layout_above="@+id/txtEncoded"            
          android:layout_alignParentLeft="true"            
          android:layout_alignParentStart="true"            
          android:gravity="center"            
          android:hint="@string/EnterText" />

        <TextView            
          android:id="@+id/txtEncoded"            
          android:layout_width="wrap_content"            
          android:layout_height="wrap_content"            
          android:layout_centerHorizontal="true"            
          android:layout_centerVertical="true"            
          android:layout_marginTop="@dimen/twenty"            
          android:textIsSelectable="true"            
          android:text="Encode Text" />

        <Button            
          android:id="@+id/btEncode"            
          android:layout_width="wrap_content"            
          android:layout_height="wrap_content"            
          android:layout_alignParentBottom="true"            
          android:layout_centerHorizontal="true"            
          android:text="@string/Encode" />

    </RelativeLayout>

    <RelativeLayout        
       android:layout_width="match_parent"        
       android:layout_height="match_parent"        
       android:layout_weight=".5">

        <LinearLayout         
           android:layout_width="match_parent"     
           android:layout_height="5dp"
           android:background="@android:color/black"></LinearLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/Decode"
            android:textColor="@android:color/holo_blue_dark"
            android:textSize="@dimen/twenty" />

        <EditText
            android:id="@+id/editDecode"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/txtDecoded"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:gravity="center"
            android:hint="@string/EnterText" />

        <TextView
            android:id="@+id/txtDecoded"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginTop="@dimen/twenty"
            android:text="Decode Text"
            android:textIsSelectable="true" />

        <Button
            android:id="@+id/btDecode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:text="@string/Decode" />
    </RelativeLayout>

</LinearLayout>

Activity Class


public class EncodeDecodeActivity extends AppCompatActivity implements View.OnClickListener {

    private final String TAG = EncodeDecodeActivity.class.getSimpleName();
    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.encode);

        initializeValues();
    }

    private void initializeValues() {

        Button btEncode = (Button) findViewById(R.id.btEncode);
        Button btDecode = (Button) findViewById(R.id.btDecode);

        btEncode.setOnClickListener(this);
        btDecode.setOnClickListener(this);
    }

    @Override    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btEncode:
                encodeData();
                break;

            case R.id.btDecode:
                decodeData();
                break;
        }
    }

    private void encodeData() {
        EditText editEncode = (EditText) findViewById(R.id.editEncode);
        TextView txtEncode = (TextView) findViewById(R.id.txtEncoded);
        String encode = editEncode.getText().toString().trim();
        if (!encode.isEmpty()){
            try {
                byte[] encodeByte = encode.getBytes("UTF-8");
                String strEncode = Base64.encodeToString(encodeByte, Base64.DEFAULT);
                txtEncode.setText(strEncode);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }else {
            Toast.makeText(this, "Enter your text to encode", Toast.LENGTH_SHORT).show();
        }
    }

    private void decodeData() {
        EditText editDecode = (EditText) findViewById(R.id.editDecode);
        TextView txtDecode = (TextView) findViewById(R.id.txtDecoded);

        String decode = editDecode.getText().toString().trim();
        if (!decode.isEmpty()){
            try {

                byte[] decodeByte = Base64.decode(decode, Base64.DEFAULT);
                String strDecode = new String(decodeByte, "UTF-8");
                txtDecode.setText(strDecode);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }else {
            Toast.makeText(this, "Enter your text to decode", Toast.LENGTH_SHORT).show();
        }
    }
}

View Image

Share this blog if you like this post:

No comments:

Post a Comment

Privacy Policy

What information do we collect? – We may collect information such as device name, device id, client user-agent for advertising. What ...