Pesquisar




Author Image
Desenvolvimento em Foco Programação Contact Me

AIDE Shared Preferences









ATENÇÃO - CONTEÚDO COMPATIVEL COM ANDROID KITKAT OU ACIMA



Arquivo - build.gradle - ..\app\build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
        applicationId "projeto.meu.com.meuprojeto"
        minSdkVersion 16
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
compile 'com.android.support:design:+'
compile 'com.android.support:appcompat-v7:+'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'

 
}


//////////////////////////////////////////////////////////////////////


Arquivo - AndroidManifest.xml - ..\app\src\main\AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="projeto.meu.com.meuprojeto">

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
//////////////////////////////////////////////////////////////////////


Arquivo - MainActivity.java ..\app\src\main\java\projeto\meu\com\meuprojeto\MainActivity.java

package projeto.meu.com.meuprojeto;

import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.*;
import android.provider.Settings;
import android.support.design.widget.*;
import android.support.v7.app.*;
import android.view.*;
import android.widget.*;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity {
    String sred = "ToolBar ser tornou: Vermelha";
    String sgreen = "ToolBar ser tornou: Verde";
    String sblue = "ToolBar ser tornou: Azul";
    String sblack = "ToolBar ser tornou: Preta";
    MediaPlayer a,b = new MediaPlayer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        a = MediaPlayer.create(this, R.raw.a);
        b = MediaPlayer.create(this, R.raw.b);
        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        SharedPreferences Botao = getSharedPreferences("Dados", MODE_PRIVATE);
        String botao = Botao.getString("botao", "");
        switch (botao) {
            case "vermelho":
                toolbar.setBackgroundResource(R.drawable.botaored);
                break;
            case "verde":
                toolbar.setBackgroundResource(R.drawable.botao2);
                break;
            case "azul":
                toolbar.setBackgroundResource(R.drawable.btn);
                break;
            case "preto":
                toolbar.setBackgroundResource(R.drawable.botaopreto);
                break;
            default:
                //noinspection deprecation
                toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                break;
        }

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                a.start();
                Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("text/plain");
                //noinspection deprecation
                share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                share.putExtra(Intent.EXTRA_TEXT, "Nome APP..." + "\n" +
                                                  "Descrição" + "\n" + "\n" +
                                                  "http://www.aksr.com.br/" + "\n");
                startActivity(Intent.createChooser(share, "Compartilhar"));


                //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        //.setAction("Action", null).show();
            }
        });

        Button padrao = (Button) findViewById(R.id.padrao);
        padrao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                a.start();
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                //noinspection deprecation
                toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));

                SharedPreferences preto = getSharedPreferences("Dados", MODE_PRIVATE);
                SharedPreferences.Editor editor = preto.edit();
                editor.putString("botao", "");
                editor.apply();
            }
        });


        Button black = (Button) findViewById(R.id.black);
        black.setBackgroundResource(R.drawable.botaopreto);
        black.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                a.start();
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                toolbar.setBackgroundResource(R.drawable.botaopreto);
                toolbar.setTitle(sblack);

                SharedPreferences preto = getSharedPreferences("Dados", MODE_PRIVATE);
                SharedPreferences.Editor editor = preto.edit();
                editor.putString("botao", "preto");
                editor.apply();
            }
        });

        Button blue = (Button) findViewById(R.id.blue);
        blue.setBackgroundResource(R.drawable.btn);
        blue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                b.start();
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                toolbar.setBackgroundResource(R.drawable.btn);
                toolbar.setTitle(sblue);

                SharedPreferences azul = getSharedPreferences("Dados", MODE_PRIVATE);
                SharedPreferences.Editor editor = azul.edit();
                editor.putString("botao", "azul");
                editor.apply();

            }
        });

Button green= (Button) findViewById(R.id.green);
        green.setBackgroundResource(R.drawable.botao2);
        green.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
                    a.start();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setBackgroundResource(R.drawable.botao2);
                    toolbar.setTitle(sgreen);

                    SharedPreferences verde = getSharedPreferences("Dados", MODE_PRIVATE);
                    SharedPreferences.Editor editor = verde.edit();
                    editor.putString("botao", "verde");
                    editor.apply();

}
});

        Button red = (Button) findViewById(R.id.red);
        red.setBackgroundResource(R.drawable.botaored);
        red.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                b.start();
                toolbar.setBackgroundResource(R.drawable.botaored);
                toolbar.setTitle(sred);

                SharedPreferences vermelho = getSharedPreferences("Dados", MODE_PRIVATE);
                SharedPreferences.Editor editor = vermelho.edit();
                editor.putString("botao", "vermelho");
                editor.apply();

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        SubMenu subMenu = menu.addSubMenu(Menu.NONE, 0, 0, "");
        subMenu.getItem().setIcon(R.drawable.m);
        subMenu.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        subMenu.add(0, 1, 0, R.string.action_settings).setIcon(R.drawable.eng);
       // subMenu.add(0, 2, 0, R.string.app_name).setIcon(R.drawable.ic_launcher);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        b.start();
        switch (item.getItemId()) {
            case 1:
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.parse("package:" + getPackageName());
                intent.setData(uri);
                startActivity(intent);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        finish();
        System.exit(0);
    }
}





