CMPE 419 Mobile Application Development Asst Prof Dr
CMPE 419 Mobile Application Development Asst. Prof. Dr. Ahmet Ünveren 2018 -2019 SPRING Computer Engineering Department CMPE 419 AU
Adding New Page and Button, Edit. Text, Text. View
How to add a new page to the existing project? package com. example. buttonw; import android. app. Activity; import android. os. Bundle; public class Button. Main. Activity extends Activity { } @Override protected void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. activity_button_main); } Button. Main. Activity. java
<Relative. Layout 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="${relative. Package}. ${activity. Class}" > </Relative. Layout> Activity_button_main. xml
• For creating a new page : right click to layout directory and select from New-Activity Empty Activity
Android. Manifest. xml • • • • • <? xml version="1. 0" encoding="utf-8"? > <manifest xmlns: android="http: //schemas. android. com/apk/res/android" package="com. example. buttonw" android: version. Code="1" android: version. Name="1. 0" > <uses-sdk android: min. Sdk. Version="8" android: target. Sdk. Version="21" /> <application android: allow. Backup="true" android: icon="@drawable/ic_launcher" android: label="@string/app_name" android: theme="@style/App. Theme" > <activity android: name=". Button. Main. Activity" android: label="@string/app_name" > <intent-filter> <action android: name="android. intent. action. MAIN" /> • • <category android: name="android. intent. category. LAUNCHER" /> </intent-filter> </activity> </application> • </manifest>
• For the new activity we have to add the following code to the Android. Manifest. xml file: <activity android: name=". New. Activity" android: label="@string/app_name" > <intent-filter> <action android: name="android. intent. action. NEW" /> <category android: name="android. intent. category. DEFAULT" /> </intent-filter> </activity>
Button, Edit. View and Text. View • Text. View is a label for displaying text • Edit. View is a textbox for entering a text • Lets add a Button, an Edit. View and a Text. View to the main activity.
• Now lets enter a text and display it after pressing a button. • For this we have to use Java code and create appropreate objects.
• • • <Relative. Layout 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="${relative. Package}. ${activity. Class}" > • • <Text. View android: id="@+id/text. View 1" android: layout_width="wrap_content" android: layout_height="wrap_content" android: layout_below="@+id/button 1" android: layout_center. Horizontal="true" android: layout_margin. Top="62 dp" android: text="Text. View" /> • • • <Edit. Text android: id="@+id/edit. Text 1" android: layout_width="wrap_content" android: layout_height="wrap_content" android: layout_align. Parent. Top="true" android: layout_center. Horizontal="true" android: layout_margin. Top="78 dp" android: ems="10" > <request. Focus /> </Edit. Text> • • <Button android: id="@+id/button 1" android: layout_width="wrap_content" android: layout_height="wrap_content" android: layout_below="@+id/edit. Text 1" android: layout_center. Horizontal="true" android: layout_margin. Top="60 dp" android: text="Button" /> • </Relative. Layout>
• package com. example. buttonw; • • • import android. app. Activity; import android. os. Bundle; import android. view. View; import android. widget. Button; import android. widget. Edit. Text; import android. widget. Text. View; • public class Button. Main. Activity extends Activity { • • • • • @Override protected void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. activity_button_main); final Text. View show=(Text. View)find. View. By. Id(R. id. text. View 1); show. set. Text(""); final Edit. Text all=(Edit. Text)find. View. By. Id(R. id. edit. Text 1); Button b=(Button)find. View. By. Id(R. id. button 1); b. set. Text("Display"); b. set. On. Click. Listener(new View. On. Click. Listener() { @Override public void on. Click(View v) { // TODO Auto-generated method stub show. set. Text(all. get. Text()); } }
• How to jump new activity? • Create a New Button • Use this button to jump to new acctivity. Button bb=(Button)find. View. By. Id(R. id. button 2); bb. set. On. Click. Listener(new View. On. Click. Listener() { @Override public void on. Click(View v) { // TODO Auto-generated method stub start. Activity(new Intent("android. intent. action. NEW")); } }); }
Example • • • protected void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. activity_main); • • • • @Override public void on. Click(View v) { // TODO Auto-generated method stub a=a+1; show. set. Text(Inputtext. get. Text()+"Button Pressed"+a); } }; show=(Text. View) find. View. By. Id(R. id. Text. View 1); Inputtext=(Edit. Text) find. View. By. Id(R. id. edit. Text 1); Display=(Button) find. View. By. Id(R. id. button 1); View. On. Click. Listener eventh=new View. On. Click. Listener() { Display. set. On. Click. Listener(eventh); }
• • • • • • • • Button a, c; Text. View b; Example @Override protected void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. activity_main); b = (Text. View) find. View. By. Id(R. id. text. View 1); a = (Button) find. View. By. Id(R. id. button 1); c= (Button) find. View. By. Id(R. id. button 2); // create click listener On. Click. Listener ocl. Btn. Ok = new On. Click. Listener() { @Override public void on. Click(View v) { // change text of the Text. View switch (v. get. Id()){ case R. id. button 1: b. set. Text("Button 1 clicked"); break; case R. id. button 2: //b. set. Text("Button 2 clicked"); start. Activity(new Intent("android. intent. action. NEW")); break; } } }; // assign click listener to the OK button (btn. OK) a. set. On. Click. Listener(ocl. Btn. Ok); c. set. On. Click. Listener(ocl. Btn. Ok); }
- Slides: 23