PRÁTICA 4 – LINEAR 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 editTexts em um linear layout horizontal com weigh=1:
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 |
<?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: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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="a" android:id="@+id/textView" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="c" android:id="@+id/textView3" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText2" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="b" android:id="@+id/textView2" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText3" android:layout_weight="1" /> </LinearLayout> |
Programa 2 – Três componentes em um linear layout vertical:
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 |
<?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: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="vertical"> <TextClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textClock" android:layout_gravity="center_horizontal" android:text="z" /> <AnalogClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/analogClock" android:layout_gravity="center_horizontal" /> <DatePicker android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/datePicker" android:layout_gravity="center_horizontal" /> </LinearLayout> |
Programa 3 – Vários linear layout diferentes dentro de um linear layout pai:
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:id="@+id/layout" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Equação de Segundo Grau" android:id="@+id/textView4" android:layout_gravity="center_horizontal" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentBottom="true"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="a" android:id="@+id/textView" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/a" android:layout_weight="1" android:inputType="numberDecimal" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="b" android:id="@+id/textView3" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/b" android:layout_weight="1" android:inputType="numberSigned|numberDecimal" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="c" android:id="@+id/textView2" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/c" android:layout_weight="1" android:inputType="numberSigned|numberDecimal" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="X1=" android:id="@+id/x1" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="X2=" android:id="@+id/x2" android:layout_weight="1" /> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Resolver" android:id="@+id/button2" android:layout_weight="1" android:layout_gravity="right" android:clickable="true" android:onClick="calculaSegundoGrau" /> </LinearLayout> |
Programa 4 – Inserindo código para resolver a equação do segundo grau:
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 |
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; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void calculaSegundoGrau(View v) { EditText a_edit = (EditText) findViewById(R.id.a); EditText b_edit = (EditText) findViewById(R.id.b); EditText c_edit = (EditText) findViewById(R.id.c); TextView x1_text = (TextView) findViewById(R.id.x1); TextView x2_text = (TextView) findViewById(R.id.x2); double a = Double.parseDouble(a_edit.getText().toString()); double b = Double.parseDouble(b_edit.getText().toString()); double c = Double.parseDouble(c_edit.getText().toString()); double delta = b * b - 4*a*c; double x1 = -b + Math.sqrt(delta); double x2 = -b - Math.sqrt(delta); x1_text.setText("X1 = " + String.valueOf(x1)); x2_text.setText("X2 = " + String.valueOf(x2)); Toast.makeText(this, "Resolvido!",Toast.LENGTH_SHORT).show(); } } |
Desafio: Melhore o programa de forma que não dê erros se o formulário estiver vazio e avise se delta for menor que zero:
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 |
package nome do seu pacote! // Esta linha deve ser mantida com o nome do pacote! import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; 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); } public void calculaSegundoGrau(View v) { EditText a_edit = (EditText) findViewById(R.id.a); EditText b_edit = (EditText) findViewById(R.id.b); EditText c_edit = (EditText) findViewById(R.id.c); TextView x1_text = (TextView) findViewById(R.id.x1); TextView x2_text = (TextView) findViewById(R.id.x2); x1_text.setText("X1 = "); x2_text.setText("X2 = "); if (a_edit.getText().length() == 0) { Toast.makeText(this, "Esqueceu o valor de a!",Toast.LENGTH_SHORT).show(); return; } if (b_edit.getText().length() == 0) { Toast.makeText(this, "Esqueceu o valor de b!",Toast.LENGTH_SHORT).show(); return; } if (c_edit.getText().length() == 0) { Toast.makeText(this, "Esqueceu o valor de c!",Toast.LENGTH_SHORT).show(); return; } double a = Double.parseDouble(a_edit.getText().toString()); double b = Double.parseDouble(b_edit.getText().toString()); double c = Double.parseDouble(c_edit.getText().toString()); double delta = b * b - 4*a*c; if (delta < 0) { Toast.makeText(this, "Delta menor que ZERO!",Toast.LENGTH_SHORT).show(); return; } double x1 = -b + Math.sqrt(delta); double x2 = -b - Math.sqrt(delta); x1_text.setText("X1 = " + String.format("%.2f",x1)); x2_text.setText("X2 = " + String.format("%.2f",x2)); } } |