Design a Greek Key

You choose an feature (such as colour or number of turns or style), then choose the option within it. The Greek Key appears.

Stringsapp/res/values/strings.xml
Screen formatapp/res/layout/activity_main.xml
Code for appapp/java/com.(the project name)/MainActivity
Code for option gridviewapp/java/com.(the project name)/ImageOption
Code for Greek key gridviewapp/java/com.(the project name)/ImageTile



app/manifests/AndroidManifest.xml - to force landscape orientation

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.edkins.greekkey" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:screenOrientation="landscape" > > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>


app/res/drawable - pictures

There are a number of files for the different options, colours, numbers and styles.




app/res/mipmap/ic_launcher.png/ - icon for launching app




app/res/values/strings.xml - strings

<resources> <string name="app_name">Design a Greek Key</string> <string name="keyColourText">Colour</string> <string name="backgroundText">Background</string> <string name="turnsText">Turns</string> <string name="bordersText">Borders</string> <string name="numberText">Number</string> <string name="styleText">Style</string> </resources>


app/res/layout/activity_main.xml - screen format

<?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: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"> <GridView android:id="@+id/optionGrid" android:layout_width="640dp" android:layout_height="wrap_content" android:paddingBottom="@dimen/activity_vertical_margin" android:numColumns="10" android:layout_centerHorizontal="true" /> <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/feature" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:orientation="horizontal" android:layout_below="@id/optionGrid" android:layout_centerHorizontal="true" > <RadioButton android:id="@+id/keyColour" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/keyColourText" android:onClick="doKeyColour" /> <RadioButton android:id="@+id/background" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/backgroundText" android:onClick="doBackground" /> <RadioButton android:id="@+id/turns" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/turnsText" android:onClick="doTurns" /> <RadioButton android:id="@+id/borders" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bordersText" android:onClick="doBorders" /> <RadioButton android:id="@+id/numbers" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/numberText" android:onClick="doNumber" /> <RadioButton android:id="@+id/style" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/styleText" android:onClick="doStyle" /> </RadioGroup> <GridView android:id="@+id/tileGrid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="@dimen/activity_vertical_margin" android:layout_below="@id/feature" /> </RelativeLayout>


app/java/com.(the project name)/MainActivity - main code for app

