Startup and Tech
Sunday, 31 May 2020
How to create Color Picker In Andoid Studio
activity_main.xml
MainActivity.java
package com.example.colorpicker; import androidx.appcompat.app.AppCompatActivity; import android.os.Build; import android.os.Bundle; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import yuku.ambilwarna.AmbilWarnaDialog; public class MainActivity extends AppCompatActivity { LinearLayout l1,l2,l3; TextView tv1,tv2,tv3; private int currentcolor1,currentcolor2,currentcolor3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); l1 = findViewById(R.id.l1); l2 = findViewById(R.id.l2); l3 = findViewById(R.id.l3); tv1 = findViewById(R.id.tv1); tv2 = findViewById(R.id.tv2); tv3 = findViewById(R.id.tv3); } public void SelectColor1(View view) { openDialog(false); } private void openDialog(boolean supportAlpha1) { AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, currentcolor1, supportAlpha1, new AmbilWarnaDialog.OnAmbilWarnaListener() { @Override public void onCancel(AmbilWarnaDialog dialog) { //you can put some Toast also } @Override public void onOk(AmbilWarnaDialog dialog, int color) { currentcolor1 = color; l1.setBackgroundColor(currentcolor1); tv1.setText("#"+Integer.toOctalString(currentcolor1)); // Toast.makeText(MainActivity.this, ""+Integer.toHexString(currentcolor1), Toast.LENGTH_LONG).show(); Window window = getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { window.setStatusBarColor(color); } } }); dialog.show(); } public void SelectColor2(View view) { openDialog2(false); } private void openDialog2(boolean supportAlpha2) { AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, currentcolor1, supportAlpha2, new AmbilWarnaDialog.OnAmbilWarnaListener() { @Override public void onCancel(AmbilWarnaDialog dialog) { } @Override public void onOk(AmbilWarnaDialog dialog, int color) { currentcolor2 = color; l2.setBackgroundColor(currentcolor2); tv2.setText("#"+Integer.toOctalString(currentcolor2)); // Toast.makeText(MainActivity.this, ""+Integer.toHexString(currentcolor1), Toast.LENGTH_LONG).show(); Window window = getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { window.setStatusBarColor(color); } } }); dialog.show(); } public void SelectColor3(View view) { openDialog3(false); } private void openDialog3(boolean supportAlpha3) { AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, currentcolor1, supportAlpha3, new AmbilWarnaDialog.OnAmbilWarnaListener() { @Override public void onCancel(AmbilWarnaDialog dialog) { } @Override public void onOk(AmbilWarnaDialog dialog, int color) { currentcolor3 = color; l3.setBackgroundColor(currentcolor3); tv3.setText("#"+Integer.toOctalString(currentcolor3)); // Toast.makeText(MainActivity.this, ""+Integer.toHexString(currentcolor3), Toast.LENGTH_LONG).show(); Window window = getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { window.setStatusBarColor(color); } } }); dialog.show(); } }
build.gradle(Module: app)
Add this dependency into your build.gradle file: implementation 'com.github.yukuku:ambilwarna:2.0.1'
Output:
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment