0% found this document useful (0 votes)
50 views38 pages

Android XML Layout Setup Guide

The document provides a step-by-step guide for creating and running an Android application, including modifying XML layout files and Java activity classes. It outlines the structure of the application, including the main activity and additional activities, along with their respective layouts. The final output confirms that the application has been executed successfully.

Uploaded by

Selvam Mano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views38 pages

Android XML Layout Setup Guide

The document provides a step-by-step guide for creating and running an Android application, including modifying XML layout files and Java activity classes. It outlines the structure of the application, including the main activity and additional activities, along with their respective layouts. The final output confirms that the application has been executed successfully.

Uploaded by

Selvam Mano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. A new project environment is shown as bellow.

2. Double click the [Link] which is located in


Android>app>res>values>[Link] at the project window.
3. Add the following code as bellow in [Link] file.

4. Open and add the as bellow in the [Link] file.

5. Double click the activity_main.xml under the layout directory to add desired coding
and design.
6. Run the project and view the output in an emulator.
7. Save and close the application.
Source Code:

activity_main.xml

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


<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/clg"
android:textSize="40dp"
android:textColor="@color/green"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.278" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dept"
android:textColor="@color/blue"
android:textSize="35dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.391" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="@color/black"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="188dp"
android:layout_height="180dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.016"
app:srcCompat="@drawable/logo" />

</[Link]>

[Link]

package [Link].ex_1;

import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
}
Output:

Result:

Thus, the above android application has been executed successfully.


Source Code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Relative Layout Example"
android:textSize="30dp"
android:layout_marginTop="60dp"
android:layout_centerHorizontal="true"
android:textColor="#FF0000"
android:id="@+id/t1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name"
android:textSize="30dp"
android:layout_marginTop="190dp"
android:layout_centerHorizontal="false"
android:textColor="#002AFF"
android:id="@+id/t2"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="190dp"
android:layout_toRightOf="@id/t2"
android:id="@+id/e1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:textSize="30dp"
android:layout_marginTop="260dp"
android:layout_centerHorizontal="false"
android:textColor="#0027FF"
android:id="@+id/t3"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/t3"
android:password="true"
android:layout_marginTop="259dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/t3"
android:layout_marginTop="28dp"
android:layout_centerHorizontal="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="25dp"
android:id="@+id/b1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign Up"
android:textSize="25dp"
android:id="@+id/b2"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"
android:textSize="30dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>

[Link]
package [Link];

import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
}
Output

Result:
Thus, the above android application has been executed successfully.
Source Code:
First Activity
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple Intent Page"
android:textSize="40dp"
android:layout_centerInParent="true"
android:id="@+id/t1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Here"
android:textSize="30dp"
android:layout_below="@id/t1"
android:layout_centerInParent="true"
android:id="@+id/b1"
android:onClick="myintents"/>
</RelativeLayout>
[Link]
package [Link].ex3;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
TextView t1;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
t1=findViewById([Link].t1);
b1=findViewById([Link].b1);
}
public void myintents(View view) {
Intent i= new Intent(this,[Link]);
startActivity(i);
}
}
Second Activity
[Link]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="This is second page"
android:textSize="40dp" />
</RelativeLayout>

[Link]
package [Link].ex3;
import [Link];
import [Link];
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link]);
}
}
Result:
Thus, the above android application has been executed successfully.
Source Code:
First Activity
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[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="150dp"
android:background="#00BCD4">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="WELCOME"
android:textColor="#FF0000"
android:textSize="60dp"
android:textStyle="bold"
android:layout_marginTop="34dp"
android:layout_marginLeft="60dp"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="45dp"
android:textColor="#000CFF"
android:layout_marginTop="250dp"
android:id="@+id/t1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textSize="30dp"
android:textColor="#000000"
android:layout_marginTop="340dp"
android:id="@+id/t2"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="380dp"
android:id="@+id/e1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:textSize="30dp"
android:textColor="#000000"
android:layout_marginTop="450dp"
android:id="@+id/t3"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:password="true"
android:layout_marginTop="505dp"
android:id="@+id/e2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:textSize="30dp"
android:layout_alignParentBottom="true"
android:onClick="Nextpage"
android:id="@+id/b1"/>
</RelativeLayout>
Second Activity
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity2">
<TextView
android:layout_width="match_parent"
android:layout_height="70dp"
android:text="Login Successfully!!"
android:textSize="40dp"
android:textColor="@color/design_default_color_background"
android:textStyle="bold"
android:background="#FF0000"
android:layout_marginTop="30dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/tv1"
android:textSize="15dp"/>
</RelativeLayout>
[Link]
package [Link].ex4;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
TextView t1,t2,t3;
EditText e1,e2;
Button b1;
String msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
t1=findViewById([Link].t1);
t2=findViewById([Link].t2);
t3=findViewById([Link].t3);
e1=findViewById([Link].e1);
e2=findViewById([Link].e2);
b1=findViewById([Link].b1);
}
public void Nextpage(View view) {
msg=[Link]().toString();
Intent i=new Intent(this,[Link]);
[Link]("key",msg); startActivity(i);
}
}
[Link]
package [Link].ex4;
import [Link];
import [Link];
import [Link];
public class MainActivity2 extends AppCompatActivity{
TextView tv1;
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_second);
tv1=findViewById([Link].tv1);
String msg = getIntent().getStringExtra("key");
[Link]("Login using "+msg);
}
}
Output:

Result:
Thus the above application has been created and executed successfully.
Source Code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context="MainActivity">
<Button
android:id="@+id/animateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animate Me"
android:layout_centerInParent="true" />
</RelativeLayout>

[Link]
package [Link].ex_5;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].ex_5.R;
import [Link];
public class MainActivity extends AppCompatActivity {
private Button animateButton;
private boolean isAnimating = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
animateButton = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
if (!isAnimating)
{ startAnimation(); }
}
});
}
private void startAnimation()
{ isAnimating = true; int fromXDelta = 0;
int toXDelta = 200;
// Change this value to adjust the distance of the animation
int duration = 1000;
// Animation duration in milliseconds
Animation animation = new TranslateAnimation(fromXDelta, toXDelta, 0, 0);
[Link](duration);
[Link](new [Link]() {
@Override
public void onAnimationStart(Animation animation)
{}
@Override
public void onAnimationEnd(Animation animation) {
isAnimating = false;
}
@Override
public void onAnimationRepeat(Animation animation)
{ } });
[Link](animation); }
}
Output:

Result:

Thus, the above simple application for animation in an android studio has been
created and executed successfully.
Source Code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculator"
android:textSize="50dp"
android:textColor="#FF0000"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NUM 1"
android:textSize="30dp"
android:layout_marginTop="160dp"
android:id="@+id/t1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28dp"
android:id="@+id/et1"
android:layout_marginTop="163dp"
android:layout_toRightOf="@+id/t1"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NUM 2"
android:textSize="30dp"
android:layout_marginTop="260dp"
android:id="@+id/t2"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28dp"
android:id="@+id/et2"
android:layout_marginTop="263.5dp"
android:layout_toRightOf="@+id/t2"/>
<Button android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="+" android:textSize="40dp"
android:id="@+id/b1" android:onClick="Add"
android:layout_marginTop="380dp"
android:layout_marginLeft="50dp"/>
<Button
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="-"
android:textSize="40dp"
android:id="@+id/b2"
android:onClick="Sub"
android:layout_marginTop="380dp"
android:layout_marginLeft="220dp"/>
<Button android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="*" android:textSize="40dp"
android:id="@+id/b3" android:onClick="Mul"
android:layout_marginTop="490dp"
android:layout_marginLeft="50dp"/>
<Button
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="/"
android:textSize="40dp"
android:id="@+id/b4"
android:onClick="Div"
android:layout_marginTop="490dp"
android:layout_marginLeft="220dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_centerHorizontal="true"
android:textSize="30dp"
android:textColor="#3F51B5"
android:layout_marginTop="600dp" android:id="@+id/t3"/>
</RelativeLayout>
[Link]
package [Link].ex6;

import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


float f1,f2,f;
TextView t1,t2,t3;
EditText et1,et2;
Button b1,b2,b3,b4;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
t1=findViewById([Link].t1);
t2=findViewById([Link].t2);
t3=findViewById([Link].t3);
et1=findViewById([Link].et1);
et2=findViewById([Link].et2);
b1=findViewById([Link].b1);
b2=findViewById([Link].b2);
b3=findViewById([Link].b3);
b4=findViewById([Link].b4);
}
public void Add(View view) {
f1= [Link]([Link]().toString());
f2= [Link]([Link]().toString());
f=f1+f2; [Link]("Addition is "+f);
}
public void Sub(View view) {
f1= [Link]([Link]().toString());
f2= [Link]([Link]().toString());
f=f1-f2; [Link]("Subraction is "+f);
}
public void Mul(View view) {
f1= [Link]([Link]().toString());
f2= [Link]([Link]().toString());
f=f1*f2; [Link]("Multiplication is "+f);
}
public void Div(View view) {
f1= [Link]([Link]().toString());
f2= [Link]([Link]().toString());
f=f1/f2;
[Link]("Division is "+f);
}
}
Output:

Result:

Thus, the above simple calculator application in an android studio has been created
and executed successfully.
Source Code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btncam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Camera"
android:textSize="35dp"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</[Link]>
[Link]
package [Link].ex7;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
Button btnCam;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
btnCam = (Button) findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View view) {
try {
Intent intent = new Intent();
[Link](MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(intent);
}catch (Exception e)
{
[Link]();
}
}
});
Output:

Result:

Thus, the above android application has been executed successfully.


Source Code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
[Link]
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
ListView l;
String languages[]
= { "C", "C++",
"Java", "Python",
"R", "C#",
"Swift", "Ruby",
"Rust" };
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
l = findViewById([Link]);
ArrayAdapter<String> arr;
arr = new ArrayAdapter<String>(this,
[Link].support_simple_spinner_dropdown_item,languages);
[Link](arr);
}
}
Output

Result:
Thus, the above application has been created and result is verified successfully.

You might also like