Fragment Example Create the following files and test the application on emulator or device.

Similar documents
EMBEDDED SYSTEMS PROGRAMMING Application Tip: Switching UIs

CSE 660 Lab 3 Khoi Pham Thanh Ho April 19 th, 2015

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Managing Screen Orientation

MAD ASSIGNMENT NO 3. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

Fragments. Lecture 11

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 2

Mobile Application Development Lab [] Simple Android Application for Native Calculator. To develop a Simple Android Application for Native Calculator.

Mobile Software Development for Android - I397

EMBEDDED SYSTEMS PROGRAMMING UI Specification: Approaches

Android - JSON Parser Tutorial


Android CardView Tutorial

Software Practice 3 Before we start Today s lecture Today s Task Team organization

Intents. Your first app assignment

Applied Cognitive Computing Fall 2016 Android Application + IBM Bluemix (Cloudant NoSQL DB)

<uses-permission android:name="android.permission.internet" />

CSE 660 Lab 7. Submitted by: Arumugam Thendramil Pavai. 1)Simple Remote Calculator. Server is created using ServerSocket class of java. Server.


Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Android Programs Day 5

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

Create Parent Activity and pass its information to Child Activity using Intents.

Tabel mysql. Kode di PHP. Config.php. Service.php

Practical 1.ListView example

EMBEDDED SYSTEMS PROGRAMMING Android Services

Arrays of Buttons. Inside Android

Tip Calculator. xmlns:tools=" android:layout_width="match_parent"

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

ANDROID PROGRAMS DAY 3

PROGRAMMING APPLICATIONS DECLARATIVE GUIS

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

// MainActivity.java ; Noah Spenser; Senior Design; Diabetic Breathalyzer

PENGEMBANGAN APLIKASI PERANGKAT BERGERAK (MOBILE)

Android UI Development

Dynamically Create Admob Banner and Interstitial Ads

Android Layout Types

<uses-permission android:name="android.permission.internet"/>

M.A.D ASSIGNMENT # 2 REHAN ASGHAR BSSE 15126

Introduction to Android Development

android-espresso #androidespresso

Android Application Development. By : Shibaji Debnath

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 4. Workshop

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Android JSON Parsing Tutorial

EMBEDDED SYSTEMS PROGRAMMING Application Basics

Chapter 8 Positioning with Layouts

ActionBar. import android.support.v7.app.actionbaractivity; public class MyAppBarActivity extends ActionBarActivity { }

MAD ASSIGNMENT NO 2. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

Programming with Android: Introduction. Layouts. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

Fragments. Fragments may only be used as part of an ac5vity and cannot be instan5ated as standalone applica5on elements.

Basic GUI elements - exercises

Topics of Discussion

APPENDIX CODE TO STORE THE BUTTON MENU AND MOVE THE PAGE

StoppUhr. <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start1"

M.A.D Assignment # 1

Tablets have larger displays than phones do They can support multiple UI panes / user behaviors at the same time

1. Location Services. 1.1 GPS Location. 1. Create the Android application with the following attributes. Application Name: MyLocation

Tutorial: Setup for Android Development

ListView Containers. Resources. Creating a ListView

Android - Widgets Tutorial

Manifest.xml. Activity.java

Getting Started. Dr. Miguel A. Labrador Department of Computer Science & Engineering

UI Fragment.

COMP61242: Task /04/18

ITU- FAO- DOA- TRCSL. Training on. Innovation & Application Development for E- Agriculture. Shared Preferences

User Interface Development. CSE 5236: Mobile Application Development Instructor: Adam C. Champion Course Coordinator: Dr.

android:layout_margintop="5dp" > <EditText android:layout_width="210dp"

Android Application Model I. CSE 5236: Mobile Application Development Instructor: Adam C. Champion, Ph.D. Course Coordinator: Dr.

Android Navigation Drawer for Sliding Menu / Sidebar

1. Simple List. 1.1 Simple List using simple_list_item_1

Android Application Model I

Developing For Android

Vienos veiklos būsena. Theory

Mobile Computing Fragments

Produced by. Mobile Application Development. Eamonn de Leastar

Our First Android Application

Getting Started With Android Feature Flags

API Guide for Gesture Recognition Engine. Version 2.0

Android SQLite Database Tutorial - CRUD Operations

ELET4133: Embedded Systems. Topic 15 Sensors

SD Module-1 Android Dvelopment

Action Bar. (c) 2010 Haim Michael. All Rights Reserv ed.

An Overview of the Android Programming

Screen Slides. The Android Studio wizard adds a TextView to the fragment1.xml layout file and the necessary code to Fragment1.java.

Fragments. Lecture 10

TextView Control. EditText Control. TextView Attributes. android:id - This is the ID which uniquely identifies the control.

User Interface Development in Android Applications

Android Workshop: Model View Controller ( MVC):

Creating a Custom ListView

LAMPIRAN PROGRAM. public class Listdata_adiktif extends ArrayAdapter<ModelData_adiktif> {

Android App Development. Mr. Michaud ICE Programs Georgia Institute of Technology

Tutorial: Setup for Android Development

05. RecyclerView and Styles

Programmation Mobile Android Master CCI

Create a local SQL database hosting a CUSTOMER table. Each customer includes [id, name, phone]. Do the work inside Threads and Asynctasks.

Android Basics. Android UI Architecture. Android UI 1

Fragments were added to the Android API in Honeycomb, API 11. The primary classes related to fragments are: android.app.fragment

Adapter.

Android Basics. - Bhaumik Shukla Android Application STEALTH FLASH

When programming in groups of people, it s essential to version the code. One of the most popular versioning tools is git. Some benefits of git are:

Transcription:

Fragment Example Create the following files and test the application on emulator or device. File: AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.atijaffna.fragmentex" > <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme" > <activity android:name=".fragmentactivity" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest> File: FragmentActivity.java package com.atijaffna.fragmentex; import android.app.fragment; import android.app.fragmentmanager; import android.app.fragmenttransaction; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.button; public class FragmentActivity extends AppCompatActivity { Button b1,b2; protected void oncreate(bundle savedinstancestate) {

super.oncreate(savedinstancestate); setcontentview(r.layout.activity_fragment); b1=(button)findviewbyid(r.id.button); b2=(button)findviewbyid(r.id.button2); View.OnClickListener listener = new View.OnClickListener() { public void onclick(view v) { Fragment fr; if (v==b1) fr=new FragmentA(); else fr=new FragmentB(); FragmentManager fm=getfragmentmanager(); FragmentTransaction ft=fm.begintransaction(); ft.replace(r.id.fragmentplace,fr); ft.commit(); ; b1.setonclicklistener(listener); b2.setonclicklistener(listener); File: FragmentA.java package com.atijaffna.fragmentex; import android.os.bundle; import android.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; public class FragmentA extends Fragment {

public FragmentA() { // Required empty public constructor public View oncreateview(layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { // Inflate the layout for this fragment return inflater.inflate(r.layout.fragment_a, container, false); File: FragmentB.java package com.atijaffna.fragmentex; import android.os.bundle; import android.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; public class FragmentB extends Fragment { public FragmentB() { // Required empty public constructor public View oncreateview(layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { // Inflate the layout for this fragment return inflater.inflate(r.layout.fragment_b, container, false);

File: activity_fragment.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" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".fragmentactivity"> <TextView android:text="hello World!" android:layout_width="wrap_content" android:id="@+id/textview" /> <Button android:layout_width="wrap_content" android:text="new Button" android:id="@+id/button" android:layout_aligntop="@+id/textview" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_alignparentright="true" android:layout_alignparentend="true" /> <Button android:layout_width="wrap_content" android:text="new Button" android:id="@+id/button2" android:layout_below="@+id/button" android:layout_alignparentleft="true" android:layout_alignparentstart="true"

android:layout_alignright="@+id/button" android:layout_alignend="@+id/button" /> <fragment android:layout_width="wrap_content" android:name="android.preference.preferencefragment" android:id="@+id/fragmentplace" android:layout_below="@+id/button2" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_alignparentbottom="true" android:layout_alignright="@+id/button2" android:layout_alignend="@+id/button2" /> </RelativeLayout> File: fragment_a.xml <FrameLayout 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.atijaffna.fragmentex.fragmenta"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_blank_fragment" android:background="#fbef6e" /> </FrameLayout>

File: fragment_b.xml <FrameLayout 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.atijaffna.fragmentex.fragmentb"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_blank_fragment" /> </FrameLayout> Try it yourself: Creating Project Create a new Android Application project in Android Studio with package as com.example.fragments. Create the main layout as activity_main and main Activity as MainActivity. Creating Fragment Layouts We are going to use two fragments with our main layout. The first fragment consists of a ListView. It is defined in list_fragment.xml. list_fragment.xml <?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"> <ListView android:layout_width="match_parent" android:id="@android:id/list" /> </LinearLayout>

The second fragment consists of two TextView to display Android OS name and Android version number. text_fragment.xml <?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:gravity="center" android:background="#5ba4e5" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:textsize="40px" android:textcolor="#ffffff" android:layout_gravity="center" android:id="@+id/androidos"/> <TextView android:layout_width="wrap_content" android:layout_gravity="center" android:textcolor="#ffffff" android:textsize="30px" android:id="@+id/version"/> </LinearLayout>

Creating Fragment Classes Our first class is TextFragment which extends to Fragment. We use LayoutInflator to display the layout for our Fragment. It consists of two Textviews. A function change is used to change the text in the TextView. TextFragment.java package com.amal.fragments; import android.app.fragment; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview; public class TextFragment extends Fragment { TextView text,vers; { public View oncreateview(layoutinflater inflater,viewgroup container, Bundle savedinstanc View view = inflater.inflate(r.layout.text_fragment, container, false); text= (TextView) view.findviewbyid(r.id.androidos); vers= (TextView)view.findViewById(R.id.Version); return view; public void change(string txt, String txt1){ text.settext(txt); vers.settext(txt1);

Our second class is MenuFragment which extends to ListFragment. A array of strings AndroidOS and Version is defined which contains Android version names and number. When a list item is clicked the TextView on the TextFragment is updated with the Android version name and number. MenuFragment.java package com.amal.fragments; import android.app.listfragment; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.listview; public class MenuFragment extends ListFragment { String[] AndroidOS = new String[] { "Cupcake","Donut","Eclair","Froyo", "Gingerbread","Honeycomb","Ice Cream SandWich","Jelly Bean","KitKat" ; String[] Version = new String[]{"1.5","1.6","2.0-2.1","2.2","2.3","3.0-3.2", "4.0","4.1-4.3","4.4"; public View oncreateview(layoutinflater inflater,viewgroup container, Bundle savedinstancestate) { View view =inflater.inflate(r.layout.list_fragment, container, false); ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.r.layout.simple_list_item_1, AndroidOS); setlistadapter(adapter); return view; public void onlistitemclick(listview l, View v, int position, long id) {

TextFragment txt = (TextFragment)getFragmentManager().findFragmentById(R.id.fragment2); txt.change(androidos[position],"version : "+Version[position]); getlistview().setselector(android.r.color.holo_blue_dark); Creating Main layout Our main layout consists of two fragments which are displayed horizontally side by side. Each fragment should link to the fragment class which is defined. activity_main.xml <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="horizontal" tools:context="com.amal.fragments.mainactivity"> <fragment android:layout_height="match_parent" android:layout_width="240px" class="com.amal.fragments.menufragment" android:id="@+id/fragment"/> <fragment android:layout_width="240px" android:layout_height="match_parent" class="com.amal.fragments.textfragment" android:id="@+id/fragment2"/> </LinearLayout>

Creating Activity Our MainActivity just extends to the FragmentActivity. MainActivity.java package com.amal.fragments; import android.os.bundle; import android.support.v4.app.fragmentactivity; public class MainActivity extends FragmentActivity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); Creating Manifest We do not need any special permissions for our Project.

Screenshots