How to enter data in my EditText using my own buttons instead of default keyboard (I mean like in calculator apps). Please write me some method or show suitable example. Thanks.
I initialize veriable, even tried to make some algorithm. But there is no reason to go further without knowing how to input data.
And of course I was looking for the answer.
public class MainActivity extends Activity implements OnClickListener{
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bPlus,bMin,bDiv,bMult,bEqual;
String sN1,sN2,func,result;
EditText etEnter;
SoundPool pool; int shot = 0;
TextView tvShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator);
initVars();
}
private void initVars(){
etEnter =(EditText)findViewById(R.id.etCalc);
tvShow = (TextView)findViewById(R.id.tvShow);
b0 = (Button)findViewById(R.id.button0);
b1 = (Button)findViewById(R.id.button1);
b2 = (Button)findViewById(R.id.button2);
b3 = (Button)findViewById(R.id.button3);
b4 = (Button)findViewById(R.id.button4);
b5 = (Button)findViewById(R.id.button5);
b6 = (Button)findViewById(R.id.butt6);
b7 = (Button)findViewById(R.id.butt7);
b8 = (Button)findViewById(R.id.butt8);
b9 = (Button)findViewById(R.id.butt9);
bMin = (Button)findViewById(R.id.bMinus);
bPlus = (Button)findViewById(R.id.bPlus);
bDiv = (Button)findViewById(R.id.bDiv);
bMult = (Button)findViewById(R.id.bMult);
bEqual = (Button)findViewById(R.id.bEqual);
b0.setOnClickListener(this);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
bMin.setOnClickListener(this);
bPlus.setOnClickListener(this);
bDiv.setOnClickListener(this);
bMult.setOnClickListener(this);
bEqual.setOnClickListener(this);
pool = new SoundPool(5,AudioManager.STREAM_MUSIC,0);
shot = pool.load(this, R.raw.shot, 1);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button0:
break;
case R.id.button1:
break;
case R.id.button2:
break;
case R.id.button3:
break;
case R.id.button4:
break;
case R.id.button5:
break;
case R.id.butt6:
break;
case R.id.butt7:
break;
case R.id.butt8:
break;
case R.id.butt9:
break;
case R.id.bMinus:
func = "-";
break;
case R.id.bPlus:
func = "+";
break;
case R.id.bDiv:
func = "/";
break;
case R.id.bMult:
func = "*";
break;
case R.id.bEqual:
pool.play(shot, 1, 1, 0, 0, 1);
if(func.contentEquals("+")){
if(sN1!=null && sN2!=null){
long l1 = Long.parseLong(sN1);
long l2 = Long.parseLong(sN2);
long lRes = l1+l2;
result = String.valueOf(lRes);
tvShow.setText(result);
}else if(sN1!=null && sN2==null){
tvShow.setText(sN1);
}
sN1=sN2=null;
}else if(func.contentEquals("-")){
if(sN1!=null && sN2!=null){
long l1 = Long.parseLong(sN1);
long l2 = Long.parseLong(sN2);
long lRes = l1-l2;
result = String.valueOf(lRes);
tvShow.setText(result);
}else if(sN1!=null && sN2==null){
tvShow.setText(sN1);
}
sN1=sN2=null;
}else if(func.contentEquals("/")){
if(sN1!=null && sN2!=null){
long l1 = Long.parseLong(sN1);
long l2 = Long.parseLong(sN2);
long lRes = l1/l2;
result = String.valueOf(lRes);
tvShow.setText(result);
}else if(sN1!=null && sN2==null){
tvShow.setText(sN1);
}
sN1=sN2=null;
}else if(func.contentEquals("*")){
if(sN1!=null && sN2!=null){
long l1 = Long.parseLong(sN1);
long l2 = Long.parseLong(sN2);
long lRes = l1*l2;
result = String.valueOf(lRes);
tvShow.setText(result);
}else if(sN1!=null && sN2==null){
tvShow.setText(sN1);
}
sN1=sN2=null;
}
break;
}
}
and XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/rainbow"
>
<LinearLayout android:paddingTop="22dp" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" >
<EditText
android:id="@+id/etCalc"
android:layout_width="260dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:gravity="right"
android:textSize="30dp"
android:inputType="number" />
</LinearLayout>
<LinearLayout android:paddingRight="33dp" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" >
<TextView
android:id="@+id/tvShow"
android:layout_width="100dp"
android:layout_height="45dp"
android:text=":-)"
android:textSize="35dp"
android:paddingRight="10dp"
android:layout_gravity="right"
android:background="@drawable/rect_buttons"
android:gravity="right"
></TextView>"
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:paddingTop="20dp" >
<Button
android:id="@+id/bPlus"
android:layout_width="70dp"
android:layout_height="50dp"
android:textSize="30dp"
android:gravity="center"
android:text="+" />
<Button
android:id="@+id/bMinus"
android:layout_width="70dp"
android:layout_height="50dp"
android:textSize="30dp"
android:text="-" />
<Button
android:layout_gravity="center"
android:id="@+id/bEqual"
android:layout_width="70dp"
android:layout_height="50dp"
android:textSize="30dp"
android:text="=" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" >
<Button
android:id="@+id/button1"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="1"
android:textSize="30dp" />
<Button
android:id="@+id/button2"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="2"
android:textSize="30dp" />
<Button
android:id="@+id/button3"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="3"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" >
<Button
android:id="@+id/button4"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="4"
android:textSize="30dp" />
<Button
android:id="@+id/button5"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="5"
android:textSize="30dp" />
<Button
android:id="@+id/butt6"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="6"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" >
<Button
android:id="@+id/butt7"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="7"
android:textSize="30dp" />
<Button
android:id="@+id/butt8"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="8"
android:textSize="30dp" />
<Button
android:id="@+id/butt9"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="9"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" >
<Button
android:id="@+id/bDiv"
android:layout_width="70dp"
android:layout_height="50dp"
android:textSize="30dp"
android:text="/" />
<Button
android:layout_gravity="center"
android:id="@+id/button0"
android:layout_width="70dp"
android:layout_height="50dp"
android:textSize="30dp"
android:text="0" />
<Button
android:id="@+id/bMult" android:layout_width="70dp" android:layout_height="50dp" android:textSize="30dp" android:text="*" />
</LinearLayout>
</LinearLayout>
You have to make your
editTextnon focusable/editable and modify the text inside when you click on your buttons. Easy !