Melanie Zhao
Created June 27, 2015

PRG01 - Translator Application

A basic translation application that allows for the translation of 5 phrases between 3 languages.

Full instructions provided22
PRG01 - Translator Application

Story

Read more

Code

MainActivity.java

Java
package itepsilons.zhaossimpletranslator;

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;


public class MainActivity extends Activity {

    private Spinner from_language;
    private Spinner to_language;
    private String [] languages;
    private String language1;
    private String language2;
    private RadioGroup radioGroup;
    private int index;
    private Button button;
    private static HashMap<String, String[]> words;
    private TextView tw;

    public void createLanguageHashMap() {
        words = new HashMap<String, String[]>();
        words.put("English",new String[] {"Hello", "Thank you", "Goodbye", "You're welcome", "Idiot!"});
        words.put("Chinese",new String[] {"", "", "", "", "!"});
        words.put("Japanese",new String[] {"", "", "", "", "!"});
        languages = new String[] {"Choose a language","English", "Chinese", "Japanese"};
        language1 = "";
        language2 = "";
    }

    private void addRadioButtons(int numButtons) {
        if(!language1.equals("Choose a language")) {
            int j = radioGroup.getChildCount();
            for (int i = 0; i < j; i++) {
                radioGroup.removeViewAt(0);
            }
            for (int i = 0; i < numButtons; i++) {
                RadioButton radioButton = new RadioButton(this);
                radioButton.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT));
                radioButton.setText(words.get(language1)[i]);
                radioButton.setId(i);
                radioButton.setTextSize(20);
                radioGroup.addView(radioButton, i);
            }
        }
    }

    public void createFromLanguageSpinner() {
        from_language = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<String> adp = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_dropdown_item, languages);
        from_language.setAdapter(adp);
        from_language.setVisibility(View.VISIBLE);
        from_language.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                language1 = parentView.getItemAtPosition(position).toString();
                addRadioButtons(5);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                return;
            }

        });
    }

    public void createToLanguageSpinner() {
        to_language = (Spinner) findViewById(R.id.spinner2);
        ArrayAdapter<String> adp = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_dropdown_item, languages);
        to_language.setAdapter(adp);
        to_language.setVisibility(View.VISIBLE);
        to_language.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                language2 = parentView.getItemAtPosition(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                return;
            }

        });
    }



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

        createLanguageHashMap();

        createFromLanguageSpinner();

        createToLanguageSpinner();

        index = -1;

        radioGroup = (RadioGroup) findViewById(R.id.radio_group);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                index = checkedId;
            }
        });

        button = (Button) findViewById(R.id.translate);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (!language2.equals("Choose a language") && !language1.equals("Choose a language") && index != -1) {
                    tw = (TextView) findViewById(R.id.result);
                    tw.setText(words.get(language2)[index]);
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

strings.xml

XML
<resources>
    <string name="app_name">ZhaosSimpleTranslator</string>
    <string name="from_language">From Language:</string>
    <string name="to_language">To Language:</string>
    <string name="translate">TRANSLATE</string>
    <string name="select_a_phrase">Select a phrase</string>
    <string name="action_settings">Settings</string>


</resources>

activity_main.xml

XML
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        >


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_horizontal" >

            <TextView
                android:text="@string/from_language"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20dp"/>

            <Spinner
                android:id="@+id/spinner1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_horizontal" >
            <TextView android:text="@string/to_language"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20dp" />

            <Spinner
                android:id="@+id/spinner2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />
        </LinearLayout>


        <TextView
            android:text="@string/select_a_phrase"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"/>

        <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/radio_group"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        </RadioGroup>

        <Button
            android:id="@+id/translate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/translate" />
        <TextView
            android:id="@+id/result"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"/>
    </LinearLayout>

</RelativeLayout>

Credits

Melanie Zhao

Melanie Zhao

3 projects • 1 follower

Comments