Skip to content

Instantly share code, notes, and snippets.

Created April 22, 2017 18:42
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.darrell.project2.MainActivity">
<LinearLayout
android:id="@+id/ll_currentUser"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_currentUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current User" />
<ImageView
android:id="@+id/iv_currentAvatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cropToPadding="true"
android:maxHeight="50px"
app:srcCompat="@mipmap/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/horizontalScrollView"
android:layout_alignParentStart="true"
android:layout_marginBottom="28dp"
android:orientation="vertical"
android:id="@+id/ll_login">
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="23dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Enter Username"
android:inputType="text"
android:textAlignment="center"
android:textStyle="bold|italic" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPassword"
android:textAlignment="center"
android:textStyle="normal|bold|italic" />
</LinearLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:fillViewport="false"
android:id="@+id/horizontalScrollView">
<LinearLayout
android:id="@+id/ll_avatars"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_avatar1"
android:layout_width="124dp"
android:layout_height="125dp"
app:srcCompat="@mipmap/avatar1"
android:onClick="avatar_onClick"/>
<ImageView
android:id="@+id/iv_avatar2"
android:layout_width="124dp"
android:layout_height="125dp"
app:srcCompat="@mipmap/avatar2"
android:onClick="avatar_onClick"/>
<ImageView
android:id="@+id/iv_avatar3"
android:layout_width="124dp"
android:layout_height="125dp"
app:srcCompat="@mipmap/avatar3"
android:onClick="avatar_onClick"/>
<ImageView
android:id="@+id/iv_avatar4"
android:layout_width="124dp"
android:layout_height="125dp"
app:srcCompat="@mipmap/avatar4"
android:onClick="avatar_onClick"/>
<ImageView
android:id="@+id/iv_avatar5"
android:layout_width="124dp"
android:layout_height="125dp"
app:srcCompat="@mipmap/avatar5"
android:onClick="avatar_onClick"/>
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:id="@+id/ll_colorChoice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/horizontalScrollView"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_colorChoice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose Color" />
<Button
android:id="@+id/btn_yellow"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Yellow"
/>
<Button
android:id="@+id/btn_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Red"
/>
<Button
android:id="@+id/btn_blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Blue" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_createAcount"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_below="@+id/ll_colorChoice"
android:layout_alignParentStart="true"
android:layout_marginTop="21dp">
<Button
android:id="@+id/btn_createAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Create Account" />
</LinearLayout>
</RelativeLayout>
package com.example.darrell.project2;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
int u_currentAvatar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* HashMap<Integer, Integer> mapViewIdToImageId = new HashMap<Integer, Integer>();
mapViewIdToImageId.put(R.id.iv_avatar1, R.mipmap.avatar1);
mapViewIdToImageId.put(R.id.iv_avatar2, R.mipmap.avatar2);
mapViewIdToImageId.put(R.id.iv_avatar3, R.mipmap.avatar3);
mapViewIdToImageId.put(R.id.iv_avatar4, R.mipmap.avatar4);
mapViewIdToImageId.put(R.id.iv_avatar5, R.mipmap.avatar5);
*/
SharedPreferences restoreAvatar = getSharedPreferences("MyPref", 0);
u_currentAvatar = restoreAvatar.getInt("MyPref", u_currentAvatar);
// if (restoreAvatar.getInt("Current Avatar", R.id.iv_avatar1) == R.id.iv_avatar1){
// iv_currentAvatar.setImageResource(R.mipmap.avatar1);
} // end of onCreate
public void avatar_onClick(View oView) {
int selectedImage;
int currentAvatar = 0;
ImageView imgClicked = (ImageView) oView;
ImageView iv_currentAvatar = (ImageView) findViewById(R.id.iv_currentAvatar);
selectedImage = imgClicked.getId();
//selectedImage = (ImageView) findViewById(R.id.iv_avatar1);
//Toast.makeText(this, "Image ID: " + imgClicked.getId(), Toast.LENGTH_SHORT).show();
//Toast.makeText(this, "IC ID: " + R.mipmap.avatar1, Toast.LENGTH_SHORT) .show();
iv_currentAvatar.setImageResource(R.mipmap.avatar1);
SharedPreferences.Editor avatarPref = getSharedPreferences("MyPref", MODE_PRIVATE).edit();
if (selectedImage == R.id.iv_avatar1) {
iv_currentAvatar.setImageResource(R.mipmap.avatar1);
avatarPref.putInt("Current Avatar", currentAvatar);
avatarPref.commit();
} else if (selectedImage == R.id.iv_avatar2) {
iv_currentAvatar.setImageResource(R.mipmap.avatar2);
avatarPref.putInt("Current Avatar", currentAvatar);
avatarPref.commit();
} else if (selectedImage == R.id.iv_avatar3) {
iv_currentAvatar.setImageResource(R.mipmap.avatar3);
avatarPref.putInt("Current Avatar", currentAvatar);
avatarPref.commit();
} else if (selectedImage == R.id.iv_avatar4) {
iv_currentAvatar.setImageResource(R.mipmap.avatar4);
avatarPref.putInt("Current Avatar", currentAvatar);
avatarPref.commit();
} else if (selectedImage == R.id.iv_avatar5) {
iv_currentAvatar.setImageResource(R.mipmap.avatar5);
avatarPref.putInt("Current Avatar", currentAvatar);
avatarPref.commit();
}
}// end of avatar onClick
}// end of class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment