Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android Widgets 1 7 August 2018

Similar presentations


Presentation on theme: "Android Widgets 1 7 August 2018"— Presentation transcript:

1 Android Widgets 1 7 August 2018
S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID 1

2 Android Widget There are given a lot of android widgets with simplified examples such as Button, EditText, AutoCompleteTextView, ToggleButton, DatePicker, TimePicker, ProgressBar etc. 7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID 2

3 Android Button Example
Android Button represents a push-button. The android.widget.Button is subclass of TextView class and CompoundButton is the subclass of Button class. There are different types of buttons in android such as RadioButton, ToggleButton, CompoundButton etc. Here, we are going to create two textfields and one button for sum of two numbers. If user clicks button, sum of two input values is displayed on the Toast. Drag the component or write the code for UI in activity_main.xml 7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID

4 The generated code for the ui components will be like this:
7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID

5 Activity class package com.example.sumof2numbers; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText edittext1,edittext2; private Button buttonSum; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); addListenerOnButton(); } public void addListenerOnButton(){ edittext1=(EditText)findViewById(R.id.editText1); edittext2=(EditText)findViewById(R.id.editText2); buttonSum=(Button)findViewById(R.id.button1); buttonSum.setOnClickListener(new OnClickListener(){ @Override public void onClick(View view) { String value1=edittext1.getText().toString(); String value2=edittext2.getText().toString(); int a=Integer.parseInt(value1); int b=Integer.parseInt(value2); int sum=a+b; Toast.makeText(getApplicationContext(),String.valueOf(sum),Toast.LENGTH_LONG).show(); }); public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; 7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID

6 OUTPUT 7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID

7 Android Toast Example Andorid Toast can be used to display information for the short period of time. A toast contains message to be displayed quickly and disappears after sometime. Toast class Toast class is used to show notification for a particular interval of time. After sometime it disappears. It doesn't block the user interaction. Constants of Toast class There are only 2 constants of Toast class which are given below. Methods of Toast class 7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID

8 Android Toast Example Toast.makeText(getApplicationContext(),"Hello Javatpoint",Toast.LENGTH_SHORT).show();   Another code: Toast toast=Toast.makeText(getApplicationContext(),"Hello Javatpoint",Toast.LENGTH_SHORT);   toast.setMargin(50,50);   toast.show();   Here, getApplicationContext() method returns the instance of Context. 7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID

9 Android Toast Example – Java Coding
7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID

10 Android Custom Toast Example
activity_main.xml customtoast.xml File: activity_main.xml <RelativeLayout xmlns:androclass="     xmlns:tools="     android:layout_width="match_parent"       android:layout_height="match_parent"       tools:context=".MainActivity" >          <TextView           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:layout_centerHorizontal="true"           android:layout_centerVertical="true"   </RelativeLayout>   <?xml version="1.0" encoding="utf-8"?>   <LinearLayout xmlns:androclass="     android:layout_width="match_parent"       android:layout_height="match_parent"       android:orientation="vertical"       android:background="#F14E23"        >               <ImageView           android:layout_width="wrap_content"           android:layout_height="wrap_content"          <TextView   </LinearLayout>   7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID

11 Activity class public class MainActivity extends Activity { @Override
        public void onCreate(Bundle savedInstanceState) {               super.onCreate(savedInstanceState);               setContentView(R.layout.activity_main);                          //Creating the LayoutInflater instance               LayoutInflater li = getLayoutInflater();           //Getting the View object as defined in the customtoast.xml file               View layout = li.inflate(R.layout.customtoast,                 (ViewGroup) findViewById(R.id.custom_toast_layout));                       //Creating the Toast object                Toast toast = new Toast(getApplicationContext());               toast.setDuration(Toast.LENGTH_SHORT);               toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);               toast.setView(layout);//setting the view of custom toast layout               toast.show();           }           public boolean onCreateOptionsMenu(Menu menu) {               getMenuInflater().inflate(R.menu.activity_main, menu);               return true;      }   7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID

12 Android ToggleButton Example
Android Toggle Button can be used to display checked/unchecked (On/Off) state on the button. XML Attributes of ToggleButton class Methods of ToggleButton class 7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID

13 activity_main.xml 7 August 2018
<RelativeLayout xmlns:androclass="     xmlns:tools="     android:layout_width="match_parent"       android:layout_height="match_parent"       tools:context=".MainActivity" >          <ToggleButton           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:layout_alignParentLeft="true"           android:layout_alignParentTop="true"           android:layout_marginLeft="60dp"           android:layout_marginTop="18dp"           android:text="ToggleButton1"           android:textOff="Off"           android:textOn="On" />           android:layout_marginLeft="44dp"           android:text="ToggleButton2"       <Button           android:layout_marginTop="82dp"           android:text="submit" />   </RelativeLayout>   7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID

14 public class MainActivity extends Activity {  
    private ToggleButton toggleButton1, toggleButton2;       private Button buttonSubmit;       protected void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.activity_main);                      addListenerOnButtonClick();       }       public void addListenerOnButtonClick(){           //Getting the ToggleButton and Button instance from the layout xml file           toggleButton1=(ToggleButton)findViewById(R.id.toggleButton1);           toggleButton2=(ToggleButton)findViewById(R.id.toggleButton2);           buttonSubmit=(Button)findViewById(R.id.button1);              //Performing action on button click           buttonSubmit.setOnClickListener(new OnClickListener(){               public void onClick(View view) {                   StringBuilder result = new StringBuilder();                      result.append("ToggleButton1 : ").append(toggleButton1.getText());                      result.append("\nToggleButton2 : ").append(toggleButton2.getText());                   //Displaying the message in toast                   Toast.makeText(getApplicationContext(), result.toString(),Toast.LENGTH_LONG).show();               }                          });       public boolean onCreateOptionsMenu(Menu menu) {           // Inflate the menu; this adds items to the action bar if it is present.           getMenuInflater().inflate(R.menu.activity_main, menu);           return true;   }   Activity class 7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID

15 Thank You 7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID


Download ppt "Android Widgets 1 7 August 2018"

Similar presentations


Ads by Google