Android: How to make background darker or lighter? - android

I want my app to be able to use an onSwipeListener, which I have coded, to make the background's color darker or lighter based on the direction of their swipe. If the user swipes right, it gets brighter and left, it gets darker.
view.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {
#Override
public void onSwipeLeft() {
//Make Darker
}
#Override
public void onSwipeRight(){
//Make Lighter
}
is my code, but I'm having trouble understanding how I would change the color outside of radio buttons.

This question has been answered in part before so here's the link explaining onSwipeTouchListener
I know this doesn't explain changing the background of the view that can be simply done in this example:
MainActivity.java
activity_main.xml

Related

Android Placing Text (bar) at bottom of Application

I want to be able to display text (a bar if you will) at the bottom of my application dynamically to indicate if the the application is online or not (an endless issue for users currently). I don't need for this to have any action, but I do need it to be able to control it displaying or not based on them toggling between online and offline mode. So the split action bar is not what I am looking for.
Not enough reputation points to post an example. Doh. Here is the link:
Ugly example, look at bottom
Something simple is fine - I am good with XML based or dynamic (though I will also need to hide and show it dynamically).
Thanks!
Dynamic solution. Define your method that the activity will use to hide / display your bottom TextView (text bar?).
In your XML:
<TextView
android:id="#+id/text_bar"
...
...
android:visibility="gone"/>
In your activity:
public MyActivity extends Activity{
private TextView mTextBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity_xml);
...
...
mTextBar = (TextView) findViewById(R.id.text_bar);
}
public void updateVisibility(boolean visible){
int visibility = (visible) ? View.VISIBLE: View.GONE;
mTextBar.setVisibility(visibility);
}
}
Initially, the TextView (your text bar) has it's visibiity set to gone. Dynamically, when you want to show it call the updateVisibility(true) method to show it, and updateVisibility(false) to hide it.
Since I'm not sure how you are handling the check for the network status (in queries, in the onCreate, etc). I don't know what you intended to do for the TextView. If you need advice on how to position the TextView on the bottom of the screen I can provide example code for that as well.

How do I partially gray out an image when pressed?

I have an image that I use as a button. It is an ImageView within a RelativeLayout. The RelativeLayout is the View that receives the touch events. While touched, I want to partially gray out the image as feedback. How do I do this?
The best way I can think of is to place another View with a black background on top of the ImageView and style it normally 100% transparent, but only 50% transparent when touched. Is there a way to style the RelativeLayout or ImageView directly without using an additional View?
Thanks in advance...
One way you could do it would be to add an onclick method for the ImageView and set a tag for it once it's grayed out and a ColorFilter to gray the view. Then you can un-gray it (provided you want the gray out to be toggled) when clicked again. Here's an example:
Add this to the xml:
<ImageView
...
android:onClick="grayOut"/>
Then in the Activity or View class
public void grayOut(View view) {
// if not grayed
if(view.getTag() != "grayed") {
view.setColorFilter(Color.argb(150,200,200,200));
view.setTag("grayed");
} else {
view.setColorFilter(null);
view.setTag("");
}
}
Use a color filter with a transparent grey color on the view when touched and remove it when you want to remove the grey color. It will be imageView.setColorFilter(transparent grey color); When you want to remove it just set the color filter to null.

android custom togglebutton border

I have a grid of 64 togglebuttons in an 8x8 form. when the app runs, it sets each togglebutton's background/drawable to a colour. this is done in the program, not the xml. my problem is that while there seems to be space between the buttons on the graphical layout of the app, when the app runs, and changes the buttons to a colour, the space disappears. this makes the buttons look like a single plate, with no definition between buttons.
What I want is to put a border on the buttons to make it clear where each button is.
the function of the buttons is to be used to indicate a musical note being pressed. when the button is pressed, the colour of the button turns from grey to light blue, and adds the note to a sequence.
So i need to be able to put a border on a togglebutton with a custom design, that changes when activated/deactivated, keeping the border in both states. i have also tried setting the max width and max height, and also setting the padding on the buttons in an attempt to seperate them.
an example of the code is as follows:
public class MainActivity extends Activity implements OnClickListener
{
ToggleButton tg1;
....
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
....
tg1 = (ToggleButton) findViewById(R.id.toggleButton1);
tg1.setOnClickListener(this);
....
tg1.setBackgroundColor(0xffcccccc); //set button grey
}
public void onClick(View v)
{
if((tg1.isChecked()))
{
tg1.setBackgroundColor(0xff00ffff); //set button blue
}
else
{
tg1.setBackgroundColor(0xffcccccc); //set button grey
}
}
}
any help is greatly appreciated.
Right way: do not call isChecked manually to change background. Use selector. You can draw 9-patch with a border or create shape (use stroke to set border color and solid to set fill color).
Or create a custom togglebutton to support desired functionality.

How to properly move a view?

Can someone please explain to a noob the correct way to animate a View so its touch area and image actually move together?!
I have read lots of posts and questions and tutorials, but none explains what moves the layout and what moves the image such that I can animate a view and then leave it at its new position.
This is an example method I'm working with, trying lots of different combinations to no success. The view is in the parent RelativeLayout. It's a touchable menu of icons, and is animated with an xml resource on a click to slide off screen leaving just a little tab showing, where it needs to stay until clicked again.
public void RetractTools (View v){
final ImageView finalImage1 = (ImageView) findViewById(R.id.paintsView);
Animation slideout = AnimationUtils.loadAnimation(this, R.anim.slideout_tools);
slideout.setFillAfter(true);
slideout.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
finalImage1.setEnabled(true);
optionMenu.showing = false;
optionMenu.inMotion = false;
finalImage1.layout(1258, 668, 1697, 752);
finalImage1.setRight(1280);
finalImage1.invalidate();
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationStart(Animation arg0) {
finalImage1.setEnabled(false);
}
});
optionMenu.inMotion = true;
v.startAnimation(slideout);
}// End RetractMenu
No matter what I try, I encounter problems. setFillAfter does nothing when set in the xml file. Set programmatically, it leaves the image in the right place but the touch controls remain where the menu was. I have tried setLeft and setRight which apparently only move the image, not the view position, and all sorts of different layout options, and fill and no fill and invalidating and not, but can't solve it. I clearly don't undersatnd the underlying mechanics needed to position and render a view! :D
Thanks.
EDIT : Solution
For anyone having similar issues, this is how I have found to work with relative layouts. You create a LayoutParams object with the specified size, and then you can assign it positions. eg.
final RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
position.leftMargin = 440;
Then assign that to your view
myView.setLayoutParams(position);
So in summary, you use a LayoutParams object as an interface to your view's position, rather than accessing the view's coordinates directly as I assumed.
What you have is basically fine, with two flaws:
You are using setFillAfter(), which is not especially useful
You are calling layout() and setRight() and stuff, which is not especially effective
Instead, in onAnimationEnd(), you need to modify the LayoutParams of the View to reflect the new position you want the widget to be in. The size and position of a widget is dictated by the layout rules it negotiates with its container. Initially, those are set via your layout XML resource. By modifying the LayoutParams at runtime, you are changing what those rules are.
What those LayoutParams are (LinearLayout.LayoutParams, RelativeLayout.LayoutParams, etc.) and what values you should specify in them, we cannot tell you, because we don't know what you are doing.

Android onClick(View v) not working?

I'm new to Android. I'm stuck at a point and would really appreciate it if anyone could please help me. I'm developing an app which has a grid of colored rectangles. These are created by changing the background colors of a number of TextViews. There are 3 Buttons which cause the background color to change according to some algorithm. There are also 2 TextViews which show the current status of the game.
Now the problem is I have a button example (one of the three buttons) which is supposed to change the background color of the rectangles.
example.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
status_val.setText("true board-example working");
level_1_true();
}
});
The level_1_true() method sets the background color of the rectangles. The above code results in an "activity not responding" dialog being shown, and the OnClickListener does not change the view. Someone suggested I try the runOnUIThread method in activity, but I can't get it to do what I want it to do.
How do I change the view of the screen by clicking the button?
Maybe you can try adding in your button xml code android:onClick="onClickMethod" and adding in your activity
public void onClickMethod(View v) {
status_val.setText("true board-example working");
level_1_true();
}
How did you define status_val? You remeber to link it like this?
status_val=(TextView)findViewById(R.id.);
Please tell me if I can be of more help

Categories

Resources