0% found this document useful (0 votes)
3 views

Mobile Application and Development-Lec8

The document outlines various scenarios for mobile application development, focusing on XML layouts and Java code for Android applications. It includes examples of button arrangements, spinner widgets, checkboxes, and arithmetic operations using EditText inputs. Each scenario illustrates different UI components and their implementation in an Android environment.

Uploaded by

h.ejaz0444
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Mobile Application and Development-Lec8

The document outlines various scenarios for mobile application development, focusing on XML layouts and Java code for Android applications. It includes examples of button arrangements, spinner widgets, checkboxes, and arithmetic operations using EditText inputs. Each scenario illustrates different UI components and their implementation in an Android environment.

Uploaded by

h.ejaz0444
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Sir Syed University of Engineering & Technology, Karachi

Mobile Application Development


Lecture 8: Scenario’s

Batch - 2019 Department of Computer Science / Information Technology


Scenario 1

<?xml version="1.0" encoding="utf-8"?> <Button


<RelativeLayout android:id="@+id/button3"
android:layout_width=“match_parent“ android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height=“match_parent"
android:text="Bottom Left Button"
xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_alignParentLeft="true"
<Button android:layout_alignParentBottom="true"/>
android:id="@+id/button1“ <Button
android:layout_width="wrap_content“ android:id="@+id/button4"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="Top Left Button“ android:layout_height="wrap_content"
android:layout_alignParentLeft="true“ android:text="Bottom Right Button"
android:layout_alignParentTop="true"/> android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>
<Button
<Button
android:id="@+id/button2" android:id="@+id/button5"
android:layout_width="wrap_content" android:layout_width="fill_parent"
android:layout_height="wrap_content“ android:layout_height="wrap_content"
android:text="Top Right Button" android:text="Middle Button"
android:layout_alignParentTop="true“ android:layout_centerVertical="true"
android:layout_alignParentRight="true"/> android:layout_centerHorizontal="true"/>

</RelativeLayout>
Scenario 1
Scenario 2

<?xml version="1.0" encoding="utf-8"?>


<!--Constraint layout which contain Spinner widget-->

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.geeksforgeeks.Spinner.MainActivity">

<Spinner
android:id="@+id/coursesspinner"
android:layout_height="50dp"
android:layout_width="160dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"/>

</LinearLayout>
Scenario 2

public class MainActivity


extends AppCompatActivity
implements AdapterView.OnItemSelectedListener {

String[] courses = { "C", "Data structures",


"Interview prep", "Algorithms",
"DSA with java", "OS" };

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spino = findViewById(R.id.coursesspinner);
spin.setOnItemSelectedListener(this);
ArrayAdapter ad
= new ArrayAdapter(this,
android.R.layout.simple_spinner_item,
courses);

// set simple layout resource file for each item of spinner


ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spino.setAdapter(ad);
}
Scenario 3

?xml version="1.0" encoding="utf-8"?> android:text="Coffee"


<android.support.constraint.ConstraintLayout
app:layout_constraintStart_toStartOf="parent"
xmlns:android="http://schemas.android.com/apk/res/android"
app:layout_constraintTop_toBottomOf="@+id/checkBox" />
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
<CheckBox
android:layout_width="match_parent"
android:id="@+id/checkBox3"
android:layout_height="match_parent"
android:layout_width="wrap_content"
tools:context="example.javatpoint.com.checkbox.MainActivity">
android:layout_height="wrap_content"
android:layout_marginLeft="144dp"
<CheckBox
android:layout_marginTop="28dp"
android:id="@+id/checkBox"
android:text="Burger"
android:layout_width="wrap_content"
app:layout_constraintStart_toStartOf="parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/checkBox2" />
android:layout_marginLeft="144dp"
android:layout_marginTop="68dp"
<Button
android:text="Pizza"
android:id="@+id/button"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="wrap_content"
app:layout_constraintTop_toTopOf="parent" />
android:layout_height="wrap_content"
android:layout_marginLeft="144dp"
<CheckBox
android:layout_marginTop="184dp"
android:id="@+id/checkBox2"
android:text="Order"
android:layout_width="wrap_content"
app:layout_constraintStart_toStartOf="parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/checkBox3" />
android:layout_marginLeft="144dp"
android:layout_marginTop="28dp"
</android.support.constraint.ConstraintLayout>
Scenario 3

@Override
public class MainActivity extends AppCompatActivity { public void onClick(View view) {
CheckBox pizza,coffe,burger; int totalamount=0;
Button buttonOrder; StringBuilder result=new StringBuilder();
@Override result.append("Selected Items:");
protected void onCreate(Bundle savedInstanceState) { if(pizza.isChecked()){
super.onCreate(savedInstanceState); result.append("\nPizza 100Rs");
setContentView(R.layout.activity_main); totalamount+=100;
addListenerOnButtonClick(); }
} if(coffe.isChecked()){
public void addListenerOnButtonClick(){ result.append("\nCoffe 50Rs");
//Getting instance of CheckBoxes and Button from the totalamount+=50;
activty_main.xml file }
pizza=(CheckBox)findViewById(R.id.checkBox); if(burger.isChecked()){
coffe=(CheckBox)findViewById(R.id.checkBox2); result.append("\nBurger 120Rs");
burger=(CheckBox)findViewById(R.id.checkBox3); totalamount+=120;
buttonOrder=(Button)findViewById(R.id.button); }
result.append("\nTotal: "+totalamount+"Rs");
//Applying the Listener on the Button click //Displaying the message on the toast
buttonOrder.setOnClickListener(new View.OnClickListener(){ Toast.makeText(getApplicationContext(), result.toString(),
Toast.LENGTH_LONG).show();
}
Scenario 3
Scenario 4

<?xml version="1.0" encoding="utf-8"?> android:layout_below="@+id/editText1"


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
xmlns:app="http://schemas.android.com/apk/res-auto" android:ems="10"
xmlns:tools="http://schemas.android.com/tools" android:inputType="number"
android:layout_width="match_parent" tools:layout_editor_absoluteX="84dp"
android:layout_height="match_parent" tools:layout_editor_absoluteY="127dp" />
tools:context="example.javatpoint.com.sumoftwonumber.MainActivity">
<Button
<EditText android:id="@+id/button"
android:id="@+id/editText1" android:layout_width="wrap_content"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:layout_below="@+id/editText2"
android:layout_alignParentTop="true" android:layout_centerHorizontal="true"
android:layout_centerHorizontal="true" android:layout_marginTop="109dp"
android:layout_marginTop="61dp" android:text="ADD"
android:ems="10" tools:layout_editor_absoluteX="148dp"
android:inputType="number" tools:layout_editor_absoluteY="266dp" />
tools:layout_editor_absoluteX="84dp" </RelativeLayout>
tools:layout_editor_absoluteY="53dp" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
Scenario 4

package example.javatpoint.com.sumoftwonumber; public void addListenerOnButton() {


edittext1 = (EditText) findViewById(R.id.editText1);
import android.support.v7.app.AppCompatActivity; edittext2 = (EditText) findViewById(R.id.editText2);
import android.os.Bundle; buttonSum = (Button) findViewById(R.id.button);
import android.view.View;
import android.widget.Button; buttonSum.setOnClickListener(new View.OnClickListener() {
import android.widget.EditText; @Override
import android.widget.Toast; public void onClick(View view) {
String value1=edittext1.getText().toString();
public class MainActivity extends AppCompatActivity { String value2=edittext2.getText().toString();
private EditText edittext1, edittext2; int a=Integer.parseInt(value1);
private Button buttonSum; int b=Integer.parseInt(value2);
int sum=a+b;
@Override
protected void onCreate(Bundle savedInstanceState) { Toast.makeText(getApplicationContext(),String.valueOf(sum),
super.onCreate(savedInstanceState); Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_main); }
});
addListenerOnButton(); }
} }
Scenario 4
Scenario 5

<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" <TextView
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/textView"
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="wrap_content"
tools:context="listview.example.com.listview.MainActivity"> android:text="Medium Text"
android:textStyle="bold"
<ListView
android:id="@+id/listView" android:textAppearance="?android:attr/textAppearanceMediu
android:layout_width="match_parent" m"
android:layout_height="fill_parent" android:layout_marginLeft="10dp"
/> android:layout_marginTop="5dp"
</android.support.constraint.ConstraintLayout> android:padding="2dp"
android:textColor="#4d4d4d"
/>
Scenario 5

<resources>
<string name="app_name">ListView</string>
<string-array name="array_technology">
<item>Android</item>
<item>Java</item>
<item>Php</item>
<item>Hadoop</item>
<item>Sap</item>
<item>Python</item>
<item>Ajax</item>
<item>C++</item>
<item>Ruby</item>
<item>Rails</item>
<item>.Net</item>
<item>Perl</item>
</string-array>
</resources>
Scenario 5

package listview.example.com.listview; listView=(ListView)findViewById(R.id.listView);


textView=(TextView)findViewById(R.id.textView);
import android.support.v7.app.AppCompatActivity; listItem = getResources().getStringArray(R.array.array_technology);
import android.os.Bundle; final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
import android.view.View;
import android.widget.AdapterView; android.R.layout.simple_list_item_1, android.R.id.text1,
import android.widget.ArrayAdapter; listItem);
import android.widget.ListView; listView.setAdapter(adapter);
import android.widget.TextView;
import android.widget.Toast; listView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
public class MainActivity extends AppCompatActivity { @Override
ListView listView; public void onItemClick(AdapterView<?> adapterView, View view,
TextView textView; int position, long l) {
String[] listItem; // TODO Auto-generated method stub
@Override String value=adapter.getItem(position);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); Toast.makeText(getApplicationContext(),value,Toast.LENGTH_SHORT).sho
setContentView(R.layout.activity_main); w();

}
});
}
}
Scenario 5
Scenario 6:

Write xml code of screen which takes two number as input and provide buttons for add, subtract,
multiply and divide buttons. Result to be displayed at the end.
Scenario 6: Linear Layout

<LinearLayout <Button
android:layout_width="match_parent" android:id="@+id/btnSubtract"
android:layout_height=" wrap_content" android:layout_width=“match_parent"
android:orientation="vertical" android:layout_height="wrap_content"
android:text="Subtract"/>
<EditText
android:id="@+id/num1" <Button
android:layout_width="match_parent" android:id="@+id/btnMultiply"
android:layout_height="wrap_content" android:layout_width=" match_parent "
android:hint="Enter first number" android:layout_height="wrap_content"
android:inputType="number"/> android:text="Multiply"/>

<EditText <Button
android:id="@+id/num2" android:id="@+id/btnDivide"
android:layout_width="match_parent" android:layout_width=" match_parent "
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:hint="Enter second number" android:text="Divide"/>
android:inputType="number" />
<TextView
<Button android:id="@+id/resultText"
android:id="@+id/btnAdd" android:layout_width=" match_parent "
android:layout_width=“match_parent" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:text="Result: "
android:text="Add"/> android:textSize="18sp"/>
</LinearLayout>
Scenario 6: Relative Layout

<RelativeLayout <Button
android:layout_width="match_parent" android:id="@+id/btnSubtract"
android:layout_height="match_parent" android:layout_width="match_parent"
<EditText android:layout_height="wrap_content"
android:id="@+id/num1" android:text="Subtract"
android:layout_width="match_parent" android:layout_below="@id/btnAdd"/>
android:layout_height="wrap_content" <Button
android:hint="Enter first number" android:id="@+id/btnMultiply"
android:inputType="number" /> android:layout_width="match_parent"
<EditText android:layout_height="wrap_content"
android:id="@+id/num2" android:text="Multiply"
android:layout_width="match_parent" android:layout_below="@id/btnSubtract"/>
android:layout_height="wrap_content" <Button
android:hint="Enter second number" android:id="@+id/btnDivide"
android:inputType="number" android:layout_width="match_parent"
android:layout_below="@id/num1"/> android:layout_height="wrap_content"
<Button android:text="Divide"
android:id="@+id/btnAdd" android:layout_below="@id/btnMultiply"/>
android:layout_width="match_parent" <TextView
android:layout_height="wrap_content" android:id="@+id/tvResult"
android:text="Add" android:layout_width="match_parent"
android:layout_below="@id/num2"/> android:layout_height="wrap_content"
android:text="Result:"
android:layout_below="@id/btnDivide"/>
</RelativeLayout>
Scenario 6: Tabular Layout

<TableLayout <TableRow>
android:layout_width="match_parent" <Button
android:layout_height="match_parent" android:id="@+id/btnAdd"
android:layout_width="match_parent"
<TableRow> android:layout_height="wrap_content"
<EditText android:text="Add"/>
android:id="@+id/num1" </TableRow>
android:layout_width="match_parent"
android:layout_height="wrap_content" <TableRow>
android:hint="Enter first number" <Button
android:inputType="number"/> android:id="@+id/btnSubtract"
</TableRow> android:layout_width="match_parent"
android:layout_height="wrap_content"
<TableRow> android:text="Subtract"/>
<EditText </TableRow>
android:id="@+id/num2"
android:layout_width="match_parent" <TableRow>
android:layout_height="wrap_content" <Button
android:hint="Enter second number" android:id="@+id/btnMultiply"
android:inputType="number"/> android:layout_width="match_parent"
</TableRow> android:layout_height="wrap_content"
android:text="Multiply"/>
</TableRow>
Scenario 6: Tabular Layout

<TableRow>
<Button
android:id="@+id/btnDivide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Divide"/>
</TableRow>

<TableRow>
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result:"/>
</TableRow>
</TableLayout>

You might also like