39
loading...
This website collects cookies to deliver better user experience
image
, text
, button
on the phone screen.res/
directory of the project.image
text
button
or anything a app can displayandroid:id
android:id="@+id/text_id"
android:id="@id/text_id"
layout_width
& layout_height
Attributes Value | Description |
---|---|
Hardcoded values |
px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters) |
MATCH_PARENT | which means the view wants to be as big as its parent (minus padding) |
WRAP_CONTENT | which means that the view wants to be just big enough to enclose its content (plus padding) |
android:layout_width="match_parent"
android:layout_height="wrap_content"
screen density
which is numbers of pixel per inch are on the screen , Hences devices with are varies in low,medium, high and extra high resolution.pixel
(px) as a unit it would appear very smaller on high resolution device as there screen density is more View
attributes.match_parent
wrap_content
which is flexible and set accordingly .Sr.No. | UI Control | Description |
---|---|---|
1 | TextView | This control is used to display text to the user |
2 | EditText | Its includes rich editing capabilities |
3 | AutoCompleteTextView | Its similar to EditText and shows a suggestions while the user is typing. |
4 | ImageView | To display image inside the view |
5 | Button | It clicked by the user to perform an action . |
6 | ImageButton | This shows a button with an image (instead of text) that can be pressed or clicked by the user. |
7 | CheckBox | An on/off switch that can be toggled by the user |
8 | ToggleButton | An on/off button with a light indicator. |
9 | RadioButton | The RadioButton has two states: either checked or unchecked. |
10 | RadioGroup | A Radiogroup is used to group together one or more RadioButtons . |
11 | TimePicker | Its enables users to select a time of the day, in either 24-hour mode or AM/PM mode. |
12 | DatePicker | Its enables users to select a date of the day |
And many more.... |
android:text
Text to display.
android:textAllCaps
Present the text in ALL CAPS. Possible value either "true" or "false".
android:textSize
We use attributes values unit as sp
here because its changes to with the preference of user for the font size, which is setting app of the Android devices
android:textStyle
<TextView
android:id="@+id/text_id"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Hello_world"
android:textAllCaps="true"
android:textColor="@android:color/holo_blue_dark
android:textSize="50sp"/>
android:src
Sets a drawable as the content of this ImageView.
How to insert and image file ❓
android:src
to refer our image in the drawable folder @
is used for the referencing a resource in an android app, here drawable
is the resource type, and then we write the filename of the specific image
android:src="@ drawable/image_name
There are many various other resources like static content that your code uses, such as bitmaps, colors, layout definitions, user interface strings, animation instructions, and more. These resources are always maintained separately in various sub-directories under res/
directory of the project.
MyProject/
app/
manifest/
AndroidManifest.xml
java/
MyActivity.java
res/
drawable/
image_name.png
layout/
activity_main.xml
info.xml
values/
strings.xml
android:scaleType
Controls how the image should be resized or moved to match the size of this ImageView.
It has some constant attributes values Lets check some of its ,
center
Center the image in the view, but
perform no scaling.centerCrop
Its scales the image to fit, the bounds of the view And it also maintain the aspect ratio of the image so that it didn't get distorted and then it crop of the edges and centres the image .<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@drawable/image_name />
<Button
android:id="@+id/button_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click me"
android:textColor="@android:color/holo_blue_dark" />
public class MyActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
final Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code Perform action on click
}
});
}
}
activity class
its creates an instance of View.OnClickListener
and wires the listener to the button using setOnClickListener
.button_id
, the above method is called which executes the code written in onClick(View)
methodandroid:onClick
attributes<Button
android:id="@+id/button_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click Me"
android:textColor="@android:color/holo_blue_dark"
android:onClick="study" />
study(View)
method, defined in MainActivity.java file. In order for this to work, the method must be public and accept a View type as its only parameter.public void study(View view) {
//Perform action on click
}
TextView
, Button
, ImageView
,etc.Linear Layout
LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally.
Relative Layout
RelativeLayout is a view group that displays child views in relative to the parent or the other child view.
Table Layout
TableLayout is a view that groups views into rows and columns.
Frame Layout
The FrameLayout is a placeholder on screen that you can use to display a single view.
Constraint Layout
Constraint Layout allows us to create large and complex layouts with a flat view hierarchy, and also allows you to position and size widgets in a very flexible way.