Skip to content

Instantly share code, notes, and snippets.

@thbighead
Created September 6, 2018 00:13
Show Gist options
  • Save thbighead/841289ab8c5df157310346ce3b8582f5 to your computer and use it in GitHub Desktop.
Save thbighead/841289ab8c5df157310346ce3b8582f5 to your computer and use it in GitHub Desktop.
Conversor de metros para pés usando SeekBar (falta implementação do botão "Limpar")
package com.example.labc.conversor_altura;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
int altemCentimetros = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView txtMetros = (TextView) findViewById(R.id.txtMetros);
final TextView txtPes = (TextView) findViewById(R.id.txtPes);
final SeekBar seekMetros = (SeekBar) findViewById(R.id.seekMetros);
seekMetros.setMax(230);
seekMetros.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
altemCentimetros = progress;
String texto = formataValorComDoisDigitos(progress/100.00);
texto += ".m";
txtMetros.setText(texto);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
txtPes.setText("pé(s) : toque em 'Converter'");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
final Button btnConvert = (Button) findViewById(R.id.btnConvert);
btnConvert.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
double altEmPes = altemCentimetros / 30.48;
String texto = formataValorComDoisDigitos(altEmPes);
texto += "pé(s)";
txtPes.setText(texto);
}
});
}
private String formataValorComDoisDigitos(double valor) {
return String.format(Locale.ENGLISH, "%.2f", valor);
}
}
<?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=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="86dp"
android:layout_height="35dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="35dp"
android:layout_marginTop="76dp"
android:text="Altura:"
android:textStyle="bold"
tools:textColor="@color/colorPrimaryDark"
tools:textSize="18sp" />
<SeekBar
android:id="@+id/seekMetros"
android:layout_width="278dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="@+id/textView"
android:layout_marginEnd="41dp" />
<TextView
android:id="@+id/txtPes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="0,0 (pés)"
android:textStyle="italic"
tools:textSize="18sp" />
<TextView
android:id="@+id/txtMetros"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="140dp"
android:text="0,0m"
tools:textSize="18sp" />
<Button
android:id="@+id/btnLimpar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="176dp"
android:text="Limpar" />
<Button
android:id="@+id/btnConvert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="175dp"
android:text="Converter" />
</RelativeLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment