Showing posts with label Android tutorials. Show all posts
Showing posts with label Android tutorials. Show all posts

Monday, 31 October 2016

Android Storage

Android gives a few choices to you to spare determined application information. The arrangement you pick relies on upon your particular needs, for example, whether the information ought to be private to your application or open to different applications (and the client) and how much space your information requires.

There are five different types of android storage.

1) Shared Preferences :-

Store private primitive information in key-esteem sets.

2) Internal Storage :-

Store private information on the gadget memory.

3) External Storage :-

Store open information on the mutual outside capacity.

4) SQLite Databases :-

Store organized information in a private database.

5) Network Connection :-

Store information on the web with your own particular system server.

Android gives an approach to you to uncover even your private information to different applications — with a substance supplier. A substance supplier is a discretionary segment that uncovered read/compose access to your application information, subject to whatever limitations you need to force. For more data about utilizing content suppliers, Content Providers documentation.

Monday, 17 October 2016

Android Activity LifeCycle

Activity means an action speaks to a solitary screen with a UI(User Interface). And its supports of java code .
There are seven different android activity lifecycle are described below .

1) onCreate() :-

Called when the movement is initially made. This is the place you ought to do the greater part of your    ordinary static set up: make sees, tie information to records, and so on.

2) onRestart() : -

Called after your action has been halted, preceding it being begun once more.

3) onStart() : -

Called when the movement is getting to be obvious to the client. Taken after by onResume() if the action goes to the frontal area, or onStop() on the off chance that it gets to be covered up.

4) onResume() : -

Called when the action will begin interfacing with the client. Now your movement is at the highest point of the action stack, with client input going to it.

5) onPause () : -

Called as a major aspect of the movement lifecycle when an action is going away from plain sight, yet has not (yet) been slaughtered.

6) onStop() : -

Called when you are no more extended unmistakable to the client. You will next get either onRestart(), onDestroy(), or nothing, contingent upon later client movement.

7) onDestroy() : -

The last call you get before your action is decimated. This can happen either on the grounds that the movement is completing (somebody called complete() on it, or in light of the fact that the framework is incidentally wrecking this occasion of the action to spare space. You can recognize these two situations with the isFinishing() strategy.

        Diagram or flow chart of Android Activity LifeCycle shown below .

Android Activity LifeCycle
Android Activity LifeCycle Figure

Code for Activity LifeCycle :-

Here in the code we are using "TOAST" (or) we can use "Log.d" content display in the Logcat. If we  want to try Log.d and replace in place of Toast. For 

       Log.id("Activity lifecycle", onCreate()");

package com.kiranapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import android.view.Menu;

public class MainActivity extends AppCompatActivity {

@Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 Toast.makeText(MainActivity.this,"onCreate() Method",Toast.LENGTH_SHORT);
 }

@Override protected void onStart() {
 super.onStart();
 Toast.makeText(MainActivity.this,"onStart() Method",Toast.LENGTH_SHORT);
 }
 
@Override protected void onResume() {
 super.onResume();
 Toast.makeText(MainActivity.this,"onResume() Method",Toast.LENGTH_SHORT);
 }

@Override protected void onPause() {
 super.onPause();
 Toast.makeText(MainActivity.this,"onPause() Method",Toast.LENGTH_SHORT);
 }

@Override protected void onStop() {
 super.onStop();
 Toast.makeText(MainActivity.this,"onStop() Method",Toast.LENGTH_SHORT);
 }

@Override protected void onRestart() {
 super.onRestart();
 Toast.makeText(MainActivity.this,"onRestart() Method",Toast.LENGTH_SHORT);
 }
 
@Override protected void onDestroy() {
 super.onDestroy();
 Toast.makeText(MainActivity.this,"onDestroy() Method",Toast.LENGTH_SHORT);
 }
}

TimePicker

TimePicker is android widget. And it allows to select the time by hour, minute. But we can't select the second only. It is nothing but FrameLayout class (i.e its subclass).So please follow example timepicker code below detail.

activitymain.xml :-

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"  
 tools:context="com.kiranapp.MainActivity">
<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="time :" 
 android:id="@+id/textView"  
 android:layout_marginTop="20dp" 
 android:layout_alignParentTop="true"  
 android:layout_centerHorizontal="true" 
 android:layout_gravity="center_horizontal" />
<Button 
 android:layout_width="wrap_content"  
 android:layout_height="wrap_content" 
 android:text="Select time"  
 android:id="@+id/button" 
 android:layout_marginTop="10dp" 
 android:layout_below="@+id/textView" 
 android:layout_centerHorizontal="true" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java :-

package com.kiranapp;

import android.app.TimePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    Button selectbtn;
    TextView displaytime;
    Calendar c=Calendar.getInstance();
    int chr,cmin;

@Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 selectbtn=(Button)findViewById(R.id.button);
 displaytime=(TextView)findViewById(R.id.textView);
 selectbtn.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
new TimePickerDialog(MainActivity.this,t1,c.get(Calendar.HOUR_OF_DAY),
                                        c.get(Calendar.MINUTE),false).show();
 }
 });
}
TimePickerDialog.OnTimeSetListener t1=new TimePickerDialog.OnTimeSetListener() {
@Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
 chr=hourOfDay;
 cmin=minute;
 displaytime.setText("Time is : "+chr+":"+cmin);
 }
};
}

Output :-

TimePicker
TimePicker fig :1





















TimePicker
TimePicker fig:2





















TimePicker
TimePicker fig :3





Saturday, 15 October 2016

DatePicker

DatePicker is android widget. And choose from the date and time under this select the datepicker widget. We can select the year,month,day.

activitymain.xml :-

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
  tools:context="com.kiranapp.MainActivity">
<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Date :"
  android:id="@+id/textView"
  android:layout_marginTop="30dp"
  android:layout_gravity="center_horizontal" />
<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="choose date"
  android:id="@+id/button"
  android:layout_marginTop="30dp"
  android:layout_gravity="center_horizontal" />
<DatePicker
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/datePicker"
  android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java :-


package com.kiranapp;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
   Button selectbtn;
   TextView displaydate;
   Calendar c=Calendar.getInstance();
   int cdate,cmonth,cyear;

@Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   selectbtn=(Button)findViewById(R.id.button);
   displaydate=(TextView)findViewById(R.id.textView);
   selectbtn.setOnClickListener(new View.OnClickListener() {
@Override
   public void onClick(View v) {
   new DatePickerDialog(MainActivity.this,d1,c.get(Calendar.YEAR),
   c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show();
   }
   });
   }
  DatePickerDialog.OnDateSetListener d1=new DatePickerDialog.OnDateSetListener() {
 @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    cdate=dayOfMonth;
    cmonth=monthOfYear+1;
    cyear=year;
    displaydate.setText("Date is :"+cdate+"-"+cmonth+"-"+cyear);
    }
    };
}

Output :-

DatePicker
DatePicker fig :1






















DatePicker
DatePicker fig :2





















DatePicker
DatePicker fig:3

Thursday, 13 October 2016

ProgressBar

Progress Bar is used to show progress of the task in android.It is android widget. For Example its shows like  Downloading or Uploading or Processing from internet or others. So see below the code of progress bar with code. They are different progress bars are there :  Circular progress bar, Horizontal progress bar, Custom progress bar, Spinner progress bar
And Add the image of the "progress bar" i.e
Resources or Res------>Drawable------->download.jpg(i.e figure)

activitymain.xml :-

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
  tools:context="com.kiranapp.MainActivity">


<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="progressbar button"
  android:id="@+id/button"
  android:layout_marginTop="30dp"
  android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java :-

package com.kiranapp :-

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button button;

@Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //USing Progress Dialog Class
                ProgressDialog pd=new ProgressDialog(MainActivity.this);
                pd.setTitle("processing");
                pd.setMessage("wait for downloading");
                pd.setIcon(R.drawable.download);

                pd.show();
            }
        });
    }
}

Output :-

ProgressBar
ProgressBar fig:1




















ProgressBar
ProgressBar fig:2


















Wednesday, 12 October 2016

CustomDialog

It is  a user defined dialog.
It is android widget.
It is similar to activity or user-interface(UI).
Using Dialog class
And create one more XML file and java file (its not mandatory)

activitymain.xml :-

<?xml version="1.0" encoding="utf-8"?>
 <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:orientation="vertical" 
 tools:context="com.kiranapp.MainActivity">


<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"       
 android:text="customdialog" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>


customdialog :-

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

<EditText 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:id="@+id/editText" 
 android:layout_gravity="center_horizontal" 
 android:layout_marginTop="30dp" 
 android:hint="enter  name" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="enter" 
 android:id="@+id/okbtn" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="cancel" 
 android:id="@+id/canbtn" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java :-

package com.kiranapp;

import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
 Button okbtn,canbtn;
 Dialog dialog;
 EditText e1;

@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 okbtn=(Button)findViewById(R.id.button);
 okbtn.setOnClickListener(new View.OnClickListener() {
@Override
 public void onClick(View v) {
 dialog=new Dialog(MainActivity.this);
 dialog.setTitle("Custom Dialog");
 dialog.setContentView(R.layout.customdialog);
 dialog.show();
 okbtn=(Button)dialog.findViewById(R.id.okbtn);
 canbtn=(Button)dialog.findViewById(R.id.canbtn);
 e1=(EditText)dialog.findViewById(R.id.editText);
 okbtn.setOnClickListener(new View.OnClickListener() {
@Override
 public void onClick(View v) {
 String s1=e1.getText().toString();
Toast.makeText(MainActivity.this, "Name is : "+s1, Toast.LENGTH_SHORT).show();
 }
});
canbtn.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 dialog.cancel();
 }
});
}
});
}
}


Customdialog.java :-

package com.kiranapp;

/** * Created by Dell on 10/12/2016. */public class customdialog {
}

Output :-

Custom dialog
Custom dialog fig :1





















custom dialog
custom dialog fig :2

AlertDialogBox

 It is display the some alert
  Using Builder class


activitymain.xml :-

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
  tools:context="com.kiranapp.MainActivity">
<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/button"
  android:layout_gravity="center_horizontal"
  android:text="Alertdialog"
  android:layout_marginTop="20dp" />
</LinearLayout>

MainActivity.java :-


package com.kiranapp;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
 Button alertbtn;

@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 alertbtn=(Button)findViewById(R.id.button);
 alertbtn.setOnClickListener(new View.OnClickListener() {
@Override
  public void onClick(View v) {

AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
 builder.setTitle("Alert?");
 builder.setMessage("Hello do u want to close the app");
 builder.setIcon(R.mipmap.ic_launcher);
 builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
  public void onClick(DialogInterface dialog, int which) {
  finish();
 Toast.makeText(MainActivity.this, "App Closed", Toast.LENGTH_SHORT).show();
  }
 });
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
  public void onClick(DialogInterface dialog, int which) {
 Toast.makeText(MainActivity.this, "Dialog is CLosed", Toast.LENGTH_SHORT).show();
  }
 });
 builder.create();
 builder.show();
 }
});
}
}

Output :-


AlertDialogBox
AlertDialogBox fig :1
AlertDialogBox
AlertDialogBox fig :2

AutoCompleteTextView

activitymain.xml :-

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
  tools:context="com.kiranapp.MainActivity">
<AutoCompleteTextView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/autoCompleteTextView"
  android:layout_gravity="center_horizontal"
  android:layout_marginTop="20dp"
  android:hint="enter letter" />
<Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/button"
 android:layout_gravity="center_horizontal"
 android:text="show" />
</LinearLayout>

MainActivity.java :-


package com.kiranapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
AutoCompleteTextView autoCompleteTextView;
Button show;
String name;
static final String[] fruits={"Apple","Orange","Watermelon","Bannana","Strawberry","Peaches"};
@Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  autoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
  show=(Button)findViewById(R.id.button);
  ArrayAdapter<String>arrayAdapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_dropdown_item_1line,fruits);
  autoCompleteTextView.setThreshold(0);
  autoCompleteTextView.setAdapter(arrayAdapter);
  autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  name= (String) parent.getItemAtPosition(position);
  }
 });
 show.setOnClickListener(new View.OnClickListener() {
@Override
    public void onClick(View v) {
  Toast.makeText(MainActivity.this,"Select Fruits :"+name,Toast.LENGTH_SHORT).show();
  }
  });
  }
}

Output :-

AutoCompleteTextView
AutoCompleteTextView fig :1



Switch

activitymain.xml :-

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
  tools:context="com.kiranapp.MainActivity"
  android:background="#ddd6d6">
<Switch
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Bluetooh"
  android:layout_marginTop="30dp"
  android:id="@+id/switch1"
  android:checked="false"
  android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java
:-


package com.kiranapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
  Switch aSwitch;


@Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   aSwitch=(Switch) findViewById(R.id.switch1);
  aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
   if (isChecked){
   Toast.makeText(MainActivity.this,"TurnOn",Toast.LENGTH_SHORT).show();
    }
  else {
 Toast.makeText(MainActivity.this,"TurnOff",Toast.LENGTH_SHORT).show();
 }
 }
});
}
}

Output :- 


Switch
Switch fig:1




Switch
Switch fig:2

Sunday, 9 October 2016

SpinnerView

It is a dropdown list.
Example Bookmyshow app or just tickets.
Here we are Using ArrayAdapter class in the code below
Similarly AutoCompleteTextView we can use as dropdown list.


activitymain.xml :-

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
  tools:context="com.kiranapp.MainActivity">
<Spinner
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/spinner"
  android:layout_marginTop="50dp"/>
</LinearLayout>

Germany.xml :-

<?xml version="1.0" encoding="utf-8"?>
 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textAppearance="?android:attr/textAppearanceLarge"
   android:id="@+id/textView" />

MainActivity.java :-

package com.kiranapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
   Spinner spinner;
   String[] countrys={"-select countrys-","INDIA","GERMANY","FRANCE","USA","UK","AUSTRALIA",
            "CANADA","CHINA","SWITERLAND","ITALY"};


@Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   spinner=(Spinner)findViewById(R.id.spinner);
   ArrayAdapter<String> aa=new ArrayAdapter<String>(MainActivity.this,
    android.R.layout.simple_list_item_1,countrys);
    spinner.setAdapter(aa);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
 @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    Toast.makeText(MainActivity.this, "COUNTRY ID : "+countrys[position],     
    Toast.LENGTH_SHORT).show();
    if(position==2){
     Intent in=new Intent(MainActivity.this,Germany.class);
     startActivity(in);
     }
    }
@Override
   public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }
}

Germany.java :-

package com.kiranapp;

/**
 * Created by Dell on 10/8/2016.
 */
public class Germany {
}

Output :-

SpinnerView
SpinnerView Fig:1




















SpinnerView
SpinnerView Fig:2

Saturday, 8 October 2016

RatingBar

activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
   tools:context="com.kiranrating.MainActivity">

<TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Hollywood movies"
   android:layout_marginTop="49dp"
   android:id="@+id/textView" />
<RatingBar
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginTop="20dp"
   android:id="@+id/ratingBar1" />
<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Click Rating"
   android:id="@+id/button"
   android:layout_marginTop="20dp"
   android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java :-

package com.kiranrating;

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

