I've made a working simple calculator with some bugs here and there, I decided to make all the buttons round by adding the appropriate drawable xml files to make them round sadly after doing so the buttons now won't work.
Here is the Main.java
package com.example.calculatorinator;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import org.w3c.dom.Text;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
public class txtEvent implements TextToSpeech.OnInitListener{
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS){
textToSpeech.setLanguage(Locale.UK);
}
}
}
Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonDecimal, buttonClear, buttonDelete, buttonSign, buttonEquals, buttonAdd, buttonSubtract, buttonMultiply, buttonDivide;
TextView textViewMain, textViewHistory;
TextToSpeech textToSpeech;
float value1, value2;
boolean add, subtract, multiply, divide;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button0 = findViewById(R.id.button0);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
button4 = findViewById(R.id.button4);
button5 = findViewById(R.id.button5);
button6 = findViewById(R.id.button6);
button7 = findViewById(R.id.button7);
button8 = findViewById(R.id.button8);
button9 = findViewById(R.id.button9);
buttonDecimal = findViewById(R.id.buttonDecimal);
buttonClear = findViewById(R.id.buttonClear);
buttonDelete = findViewById(R.id.buttonDelete);
buttonSign = findViewById(R.id.buttonSign);
buttonAdd = findViewById(R.id.buttonAdd);
buttonSubtract = findViewById(R.id.buttonSubtract);
buttonMultiply = findViewById(R.id.buttonMultiply);
buttonDivide = findViewById(R.id.buttonDivide);
textViewMain = findViewById(R.id.textViewMain);
textViewHistory = findViewById(R.id.textViewHistory);
buttonEquals = findViewById(R.id.buttonEquals);
button0.setOnClickListener(new button0Event());
button1.setOnClickListener(new button1Event());
button2.setOnClickListener(new button2Event());
button3.setOnClickListener(new button3Event());
button4.setOnClickListener(new button4Event());
button5.setOnClickListener(new button5Event());
button6.setOnClickListener(new button6Event());
button7.setOnClickListener(new button7Event());
button8.setOnClickListener(new button8Event());
button9.setOnClickListener(new button9Event());
buttonDecimal.setOnClickListener(new buttonDecimalEvent());
buttonClear.setOnClickListener(new buttonClearEvent());
buttonDelete.setOnClickListener(new buttonDeleteEvent());
buttonSign.setOnClickListener(new buttonSignEvent());
buttonAdd.setOnClickListener(new buttonAddEvent());
buttonSubtract.setOnClickListener(new buttonSubtractEvent());
buttonMultiply.setOnClickListener(new buttonMultiplyEvent());
buttonDivide.setOnClickListener(new buttonDivideEvent());
buttonEquals.setOnClickListener(new buttonEqualsEvent());
textToSpeech = new TextToSpeech(getApplicationContext(), new txtEvent());
}
private class button0Event implements View.OnClickListener{
#Override
public void onClick(View v){
textViewMain.setText(textViewMain.getText() + "0");
textToSpeech.speak(textViewMain.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
}
private class button1Event implements View.OnClickListener{
#Override
public void onClick(View v){
textViewMain.setText(textViewMain.getText() + "1");
textToSpeech.speak(textViewMain.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
}
private class button2Event implements View.OnClickListener{
#Override
public void onClick(View v){
textViewMain.setText(textViewMain.getText() + "2");
textToSpeech.speak(textViewMain.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
}
private class button3Event implements View.OnClickListener{
#Override
public void onClick(View v){
textViewMain.setText(textViewMain.getText() + "3");
textToSpeech.speak(textViewMain.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
}
private class button4Event implements View.OnClickListener{
#Override
public void onClick(View v){
textViewMain.setText(textViewMain.getText() + "4");
textToSpeech.speak(textViewMain.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
}
private class button5Event implements View.OnClickListener{
#Override
public void onClick(View v){
textViewMain.setText(textViewMain.getText() + "5");
textToSpeech.speak(textViewMain.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
}
private class button6Event implements View.OnClickListener{
#Override
public void onClick(View v){
textViewMain.setText(textViewMain.getText() + "6");
textToSpeech.speak(textViewMain.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
}
private class button7Event implements View.OnClickListener{
#Override
public void onClick(View v){
textViewMain.setText(textViewMain.getText() + "7");
textToSpeech.speak(textViewMain.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
}
private class button8Event implements View.OnClickListener{
#Override
public void onClick(View v){
textViewMain.setText(textViewMain.getText() + "8");
textToSpeech.speak(textViewMain.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
}
private class button9Event implements View.OnClickListener{
#Override
public void onClick(View v){
textViewMain.setText(textViewMain.getText() + "9");
textToSpeech.speak(textViewMain.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
}
private class buttonDecimalEvent implements View.OnClickListener{
#Override
public void onClick(View v){
textViewMain.setText(textViewMain.getText() + ".");
textToSpeech.speak(textViewMain.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
}
private class buttonClearEvent implements View.OnClickListener{
#Override
public void onClick(View v){
value1 = 0;
value2 = 0;
textViewMain.setText("");
textViewHistory.setText("");
textToSpeech.speak("Clear", TextToSpeech.QUEUE_FLUSH, null);
}
}
private class buttonDeleteEvent implements View.OnClickListener{
#Override
public void onClick(View v){
String display = textViewMain.getText().toString();
if(!TextUtils.isEmpty(display)){
display = display.substring(0, display.length() -1);
}
textViewMain.setText(display);
}
}
private class buttonSignEvent implements View.OnClickListener{
#Override
public void onClick(View v){
textViewMain.setText("-" + textViewMain.getText());
textToSpeech.speak("Negative" + textViewMain.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
}
private class buttonAddEvent implements View.OnClickListener{
#Override
public void onClick(View v){
if(!textViewMain.getText().toString().matches("")){
value1 = Float.parseFloat(textViewMain.getText() + "");
add = true;
textViewHistory.setText(textViewMain.getText() + "+");
textViewMain.setText("");
textToSpeech.speak("Plus", TextToSpeech.QUEUE_FLUSH, null);
}
else{
Toast.makeText(MainActivity.this, "Please enter a number first", Toast.LENGTH_LONG).show();
}
}
}
private class buttonSubtractEvent implements View.OnClickListener{
#Override
public void onClick(View v){
if(!textViewMain.getText().toString().matches("")){
value1 = Float.parseFloat(textViewMain.getText() + "");
subtract = true;
textViewHistory.setText(textViewMain.getText() + "-");
textViewMain.setText("");
textToSpeech.speak("Minus", TextToSpeech.QUEUE_FLUSH, null);
}
else{
Toast.makeText(MainActivity.this, "Please enter a number first", Toast.LENGTH_LONG).show();
}
}
}
private class buttonMultiplyEvent implements View.OnClickListener{
#Override
public void onClick(View v){
if(!textViewMain.getText().toString().matches("")){
value1 = Float.parseFloat(textViewMain.getText() + "");
multiply = true;
textViewHistory.setText(textViewMain.getText() + "x");
textViewMain.setText("");
textToSpeech.speak("Times", TextToSpeech.QUEUE_FLUSH, null);
}
else{
Toast.makeText(MainActivity.this, "Please enter a number first", Toast.LENGTH_LONG).show();
}
}
}
private class buttonDivideEvent implements View.OnClickListener{
#Override
public void onClick(View v){
if(!textViewMain.getText().toString().matches("")){
value1 = Float.parseFloat(textViewMain.getText() + "");
divide = true;
textViewHistory.setText(textViewMain.getText() + "/");
textViewMain.setText("");
textToSpeech.speak("Divided by", TextToSpeech.QUEUE_FLUSH, null);
}
else{
Toast.makeText(MainActivity.this, "Please enter a number first", Toast.LENGTH_LONG).show();
}
}
}
private class buttonEqualsEvent implements View.OnClickListener{
#Override
public void onClick(View v){
value2 = Float.parseFloat(textViewMain.getText() + "");
textViewHistory.setText(textViewHistory.getText() + (value2 + ""));
if(add == true){
textViewMain.setText(value1 + value2 + "");
add = false;
}
if(subtract == true){
textViewMain.setText(value1 - value2 + "");
subtract = false;
}
if(multiply == true){
textViewMain.setText(value1 * value2 + "");
multiply = false;
}
if(divide == true){
textViewMain.setText(value1 / value2 + "");
divide = false;
}
textViewHistory.setText(textViewHistory.getText() + "=" + (textViewMain.getText() + ""));
textToSpeech.speak("Equals" + textViewMain.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
value1 = 0;
value2 = 0;
}
}
}
Here is the main xml
<?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.calculatorinator.MainActivity"
android:background="#color/Black">
<TextView
android:id="#+id/textViewHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="top|center"
android:padding="35dp"
android:textAlignment="viewEnd"
android:textSize="25sp" />
<TextView
android:id="#+id/textViewMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="71dp"
android:gravity="top|center"
android:padding="35dp"
android:textSize="35sp" />
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="665dp"
android:layout_below="#+id/textViewMain"
android:layout_marginTop="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="#+id/button7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/round_button_black"
android:text="7"
android:textColor="#FFFFFF"
android:textSize="20dp"
android:layout_margin="5dp"/>
<Button
android:id="#+id/button8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/round_button_black"
android:text="8"
android:textColor="#FFFFFF"
android:textSize="20dp"
android:layout_margin="5dp"/>
<Button
android:id="#+id/button9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/round_button_black"
android:text="9"
android:textColor="#FFFFFF"
android:textSize="20dp"
android:layout_margin="5dp"/>
<Button
android:id="#+id/buttonClear"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/round_button_clear"
android:shadowColor="#F7E43232"
android:text="C"
android:textSize="20dp"
android:layout_margin="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/button4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/round_button_black"
android:text="4"
android:textColor="#FFFFFF"
android:textSize="20dp"
android:layout_margin="5dp"/>
<Button
android:id="#+id/button5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/round_button_black"
android:text="5"
android:textColor="#FFFFFF"
android:textSize="20dp"
android:layout_margin="5dp"/>
<Button
android:id="#+id/button6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/round_button_black"
android:text="6"
android:textColor="#FFFFFF"
android:textSize="20dp"
android:layout_margin="5dp"/>
<Button
android:id="#+id/buttonDivide"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/round_button_orange"
android:text="÷"
android:textSize="20dp"
android:layout_margin="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/round_button_black"
android:text="1"
android:textColor="#FFFFFF"
android:textSize="20dp"
android:layout_margin="5dp"/>
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/round_button_black"
android:text="2"
android:textColor="#FFFFFF"
android:textSize="20dp"
android:layout_margin="5dp"/>
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/round_button_black"
android:text="3"
android:textColor="#FFFFFF"
android:textSize="20dp"
android:layout_margin="5dp"/>
<Button
android:id="#+id/buttonAdd"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/round_button_orange"
android:text="+"
android:textSize="20dp"
android:layout_margin="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/buttonDecimal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/round_button_orange"
android:text="."
android:textColor="#FFFFFF"
android:textSize="20dp"
android:layout_margin="5dp"/>
<Button
android:id="#+id/button0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/round_button_black"
android:text="0"
android:textColor="#FFFFFF"
android:textSize="20dp"
android:layout_margin="5dp"/>
<Button
android:id="#+id/buttonDelete"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/round_button_delete"
android:text="Del"
android:textSize="20dp"
android:layout_margin="5dp"/>
<Button
android:id="#+id/buttonSubtract"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/round_button_orange"
android:text="-"
android:textSize="20dp"
android:layout_margin="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/buttonSign"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/round_button_orange"
android:text="+/-"
android:textColor="#FFFFFF"
android:textSize="20dp"
android:layout_margin="5dp"/>
<Button
android:id="#+id/buttonEquals"
android:layout_width="108dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/round_button_orange"
android:text="="
android:textColor="#FFFFFF"
android:textSize="20dp"
android:layout_margin="5dp"/>
<Button
android:id="#+id/buttonMultiply"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/round_button_orange"
android:text="*"
android:textSize="20dp"
android:layout_margin="5dp"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Added xml files that changes the buttons to circle
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:shape="oval">
<solid android:color="#android:color/darker_gray"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:shape="oval">
<solid android:color="#android:color/holo_orange_dark"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:shape="oval">
<solid android:color="#android:color/holo_red_light"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:shape="oval">
<solid android:color="#android:color/holo_red_dark"/>
</shape>
I've been trying to find the problem for a while now any help is greatly appreciated thank you.
PS. How do I lessen the number of xml files but still have a variety of round buttons with different colors?
My calculator was not showing any numbers after pushing the corresponding buttons, I realized the I changed the background color to black while my textView font color was also black, for anyone who was trying to comprehend my question and trying their best to help me I am deeply sorry for wasting your time and thank you.
Related
In the below code, I've set the new Intent in the autocomplete_next TextView, but in my Mobile, when I run the app, and When I try to move to the next activity, It does not go the next Activity, Instead of that, it comes back again to the same previous Activity. In the Android Studio LOGCAT, I've seen the RenderScript: SETAFFINITY ret = -1 . I got no trace about this error on Google? Please Help me to fix my issue. Thank You in Advance...
activity_calculator.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CalculatorActivity"
android:layout_centerHorizontal="true">
<TableLayout
android:id="#+id/tableinput"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Operand 1 : "
android:textSize="20dp"
android:textAlignment="textStart"
/>
<EditText
android:id="#+id/oper1_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<TextView
android:id="#+id/autocomplete_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Operand 2 : "
android:textSize="20dp"
android:textAlignment="textStart"
android:layout_weight="0"/>
<EditText
android:id="#+id/oper2_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_weight="1"/>
</TableRow>
</TableLayout>
<GridLayout
android:id="#+id/calculator"
android:layout_below="#id/tableinput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<Button
android:id="#+id/add_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="0"
android:layout_column="0"
android:text="Add"/>
<Button
android:id="#+id/sub_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="0"
android:layout_column="1"
android:text="Sub"/>
<Button
android:id="#+id/min_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="0"
android:layout_column="2"
android:text="Min"/>
<Button
android:id="#+id/mul_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="1"
android:layout_column="0"
android:text="Multiply"/>
<Button
android:id="#+id/div_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="1"
android:layout_column="1"
android:text="Divide"/>
<Button
android:id="#+id/pow_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="2"
android:layout_column="1"
android:text="Power"/>
<Button
android:id="#+id/max_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="1"
android:layout_column="2"
android:text="Max"/>
<Button
android:id="#+id/rem_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="2"
android:layout_column="0"
android:text="Remainder"/>
<Button
android:id="#+id/clear_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="2"
android:layout_column="2"
android:text="clear"/>
</GridLayout>
<TableRow
android:layout_below="#id/calculator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<TextView
android:id="#+id/video_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ans: "
android:textSize="20dp"
android:textAlignment="textStart"
android:layout_weight="0"/>
<EditText
android:id="#+id/ans_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_weight="1"/>
</TableRow>
</RelativeLayout>
CalculatorActivity.java (Activity which calls another Activity)
package com.example.lab_excercise_2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.lang.Math;
public class CalculatorActivity extends AppCompatActivity {
private EditText oper1, oper2, ans;
private Button add_btn, sub_btn, div_btn, mul_btn, rem_btn, max_btn, min_btn, pow_btn, clear_btn;
private Integer oper1_text, oper2_text;
private TextView video_next,autocomplete_next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
add_btn = findViewById(R.id.add_btn);
sub_btn = findViewById(R.id.sub_btn);
mul_btn = findViewById(R.id.mul_btn);
div_btn = findViewById(R.id.div_btn);
rem_btn = findViewById(R.id.rem_btn);
min_btn = findViewById(R.id.min_btn);
max_btn = findViewById(R.id.max_btn);
pow_btn = findViewById(R.id.pow_btn);
clear_btn = findViewById(R.id.clear_btn);
oper1 = findViewById(R.id.oper1_edit_text);
oper2 = findViewById(R.id.oper2_edit_text);
ans = findViewById(R.id.ans_edit_text);
video_next = findViewById(R.id.video_textView);
autocomplete_next = findViewById(R.id.autocomplete_textView);
add_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText(String.valueOf(oper1_text+oper2_text));
}
});
sub_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText(String.valueOf(oper1_text-oper2_text));
}
});
mul_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText(String.valueOf(oper1_text*oper2_text));
}
});
div_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText(String.valueOf(oper1_text/oper2_text));
}
});
rem_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText(String.valueOf(oper1_text%oper2_text));
}
});
max_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText((oper1_text>oper2_text ? oper1_text: oper2_text).toString());
}
});
min_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText((oper1_text<oper2_text ? oper1_text: oper2_text).toString());
}
});
pow_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
Double pow = Math.pow(oper1_text,oper2_text);
ans.setText(pow.toString());
}
});
clear_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1.setText("");
oper2.setText("");
ans.setText("");
}
});
video_next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent videoIntent = new Intent(CalculatorActivity.this,VideoActivity.class);
startActivity(videoIntent);
}
});
autocomplete_next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent autocompleteIntent = new Intent(CalculatorActivity.this,AutocompleteActivity.class);
startActivity(autocompleteIntent);
}
});
}
}
activity_autocomplete.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AutocompleteActivity">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="30sp"
android:textAlignment="center"
android:layout_width="wrap_content"
android:text="Date-Time Picker Autocomplete"
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:layout_margin="20dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/setTime_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:text="Set Time"
android:layout_weight="1"
android:layout_margin="20dp"/>
<TextView
android:id="#+id/time_text"
android:textSize="20sp"
android:textAlignment="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_weight="3"/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/setDate_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:text="Set Date"
android:layout_margin="20dp"
android:layout_weight="1" />
<TextView
android:id="#+id/date_text"
android:textSize="20sp"
android:textAlignment="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_weight="3"/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/label_text"
android:textSize="20sp"
android:textAlignment="center"
android:layout_width="wrap_content"
android:text="Label "
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:layout_margin="20dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<MultiAutoCompleteTextView
android:id="#+id/label_edit"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_weight="3" />
</TableRow>
<Button
android:id="#+id/setalarm_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="20dp"
android:text="Set Time" />
</TableLayout>
</RelativeLayout>
AutocompleteActivity.java (Activity which was called by the above Activity)
package com.example.lab_excercise_2;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.MultiAutoCompleteTextView;
import android.widget.TextView;
import android.widget.TimePicker;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class AutocompleteActivity extends AppCompatActivity {
private Button setTime, setDate, setalarm;
private TextView time_text, date_text, label_text;
private Calendar calendar = Calendar.getInstance();
private String date_string, time_string, label_string;
private String[] labels = {"Wake Up","Time to School","Time to College","Time to Work","Complete Activity","Finish Project"};
private MultiAutoCompleteTextView label_autocomplete;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_autocomplete);
setTime = findViewById(R.id.setTime_btn);
setDate = findViewById(R.id.setDate_btn);
setalarm = findViewById(R.id.setalarm_btn);
time_text = findViewById(R.id.time_text);
date_text = findViewById(R.id.date_text);
label_autocomplete = findViewById(R.id.label_edit);
label_text = findViewById(R.id.label_textView);
setDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDateDialog(setDate);
}
});
setTime.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showTimeDialog(setTime);
}
});
setalarm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextpage(v);
}
});
label_autocomplete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent alarmIntent = new Intent(AutocompleteActivity.this,AlarmActivity.class);
startActivity(alarmIntent);
}
});
label_text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent alarmIntent = new Intent(AutocompleteActivity.this,AlarmActivity.class);
startActivity(alarmIntent);
}
});
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,labels);
label_autocomplete.setAdapter(adapter);
label_autocomplete.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
public void showTimeDialog(final Button time){
TimePickerDialog.OnTimeSetListener timeSetListener = new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendar.set(Calendar.MINUTE,minute);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
time_text.setText(simpleDateFormat.format(calendar.getTime()));
}
};
new TimePickerDialog(AutocompleteActivity.this,timeSetListener,calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),false).show();
}
public void showDateDialog(final Button date){
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-MM-dd");
date_text.setText(simpleDateFormat.format(calendar.getTime()));
}
};
new DatePickerDialog(AutocompleteActivity.this, dateSetListener,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}
public void nextpage(View view){
date_string = date_text.getText().toString();
time_string = time_text.getText().toString();
label_string = label_autocomplete.getText().toString();
Intent nextIntent = new Intent(this,Autocomplete2Activity.class);
nextIntent.putExtra("date",date_string);
nextIntent.putExtra("time",time_string);
nextIntent.putExtra("label",label_string);
startActivity(nextIntent);
}
}
LOGCAT
2020-03-02 19:55:10.865 4078-4136/com.example.lab_excercise_2E/RenderScript: SETAFFINITY ret = -1
2020-03-02 19:55:10.865 4078-4137/com.example.lab_excercise_2 E/RenderScript: SETAFFINITY ret = -1
2020-03-02 19:55:10.865 4078-4139/com.example.lab_excercise_2 E/RenderScript: SETAFFINITY ret = -1
2020-03-02 19:55:10.865 4078-4138/com.example.lab_excercise_2 E/RenderScript: SETAFFINITY ret = -1
2020-03-02 19:55:10.865 4078-4140/com.example.lab_excercise_2 E/RenderScript: SETAFFINITY ret = -1
2020-03-02 19:55:10.865 4078-4142/com.example.lab_excercise_2 E/RenderScript: SETAFFINITY ret = -1
2020-03-02 19:55:10.865 4078-4141/com.example.lab_excercise_2 E/RenderScript: SETAFFINITY ret = -1`
I'm creating a simple calculator in Android Studio but the equal button didn't work. When I clicked the equal button, the app crash but it didn't show any error message. Other buttons work fine, I couldn't figure out why my equal button didn't work.
This is my MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private EditText showNum;
private Button one;
private Button two;
private Button three;
private Button four;
private Button five;
private Button six;
private Button seven;
private Button eight;
private Button nine;
private Button zero;
private Button divide;
private Button multiple;
private Button minus;
private Button equal;
private Button dot;
private Button plus;
private Button clear;
private Boolean addButton, multipleButton, minusButton, divideButton;
Float value, currentValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showNum=(EditText) findViewById(R.id.editView);
one = (Button)findViewById(R.id.button1);
two = (Button)findViewById(R.id.button2);
three = (Button)findViewById(R.id.button3);
four = (Button)findViewById(R.id.button4);
five = (Button)findViewById(R.id.button5);
six = (Button)findViewById(R.id.button6);
seven = (Button)findViewById(R.id.button7);
eight = (Button)findViewById(R.id.button8);
nine = (Button)findViewById(R.id.button9);
zero = (Button)findViewById(R.id.button0);
divide = (Button)findViewById(R.id.buttonDivide);
multiple = (Button)findViewById(R.id.buttonMultiple);
minus = (Button)findViewById(R.id.buttonMinus);
equal = (Button)findViewById(R.id.buttonEqual);
dot = (Button)findViewById(R.id.buttonDot);
plus = (Button)findViewById(R.id.buttonPlus);
clear = (Button)findViewById(R.id.buttonC);
clear.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText("");
}
});
zero.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"0");
}
});
one.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"1");
}
});
two.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"2");
}
});
three.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"3");
}
});
four.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"4");
}
});
five.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"5");
}
});
six.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"6");
}
});
seven.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"7");
}
});
eight.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"8");
}
});
nine.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"9");
}
});
dot.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+".");
}
});
divide.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
value = Float.parseFloat(showNum.getText()+"");
divideButton=true;
showNum.setText(null);
}
});
multiple.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if(showNum==null){
showNum.setText("");
}
else{
value = Float.parseFloat(showNum.getText()+"");
multipleButton=true;
showNum.setText(null);
}
}
});
plus.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
value = Float.parseFloat(showNum.getText()+"");
addButton=true;
showNum.setText(null);
}
});
minus.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
value = Float.parseFloat(showNum.getText()+"");
minusButton=true;
showNum.setText(null);
}
});
equal.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
currentValue = Float.parseFloat(showNum.getText()+"");
if (divideButton) {
showNum.setText(value / currentValue + "");
divideButton = false;
Log.i("gina", "b"+divideButton);
}
if (multipleButton){
showNum.setText(value * currentValue + "");
multipleButton = false;
}
if (addButton){
showNum.setText(value+currentValue + "");
addButton = false;
}
if (minusButton){
showNum.setText(value-currentValue + "");
minusButton = false;
}
}
});
}
}
Below is my activity_main.xml
<?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.iu.hsiaoyuanwang.proficiency2.MainActivity">
<EditText
android:id="#+id/editView"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:padding="35dp"
android:textSize="30sp"
android:gravity="top|center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/editView"
android:orientation="vertical"
android:id="#+id/linear1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="#+id/button7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="7"
android:textSize="20dp" />
<Button
android:id="#+id/button8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="8"
android:textSize="20dp"
/>
<Button
android:id="#+id/button9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="20dp"
android:text="9" />
<Button
android:id="#+id/buttonDivide"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="÷"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="#+id/button4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"
android:textSize="20dp" />
<Button
android:id="#+id/button5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="5"
android:textSize="20dp"
/>
<Button
android:id="#+id/button6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="20dp"
android:text="6" />
<Button
android:id="#+id/buttonMultiple"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="x"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"
android:textSize="20dp" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"
android:textSize="20dp"
/>
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="20dp"
android:text="3" />
<Button
android:id="#+id/buttonMinus"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="-"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="#+id/button0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="0"
android:textSize="20dp" />
<Button
android:id="#+id/buttonDot"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="."
android:textSize="20dp"
/>
<Button
android:id="#+id/buttonEqual"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="20dp"
android:text="=" />
<Button
android:id="#+id/buttonPlus"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="+"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="#+id/buttonC"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="C"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
As Kuti suggested, its better to keep only one OnClickListener, so keep his advice in mind.
A Boolean is an object, is not true or false, so the sentence if(addButton) when addButton is a Boolean is always false. Try using addButton.booleanValue() or use a boolean instead of a Boolean
I need to build a Passcode screen which should get 4 numeric characters and I have 4 input image view inside the grid layout above the keypad while i am giving input the circle should fill color to indicate that the input is taken.
Like this it should fill the color:
Passcode_Screen.Java
public class Passcode_Screen extends Activity implements View.OnClickListener{
String userEntered;
String userPin = "1234";
final int PIN_LENGTH = 4;
boolean keyPadLockedFlag = false;
Context appContext;
TextView titleView;
TextView pinBox0;
TextView pinBox1;
TextView pinBox2;
TextView pinBox3;
TextView pinBox4;
TextView statusView;
Button button0;
Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Button button6;
Button button7;
Button button8;
Button button9;
Button button10;
Button buttonExit;
Button buttonDelete;
EditText passwordInput;
ImageView backSpace;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
appContext = this;
userEntered = "";
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// set the view content
setContentView(R.layout.activity_passcode__screen);
//Typeface xpressive=Typeface.createFromAsset(getAssets(), "fonts/XpressiveBold.ttf");
statusView = (TextView) findViewById(R.id.statusview);
passwordInput = (EditText) findViewById(R.id.Result);
backSpace = (ImageView) findViewById(R.id.imageView);
buttonExit = (Button) findViewById(R.id.buttonexit);
backSpace.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
passwordInput.setText(passwordInput.getText().toString().substring(0, passwordInput.getText().toString().length() - 2));
}
});
buttonExit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// //Exit app
// Intent i = new Intent();
// i.setAction(Intent.ACTION_MAIN);
// i.addCategory(Intent.CATEGORY_HOME);
// appContext.startActivity(i);
// finish();
Intent camera = new Intent(Passcode_Screen.this, MainActivity.class);
startActivity(camera);
}
}
);
//buttonExit.setTypeface(xpressive);
buttonDelete = (Button) findViewById(R.id.buttondelete);
buttonDelete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (keyPadLockedFlag == true) {
return;
}
if (userEntered.length() > 0) {
userEntered = userEntered.substring(0, userEntered.length() - 1);
passwordInput.setText("");
}
}
}
);
titleView = (TextView) findViewById(R.id.time);
//titleView.setTypeface(xpressive);
View.OnClickListener pinButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
if (keyPadLockedFlag == true) {
return;
}
Button pressedButton = (Button) v;
if (userEntered.length() < PIN_LENGTH) {
userEntered = userEntered + pressedButton.getText();
Log.v("PinView", "User entered=" + userEntered);
//Update pin boxes
passwordInput.setText(passwordInput.getText().toString() + "*");
passwordInput.setSelection(passwordInput.getText().toString().length());
if (userEntered.length() == PIN_LENGTH) {
//Check if entered PIN is correct
if (userEntered.equals(userPin))
{
Intent login = new Intent(Passcode_Screen.this,Site_screen.class);
startActivity(login);
}
else
{
Toast.makeText(getApplicationContext(), "PIN Invalid",Toast.LENGTH_SHORT).show();
}
}
} else {
//Roll over
passwordInput.setText("");
userEntered = "";
// statusView.setText("");
userEntered = userEntered + pressedButton.getText();
Log.v("PinView", "User entered=" + userEntered);
//Update pin boxes
passwordInput.setText("8");
}
}
};
button0 = (Button) findViewById(R.id.button0);
//button0.setTypeface(xpressive);
button0.setOnClickListener(pinButtonHandler);
button1 = (Button) findViewById(R.id.button1);
//button1.setTypeface(xpressive);
button1.setOnClickListener(pinButtonHandler);
button2 = (Button) findViewById(R.id.button2);
//button2.setTypeface(xpressive);
button2.setOnClickListener(pinButtonHandler);
button3 = (Button) findViewById(R.id.button3);
//button3.setTypeface(xpressive);
button3.setOnClickListener(pinButtonHandler);
button4 = (Button) findViewById(R.id.button4);
//button4.setTypeface(xpressive);
button4.setOnClickListener(pinButtonHandler);
button5 = (Button) findViewById(R.id.button5);
//button5.setTypeface(xpressive);
button5.setOnClickListener(pinButtonHandler);
button6 = (Button) findViewById(R.id.button6);
//button6.setTypeface(xpressive);
button6.setOnClickListener(pinButtonHandler);
button7 = (Button) findViewById(R.id.button7);
//button7.setTypeface(xpressive);
button7.setOnClickListener(pinButtonHandler);
button8 = (Button) findViewById(R.id.button8);
//button8.setTypeface(xpressive);
button8.setOnClickListener(pinButtonHandler);
button9 = (Button) findViewById(R.id.button9);
//button9.setTypeface(xpressive);
button9.setOnClickListener(pinButtonHandler);
buttonDelete = (Button) findViewById(R.id.buttondelete);
//buttonDelete.setTypeface(xpressive);
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
//App not allowed to go back to Parent activity until correct pin entered.
return;
//super.onBackPressed();
}
#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_pin_entry_view, menu);
return true;
}
#Override
public void onClick(View view) {
}
private class LockKeyPadOperation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
for (int i = 0; i < 2; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "Executed";
}
#Override
protected void onPostExecute(String result) {
statusView.setText("");
//Roll over
passwordInput.setText("");
;
userEntered = "";
keyPadLockedFlag = false;
}
#Override
protected void onPreExecute() {
}
#Override
protected void onProgressUpdate(Void... values) {
}
}
}
activity_passcode_screen:
<?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"
android:layout_centerInParent="true"
>
<View
android:layout_width="200dp"
android:layout_height="1dp"
android:background="#FFF"
android:layout_above="#+id/numericPad"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:id="#+id/view" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_above="#+id/view"
android:layout_marginBottom="10dp"
android:id="#+id/imageView" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="1"
android:rowCount="4"
android:id="#+id/glResult"
android:layout_below="#+id/statusview"
android:orientation="horizontal"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"
>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:id="#+id/imvPin1"
android:background="#drawable/round"
android:layout_marginRight="10dp"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginBottom="10dp"
android:id="#+id/imvPin2"
android:background="#drawable/round"
android:orientation="horizontal"
android:layout_marginRight="10dp"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginBottom="10dp"
android:id="#+id/imvPin3"
android:background="#drawable/round"
android:orientation="horizontal"
android:layout_marginRight="10dp"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginBottom="10dp"
android:id="#+id/imvPin4"
android:background="#drawable/round"
android:orientation="horizontal"
android:layout_marginRight="10dp"
/>
</LinearLayout>
<GridLayout
android:id="#id/numericPad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:columnCount="3"
android:rowCount="4"
android:orientation="horizontal"
android:layout_centerVertical="true"
android:layout_below="#+id/glResult"
android:layout_marginTop="20dp"
android:layout_centerInParent="true">
<Button android:text="1"
android:layout_row="0"
android:layout_column="0"
android:id="#+id/button1"
android:background="#drawable/round"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:layout_width="48dp"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
/>
<Button android:text="2"
android:layout_row="0"
android:layout_column="1"
android:id="#+id/button2"
android:background="#drawable/round"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:layout_width="48dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"/>
<Button android:text="3"
android:layout_row="0"
android:layout_column="2"
android:id="#+id/button3"
android:background="#drawable/round"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:layout_width="48dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
/>
<Button android:text="4"
android:layout_row="1"
android:layout_column="0"
android:id="#+id/button4"
android:background="#drawable/round"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:layout_width="48dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"/>
<Button android:text="5"
android:layout_row="1"
android:layout_column="1"
android:id="#+id/button5"
android:background="#drawable/round"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:layout_width="48dp" android:layout_marginRight="15dp"
android:layout_marginBottom="15dp" />
<Button android:text="6"
android:layout_row="1"
android:layout_column="2"
android:id="#+id/button6"
android:background="#drawable/round"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:layout_width="48dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
/>
<Button android:text="7"
android:layout_row="2"
android:layout_column="0"
android:id="#+id/button7"
android:background="#drawable/round"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:layout_width="48dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"/>
<Button android:text="8"
android:layout_row="2"
android:layout_column="1"
android:id="#+id/button8"
android:background="#drawable/round"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:layout_width="48dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"/>
<Button android:text="9"
android:layout_row="2"
android:layout_column="2"
android:id="#+id/button9"
android:background="#drawable/round"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:layout_width="48dp"/>
<Button
android:text="0"
android:layout_row="3"
android:layout_column="1"
android:id="#+id/button0"
android:background="#drawable/round"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:layout_width="48dp"
/>
</GridLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Enter Password"
android:layout_centerInParent="true"
android:textSize="20dp"
android:textStyle="bold"
android:id="#+id/statusview"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:gravity="center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="#+id/buttonexit"
android:background="#00000000"
android:layout_alignBottom="#+id/buttondelete" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:id="#+id/buttondelete"
android:background="#00000000"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Here instead of Edit text (passcodeInput) where i am storing the input value i have removed that and i need to add image view field and to fill the circles with color when a button is pressed, should fill from circle 1 to 4 linearly and when the 4th input is filling it should validate the entire input to the default given input if it matches it should move to the next activity else should show error message.
I need like this image :
https://play.google.com/store/apps/details?id=com.smart.mobile.lin.pin.locker
I don't have much knowledge in Android so please help me to figure out this. Please share any tutorial links or sample code for the above question. Thanks in advance.
I am trying to generate Multiplication Table of any number that is input by user. I have developed the interface for the application but cannot understand where to start with the logical part(coding). What i want is when a user inputs a number into the EditText then in the TextView (id: printArea) should show the table of the input number in the format as given in image 2. [Just to show you example i used TextView in the printArea part and i do not know what to use instead]
<?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:orientation="vertical"
tools:context=".MainActivity"
android:weightSum="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Please Enter a Number: "
android:textSize="20sp" />
<EditText
android:id="#+id/num"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginLeft="30dp"
android:textSize="20sp" />
</LinearLayout>
<Button
android:id="#+id/calculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/log"
android:onClick="submitNumber"
android:text="Get Table" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:id="#+id/printArea" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/clear"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="1dp"
android:text="Clear" />
<Button
android:id="#+id/credits"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:text="Credits" />
<Button
android:id="#+id/exit"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_marginRight="2dp"
android:text="Exit" />
</LinearLayout>
</LinearLayout>
and the MainActivity.java is:
package com.example.tara.multiplicationtable;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText num;
Button credits;
Button calculate;
Button clear;
Button exit;
TextView printArea;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num = new EditText(this);
clear = new Button(this);
calculate = new Button(this);
credits = new Button(this);
calculate = new Button(this);
printArea = new TextView(this);
num = (EditText) findViewById(R.id.num);
credits = (Button) findViewById(R.id.credits);
clear = (Button) findViewById(R.id.clear);
exit = (Button) findViewById(R.id.exit);
printArea = (TextView) findViewById(R.id.printArea);
calculate = (Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//change the integer value into string
int num1 = Integer.parseInt(num.getText().toString());
// Perform action on click
for (int i = 1; i <= 10; i++) {
printArea.setText(num1 + "X" + i + "=" + i * num1);
}
}
});
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num.setText("");
}
});
credits.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, Credits.class);
startActivity(i);
}
});
exit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
System.exit(0);
}
});
}
}
when application is launched it should show like thisThe initial state of application
and i want to make the application show the table like this if user input is 2 The Final Result
Make a Listview and then in its adapter add your table data.
Make adapter's layout like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="=" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />
Here are the codes that i used to build the Multiplication Table that i imagined to build. The code for activity_main.xml file will be:
<?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:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginStart="2dp"
android:text="#string/Enter_number"
android:textSize="16sp" />
<EditText
android:id="#+id/num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="2dp"
android:layout_marginStart="1dp"
android:digits="0123456789"
android:hint="#string/hidden_text"
android:inputType="number"
android:maxLength="3"
android:textSize="16sp" />
</LinearLayout>
<Button
android:id="#+id/calculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/calculate"
android:imeOptions="actionDone"
android:textSize="16sp"/>
<!--android:onClick="submitNumber"-->
<TextView
android:id="#+id/printArea"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="20sp"
android:background="#drawable/backimage" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginStart="2dp"
android:textSize="16sp"
android:text="#string/clear"
/>
<Button
android:id="#+id/credits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginStart="1dp"
android:textSize="16sp"
android:text="#string/credits" />
<Button
android:id="#+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="2dp"
android:layout_marginStart="1dp"
android:layout_weight="1"
android:textSize="16sp"
android:text="#string/exit" />
</LinearLayout>
</LinearLayout>
The Code for the MainActivity.java file will be:
package com.example.tara.multiplicationtable;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText num;
Button credits;
Button calculate;
Button clear;
Button exit;
TextView printArea;
//private static final String result = MainActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num = new EditText(this);
clear = new Button(this);
calculate = new Button(this);
credits = new Button(this);
calculate = new Button(this);
printArea = new TextView(this);
num = (EditText) findViewById(R.id.num);
credits = (Button) findViewById(R.id.credits);
clear = (Button) findViewById(R.id.clear);
exit = (Button) findViewById(R.id.exit);
calculate = (Button) findViewById(R.id.calculate);
printArea = (TextView) findViewById(R.id.printArea);
calculate.setOnClickListener(new View.OnClickListener() {
// Perform action on Get Table Button click
public void onClick(View v) {
if (num.getText().length() == 0 ){
printArea.setText(R.string.err_msg);
}
else {
//change the integer value into string
printArea.setText("");
int num1 = Integer.parseInt(num.getText().toString());
String result;
for (int i = 1; i <= 10; i++) {
result = (num1 + " X " + i + " = " + i * num1);
printArea.append("\n" + result);
}
}
}
});
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num.setText("");
printArea.setText("");
}
});
credits.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Credits.class));
}
});
exit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Good Bye, User!", Toast.LENGTH_SHORT).show();
finish();
}
});
}
}
Thank you everyone for the guidance.
Hello every one i am new in android and i want to customize android key board in the following manner. I want to three buttons previous , next & done above the default keyboard of android. Can we customize default keyboard of android in such manner?
thanks in advance
my pic:
well after so much of googling and finding various question on stackoverflow i get what i am looking for and i am posting all the thing which i had created .
my activity class
package com.example.demoappkeyboard;
import org.w3c.dom.Text;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.InputType;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText ed1,ed2,ed3;
EditText edit3,edit4,edit5;
// private Button sub1 , sub2 , sub3;
private EditText[] bed = {ed1,ed2,ed3};
private static int i = 1,j = 3 , k = 0 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Dialog alertDialog = new Dialog(MainActivity.this);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
ed3 = (EditText) findViewById(R.id.editText3);
edit3 = (EditText) findViewById(R.id.edit3);
ed1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
initiatePopupWindow("");
}
});
ed2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
initiatePopupWindow("");
}
});
ed3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
initiatePopupWindow("");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
protected void initiatePopupWindow(String value) {
final String data ="";
final Button sub1,sub2, sub3;
final EditText edit3;
final EditText ed1,ed2,ed3;
final RelativeLayout bottom1;
final Dialog alertDialog = new Dialog(MainActivity.this);
final LinearLayout mainlayout;
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
alertDialog.setContentView(R.layout.screen_popup);
edit3 = (EditText) alertDialog.findViewById(R.id.edit3);
mainlayout = (LinearLayout)alertDialog.findViewById(R.id.popup_element);
sub1 = (Button) alertDialog.findViewById(R.id.submit1);
sub2 = (Button) alertDialog.findViewById(R.id.submit2);
sub3 = (Button) alertDialog.findViewById(R.id.submit3);
bottom1 = (RelativeLayout) alertDialog.findViewById(R.id.linearLayout3);
Button cancel = (Button) alertDialog.findViewById(R.id.btncancel);
Button submitbtn = (Button) alertDialog.findViewById(R.id.btnsubmit);
ed1=(EditText)findViewById(R.id.editText1);
ed2=(EditText)findViewById(R.id.editText2);
ed3=(EditText)findViewById(R.id.editText3);
if(ed1.hasFocus())
{
String s1 = ed1.getText().toString();
edit3.setText(s1);
edit3.setInputType(InputType.TYPE_CLASS_TEXT);
sub1.setEnabled(true);
}
if(ed2.hasFocus())
{
String s1 = ed2.getText().toString();
edit3.setText(s1);
edit3.setInputType(InputType.TYPE_CLASS_NUMBER);
}
if(ed3.hasFocus())
{
String s1 = ed3.getText().toString();
edit3.setText(s1);
edit3.setInputType(InputType.TYPE_CLASS_TEXT);
}
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
if (value.equals("")) {
edit3.setHint("Please enter Username");
} else {
edit3.setText(value);
int textLength = edit3.getText().length();
edit3.setSelection(textLength, textLength);
}
alertDialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
alertDialog.show();
//edittext click event
edit3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
bottom1.setVisibility(View.VISIBLE);
sub1.setVisibility(View.VISIBLE);
sub2.setVisibility(View.VISIBLE);
sub3.setVisibility(View.VISIBLE);
}
});
//button click event
submitbtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(ed1.hasFocus())
{
String data1 = edit3.getText().toString();
ed1.setText(data1);
edit3.setText(data1);
}
if(ed2.hasFocus())
{
String data1 = edit3.getText().toString();
ed2.setText(data1);
edit3.setText(data1);
}
if(ed3.hasFocus())
{
String data1 = edit3.getText().toString();
ed3.setText(data1);
edit3.setText(data1);
}
final String value = edit3.getText().toString().toUpperCase();
// usern.setText(value);
alertDialog.cancel();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.cancel();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
});
sub1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(i==1)
{
ed1.requestFocus();
String s1 = ed1.getText().toString();
edit3.setText(s1);
edit3.setInputType(InputType.TYPE_CLASS_TEXT);
sub1.setEnabled(true);
sub2.setEnabled(false);
//j=3;
}
i++;
if(i==2)
{
ed2.requestFocus();
String s1 = ed2.getText().toString();
edit3.setText(s1);
edit3.setInputType(InputType.TYPE_CLASS_NUMBER);
i=2;
sub1.setEnabled(true);
sub2.setEnabled(true);
}
if(i==3)
{
ed3.requestFocus();
String s1 = ed3.getText().toString();
edit3.setText(s1);
edit3.setInputType(InputType.TYPE_CLASS_TEXT);
//i=0;
sub1.setEnabled(false);
sub2.setEnabled(true);
i=0;
}
}
});
sub2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
j--;
if(j==1)
{
ed1.requestFocus();
sub2.setEnabled(false);
sub1.setEnabled(true);
String s1 = ed1.getText().toString();
edit3.setText(s1);
edit3.setInputType(InputType.TYPE_CLASS_TEXT);
j=4;
}
if(j==2)
{
sub2.setEnabled(true);
sub1.setEnabled(true);
ed2.requestFocus();
String s1 = ed2.getText().toString();
edit3.setText(s1);
edit3.setInputType(InputType.TYPE_CLASS_NUMBER);
}
if(j==3)
{
sub2.setEnabled(true);
sub1.setEnabled(false);
ed3.requestFocus();
String s1 = ed3.getText().toString();
edit3.setText(s1);
edit3.setInputType(InputType.TYPE_CLASS_TEXT);
}
}
});
sub3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mainlayout.getWindowToken(), 0);
bottom1.setVisibility(View.INVISIBLE);
}
});
}
}
my main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/linearLayout1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:background="#ffffff">
<RelativeLayout
android:layout_width="fill_parent" android:layout_height="50dp">
</RelativeLayout>
<ScrollView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
<LinearLayout android:id="#+id/linearLayout1"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:background="#ffffff"
>
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:text="Name"
android:maxLength="10"
android:inputType="text"
>
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:text="Phone"
android:maxLength="10"
android:inputType="number"
/>
<EditText
android:id="#+id/editText3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="14dp"
android:imeOptions="actionNext"
android:text="Email"
android:maxLength="10"
android:inputType="text"
>
<requestFocus />
</EditText>
</LinearLayout>
</ScrollView>
<!--for bottom bar -->
<RelativeLayout android:layout_height="50dp"
android:gravity="center" android:layout_width="match_parent"
android:id="#+id/linearLayout2" android:background="#ffffff"
>
</RelativeLayout>
</LinearLayout>
my screen_popup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:layout_weight=".2"
android:gravity="bottom|center_horizontal"
android:orientation="vertical"
>
<EditText
android:id="#+id/edit3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:maxLength="10"
android:singleLine="true"
android:inputType="text"
>
<requestFocus />
</EditText>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:maxLength="10"
android:singleLine="true"
android:inputType="text"
android:visibility="invisible"
>
</EditText>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:maxLength="10"
android:singleLine="true"
android:inputType="text"
android:visibility="invisible"
>
</EditText>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight=".8"
android:orientation="horizontal"
android:weightSum="2"
android:layout_marginTop="10dp"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:gravity="center" >
<Button
android:id="#+id/btnsubmit"
android:layout_width="137dp"
android:layout_height="40dp"
android:text="Submit"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:gravity="center" >
<Button
android:id="#+id/btncancel"
android:layout_width="137dp"
android:layout_height="40dp"
android:text="Cancel"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
<RelativeLayout android:layout_height="50dp"
android:gravity="bottom" android:layout_width="match_parent"
android:id="#+id/linearLayout3" android:background="#ffffff"
android:visibility="invisible"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:orientation="horizontal"
>
<Button
android:id="#+id/submit1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Next"
android:visibility="invisible"
android:layout_weight=".3"
android:layout_gravity="left"
>
</Button>
<Button
android:id="#+id/submit2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Previous"
android:visibility="invisible"
android:layout_weight=".3"
android:layout_gravity="center"
>
</Button>
<Button
android:id="#+id/submit3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Done"
android:visibility="invisible"
android:layout_weight=".3"
android:layout_gravity="center"
>
</Button>
</LinearLayout>
</RelativeLayout>
</LinearLayout>