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

Tutorial 2 Android

The document discusses using LinearLayout in Android development. It describes how to use layout_weight to distribute extra space equally among views. It provides an XML layout code example with three TextViews distributed evenly using layout_weight. It then shows another example using Buttons where the second Button is given double the weight of the others. The text also explains how to use LinearLayout with orientation set to vertical and horizontal to position views. It describes initializing views and using OnClick listeners to respond to button clicks with Toast notifications.

Uploaded by

Simi Sim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Tutorial 2 Android

The document discusses using LinearLayout in Android development. It describes how to use layout_weight to distribute extra space equally among views. It provides an XML layout code example with three TextViews distributed evenly using layout_weight. It then shows another example using Buttons where the second Button is given double the weight of the others. The text also explains how to use LinearLayout with orientation set to vertical and horizontal to position views. It describes initializing views and using OnClick listeners to respond to button clicks with Toast notifications.

Uploaded by

Simi Sim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

LinearLayout

Proprietatea layout_weight ocupa spatiu suplimentar

LinearLayout

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:gravity="center|top"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView android:text="Hello World!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Goodbye"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:text="More Random Text"
android:layout_weight="1"/>
</LinearLayout>

MARGIN
BORDER
PADDING
CONTENT

CONTENT

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:gravity="center|top"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1st Button"
android:textSize="15sp"
android:padding="15dp"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2nd Button"
android:textSize="15sp"
android:padding="15dp"
android:layout_weight="2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3rd Button"
android:textSize="15sp"
android:padding="15dp"/>
</LinearLayout>

Functionalitatea aplicatiei

EditText

Notificare: Toast

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:gravity="center|top"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/happy_question"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/yes_button"
android:hint="Pick me"
android:onClick="onYesButtonClick"
android:id="@+id/yes_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_button"
android:hint="Don't pick me"
android:onClick="onNoButtonClick"
android:id="@+id/no_button"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="200dp"
android:layout_height="50dp"
android:inputType="textPersonName"
android:hint="@string/users_name_editText"
android:id="@+id/user_name_editText"/>
</LinearLayout>
</LinearLayout>

LinearLayout
Are you happy?
Orientation: horizontal
LinearLayout
YES

NO

Orientation: vertical

LinearLayout

Orientation: horizontal

Your name

hint

MainActivity.java
package com.example.simi.messingaround;
import
import
import
import
import
import

android.support.v7.app.AppCompatActivity;
android.os.Bundle;
android.view.View;
android.widget.Button;
android.widget.EditText;
android.widget.Toast;

public class MainActivity extends AppCompatActivity {

//declarare controale
private Button YesButton;
private Button NoButton;
private EditText userName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//initializare controale
YesButton = (Button)findViewById(R.id.yes_button);
NoButton = (Button)findViewById(R.id.no_button);
userName = (EditText)findViewById(R.id.user_name_editText);
}
public void onYesButtonClick(View view) {
//notificare
//memoram numele introdus in controlul EditText intr-o variabila de

tip String
String Name = String.valueOf(userName.getText());
String yourYesResponse = "That's great "+Name;
Toast.makeText(this, yourYesResponse, Toast.LENGTH_SHORT).show();
}
public void onNoButtonClick(View view) {
String Name = String.valueOf(userName.getText());
String yourNoResponse = "To bad "+Name;
Toast.makeText(this, yourNoResponse, Toast.LENGTH_SHORT).show();
}
}

Are you happy?

Yes

Ne folosim de proprietatea onClick a


butonului pentru a-l face responsive

No
Luam numele din EditText si il memoram

Simi
String Name =
String.valueOf(userName.getText());

Memoram mesajul care vrem sa apara la apasarea


butonului si numele persoanei introdus in EditText
intr-o alta variabila de tip String
String yourYesResponse = "That's great "+Name;

Toast.makeText(this, yourNoResponse, Toast.LENGTH_SHORT).show();

Parametrii:
Ce activitate

String

Mesaj dorit

Cat timp vrem sa apara mesajul pe


ecran

Toast.LENGTH_SHORT

Toast.LENGTH_LONG

You might also like