Saturday, October 24, 2015

// //

Display text/String randomly on textview in android programmatically

Hellow?! Today i will show you how to display text or string randomly in a textview in android.

Okay?! So let's get started...:)

First create xml file and named it, random:

random.xml


    
    
    
    
    
    
   

        
    
    


    


And in your MainActivity.java

import java.util.Random;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
      Random random = new Random();
      private static final String _CHAR = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
      private static final int RANDOM_STR_LENGTH = 12;
      EditText edit;
      TextView text_Random;
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
      
          setContentView(R.layout.capcha);
            
 
            text_Random = (TextView) findViewById(R.id.captcha);
          edit = (EditText) findViewById(R.id.putcaptcha);
            
            text_Random.setText(getRandomString());
            
            
            Button btn = (Button) findViewById(R.id.button1);
            btn.setOnClickListener(new OnClickListener() {
                  @Override
                  public void onClick(View v) {
                   String waah = edit.getText().toString();
                   String waah2 =  text_Random.getText().toString();
                   
                 
                   
                   if (edit.getText().toString().equalsIgnoreCase(""+waah2))  {
                       // Toast.makeText(Main.this, "Genius! You can click dismiss now! " , Toast.LENGTH_LONG).show();
                    finish();
                 
                   }
                   //    TextView text_Random = (TextView) findViewById(R.id.captcha);
                      //  text_Random.setText(getRandomString());
                  }
            });
      
    }
    public String getRandomString(){
            StringBuffer randStr = new StringBuffer();
            for (int i = 0; i < RANDOM_STR_LENGTH; i++) {
                  int number = getRandomNumber();
                  char ch = _CHAR.charAt(number);
                  randStr.append(ch);
            }
            return randStr.toString();
      }

      private int getRandomNumber() {
            int randomInt = 0;
            randomInt = random.nextInt(_CHAR.length());
            if (randomInt - 1 == -1) {
                  return randomInt;
            } else {
                  return randomInt - 1;
            }
      }
      


Let me know, if you are having problems!

HAPPY CODING!