Junzhu Cong
Created June 27, 2015

Translate

Translate app, full code and executable here: https://www.dropbox.com/sh/w0m1sj4jqwkflmf/AABQfOWykRRi8O0-9WkSgVMZa?dl=0

Full instructions provided26
Translate

Things used in this project

Hardware components

Android device
Android device
×1

Story

Read more

Code

main

Java
full repo here: https://www.dropbox.com/s/r2zr8ezidxpkvpj/Translate.zip?dl=0
apk: https://www.dropbox.com/s/9nbxs7279sned0q/app-release.apk?dl=0
package com.junecong.translate;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.ArrayAdapter;
import android.widget.NumberPicker;
import android.widget.Spinner;
import android.view.View;
import android.widget.AdapterView;

public class main extends Activity {

    private Spinner spin;
    private NumberPicker pick1, pick2, otherPick2;
    private String[] phrasesE, phrasesM, phrasesS;
    private TextView result;
    private int secondval, phrase;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Set Picker1
        pick1 = (NumberPicker) findViewById(R.id.numberPicker1);
        pick1.setMinValue(0);
        pick1.setMaxValue(2);
        pick1.setDisplayedValues(new String[]{"English", "", "Espaol"});
        populateSpinner(0);
        pick1.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {

            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

                int pick1Value = pick1.getValue(); //return int
                switch (pick1Value) {

                    case 0: /** English */
                        populateSpinner(0);
                        break;
                    case 1: /** Mandarin */
                        populateSpinner(1);
                        break;
                    case 2: /** Spanish */
                        populateSpinner(2);
                        break;
                }
            }
        });

        //Set Picker2
        pick2 = (NumberPicker) findViewById(R.id.numberPicker2);
        pick2.setMinValue(0);
        pick2.setMaxValue(2);
        pick2.setDisplayedValues(new String[]{"English", "", "Espaol"});

        //sidebar
        result  = (TextView) findViewById(R.id.textResult);
        otherPick2 = (NumberPicker) findViewById(R.id.numberPicker2);

        pick2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {

            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                //find spinner settings
                Spinner wordToTranslate = (Spinner) findViewById(R.id.spinner1);
                phrase = (int)wordToTranslate.getSelectedItemId();

                secondval = otherPick2.getValue(); //return int
                populateTextView(secondval, phrase, result);
            }
        });

        Spinner wordToTranslate = (Spinner) findViewById(R.id.spinner1);

        wordToTranslate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

                Spinner wordToTranslate = (Spinner) findViewById(R.id.spinner1);
                phrase = (int)wordToTranslate.getSelectedItemId();

                secondval = otherPick2.getValue(); //return int
                populateTextView(secondval, phrase, result);
            }

            public void onNothingSelected(AdapterView<?> adapterView) {
                return;
            }
        });

    }


    //takes in the input lang, output lang, and phrase, and sets them in the view
    public void populateTextView(int secondval, int phrase, TextView view) {

        phrasesE = new String[]{"Hello", "wtf", "Please", "How Much", "Thank You"};
        phrasesM = new String[]{"", "", "", "", ""};
        phrasesS = new String[]{"Hola", "Adis", "Por favor", "Cunto", "Gracias"};

        switch(secondval){

            case 0: /** To English */
                view.setText(phrasesE[phrase]);
                break;
            case 1: /** To Mandarin */
                view.setText(phrasesM[phrase]);
                break;
            case 2: /** To Spanish */
                view.setText(phrasesS[phrase]);
                break;
        }
    }

    public void populateSpinner(int id) {

        spin = (Spinner) findViewById(R.id.spinner1);
        List<String> list = new ArrayList<String>();

        switch(id){

            case 0: /** English */
                list.add("Hello");
                list.add("Goodbye");
                list.add("Please");
                list.add("How much");
                list.add("Thank you");
                break;

            case 1: /** Mandarin */
                list.add("");
                list.add("");
                list.add("");
                list.add("");
                list.add("");
                break;

            case 2: /** Spanish */
                list.add("Hola");
                list.add("Adis");
                list.add("Por favor");
                list.add("Cunto");
                list.add("Gracias");
                break;
        }

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(dataAdapter);

    }
}

activity_main

XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#a6cfff"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1"
    android:gravity="center_horizontal"
    android:id="@+id/bigLayout">

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/translateicon"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true"
        android:maxWidth="150dp"
        android:paddingTop="5dp"
        android:id="@+id/image"/>

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:prompt="@string/begin"
        android:background="@color/pink"
        android:clickable="true"
        android:textAlignment="center"
        android:layout_gravity="center"
        android:layout_toRightOf="@id/image"
        android:gravity="center_vertical|center_horizontal"
        android:padding="50dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/clickable"
        android:id="@+id/textView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/image"
        android:layout_toEndOf="@+id/image"
        android:gravity="center"
        android:paddingTop="10dp" />

</RelativeLayout>



    <RelativeLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        android:id="@+id/outerLayout"
        android:gravity="center"
        android:paddingBottom="15dp">

        <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_weight="0.05"
        android:id="@+id/leftLayout">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="From"
            android:id="@+id/textView1"
            android:layout_gravity="center_horizontal"
            android:paddingBottom="10dp"
            android:paddingTop="5dp"
            android:textColor="@color/white"
            android:textStyle="normal" />

        <NumberPicker
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/numberPicker1"
            android:layout_gravity="end"
            android:layout_weight="0.05" />


        </LinearLayout>

        <Space
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:layout_toRightOf="@id/leftLayout"
            android:id="@+id/space"/>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.05"
            android:id="@+id/rightLayout"
            android:layout_toRightOf="@id/space">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/to"
            android:id="@+id/textView2"
            android:layout_gravity="center_horizontal"
            android:paddingBottom="10dp"
            android:paddingTop="5dp"
            android:textColor="@color/white" />

        <NumberPicker
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/numberPicker2"
            android:layout_gravity="start"
            android:layout_weight="0.05" />

        </LinearLayout>

    </RelativeLayout>





    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textResult"
        android:layout_gravity="center"
        android:layout_weight="0.5"
        android:text="hello"
        android:textSize="35dp"
        android:focusableInTouchMode="false"
        android:gravity="center_vertical|center_horizontal" />

</LinearLayout>

strings

XML
<resources>
    <string name="app_name">Translate</string>
    <string name="begin">Pick Phrase</string>
    <string name="action_settings">Settings</string>
    <string name="clickable">What word do you want to translate?</string>

    <color name="pink">#a6cfff</color>
    <color name="white">#ffffff</color>
    <string name="go">Go</string>
    <string name="word">Word</string>
    <string name="from">From</string>
    <string name="to">To</string>
    <string name="translate">Translate</string>
    <string name="result">Result Here</string>
    <string name="blank">  </string>


</resources>

Credits

Junzhu Cong

Junzhu Cong

4 projects • 5 followers

Comments