Onscreen Number Pad using GridLayout
GridLayout_Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:columnCount="5"
android:orientation="horizontal"
android:rowCount="9" >
<TextView
android:id="@+id/textView2"
android:layout_column="0"
android:layout_columnSpan="4"
android:layout_gravity="center|fill"
android:layout_marginBottom="5dp"
android:layout_row="0"
android:text="@string/accessid" />
<EditText
android:id="@+id/numberpadtext"
android:layout_column="0"
android:layout_columnSpan="4"
android:layout_gravity="center|fill"
android:layout_row="1"
android:ems="10"
android:inputType="text|textPassword" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_column="0"
android:layout_columnSpan="4"
android:layout_gravity="center|fill"
android:layout_marginBottom="20dp"
android:layout_row="2"
android:layout_rowSpan="2"
android:text="@string/forgotmsg" />
<Button
android:id="@+id/button1"
android:layout_column="0"
android:layout_margin="5dp"
android:layout_row="4"
android:background="@drawable/buttonborder"
android:text="@string/button1" />
<Button
android:id="@+id/button2"
android:layout_column="1"
android:layout_margin="5dp"
android:layout_row="4"
android:background="@drawable/buttonborder"
android:text="@string/button2" />
<Button
android:id="@+id/button3"
android:layout_column="2"
android:layout_margin="5dp"
android:layout_row="4"
android:background="@drawable/buttonborder"
android:text="@string/button3" />
<Button
android:id="@+id/deletebutton"
android:layout_width="108dp"
android:layout_column="3"
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:layout_margin="5dp"
android:layout_row="4"
android:background="@drawable/buttonborder"
android:text="@string/deletebutton" />
<Button
android:id="@+id/button4"
android:layout_column="0"
android:layout_margin="5dp"
android:layout_row="5"
android:background="@drawable/buttonborder"
android:text="@string/button4" />
<Button
android:id="@+id/button5"
android:layout_column="1"
android:layout_margin="5dp"
android:layout_row="5"
android:background="@drawable/buttonborder"
android:text="@string/button5" />
<Button
android:id="@+id/button6"
android:layout_column="2"
android:layout_margin="5dp"
android:layout_row="5"
android:background="@drawable/buttonborder"
android:text="@string/button6" />
<Button
android:id="@+id/button7"
android:layout_column="0"
android:layout_margin="5dp"
android:layout_row="6"
android:background="@drawable/buttonborder"
android:text="@string/button7" />
<Button
android:id="@+id/button8"
android:layout_column="1"
android:layout_margin="5dp"
android:layout_row="6"
android:background="@drawable/buttonborder"
android:text="@string/button8" />
<Button
android:id="@+id/button9"
android:layout_column="2"
android:layout_margin="5dp"
android:layout_row="6"
android:background="@drawable/buttonborder"
android:text="@string/button9" />
<Button
android:id="@+id/clearbutton"
android:layout_width="108dp"
android:layout_column="3"
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:layout_margin="5dp"
android:layout_row="6"
android:background="@drawable/buttonborder"
android:text="@string/clearbutton" />
<Button
android:id="@+id/button0"
android:layout_column="1"
android:layout_margin="5dp"
android:layout_row="7"
android:background="@drawable/buttonborder"
android:text="@string/button0" />
<Button
android:id="@+id/button00"
android:layout_column="2"
android:layout_margin="5dp"
android:layout_row="7"
android:background="@drawable/buttonborder"
android:text="@string/button00" />
<Button
android:id="@+id/loginbutton"
android:layout_width="108dp"
android:layout_column="3"
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:layout_margin="5dp"
android:layout_row="7"
android:background="@drawable/buttonborder"
android:text="@string/loginbutton" />
</GridLayout>
Round Corner of the Buttons:
Create buttonborder.xml file in res/drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="5dp"
android:shape="rectangle" >
<!-- you can use any color you want I used here gray color -->
<solid android:color="#ABABAB" />
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
</shape>
MainActivity.java Class
package com.example.numberpad;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements View.OnClickListener {
Button btn[] = new Button[14];
EditText userinput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridlayout_activity);
//register the buttons
btn[0] = (Button)findViewById(R.id.button1);
btn[1] = (Button)findViewById(R.id.button2);
btn[2] = (Button)findViewById(R.id.button3);
btn[3] = (Button)findViewById(R.id.button4);
btn[4] = (Button)findViewById(R.id.button5);
btn[5] = (Button)findViewById(R.id.button6);
btn[6] = (Button)findViewById(R.id.button7);
btn[7] = (Button)findViewById(R.id.button8);
btn[8] = (Button)findViewById(R.id.button9);
btn[9] = (Button)findViewById(R.id.button0);
btn[10] = (Button)findViewById(R.id.button00);
btn[11] = (Button)findViewById(R.id.loginbutton);
btn[12] = (Button)findViewById(R.id.deletebutton);
btn[13] = (Button)findViewById(R.id.clearbutton);
//register onClick event
for(int i =0;i<14;i++){
btn[i].setOnClickListener(this);
}
}
public void onClick(View v){
switch(v.getId()){
case R.id.button1:
addtoarray("1");
break;
case R.id.button2:
addtoarray("2");
break;
case R.id.button3:
addtoarray("3");
break;
case R.id.button4:
addtoarray("4");
break;
case R.id.button5:
addtoarray("5");
break;
case R.id.button6:
addtoarray("6");
break;
case R.id.button7:
addtoarray("7");
break;
case R.id.button8:
addtoarray("8");
break;
case R.id.button9:
addtoarray("9");
break;
case R.id.button0:
addtoarray("0");
break;
case R.id.button00:
addtoarray("00");
break;
case R.id.loginbutton:
break;
case R.id.deletebutton:
//get the length of input
int slength = userinput.length();
if(slength > 0){
//get the last character of the input
String selection = userinput.getText().toString().substring(slength-1, slength);
Log.e("Selection",selection);
String result = userinput.getText().toString().replace(selection, "");
Log.e("Result",result);
userinput.setText(result);
userinput.setSelection(userinput.getText().length());
}
break;
case R.id.clearbutton:
userinput = (EditText)findViewById(R.id.numberpadtext);
userinput.setText("");
break;
default:
}
}
public void addtoarray(String numbers){
//register TextBox
userinput = (EditText)findViewById(R.id.numberpadtext);
userinput.append(numbers);
}
}