CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department CMPE419 AU
Adding New Page and Button, EditText, TextView
How to add a new page to the existing project? package com.example.buttonw; import android.app.Activity; import android.os.Bundle; public class ButtonMainActivity extends Activity protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_button_main); } ButtonMainActivity.java
<RelativeLayout xmlns:android=" /android" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > Activity_button_main.xml
For creating a new page : right click to layout directory and select from New-Other-Android- Android XML File
Now we have to create Java Controller file for newpage.xml For this create a class under scr directory.
package com.example.buttonw; public class NewActivity { } Modify this class as an activity class: package com.example.buttonw; import android.app.Activity; public class NewActivity extends Activity{ }
We have to add onCreate() and setContentView to the NewActivity.java file.
package com.example.buttonw; import android.app.Activity; import android.os.Bundle; public class NewActivity extends protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } Now we have to connect our Java class to xml file by using setContentView setContentView(R.layout.newpage);
package com.example.buttonw; import android.app.Activity; import android.os.Bundle; public class NewActivity extends protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.newpage); } Now we have to modify Manifest file.
AndroidManifest.xml <manifest xmlns:android=" package="com.example.buttonw" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" > <activity android:name=".ButtonMainActivity" >
For the new activity we have to add the following code to the AndroidManifest.xml file: <activity android:name=".NewActivity" >
<manifest xmlns:android=" package="com.example.buttonw" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" > <activity android:name=".ButtonMainActivity" > <activity android:name=".NewActivity" > DEFAULT for all other activities
Button, EditView and TextView TextView is a label for displaying text EditView is a textbox for entering a text Lets add a Button, an EditView and a TextView 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.
<RelativeLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="62dp" android:text="TextView" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="78dp" android:ems="10" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="60dp" android:text="Button" />
package com.example.buttonw; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class ButtonMainActivity extends Activity protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_button_main); final TextView show=(TextView)findViewById(R.id.textView1); show.setText(""); final EditText all=(EditText)findViewById(R.id.editText1); Button b=(Button)findViewById(R.id.button1); b.setText("Display"); b.setOnClickListener(new View.OnClickListener() public void onClick(View v) { // TODO Auto-generated method stub show.setText(all.getText()); } }); }
How to jump new activity? Create a New Button Use this button to jump to new acctivity. Button bb=(Button)findViewById(R.id.button2); bb.setOnClickListener(new View.OnClickListener() public void onClick(View v) { // TODO Auto-generated method stub startActivity(new Intent("android.intent.action.NEW")); } }); }