Android Application Sample(display analogClock, and change background image by tap background or orientationchange)


---(Application Description of)---

Portrait1 Portrait2 Landscape1 Landscape2

---(Source List java/jp.co-site.yukos.testapp/MainActivity.java)---

package jp.co_site.yukos.ourclock;;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.content.SharedPreferences;                      //for save counter
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
    SharedPreferences pref;
    int counter=1;
    View layout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pref=getSharedPreferences("iCount", MODE_PRIVATE);
        counter=pref.getInt("count", 1);                       //get before counter
        layout=findViewById(R.id.RelativeLayout01);
        layout.setOnClickListener(new View.OnClickListener(){  //function of click background
            @Override
            public void onClick(View v) {
                counter++;                                     //set other background and counter
                if(counter==3){counter=1;}
                String resourceName="img"+counter;             //set background match counter
                int viewId=getResources().getIdentifier(resourceName,"drawable",getPackageName());
                v.setBackgroundResource(viewId);
            }
        });
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }
    @Override
    protected  void onResume(){
        String resourceName="img"+counter;                     //set background match counter
        int viewId=getResources().getIdentifier(resourceName,"drawable",getPackageName());
        layout.setBackgroundResource(viewId);
        super.onResume();
    }
    @Override
    protected void onStop() {
        SharedPreferences.Editor editor=pref.edit();
        editor.putInt("count",counter);                        //save current counter
        editor.commit();
        super.onStop();
    }
    @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);
    }
}

---(res/layout/content_main.xml)---

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" 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"
    android:id="@+id/RelativeLayout01"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main" tools:context=".MainActivity"
    android:background="@drawable/img1">
<AnalogClock 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/analogClock"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:dial="@drawable/dial"
    android:hand_hour="@drawable/hand_hour"
    android:hand_minute="@drawable/hand_minute">
</AnalogClock>
</RelativeLayout>

---(res/values/strings.xml)---

<resources>
    <string name="app_name">OurClock</string>
    <string name="action_settings">Settings</string>
</resources>

---(Resource)---

Resource