0% found this document useful (0 votes)
13 views7 pages

AP Assignment 2

The document provides an overview of various Android UI components including TextView, Implicit Intent, EditText, Layouts, and Toast. It explains the properties and usage of each component with code examples. Key attributes and methods for each component are highlighted to aid in understanding their functionality in Android development.

Uploaded by

akar080928
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)
13 views7 pages

AP Assignment 2

The document provides an overview of various Android UI components including TextView, Implicit Intent, EditText, Layouts, and Toast. It explains the properties and usage of each component with code examples. Key attributes and methods for each component are highlighted to aid in understanding their functionality in Android development.

Uploaded by

akar080928
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
You are on page 1/ 7

ASSIGNMENT 2

1.Explain Textview with example.


¬​ The simplest widgets the label, referred to in Android as a TextView.
¬​ As in most GUI toolkits, labels are bits of text that can’t be edited directly by users.
¬​ Typically, labels are used to identify adjacent widgets (e.g., a “Name:” label
next to a field where the user fills in a name
¬​ TextView has numerous other properties of relevance for labels, such as the
following
♣​ text: defines the text that would be displayed on the screen.
♣​ textStyle: sets the style of the text. The possible choices are bold, italic and
normal.
♣​ fontFamily: specifies the font family for the text.
♣​ typeFace: as you can imagine it defines the typeface for the text. The
possible values are normal, sans, serif and monospace.
♣​ textSize: defines the size of the text. It is recommended to use sp for the size.
♣​ textColor: sets the color of the text. The color has to be defined in hex
encoding or as a reference to another resource.
♣​ background: sets the background color of the text. Again the color
should be defined in hex encoding or as a reference to another
resource.
♣​ textColorHighlight: defines the color when the text is marked as highlighted.
♣​ textIsSelectable: indicates whether the text can be selected (true) or not
(false). This attribute can be used in order to allow copy-paste controls.
♣​ lines: defines the exact height of the TextView in lines.
♣​ clickable: indicates if the view reacts to click events.
♣​ autoLink: identifies the links into the text and converts them into
clickable ones. The available choices are web, email, phone, map or all of
them.
Example 1
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/txt1"
android:text="Click to text view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:clickable="true"
android:onClick="perform_action"
/>
</LinearLayout>

2.Explain implicit intent with example.


ASSIGNMENT 2

Implicit Intent

•​ Implicit Intent doesn’t specifiy the component. In such case, intent


provides information of available components provided by the system that
is to be invoked.

•​ For example, you may write the following code to view the webpage. Intent
intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(http://www.javatpoint.com)); startActivity(intent);

Example:-1(IMPLICIT INTENTS)

▪​ Step 1: (activity_main.xml)

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText android:id="@+id/et1"

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />

<Button android:id="@+id/bt1"

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Visit" />

</LinearLayout>

Step 2: (MainActivity.java)
public class MainActivity extends Activity

{
ASSIGNMENT 2

EditText et1;

Button bt1;

@Override

public void onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
et1=(EditText)findViewById(R.id.et1); bt1=(Button)findViewById(R.id.bt1);
bt1.setOnClickListener(new OnClickListener()

public void onClick(View arg0)

// TODO Auto-generated method stub

String url=et1.getText().toString();

Intent I1=new Intent(Intent.ACTION_VIEW,Uri.parse(url));


startActivity(I1);
}

}};

3.Explain Edittext with example.


​ attributes and methods of
▪​ hint: defines the hint text that would be displayed in the edit text. The text
can be defined directly or as a reference tovalues/strings.xml resource.
▪​ singleLine: sets whether the text is appeared in one line (true) or if it is
wrapped into multiple ones (false).
▪​ maxLength: specifies the maximum number of characters that the user could
put into the text field.
▪​ digits: specifies the specific numbers that are accepted to be used.
▪​ inputType: declares the type of data that the user can fill in the text field.
Some of the possible choices are textCapWords, email, phone etc, so the
input in the text field is adjusted respectively. For multiple than one
choices we should use | character for separation.
▪​ password: indicates that the input is going to be a password, so the text
is hidden to the user. Another way to do this, is to set the attribute
inputType into textPassword.
ASSIGNMENT 2

▪​ As we mentioned we can use the components of TextView, where some


of them are defined to our example:
▪​ background: sets the background color of the edit text. Again the color
should be defined in hex encoding or as a reference to another resource.
▪​ clickable: indicates if the view reacts to click events.
▪​ Also​ in​ this​ example,​ we​ used​ more​ general
​ components​ such as layout_width and layout_height or id,
which uniquely identifies the respective view

Example:-1

Step 1: (activity_main.xml)

<LinearLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<TextView android:id="@+id/txt1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Enter Your Name" />

<EditText android:id="@+id/et1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:inputType="text" />

<Button android:id="@+id/btn1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Display" />

</LinearLayout>

Step 2: (MainActivity.java)

public class MainActivity extends Activity

EditText e1; Button b1; @Override


ASSIGNMENT 2

public void onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

e1 = (EditText) findViewById(R.id.et1);

b1 = (Button) findViewById(R.id.btn1);

b1.setOnClickListener(new OnClickListener()

public void onClick(View v)

String str = e1.getText().toString();


Toast.makeText(getBaseContext(),str,Toast.LENGTH_LONG).show();

});

4.Explain any two Layout with example.


ϖ​ Absolute Layout:-AbsoluteLayout enables you to specify the exact location of its
children.
¬​ android:id:-This is the ID which uniquely identifies the layout
¬​ android:layout_x:-This specifies the x-coordinate of the view.
¬​ android:layout_y:-This specifies the y-coordinate of the view.

Example:-
♣​ Step 1: (activity_main.xml)
<AbsoluteLayout >
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:layout_x="55px"
android:layout_y="361px" />
</AbsoluteLayout>
ϖ​ Frame Layout:-The FrameLayout is a placeholder on screen that you can use to display
a single view
ASSIGNMENT 2

¬​ android:id:-This is the ID which uniquely identifies the layout


¬​ android:foreground:-This defines the drawable to draw over the content and
possible values may be a color value, in the form of "#rgb", "#argb",
"#rrggbb", or "#aarrggbb".
¬​ android:foregroundGravity:-Defines the gravity to apply to the foreground
drawable. The gravity defaults to fill. Possible values are top, bottom, left,
right, center, center_vertical, center_horizontal etc.
¬​ android:measureAllChildren:-Determines whether to measure all children
or just those in the VISIBLE or INVISIBLE state when measuring. Defaults to
false.

Example:-
♣​ Step 1: (activity_main.xml)

<FrameLayout />

<TextView android:text="Frame Demo"

android:layout_height="fill_parent"

android:layout_width="fill_parent"

android:gravity="center"/>

</FrameLayout>

5.Explain Toast with example.

Andorid Toast can be used to display information for the short period of time. A toast
contains message to be displayed quickly and disappears after sometime.

 The android.widget.Toast class is the subclass of java.lang.Object class.

 You can also create custom toast as well for example toast displaying image. You
can visit next page to see the code for custom toast.

Example

Activity_main.xml

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

<androidx.constraintlayout.widget.ConstraintLayout >

<TextView

Android:layout_width=”wrap_content”

Android:layout_height=”wrap_content”

Android:text=”display toast message!”


ASSIGNMENT 2

/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Toast.makeText(getApplicationContext(),”Hello students its Toast”,

Toast.LENGTH_SHORT).show();

You might also like