Monday, July 27, 2015

// //

Simple addition using random number picker

Today im going to show you a simple addition using random number picker...
Main.xml




    
Main.java

import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Main extends Activity
{
 
 int min = 65;
 int max = 80;
 private TextView Tv1,Tv2;
 EditText edit;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
        
        final  Random r = new Random();
        
        
        
        Button btn = (Button)findViewById(R.id.button1);
        Button gen = (Button)findViewById(R.id.Button01);
        Tv1 = (TextView) findViewById(R.id.tv1);
        Tv2 = (TextView) findViewById(R.id.tv2);
      //  edit = (EditText) findViewById(R.id.edit);
        
        
       
        
        
        final TextView result = (TextView)findViewById(R.id.textView1);
        btn.setOnClickListener(new OnClickListener()
        {
                 
                  @Override
                  public void onClick(View v)
                  {
                  // String editText1 = edit.getText().toString();
                 
                        int x = new Integer(Tv1.getText().toString());
                        int y = new Integer(Tv2.getText().toString());
                        int sum = x + y;
                        result.setText(""+sum);
                        
                      
                  }
            });
  
    
        gen.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    
     Tv1.setText(String.valueOf(r.nextInt(max - min + 1)+ min));
           Tv2.setText(String.valueOf(r.nextInt(max - min + 1)+ min));
   
   }
    
        
    });
    
    
    
    }
}

Now were done!
Test now on the emulator.