Wednesday, 24 July 2019

Android rating bar


import android.os.Bundle;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.view.*;
import android.widget.Button; 
import android.widget.RatingBar; 
import android.widget.Toast; 

public class MainActivity extends Activity {
RatingBar r1;
Button b1;

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

        public void addListenerOnButtonClick(){ 
               r1=(RatingBar)findViewById(R.id.ratingBar1); 
               b1=(Button)findViewById(R.id.button1); 
               //Performing action on Button Click 
               b1.setOnClickListener(new OnClickListener(){ 
        
                   @Override 
                   public void onClick(View arg0) { 
                       //Getting the rating and displaying it on the toast 
                       String rating=String.valueOf(r1.getRating()); 
                       Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show(); 
                   } 
                    
               }); 
           } 
} 

Android spinner


import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;
public class MainActivity extends Activity implements OnItemSelectedListener{
Spinner s1;
String[] country = { "India", "USA", "China", "Japan", "Other",  }; 
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_main);
                        s1=(Spinner)findViewById(R.id.spinner1);
                        s1.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the country list 
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,country); 
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
   //Setting the ArrayAdapter data on the Spinner 
        s1.setAdapter(aa); 
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String item = parent.getItemAtPosition(position).toString();
        Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
    }
public void onNothingSelected(AdapterView<?> arg0)
   {
   }
}

Android check box


import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class MainActivity extends Activity {
CheckBox c1,c2,c3;
Button b1;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_main);
                        c1=(CheckBox) findViewById(R.id.checkBox1);
                        c2=(CheckBox) findViewById(R.id.checkBox2);
                        c3=(CheckBox) findViewById(R.id.checkBox3);
                        b1=(Button) findViewById(R.id.button1);
                        b1.setOnClickListener(L1);
            }
public OnClickListener L1=new OnClickListener()
    {
    public void onClick(View v)    {
            String h1="",h2="",h3="",h4;
            if(v==b1) {
                  if(c1.isChecked()==true)
                         h1=c1.getText().toString();
                  if(c2.isChecked()==true)
                         h2=c2.getText().toString();
                  if(c3.isChecked()==true)
                         h3=c3.getText().toString();
                    h4=h1 +" "+h2 +" "+h3;
                   Toast.makeText(getBaseContext(),h4,Toast.LENGTH_LONG).show();
            }
    }
    }; }

android Radio Button


import android.widget.*; 
import android.view.*;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
RadioButton r1,r2;
RadioGroup rg;
Button b1;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_main);
                        r1=(RadioButton) findViewById(R.id.radio0);
                        r2=(RadioButton) findViewById(R.id.radio1);
                         rg=(RadioGroup) findViewById(R.id.radioGroup1);
                         b1=(Button) findViewById(R.id.button1);
                         b1.setOnClickListener(L1);
            }
public OnClickListener L1=new OnClickListener()
    {
    public void onClick(View v)
    {
            if(v==b1)
            {
                   if(r1.isChecked()==true)
                        Toast.makeText(getBaseContext(),r1.getText(),Toast.LENGTH_LONG).show();
                   if(r2.isChecked()==true)
                        Toast.makeText(getBaseContext(),r2.getText(),Toast.LENGTH_LONG).show();
            }
        }
    };
}

Calculator


import android.widget.*;
import android.view.*;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
EditText t1,t2,t3;
Button b1,b2,b3,b4,b5,b6;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_main);
                        t1=(EditText) findViewById(R.id.editText1);
                        t2=(EditText) findViewById(R.id.editText2);
                        t3=(EditText) findViewById(R.id.editText3);
                        b1=(Button) findViewById(R.id.button1);
                        b2=(Button) findViewById(R.id.button2);
                        b3=(Button) findViewById(R.id.button3);
                        b4=(Button) findViewById(R.id.button4);
                        b5=(Button) findViewById(R.id.button5);
                        b6=(Button) findViewById(R.id.button6);
                        b1.setOnClickListener(s1);
                        b2.setOnClickListener(s1);
                        b3.setOnClickListener(s1);
                        b4.setOnClickListener(s1);
                        b5.setOnClickListener(s1);
                        b6.setOnClickListener(s1);     }
public OnClickListener s1=new OnClickListener()
            {
               public void onClick(View v){
                           if(v==b1){
                           int x=Integer.parseInt(t1.getText().toString());
                           int y=Integer.parseInt(t2.getText().toString());
                           int z=x+y;
                           t3.setText(String.valueOf(z));
                          }
                           if(v==b2){
                                       int x=Integer.parseInt(t1.getText().toString());
                                       int y=Integer.parseInt(t2.getText().toString());
                                       int z=x-y;
                                       t3.setText(String.valueOf(z));
                                      }
                           if(v==b3){
                                       int x=Integer.parseInt(t1.getText().toString());
                                       int y=Integer.parseInt(t2.getText().toString());
                                       int z=x*y;
                                       t3.setText(String.valueOf(z));
                                      }
                           if(v==b4){
                                       int x=Integer.parseInt(t1.getText().toString());
                                       int y=Integer.parseInt(t2.getText().toString());
                                       int z=x/y;
                                       t3.setText(String.valueOf(z));
                                      }
                           if(v==b5){
                                       int x=Integer.parseInt(t1.getText().toString());
                                       int y=Integer.parseInt(t2.getText().toString());
                                       int z=x%y;
                                       t3.setText(String.valueOf(z));
                                      }
                           if(v==b6){
                                       t1.setText("");
                                       t2.setText("");
                                       t3.setText("");
                                      }
               }
             };
}



simple interest calculations


import android.widget.*;
import android.view.*;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button b1;
EditText t1,t2,t3,t4;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
                  t1=(EditText) findViewById(R.id.editText1);
                  t2=(EditText) findViewById(R.id.editText2);
                  t3=(EditText) findViewById(R.id.editText3);
                  t4=(EditText) findViewById(R.id.editText4);
                  b1=(Button) findViewById(R.id.button1);
                  b1.setOnClickListener(s1);
      }
      public OnClickListener s1=new OnClickListener()
      {
         public void onClick(View v){
                     if(v==b1){
                     float p=Float.parseFloat(t1.getText().toString());
                     float n=Float.parseFloat(t2.getText().toString());
                     float r=Float.parseFloat(t3.getText().toString());
                     float si=p*n*r/100;
                     t4.setText(String.valueOf(si));
                    }
         }};}