PRÁTICA 5 – RELATIVE LAYOUT
Objetivo:
Utilizar Linear Layout para organizar componentes no Layout.
Introdução
Existem 4 tipos básicos de layout de componentes em um Activity Android:
- Linear Horizontal: Os componentes ficam dispostos na horizontal dividindo o espaço.
- Linear Vertical: Os componentes ficam dispostos na vertical dividindo o espaço.
- Table: Os componentes ficam dispostos em uma tabela.
- Relative: Os componentes ficam dispostos de forma relativa a outros.
Tarefa:
Criar tipos diferentes de layout com buttons, editText e textView:
Programa 1 – Três componentes em layout relativo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="empty.ammsoft.empty.MainActivity" android:id="@+id/layout" android:orientation="horizontal"> <TextClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textClock" android:layout_above="@+id/calendarView" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/analogClock" android:layout_toStartOf="@+id/analogClock" android:gravity="center" /> <AnalogClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/analogClock" android:layout_alignParentTop="true" android:layout_alignRight="@+id/calendarView" android:layout_alignEnd="@+id/calendarView" /> <CalendarView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/calendarView" android:layout_below="@+id/analogClock"/> </RelativeLayout> |
Programa 2 – Componentes coloridos em várias posições na tela com layout relativo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent"> <TextView android:text="RED" android:id="@+id/TextView01" android:layout_height="wrap_content" android:background="#f00" android:gravity="center" android:textColor="#000" android:layout_width="wrap_content" android:padding="25dp"></TextView> <TextView android:text="ORANGE" android:layout_height="wrap_content" android:background="#ffa500" android:gravity="center" android:textColor="#000" android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_centerHorizontal="true" android:padding="25dp"></TextView> <TextView android:text="YELLOW" android:layout_height="wrap_content" android:background="#ffff00" android:gravity="center" android:textColor="#000" android:id="@+id/TextView03" android:layout_width="wrap_content" android:layout_alignParentRight="true" android:padding="25dp"></TextView> <TextView android:text="GREEN" android:layout_height="wrap_content" android:background="#0f0" android:gravity="center" android:textColor="#000" android:id="@+id/TextView04" android:layout_width="wrap_content" android:layout_toLeftOf="@+id/TextView05" android:padding="25dp" android:layout_centerVertical="true"></TextView> <TextView android:text="BLUE" android:layout_height="wrap_content" android:background="#00f" android:gravity="center" android:textColor="#fff" android:id="@+id/TextView05" android:layout_width="wrap_content" android:layout_centerInParent="true" android:layout_margin="10dp" android:padding="25dp"></TextView> <TextView android:text="INDIGO" android:layout_height="wrap_content" android:gravity="center" android:textColor="#fff" android:id="@+id/TextView06" android:layout_width="wrap_content" android:layout_toRightOf="@+id/TextView05" android:background="#4b0082" android:padding="25dp" android:layout_centerVertical="true"></TextView> <TextView android:text="VIOLET" android:layout_height="wrap_content" android:background="#ee82ee" android:gravity="center" android:textColor="#000" android:id="@+id/TextView07" android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:padding="25dp"></TextView> </RelativeLayout> |
Programa 3 – Componentes com layout relativo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerInParent="false" android:text="Passwords!" android:layout_marginTop="14dp" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="Password :" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_below="@+id/text1" /> <EditText android:id="@+id/senha" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@+id/textView2" android:layout_toRightOf="@+id/textView2" android:inputType="textPassword" /> <Button android:id="@+id/btnSubmit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="false" android:layout_below="@+id/senha" android:layout_centerInParent="true" android:text="Ver" android:clickable="true" android:onClick="senha" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Lista de passwords: \n adriano -> 1234 \n cefet ->4321" android:id="@+id/senhasTexto" android:layout_below="@+id/btnSubmit" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout> |
Programa 3 – Código java de forma a implementar a ideia de um APP que armazene uma lista de senhas, protegido com uma senha:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package nome do seu pacote! // Esta linha deve ser mantida com o nome do pacote! import android.content.Intent; import android.renderscript.Double2; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.widget.EditText; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView senhastexto = (TextView) findViewById(R.id.senhasTexto); senhastexto.setVisibility(View.INVISIBLE); } public void senha(View v) { EditText senha = (EditText) findViewById(R.id.senha); TextView senhastexto = (TextView) findViewById(R.id.senhasTexto); if (senha.getText().toString().equals("1234")) { senhastexto.setVisibility(View.VISIBLE); } } } |
Desafio: Modifique o código para exibir uma mensagem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package nome do seu pacote! // Esta linha deve ser mantida com o nome do pacote! import android.content.Intent; import android.renderscript.Double2; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.widget.EditText; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView senhastexto = (TextView) findViewById(R.id.senhasTexto); senhastexto.setVisibility(View.INVISIBLE); } public void senha(View v) { EditText senha = (EditText) findViewById(R.id.senha); TextView senhastexto = (TextView) findViewById(R.id.senhasTexto); if (senha.getText().toString().equals("1234")) { senhastexto.setVisibility(View.VISIBLE); } else { Toast.makeText(this, "Erro!!",Toast.LENGTH_SHORT).show(); } } } |