Skip to content

Instantly share code, notes, and snippets.

@policante
Created July 27, 2016 20:38
Show Gist options
  • Save policante/08a02cc65384154b47b103cca61c3653 to your computer and use it in GitHub Desktop.
Save policante/08a02cc65384154b47b103cca61c3653 to your computer and use it in GitHub Desktop.
Plate keyboard layout
package com.example.policante.meuapptest;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TextView;
public class KeyboardPlate extends AppCompatActivity {
private static final int MAX_CHARACTER = 7;
private GridLayout gridLetters;
private GridLayout gridNumbers;
private TextView plate;
private String strPlate = "";
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridLetters = (GridLayout) findViewById(R.id.grid_keyboard_letters);
gridNumbers = (GridLayout) findViewById(R.id.grid_keyboard_numbers);
plate = (TextView) findViewById(R.id.plate);
setClickButtons(gridLetters);
setClickButtons(gridNumbers);
findViewById(R.id.btn_clear).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
updateText(null);
}
});
updateText("");
}
private void setClickButtons(GridLayout grid) {
int childCount = grid.getChildCount();
for (int i = 0; i < childCount; i++) {
Button bt = (Button) grid.getChildAt(i);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (view instanceof Button) {
String chr = ((Button) view).getText().toString();
updateText(chr);
}
}
});
}
}
private void updateText(String value) {
if (value != null) {
if (strPlate.length() >= MAX_CHARACTER) {
return;
}
strPlate += value;
} else {
if (strPlate.length() > 0) {
strPlate = strPlate.substring(0, strPlate.length() - 1);
}
}
plate.setText(strPlate);
updateKeyboard();
}
private void updateKeyboard() {
if (strPlate.length() < 3) {
gridLetters.setVisibility(View.VISIBLE);
gridNumbers.setVisibility(View.GONE);
} else {
gridLetters.setVisibility(View.GONE);
gridNumbers.setVisibility(View.VISIBLE);
}
if (strPlate.length() == MAX_CHARACTER) {
new AlertDialog.Builder(this).setMessage("Plate: " + strPlate).setNeutralButton("OK", null).show();
}
}
}
<?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="match_parent"
android:layout_height="match_parent"
tools:context="com.example.policante.meuapptest.KeyboardPlate">
<TextView
android:id="@+id/plate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:textSize="22sp"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:layout_below="@+id/plate"
android:layout_above="@+id/btn_clear">
<GridLayout
android:id="@+id/grid_keyboard_letters"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:columnCount="4"
android:orientation="horizontal"
android:visibility="visible">
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="A"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="B"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="C"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="D"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="E"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="F"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="G"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="H"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="I"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="J"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="K"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="L"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="M"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="N"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="O"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="P"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="Q"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="R"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="S"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="T"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="U"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="V"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="X"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="W"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="Y"
android:textSize="20sp"/>
<Button
android:layout_width="80dp"
android:layout_height="60dp"
android:text="Z"
android:textSize="20sp"/>
</GridLayout>
<GridLayout
android:id="@+id/grid_keyboard_numbers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:columnCount="3"
android:orientation="horizontal"
android:visibility="gone">
<Button
android:layout_width="100dp"
android:layout_height="70dp"
android:text="1"
android:textSize="20sp"/>
<Button
android:layout_width="100dp"
android:layout_height="70dp"
android:text="2"
android:textSize="20sp"/>
<Button
android:layout_width="100dp"
android:layout_height="70dp"
android:text="3"
android:textSize="20sp"/>
<Button
android:layout_width="100dp"
android:layout_height="70dp"
android:text="4"
android:textSize="20sp"/>
<Button
android:layout_width="100dp"
android:layout_height="70dp"
android:text="5"
android:textSize="20sp"/>
<Button
android:layout_width="100dp"
android:layout_height="70dp"
android:text="6"
android:textSize="20sp"/>
<Button
android:layout_width="100dp"
android:layout_height="70dp"
android:text="7"
android:textSize="20sp"/>
<Button
android:layout_width="100dp"
android:layout_height="70dp"
android:text="8"
android:textSize="20sp"/>
<Button
android:layout_width="100dp"
android:layout_height="70dp"
android:text="9"
android:textSize="20sp"/>
<Button
android:layout_width="100dp"
android:layout_height="70dp"
android:text="0"
android:textSize="20sp"/>
</GridLayout>
</LinearLayout>
<Button
android:id="@+id/btn_clear"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:text="limpar"/>
</RelativeLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment