Android Application that uses GUI
components, Font and Colors:
[Link]:
public class MainActivity extends AppCompatActivity {
int ch=1;
float font=30;
@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
final TextView t= (TextView) findViewById([Link]);
Button b1= (Button) findViewById([Link].button1);
[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link](font);
font = font + 5;
if (font == 50)
font = 30;
}
});
Button b2= (Button) findViewById([Link].button2);
[Link](new [Link]() {
@Override
public void onClick(View v) {
switch (ch) {
case 1:
[Link]([Link]);
break;
case 2:
[Link]([Link]);
break;
case 3:
[Link]([Link]);
break;
case 4:
[Link]([Link]);
break;
case 5:
[Link]([Link]);
break;
case 6:
[Link]([Link]);
break;
}
ch++;
if (ch == 7)
ch = 1;
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:gravity="center"
android:text="Hello World!"
android:textSize="25sp"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change font size"
android:textSize="25sp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change color"
android:textSize="25sp" />
</LinearLayout>
Simple Android Application for Native Calculator:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:textSize="20sp" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<Button
android:id="@+id/Add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"
android:textSize="30sp"/>
<Button
android:id="@+id/Sub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:textSize="30sp"/>
<Button
android:id="@+id/Mul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"
android:textSize="30sp"/>
<Button
android:id="@+id/Div"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"
android:textSize="30sp"/>
</LinearLayout>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Answer is"
android:textSize="30sp"
android:gravity="center"/>
</LinearLayout>
MainActivity:
public class MainActivity extends AppCompatActivity implements OnClickListener
{
//Defining the Views
EditText Num1;
EditText Num2;
Button Add;
Button Sub;
Button Mul;
Button Div;
TextView Result;
@Override
public void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
//Referring the Views
Num1 = (EditText) findViewById([Link].editText1);
Num2 = (EditText) findViewById([Link].editText2);
Add = (Button) findViewById([Link]);
Sub = (Button) findViewById([Link]);
Mul = (Button) findViewById([Link]);
Div = (Button) findViewById([Link]);
Result = (TextView) findViewById([Link]);
// set a listener
[Link](this);
[Link](this);
[Link](this);
[Link](this);
}
@Override
public void onClick (View v)
{
float num1 = 0;
float num2 = 0;
float result = 0;
String oper = "";
// check if the fields are empty
if ([Link]([Link]().toString()) ||
[Link]([Link]().toString()))
return;
// read EditText and fill variables with numbers
num1 = [Link]([Link]().toString());
num2 = [Link]([Link]().toString());
// defines the button that has been clicked and performs the corresponding
operation
// write operation into oper, we will use it later for output
switch ([Link]())
{
case [Link]:
oper = "+";
result = num1 + num2;
break;
case [Link]:
oper = "-";
result = num1 - num2;
break;
case [Link]:
oper = "*";
result = num1 * num2;
break;
case [Link]:
oper = "/";
result = num1 / num2;
break;
default:
break;
}
// form the output line
[Link](num1 + " " + oper + " " + num2 + " = " + result);
}
}
Android Application for Layout Managers and Event Listeners:
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:text="Details Form"
android:textSize="25sp"
android:gravity="center"/>
</LinearLayout>
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="100dp"
android:layout_marginBottom="200dp"
android:columnCount="2"
android:rowCount="3">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="0"
android:layout_column="0"
android:text="Name"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="0"
android:layout_column="1"
android:ems="10"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="1"
android:layout_column="0"
android:text="[Link]"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="1"
android:layout_column="1"
android:inputType="number"
android:ems="10"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="2"
android:layout_column="0"
android:text="Dept"
android:textSize="20sp"
android:gravity="center"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="2"
android:layout_column="1"
android:spinnerMode="dropdown"/>
</GridLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginBottom="150dp"
android:text="Submit"/>
</RelativeLayout>
activity_2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="[Link].pc_220.[Link]"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
</LinearLayout>
[Link]:
public class MainActivity extends AppCompatActivity {
//Defining the Views
EditText e1,e2;
Button bt;
Spinner s;
//Data for populating in Spinner
String [] dept_array={"CSE","ECE","IT","Mech","Civil"};
String name,reg,dept;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
//Referring the Views
e1= (EditText) findViewById([Link]);
e2= (EditText) findViewById([Link].editText2);
bt= (Button) findViewById([Link]);
s= (Spinner) findViewById([Link]);
//Creating Adapter for Spinner for adapting the data from array to Spinner
ArrayAdapter adapter= new
ArrayAdapter([Link],[Link].simple_spinner_item,dept_array);
[Link](adapter);
//Creating Listener for Button
[Link](new [Link]() {
@Override
public void onClick(View v) {
//Getting the Values from Views(Edittext & Spinner)
name=[Link]().toString();
reg=[Link]().toString();
dept=[Link]().toString();
//Intent For Navigating to Second Activity
Intent i = new Intent([Link],[Link]);
//For Passing the Values to Second Activity
[Link]("name_key", name);
[Link]("reg_key",reg);
[Link]("dept_key", dept);
startActivity(i);
}
});
}
}
[Link]:
package [Link].pc_220.intent;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class SecondActivity extends Activity{
TextView t1,t2,t3;
String name,reg,dept;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_2);
t1= (TextView) findViewById([Link].textView1);
t2= (TextView) findViewById([Link].textView2);
t3= (TextView) findViewById([Link].textView3);
//Getting the Intent
Intent i = getIntent();
//Getting the Values from First Activity using the Intent received
name=[Link]("name_key");
reg=[Link]("reg_key");
dept=[Link]("dept_key");
//Setting the Values to Intent
[Link](name);
[Link](reg);
[Link](dept);
}
}
Change this in Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
package="[Link].pc_220.intent">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity">
</activity>
</application>
</manifest>