//////////////////////////////////////////////////////////////////////


Imagens ..\app\src\main\res\drawable\



















//////////////////////////////////////////////////////////////////////



Sons colocados na Pasta - raw





Arquivo- activity_main.xml ..\app\src\main\res\layout\activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="projeto.meu.com.meuprojeto.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_menu_share" />

</android.support.design.widget.CoordinatorLayout>



//////////////////////////////////////////////////////////////////////


Arquivo- content_main.xml ..\app\src\main\res\layout\content_main.xml


<?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: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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="projeto.meu.com.meuprojeto.MainActivity"
tools:showIn="@layout/activity_main">

<ScrollView
android:layout_width="match_parent"
android:layout_height="fill_parent">

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp">

<RelativeLayout
android:contentDescription="@string/app_name"
android:layout_width="fill_parent"
android:layout_height="500dp"
android:layout_marginTop="15dp"
android:id="@+id/relativeLayout7">

<Button
android:layout_width="120dp"
android:layout_height="50dp"
android:text="Preto"
android:id="@+id/black"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textAlignment="center" />

<Button
android:layout_height="50dp"
android:layout_width="120dp"
android:text="Vermelho"
android:id="@+id/red"
android:layout_below="@+id/green"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textAlignment="center" />

<Button
android:layout_height="50dp"
android:layout_width="120dp"
android:text="Verde"
android:id="@+id/green"
android:layout_below="@+id/blue"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textAlignment="center" />

<Button
android:layout_height="50dp"
android:layout_width="120dp"
android:text="Azul"
android:id="@+id/blue"
android:layout_below="@+id/black"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textAlignment="center" />

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/red"
android:layout_centerHorizontal="true"
android:layout_marginTop="82dp">

<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/face"
android:background="@drawable/face"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/tube"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/face"
android:layout_toEndOf="@+id/face"
android:background="@drawable/tube" />

<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/twitter"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/tube"
android:layout_toEndOf="@+id/tube"
android:background="@drawable/twitter" />

<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/site"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/twitter"
android:layout_toEndOf="@+id/twitter"
android:background="@drawable/blogspot" />

<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/linked"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/site"
android:layout_toEndOf="@+id/site"
android:background="@drawable/linked" />

<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/linked"
android:layout_toEndOf="@+id/linked"
android:background="@android:drawable/btn_star_big_on" />

<Button
            android:layout_width="250dp"
            android:layout_height="50dp"
            android:id="@+id/link"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:background="@drawable/def" />
</RelativeLayout>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Padrão"
android:id="@+id/padrao"
android:layout_above="@+id/green"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

</RelativeLayout>

</RelativeLayout>
</ScrollView>

</RelativeLayout>


//////////////////////////////////////////////////////////////////////


Arquivo- menu_main.xml ..\app\src\main\res\menu\menu_main.xml


<menu 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"
    tools:context="projeto.meu.com.meuprojeto.MainActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
</menu>


//////////////////////////////////////////////////////////////////////


Arquivo- colors.xml ..\app\src\main\res\values\colors.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#000000</color>
    <color name="colorPrimaryDark">#000000</color>
    <color name="colorAccent">#000000</color>
</resources>


//////////////////////////////////////////////////////////////////////


Arquivo- dimens.xml ..\app\src\main\res\values\dimens.xml


<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="fab_margin">16dp</dimen>
</resources>


//////////////////////////////////////////////////////////////////////


Arquivo- strings.xml ..\app\src\main\res\values\strings.xml


<resources>
    <string name="app_name">Meu Projeto</string>
    <string name="action_settings">Settings</string>
<string name="hello_world">Hello World</string>
</resources>


//////////////////////////////////////////////////////////////////////


Arquivo- styles.xml ..\app\src\main\res\values\styles.xml


<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>


//////////////////////////////////////////////////////////////////////



Nenhum comentário:

Postar um comentário

Obrigado por comentar, volte sempre!

Seguidores

Não encontrou o que procura ?