public class MainActivity extends Activity {
    RatingBar r1;
    Button btnsubmit;
@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    addListenerOnButtonClick();
    }

   public void addListenerOnButtonClick(){
   r1=(RatingBar)findViewById(R.id.ratingBar1);
   btnsubmit=(Button)findViewById(R.id.button);
   btnsubmit.setOnClickListener(new OnClickListener(){
@Override
    public void onClick(View arg0) {
    String rating=String.valueOf(r1.getRating());
   Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();
    }
    });
   }
}

Output :-

RatingBar
RatingBar fig:1




















RatingBar
RatingBar fig:2

RadioButton

activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
   tools:context="com.kiranapp.MainActivity">

 <RadioGroup
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/radiogroup"
    android:weightSum="2"
    android:layout_marginTop="25dp">
<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/radio1"
    android:text="Android"
    android:layout_gravity="center"
    android:checked="false" />
<RadioButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/radio2"
   android:text="IOS"
   android:layout_gravity="center" />
<RadioButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/radio3"
   android:text="PhoneGap"
   android:layout_gravity="center" />
</RadioGroup>
<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Course"
   android:id="@+id/button"
   android:layout_gravity="center_horizontal"
   android:layout_marginTop="39dp"/>
</LinearLayout>

MainActivity.java :-

package com.kiranapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    RadioGroup radioGroup;
    RadioButton radioButton;
    Button button;
@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    radioGroup=(RadioGroup)findViewById(R.id.radiogroup);
    button=(Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener(){
@Override
    public void onClick(View v) {
    int id=radioGroup.getCheckedRadioButtonId();
    radioButton=(RadioButton)findViewById(id);
    String s=radioButton.getText().toString();
    Toast.makeText(getApplicationContext(),"COURSE IS :"+s,6000).show();
     }
    });
   }
}

Output :-

RadioButton
RadioButton fig:1




















RadioButton
RadioButton fi:2




















RadioButton
RadioButton fig:3

Thursday, 6 October 2016

CheckBox

ActivityMain.XML :-

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
 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" 
 tools:context="com.kiranapp.MainActivity">


 <CheckBox 
  android:layout_width="wrap_content"         
  android:layout_height="wrap_content" 
  android:text="Apple" 
  android:id="@+id/checkBox"         
  android:layout_alignParentTop="true" 
  android:layout_toStartOf="@+id/button1" 
  android:layout_marginEnd="34dp" />

 <CheckBox 

  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="Orange" 
  android:id="@+id/checkBox2" 
  android:layout_alignParentTop="true" 
  android:layout_toEndOf="@+id/button1" 
  android:layout_marginStart="24dp" />
 <Button 
  android:id="@+id/button1" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_marginTop="61dp" 
  android:text="Order" 
  android:layout_alignParentTop="true" 
  android:layout_centerHorizontal="true" />

</RelativeLayout>


MainActivity.java :-

package com.kiranapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class MainActivity extends Activity {
   CheckBox apple,orange;
   Button Order;
 @Override 
  protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   addListenerOnButtonClick();
  }
  public void addListenerOnButtonClick(){

   apple=(CheckBox)findViewById(R.id.checkBox);
   orange=(CheckBox)findViewById(R.id.checkBox2);

   Order=(Button)findViewById(R.id.button1);


   Order.setOnClickListener(new OnClickListener(){

 @Override 
   public void onClick(View view) {
    int totalamount=0;
    StringBuilder result=new StringBuilder();
    result.append("Selected Items:");
    if(apple.isChecked()){
    result.append("\nApple 100Rs");
    totalamount+=100;
   }
    if(orange.isChecked()){
    result.append("\nOrange 150Rs");
    totalamount+=150;
   }

   result.append("\nTotal: "+totalamount+"Rs");

   Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();
  }

  });
 }
 @Override 
  public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }
}

Output :-

CheckBox
CheckBox fig:1
CheckBox