Skip to content

Instantly share code, notes, and snippets.

@fedexdear
Last active May 16, 2016 14:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fedexdear/6228026 to your computer and use it in GitHub Desktop.
Save fedexdear/6228026 to your computer and use it in GitHub Desktop.
[Lesson 3] ความสัมพันธ์ของ Layout และโค้ด Java ในการเขียน Android ... อ่านรีวิวต่อได้ที่ http://review.thaiware.com/435-Lesson-3-Layout-Java-Android.html
<RelativeLayout 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"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical" >
<TextView
android:id="@+id/topText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textStyle="bold"
android:text="@string/main_text" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Name: " />
<EditText
android:id="@+id/editName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Surname: " />
<EditText
android:id="@+id/editSurname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Email: " />
<EditText
android:id="@+id/editEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Tel: " />
<EditText
android:id="@+id/editTel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:gravity="center" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Done"
android:id="@+id/doneMember"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:id="@+id/clearMember"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result: " />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/showResult"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
package com.thaiware.codetesting;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
//Declare Widgets
EditText fieldName, fieldSurname, fieldEmail, fieldTel;
TextView fieldText;
Button btnDone, btnClear;
String showName = "";
String showSurname, showEmail, showTel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Reference with layout file that is "activity_main.xml"
fieldName = (EditText) findViewById(R.id.editName);
fieldSurname = (EditText) findViewById(R.id.editSurname);
fieldEmail = (EditText) findViewById(R.id.editEmail);
fieldTel = (EditText) findViewById(R.id.editTel);
fieldText = (TextView) findViewById(R.id.showResult);
btnDone = (Button) findViewById(R.id.doneMember);
//Set event to button "Done"
btnDone.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(fieldName.getText().toString().equals("")){
//Display alertdialog
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setIcon(android.R.drawable.ic_dialog_alert);
alert.setTitle("Please insert your information !");
alert.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}else{
//Get text from EditText
showName = fieldName.getText().toString();
showSurname = fieldSurname.getText().toString();
showEmail = fieldEmail.getText().toString();
showTel = fieldTel.getText().toString();
//Display result of input
fieldText.setText(showName +" "+ showSurname +"\n"+ showEmail+"\n"+showTel);
Toast.makeText(getBaseContext(), "Insert "+ "'"+showName+"'" +" Succeed.", Toast.LENGTH_SHORT).show();
//Clear EditText after preess Done button
fieldName.setText("");
fieldSurname.setText("");
fieldEmail.setText("");
fieldTel.setText("");
}
}
});
//Clear EditText if user presses "Clear" button
btnClear = (Button) findViewById(R.id.clearMember);
btnClear.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
fieldName.setText("");
fieldSurname.setText("");
fieldEmail.setText("");
fieldTel.setText("");
fieldText.setText("");
Toast.makeText(getBaseContext(), "Clear Succeed.", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">First App Ja</string>
<string name="menu_settings">Settings</string>
<string name="main_text">Member System Testing</string>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment