11 Ocak 2017 Çarşamba

Android Programlama: Arkaplan (background color) renklerini değiştirme.

2017-01-11_11-51-38
activity_main.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"
    android:id="@+id/rlZemin">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Renk Değiş"
        android:id="@+id/btnRenkDegis"
        android:onClick="btnRenkDegisClick"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="buton 2"
        android:id="@+id/btnNew"
        android:onClick="btnYeniClick"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_above="@+id/btnRenkDegis">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Kırmızı"
            android:id="@+id/txtRed" />

        <SeekBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/sbRed"
            android:indeterminate="false"
            android:max="255"
            android:progress="128" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Yeşil"
            android:id="@+id/txtGreen" />

        <SeekBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/sbGreen"
            android:indeterminate="false"
            android:max="255" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mavi"
            android:id="@+id/txtBlue" />

        <SeekBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/sbBlue"
            android:indeterminate="false"
            android:max="255" />

    </LinearLayout>

</RelativeLayout>
MainActivity.java;
package com.example.okul.rastgelerenkdegis;

import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import java.util.Random;


public class MainActivity extends ActionBarActivity {
    RelativeLayout rl;
    Button btnD,btnY;
    SeekBar sbRed,sbGreen,sbBlue;
    TextView txtRed,txtGreen,txtBlue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rl=(RelativeLayout) findViewById(R.id.rlZemin);//nesnenin zemin rengini yakala
        btnD=(Button) findViewById(R.id.btnRenkDegis);
        btnY=(Button) findViewById(R.id.btnNew);
        sbRed=(SeekBar) findViewById(R.id.sbRed);
        sbGreen=(SeekBar) findViewById(R.id.sbGreen);
        sbBlue=(SeekBar) findViewById(R.id.sbBlue);
        sbRed.setOnSeekBarChangeListener(new KaydirmaOlayTutucu());
        sbGreen.setOnSeekBarChangeListener(new KaydirmaOlayTutucu());
        sbBlue.setOnSeekBarChangeListener(new KaydirmaOlayTutucu());
        txtRed=(TextView) findViewById(R.id.txtRed);
        txtGreen=(TextView) findViewById(R.id.txtGreen);
        txtBlue=(TextView) findViewById(R.id.txtBlue);

     
    }

    @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);
    }

    public void btnRenkDegisClick(View view) {
        int renk = RenkGetir();
        rl.setBackgroundColor(renk);
        btnD.setBackgroundColor(RenkGetir());
        btnY.setBackgroundColor(RenkGetir());



    }

    private int RenkGetir() {
        Random rnd=new Random();//rastgele sayy üretme
        int red=rnd.nextInt(255);
        int green=rnd.nextInt(255);
        int blue=rnd.nextInt(255);
        return Color.rgb(red, green, blue);
    }//sag tuş refactor/extract/method otomatik metod oluştur.

    public void btnYeniClick(View deneme){

    }
    public class KaydirmaOlayTutucu implements  SeekBar.OnSeekBarChangeListener
    {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            int kirmizi=sbRed.getProgress();
            int yesil=sbGreen.getProgress();
            int mavi=sbBlue.getProgress();
            rl.setBackgroundColor(Color.rgb(kirmizi,yesil,mavi));
            txtRed.setText("Kırmızı " + kirmizi);
            txtGreen.setText("Yeşil " + yesil);
            txtBlue.setText("Mavi "+ mavi);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    }


}

AndroidManifest.xml;
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.okul.rastgelerenkdegis" >

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

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

</manifest>


Hiç yorum yok :

Yorum Gönder