I have the following code:
public class MainActivity extends Activity {
private BufferedReader br;
private Socket s;
private View v,v1;
private RelativeLayout rl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
s = new Socket("192.168.1.36",50000);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
}catch(Exception e){e.printStackTrace();}
color();
}
private void color(){
rl = (RelativeLayout) this.findViewById(R.id.rellay);
while(true){
try{
String received = br.readLine();
if(received != null){
// System.out.println(received);
String[] color = received.split(",");
setColor(color);
}
}catch(Exception e){e.printStackTrace();}
}
}
private void setColor(String[] color){
rl = (RelativeLayout) this.findViewById(R.id.rellay);
int red = Integer.parseInt(color[0]);
int green = Integer.parseInt(color[1]);
int blue = Integer.parseInt(color[2]);
int a = Integer.parseInt(color[3]);
rl.setBackgroundColor(Color.argb(a, red, green, blue));
What I want to do is receive 4 values separated by commas (this works), and I want the values in the range 0-255 to be the RGB color.
I want to change the background of the android activity. I can change the color once, from the onCreate method, but when I try to change it further times I get the default white screen. The values never exceed 255.
How can this be done? Thanks!!
Make sure the alpha value != 0.
The alpha value represent the transparency of the color so 0 is fully transparent.
try to start with fixed values, like:
rl.setBackgroundColor(Color.argb(255, 0, 0, 0));
and make sure it works.
after that, pull your values from the server and parse them into a color.
BTW, Listen to Nick Cardoso comments, It's a bad user experience to feel the network latency.
Related
I want to create a colored strobe effect on the screen using the below code but it didn't work. It's changed the screen color only one time.
int[] colorArray = new int[]{R.color.blue, R.color.purple, R.color.brown, R.color.red, R.color.green, R.color.valvet
, R.color.darkBlue, R.color.yellow, R.color.litegreen, R.color.orange, R.color.pink, R.color.pgreen,
R.color.liteBlue, R.color.divider, R.color.icons, R.color.colorAccent, R.color.colorPrimaryDark};
final Handler handler = new Handler() {
final Runnable runnable = new Runnable() {
int i=0;
public void run() {
multi_disco.setBackgroundColor(colorArray[i]);
i++;
if(i>colorArray.length-1)
{
i=0;
}
handler.postDelayed(this, 50);
}
};
handler.postDelayed(this, 2000)};
The flashlight from smartphones can't be colored just because the flash is created for the camera, and in 99,99% of the smartphones is just white.
So, what you are asking, cannot be done.
If you are talking about changing the main view of the app color try to create a loop that will
//generate random color
String letters = "0123456789ABCDEF";
String color = "#";
for (int i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
//now set the color of the view
v.setBackgroundColor(color);
Do this in a loop...
I want to create a single drawable that shows two lines of text, one above the other. Each line of text has to be in it's own typeface and textsize and it has to create a single drawable because I want to then set it as the drawable for a floating action button.
private void updateFloatingButtonText(String headlineText, String subHeadlineText, FloatingActionButton floatingActionButton) {
int headlineTextSize = getResources().getDimensionPixelSize(R.dimen.headlineTextSize);
int subheadlineTextSize = getResources().getDimensionPixelSize(R.dimen.subheadlineTextSize);
Spannable spannableStringHeadline = new SpannableString(headlineText);
Spannable spannableStringSubheadline = new SpannableString(subHeadlineText);
CustomTypefaceSpan boldSpan = new CustomTypefaceSpan("FontOne", FontCache.get("FontOne.ttf", this));
CustomTypefaceSpan regularSpan = new CustomTypefaceSpan("FontTwo", FontCache.get("FontTwo.ttf", this));
// set typeface headline
spannableStringHeadline.setSpan(regularSpan, 0,
headlineText.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE
);
// set typeface subtitle
spannableStringSubheadline.setSpan(boldSpan, 0,
subHeadlineText.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE
);
// set text size headline
spannableStringHeadline.setSpan(new AbsoluteSizeSpan(headlineTextSize), 0,
headlineText.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE
);
// set text size subline
spannableStringSubheadline.setSpan(new AbsoluteSizeSpan(subheadlineTextSize), 0,
subHeadlineText.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE
);
String finalString = TextUtils.concat(spannableStringHeadline, "\n", spannableStringSubheadline);
floatingActionButton.setImageDrawable([put the resulting drawable here]);
}
I've written this method that creates a single string formatted exactly the way that I need it, but I still have the issue of creating a drawable out of it.
I've tried to use this third party library, but although it displays the text in the correct typefaces it doesn't change the textsize of the lines of text.
https://github.com/devunwired/textdrawable
Is there a trivial (or nontrivial) way of doing this?
Solved by creating a new class that looks like this:
public class TextToDrawable extends Drawable {
private String headlineText = "";
private String subHeadlineText = "";
private final TextPaint headlinePaint = new TextPaint();
private final TextPaint subHeadlinePaint = new TextPaint();
public TextToDrawable(Context context, String headlineText, String subHeadlineText) {
this.headlineText = headlineText;
headlinePaint.setAntiAlias(true);
headlinePaint.setTypeface(FontCache.get("FontA.ttf", context));
headlinePaint.setTextSize(context.getResources().getDimensionPixelSize(R.dimen.headlineTextSize));
headlinePaint.setColor(ContextCompat.getColor(context, android.R.color.white));
headlinePaint.setStyle(Paint.Style.FILL);
headlinePaint.setTextAlign(Paint.Align.LEFT);
this.subHeadlineText = subHeadlineText;
subHeadlinePaint.setAntiAlias(true);
subHeadlinePaint.setTypeface(FontCache.get("FontB.ttf", context));
subHeadlinePaint.setTextSize(context.getResources().getDimensionPixelSize(R.dimen.subheadlineTextSize));
subHeadlinePaint.setColor(ContextCompat.getColor(context, android.R.color.white));
subHeadlinePaint.setStyle(Paint.Style.FILL);
subHeadlinePaint.setTextAlign(Paint.Align.LEFT);
}
#Override
public void draw(Canvas canvas) {
Rect headlineWidth = new Rect();
Rect subheadlineWidth = new Rect();
headlinePaint.getTextBounds(headlineText, 0, headlineText.length(), headlineWidth);
subHeadlinePaint.getTextBounds(subHeadlineText, 0, subHeadlineText.length(), subheadlineWidth);
Rect bounds = new Rect();
headlinePaint.getTextBounds(headlineText, 0, headlineText.length(), bounds);
int x = getBounds().width() / 2 - (headlineWidth.width()/2);
int y = (getBounds().height() / 2);
canvas.drawText(headlineText, x, y, headlinePaint);
x = getBounds().width()/2 - (subheadlineWidth.width()/2);
y += headlinePaint.getFontSpacing();
canvas.drawText(subHeadlineText, x, y, subHeadlinePaint);
}
#Override
public void setAlpha(int alpha) {
}
#Override
public void setColorFilter(ColorFilter colorFilter) {
}
#Override
public int getOpacity() {
return 0;
}
}
Then using the new class like this:
mFloatingActionButton.setImageDrawable(new TextToDrawable(this, "Headline", "Subheadline"));
This isn't a great solution because it only supports two lines of text - there's nothing dynamic going on here. However, I suppose it would be fairly easy to rewrite to support even more lines and more fonts and it solves the current problem.
I need to get current text color form TextView and then assign this value to TextView.setTextColor(). But I get a large int -1979711488138, how can I get a color from it?
Integer intColor = -1979711488138;
String hexColor = "#" + Integer.toHexString(intColor).substring(2);
or
String hexColor = String.format("#%06X", (0xFFFFFF & intColor));
Suppose you want to set color from textView1 to textView then you can do like this:
textView.setTextColor(textView1.getCurrentTextColor());
public class MainActivity extends Activity
{
private TextView txtViewIpLable,textView1;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
textView1.setTextColor(txtViewIpLable.getTextColors());
}
private void init()
{
// TODO Auto-generated method stub
txtViewIpLable = (TextView) findViewById(R.id.txtViewIpLable);
textView1 = (TextView) findViewById(R.id.textView1);
}
}
You cannot fit the number into an int, I get -1979711488 which is #8A000000 i.e. black with 138 alpha. You can get all parts of the color like this:
int color = getCurrentTextColor();
int a = Color.alpha(color);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
It's really strange why the default text color is not a solid color but rather black with an alpha value as that is more expensive for the system.
I have a Sudoku grid image loaded into my app, but I'm having a lot of trouble getting EditTexts to appear in each square. When I run the app, the numbers in the EditTexts are hardly visible. I've tried changing the background color and text color and nothing has changed.
/* PROGRAMATICALLY ADD EDITVIEWS via waiting for draw */
layout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener()
{
public void onGlobalLayout()
{
layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
/* not used yet
int height = grid.getMeasuredHeight();
int width = grid.getMeasuredWidth();
int top = grid.getTop();
//int bottom = grid.getBottom();
int left = grid.getLeft();
int square_height = height/9;
int square_width = width/9;*/
for ( int r = 0; r < 9; r++ )
{
for ( int c = 0; c < 9; c++ )
{
text_boxes[r][c] = new EditText(getApplicationContext());
text_boxes[r][c].setId(r+c);
text_boxes[r][c].setText("1");
text_boxes[r][c].setInputType(InputType.TYPE_CLASS_NUMBER);
text_boxes[r][c].setFilters(new InputFilter[] {new InputFilter.LengthFilter(1)});
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(c*10, r*50, 0, 0);
layout.addView(text_boxes[r][c], params);
text_boxes[r][c].setBackgroundColor(0x0FF00);
}
}
}
});
you didn't remove the listener when you are finished using it, so it can keep being called.
also, about the color of the EditText, you can change it to whatever you wish , by just changing the background of the EditTexts, or use a global style that sets a default background for EditTexts. you can even use this tool
How do i get the background color of a button.
In the xml i set the background color using ---- android:background = XXXXX
now in the activity class how can i retrieve this value that it has ?
Unfortunately I don't know how to retrieve the actual color.
It's easy to get this as a Drawable
Button button = (Button) findViewById(R.id.my_button);
Drawable buttonBackground = button.getBackground();
If you know this is a color then you can try
ColorDrawable buttonColor = (ColorDrawable) button.getBackground();
And if you're on Android 3.0+ you can get out the resource id of the color.
int colorId = buttonColor.getColor();
And compare this to your assigned colors, ie.
if (colorID == R.color.green) {
log("color is green");
}
private Bitmap mBitmap;
private Canvas mCanvas;
private Rect mBounds;
public void initIfNeeded() {
if(mBitmap == null) {
mBitmap = Bitmap.createBitmap(1,1, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mBounds = new Rect();
}
}
public int getBackgroundColor(View view) {
// The actual color, not the id.
int color = Color.BLACK;
if(view.getBackground() instanceof ColorDrawable) {
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
initIfNeeded();
// If the ColorDrawable makes use of its bounds in the draw method,
// we may not be able to get the color we want. This is not the usual
// case before Ice Cream Sandwich (4.0.1 r1).
// Yet, we change the bounds temporarily, just to be sure that we are
// successful.
ColorDrawable colorDrawable = (ColorDrawable)view.getBackground();
mBounds.set(colorDrawable.getBounds()); // Save the original bounds.
colorDrawable.setBounds(0, 0, 1, 1); // Change the bounds.
colorDrawable.draw(mCanvas);
color = mBitmap.getPixel(0, 0);
colorDrawable.setBounds(mBounds); // Restore the original bounds.
}
else {
color = ((ColorDrawable)view.getBackground()).getColor();
}
}
return color;
}
You can also try something like set the color value as the tag like
android:tag="#ff0000"
And access it from the code
String colorCode = (String)btn.getTag();
The simpliest way to get the color for me is:
int color = ((ColorDrawable)button.getBackground()).getColor();
Tested and working on Lollipop 5.1.1
To get the background Drawable, you use
public Drawable getBackground();
as defined in the base View class.
Don't forget that the Button can have a background that is an image, a color, a gradient. If you use android:background="#ffffff", the class of the background will be
android.graphics.drawable.ColorDrawable
From there you can simply call
public int getColor()
Try this:
list_view.getChildAt(position).setBackgroundColor(Color.YELLOW);
ColorDrawable corItem = (ColorDrawable) list_view.getChildAt(position).getBackground();
if(corItem.getColor() == Color.YELLOW){
Toast.makeText(NovoProcessoActivity.this,"Right Color!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(NovoProcessoActivity.this,"Wrong Color!", Toast.LENGTH_SHORT).show();
}
or
int color =( (ColorDrawable) list_view.getChildAt(position).getBackground()).getColor();
int colornumber=((ColorDrawable)v.getBackground()).getColor();
This is the best and simple way to get the color of a View (Button, TextView...)
To get set color to a View using java we use
v.setBackgroundColor(getColor(R.color.colorname_you_used));