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.
Related
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
Hello my name is Fabian and at the moment I try to programming an Android App.
I have a ListView with some items and above this ListView I added a LinearLayout, which I want to make clickable, to add items to the ListView.
If I touch one entry in the ListView the item gets colorized with the default color, a light grey. I like to have this behaviour for my LinearLayout, too.
I know how to get the LinearLayout clickable.
I did this by
android:clickable="true"
android:onClick="addProject"
Also I know how to define the backgroundcolor, but I don't know how I can pass the default colors from android of the ListView (android:listSelector) to the LinearLayout.
I tested to define
android:background="?android:attr/listChoiceBackgroundIndicator"
but in that case the LinearLayout gets colorized blue and only if I touch it, it gets the right color.
I hope you can help me, to pass the default color of a ListView to a LinearLayout.
Remove below line from your LinearLayout
android:onClick="addProject"
and add an id to a LinearLayout,
android:id="#+id/linear_layout"
From JAVA add this,
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout);
layout.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Toast.makeText(this, "Layout Clicked", Toast.LENGTH_LONG).show();
}
});
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.
I've a border set around a drawable using LinearLayout (bg: rounded rectangle).
The drawable and the border is used as a tab view. I'd like to change the border color of the tab when it is selected.
How can I do this?
Color state list doesn't seem to work as the view being selected is not the shape (i.e border) but the tab. Drawable state list doesn't seem to work either as I'm trying to swivel between views, not drawables. Moreover, I can't find any "onSelectedListener" of the sort...
You need to make an OnClickListener to handle clicks, and get it to change the appropriate background:
private OnClickListener tabClick(Context mContext){
#Override
onClick(View v){
v.setBackgroundResource(R.drawable.active);
}
}
myTab.setOnClickListener(tabClick);
The solution is to call setOnTabChangeListener in the TabHost and then change everything manually.
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