Skip to content

Commit

Permalink
Ahora la interfaz de usuario se adapta mejor a los dispositivos. Se r…
Browse files Browse the repository at this point in the history
…efactoriza el codigo.

Signed-off-by: Frank Montalvo Ochoa <fmontalvoo@ups.edu.ec>
  • Loading branch information
fmontalvoo committed Mar 22, 2021
1 parent 938f74c commit b134042
Show file tree
Hide file tree
Showing 7 changed files with 766 additions and 651 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ android {

defaultConfig {
applicationId "com.fmontalvoo.converter"
minSdkVersion 17
minSdkVersion 16
targetSdkVersion 30
versionCode 3
versionName "1.3"
versionCode 4
versionName "1.4"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
30 changes: 9 additions & 21 deletions app/src/main/java/com/fmontalvoo/converter/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.SpannableStringBuilder;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.View;
Expand All @@ -21,15 +22,13 @@

import com.fmontalvoo.converter.controller.ConverterController;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public class MainActivity extends AppCompatActivity {

private EditText txtNumber;
private Spinner spnBase;
private Spinner spnGoal;
private TextView txtResult;
private Button[] numbers;
private Button btnClear;
private Button btnDelete;

private ConverterController converterController;

Expand All @@ -49,16 +48,15 @@ protected void onCreate(Bundle savedInstanceState) {
spnBase = findViewById(R.id.spnBase);
String[] spnBaseValues = {getString(R.string.binary), getString(R.string.octal),
getString(R.string.decimal), getString(R.string.hexadecimal)};
spnBase.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, spnBaseValues));
spnBase.setAdapter(new ArrayAdapter(this, R.layout.spinner_item, spnBaseValues));
spnBase = findViewById(R.id.spnBase);
spnBase.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int a = optionA, b = -1;
String txt = txtNumber.getText().toString();
optionA = spnBase.getSelectedItemPosition();
view.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
((TextView) view).setGravity(Gravity.CENTER);

switch (optionA) {
case 0:
binaryConfig();
Expand All @@ -78,7 +76,7 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
break;
}
txtNumber.setText("");
txtNumber.append(converterController.convert(format(txt), options(a, b)));
txtNumber.append(format(converterController.convert(txt, options(a, b))));
}

@Override
Expand All @@ -90,12 +88,10 @@ public void onNothingSelected(AdapterView<?> parent) {
spnGoal = findViewById(R.id.spnGoal);
String[] spnGoalValues = {getString(R.string.hexadecimal), getString(R.string.decimal),
getString(R.string.octal), getString(R.string.binary)};
spnGoal.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, spnGoalValues));
spnGoal.setAdapter(new ArrayAdapter<>(this, R.layout.spinner_item, spnGoalValues));
spnGoal.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
view.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
((TextView) view).setGravity(Gravity.CENTER);
optionB = spnGoal.getSelectedItemPosition();
convert();
}
Expand Down Expand Up @@ -134,18 +130,10 @@ public void onNothingSelected(AdapterView<?> parent) {
numbers[14] = findViewById(R.id.btnE);
numbers[15] = findViewById(R.id.btnF);

for (Button number : numbers) number.setOnClickListener(this);

btnClear = findViewById(R.id.btnClear);
btnClear.setOnClickListener(this);

btnDelete = findViewById(R.id.btnDelete);
btnDelete.setOnClickListener(this);

}

@Override
public void onClick(View v) {

public void onPressed(View v) {
int len = txtNumber.length();
int id = v.getId();
if (id == R.id.btnZero) {
Expand Down Expand Up @@ -185,7 +173,7 @@ public void onClick(View v) {
txtResult.setText("");
} else if (id == R.id.btnDelete) {
if (len != 0) {
String txt = txtNumber.getText().toString().substring(0, len - 1);
String txt = txtNumber.getText().toString().substring(0, len - 1).trim();
txtNumber.setText("");
txtNumber.append(txt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ConverterController {

public String convert(String number, int option) {
if (number == null || number.isEmpty()) return "";
number = number.replace(" ","");
number = number.replace(" ", "");
switch (option) {
case 1:
return binaryToHexadecimal(number);
Expand Down Expand Up @@ -163,24 +163,24 @@ private String hexadecimalToDecimal(String hexadecimal) {
BigInteger decimal = BigInteger.ZERO;
final String characters = "ABCDEF";
BigInteger len = new BigInteger(String.valueOf(hexadecimal.length()));
BigInteger[] oremun = new BigInteger[len.intValue()];
BigInteger[] number = new BigInteger[len.intValue()];

for (BigInteger i = BigInteger.ZERO; i.compareTo(len) < 0; i = i.add(BigInteger.ONE)) {
for (BigInteger j = BigInteger.ZERO; j.compareTo(BigInteger.valueOf(characters.length())) < 0; j = j
.add(BigInteger.ONE)) {
char c = hexadecimal.charAt(i.intValue());
if (c == characters.charAt(j.intValue())) {
oremun[i.intValue()] = numbers(c);
number[i.intValue()] = numbers(c);
} else if (c < 65) {
oremun[i.intValue()] = new BigInteger("" + hexadecimal.charAt(i.intValue()));
number[i.intValue()] = new BigInteger("" + hexadecimal.charAt(i.intValue()));
}
}

}

for (BigInteger i = len.subtract(BigInteger.ONE), j = BigInteger.ZERO; i.compareTo(BigInteger.ZERO) >= 0; i = i
.subtract(BigInteger.ONE), j = j.add(BigInteger.ONE)) {
decimal = decimal.add(oremun[i.intValue()].multiply(powerOf(SIXTEEN, j)));
decimal = decimal.add(number[i.intValue()].multiply(powerOf(SIXTEEN, j)));
}
return decimal.toString();
}
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/drawable/backspace.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:width="40dp" android:height="40dp" android:drawable="@android:drawable/ic_input_delete" />
</selector>
Loading

0 comments on commit b134042

Please sign in to comment.