Android Widgets 1 7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID 1
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
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
The generated code for the ui components will be like this: 7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID
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
OUTPUT 7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID
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
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
Android Toast Example – Java Coding 7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID
Android Custom Toast Example activity_main.xml customtoast.xml File: activity_main.xml <RelativeLayout xmlns:androclass="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=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /> </RelativeLayout> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:androclass="http://schemas.android.com/apk/res/android" android:id="@+id/custom_toast_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#F14E23" > <ImageView android:id="@+id/custom_toast_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/hello_world" android:src="@drawable/ic_launcher"/> <TextView android:id="@+id/custom_toast_message" android:contentDescription="@string/Toast" android:text="@string/Toast" /> </LinearLayout> 7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID
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(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } 7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID
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
activity_main.xml 7 August 2018 <RelativeLayout xmlns:androclass="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=".MainActivity" > <ToggleButton android:id="@+id/toggleButton1" 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:id="@+id/toggleButton2" android:layout_alignBaseline="@+id/toggleButton1" android:layout_alignBottom="@+id/toggleButton1" android:layout_marginLeft="44dp" android:layout_toRightOf="@+id/toggleButton1" android:text="ToggleButton2" <Button android:id="@+id/button1" android:layout_below="@+id/toggleButton2" android:layout_marginTop="82dp" android:text="submit" /> </RelativeLayout> 7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID
public class MainActivity extends Activity { private ToggleButton toggleButton1, toggleButton2; private Button buttonSubmit; @Override 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(){ @Override 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
Thank You 7 August 2018 S.RENUKADEVI/AP/SCD/INTRODUCTION TO ANDROID