package com.edkins.greekkey; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.GridView; import android.widget.RadioButton; import android.widget.Toast; public class MainActivity extends AppCompatActivity { public static int feature=0; public static int numOpt=10; public static int keyColour=5; public static int background=4; public static int turns=3; public static int borders=0; public static int num=4; public static int style=1; public static int heightKey; public static int widthKey; public static int widthGen; public static int heightScreen; public static int widthScreen; public static int [] [] tile; public static int [] [] key; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RadioButton acc = (RadioButton) findViewById(R.id.keyColour); acc.setChecked(true); feature = 0; GridView optionV = (GridView) findViewById(R.id.optionGrid); optionV.setAdapter(new ImageOption(this)); optionV.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { processOptionClick(position); } }); setUp(); GridView tileV = (GridView) findViewById(R.id.tileGrid); tileV.setNumColumns(widthScreen); tileV.setAdapter(new ImageTile(this)); } public void processOptionClick(int position) { if (feature>=2 && feature<= 4 && (position<2 || position>7)) { Toast.makeText(getApplicationContext(), "Tap on number", Toast.LENGTH_SHORT).show(); return; } if (feature==0) { if (position==background) {background = keyColour;} keyColour = position; } else if (feature==1) { if (position==keyColour) {keyColour = background;} background = position; } else if (feature==2) {turns = position+1;} else if (feature==3) {borders = position-2;} else if (feature==4) {num = position+2; if (style==0) {style = 1;}} else if (feature==5) {style = position;} setUp(); redoKey(); } public void setUp() { if (style==0 || style==1) { heightKey = 2 * turns - 1; widthKey = heightKey + 1; widthGen = heightKey + 2; } else if (style==2) { heightKey = 2 * turns - 1; widthKey = 2 * (heightKey - 1); widthGen = heightKey + 2; } else if (style==3 || style==4) { heightKey = turns + 1; widthKey = heightKey +1; widthGen = heightKey + 1; } else if (style==5) { heightKey = turns + 1; widthKey = 2 * heightKey; widthGen = heightKey + 1; } heightScreen = heightKey + 2 * borders; widthScreen = widthKey * num; tile = new int [heightScreen] [widthScreen]; key = new int [heightKey] [widthGen]; for (int y=0; y<heightScreen; y++) {for (int x=0; x<widthScreen; x++) {tile [y] [x]=10;}} if (style<3) {processKey1();} else {processKey2();} processStyle(); processBorders(); } public void processKey1() { int x; int y; for (y=0; y<heightKey; y++) {for (x=0; x<widthGen; x++) {key [y] [x]=background;}} x = widthGen/2; y = heightKey/2; key[y][x]=keyColour; x = 0; y = 0; int xi = 1; int yi = 0; for (int arm = heightKey; arm > 0; arm=arm-2) { for (int i=0; i<arm; i++) { key[y][x]=keyColour; key[heightKey-y-1][widthGen-x-1]=keyColour; x=x+xi; y=y+yi; } x=x-xi; y=y-yi; int save=yi; yi=xi; xi=-save; } } public void processKey2() { int x; int y; for (y=0; y<heightKey; y++) {for (x=0; x<widthGen; x++) {key [y] [x]=background;}} x = 0; y = 0; int xi = 1; int yi = 0; for (int arm = heightKey; arm > 1; arm=arm-1) { for (int i=0; i<arm; i++) { key[y][x]=keyColour; x=x+xi; y=y+yi; } x=x-xi; y=y-yi; int save=yi; yi=xi; xi=-save; } } public void processStyle() { if (style==0 || style==3) { int i = (num-1)/2; oneKey(i * widthKey); } else if (style==1 || style==4) { for (int x = 0; x < widthScreen; x = x + widthKey) {oneKey(x);} } else if (style==2) { for (int x = 0; x < widthScreen; x = x + widthKey) {flipKey1(x);} } else if (style==5) { for (int x = 0; x < widthScreen; x = x + widthKey) {flipKey2(x);} } } public void oneKey(int startX) { for (int y=0; y<heightKey; y++) {tile [y + borders] [startX]=keyColour;} for (int x = 1; x < widthKey; x++) { for (int y = 0; y < heightKey; y++) { tile[y + borders][x + startX] = key[y][x]; } } } public void flipKey1(int startX) { for (int x = 1; x < (widthGen-1); x++) { for (int y = 0; y < heightKey; y++) { tile[y + borders][startX + x - 1] = key[y][x]; if ((startX + widthKey - x + 1) < widthScreen) { tile[y + borders][startX + widthKey - x + 1] = key[y][x]; } } } } public void flipKey2(int startX) { for (int y=0; y<heightKey; y++) {key [y] [0]=keyColour;} for (int x = 0; x < widthGen; x++) { for (int y = 0; y < heightKey; y++) { tile[y + borders][startX + x] = key[y][x]; if ((startX + widthKey - x) < widthScreen) { tile[y + borders][startX + widthKey - x] = key[y][x]; } } } } public void processBorders() { int startX = 0; int endX = widthScreen; if (style==0 || style==3) { int i = (num-1)/2; startX = i * widthKey; endX = startX + widthKey; } for (int x = startX; x < endX; x++) { int colNum = 0; if (borders%2==1) {colNum=1;} for (int j=0; j<borders; j++) { int col=background; int col2 = background; if (style>2) {col2 = keyColour;} if (colNum==0) { col=keyColour; col2 = keyColour; if (style>2) {col2 = background;} } colNum = 1 - colNum; tile[j][x] = col; tile[heightScreen-j-1][x]=col2; } } } public void redoKey() { GridView tileV = (GridView) findViewById(R.id.tileGrid); tileV.setNumColumns(widthScreen); tileV.setAdapter(new ImageTile(this)); } public void redoOptions() { GridView optionV = (GridView) findViewById(R.id.optionGrid); optionV.setNumColumns(numOpt); optionV.setAdapter(new ImageOption(this)); } public void doKeyColour(View view) {feature=0; setUp(); numOpt=10; redoOptions();} public void doBackground(View view) {feature=1; setUp(); numOpt=10; redoOptions();} public void doTurns(View view) {feature=2; setUp(); numOpt=10; redoOptions();} public void doBorders(View view) {feature=3; setUp(); numOpt=10; redoOptions();} public void doNumber(View view) {feature=4; setUp(); numOpt=10; redoOptions();} public void doStyle(View view) {feature=5; setUp(); numOpt=6; redoOptions();} public static Integer[] colour = { R.drawable.colour0, R.drawable.colour1, R.drawable.colour2, R.drawable.colour3, R.drawable.colour4, R.drawable.colour5, R.drawable.colour6, R.drawable.colour7, R.drawable.colour8, R.drawable.colour9, R.drawable.colourx }; public static Integer[] number = { R.drawable.number0, R.drawable.number1, R.drawable.number2, R.drawable.number3, R.drawable.number4, R.drawable.number5, R.drawable.number6, R.drawable.number7, R.drawable.number8, R.drawable.number9, R.drawable.blank }; public static Integer[] styleName = { R.drawable.style0, R.drawable.style1, R.drawable.style2, R.drawable.style3, R.drawable.style4, R.drawable.style5 }; }


app/java/com.(the project name)/ImageOption - code for option gridview. You create this new file by right-clicking on the MainActvity and select New, then Java class

package com.edkins.greekkey; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; public class ImageOption extends BaseAdapter { private AppCompatActivity mContext; public ImageOption(AppCompatActivity c) {mContext = c;} public int getCount() {return MainActivity.numOpt;} public Object getItem(int position) {return null;} public long getItemId(int position) {return 0;} public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(mContext); GridView opt = (GridView) mContext.findViewById(R.id.optionGrid); int w = opt.getWidth() / MainActivity.numOpt; int h = w; if (MainActivity.feature==5) {h=h/2;} imageView.setLayoutParams(new GridView.LayoutParams(w,h)); } else { imageView = (ImageView) convertView; } if (MainActivity.feature<2) { if (position==0) {imageView.setImageResource(MainActivity.colour[10]);} else{imageView.setImageResource(MainActivity.colour[position]);} } else if (MainActivity.feature==2) { if (position<2 || position>7) {imageView.setImageResource(MainActivity.number[10]);} else {imageView.setImageResource(MainActivity.number[position+1]);} } else if (MainActivity.feature==3) { if (position<2 || position>7) {imageView.setImageResource(MainActivity.number[10]);} else {imageView.setImageResource(MainActivity.number[position-2]);} } else if (MainActivity.feature==4) { if (position<2 || position>7) {imageView.setImageResource(MainActivity.number[10]);} else {imageView.setImageResource(MainActivity.number[position+2]);} } else if (MainActivity.feature==5) {imageView.setImageResource(MainActivity.styleName[position]);} return imageView; } }


app/java/com.(the project name)/ImageTile - code for tile gridview - a new file, see above

package com.edkins.greekkey; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; public class ImageTile extends BaseAdapter { private AppCompatActivity mContext; public ImageTile(AppCompatActivity c) {mContext = c;} public int getCount() {return (MainActivity.widthScreen * MainActivity.heightScreen);} public Object getItem(int position) {return null;} public long getItemId(int position) {return 0;} public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(mContext); GridView opt = (GridView) mContext.findViewById(R.id.tileGrid); int w = opt.getWidth() / MainActivity.widthScreen; imageView.setLayoutParams(new GridView.LayoutParams(w, w)); } else { imageView = (ImageView) convertView; } int x = position % MainActivity.widthScreen; int y = position / MainActivity.widthScreen; int i = MainActivity.tile[y][x]; if (i==10) {imageView.setImageResource(MainActivity.number[10]);} else {imageView.setImageResource(MainActivity.colour[i]);} return imageView; } }


Index to my other websites

© Jo Edkins 2016