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
Related
This is my Screen layout, and i want to do this, when I clicked on Popular tab, then Popular block should turns Blue.
Same for the Price, Time and Duration tab.
I've tried to do with Linear Layout but it doesn't.
i have taken it in Text View, and the vertical and Horizontal Lines are used using View tag and I have not use TabLayout, just to make it simple I just used TextView With Background.
Please suggest me a proper solution with code if you can.
Any help would be Appreciated.
In your xml file add following line in your TabLayout,
app:tabBackground="#0000FA"
by this you can chage color of selected tab.
If you have used TextView then just simply create or add this method to you java file & call it from each textviews onclick event by passing the textview to it.
public void changeTabColor(TextView tvSelected){
tv1.setBackgroundColor(Color.White);
tv2.setBackgroundColor(Color.White);
tv3.setBackgroundColor(Color.White);
tvSelected.setBackgroundColor(Color.Blue);
}
try this :
public void onClick(View v) {
switch (v.getId()) {
case R.id.popular_tab:
ResetTabColor();
popular_tab.setBackgroundColor(Color.blue);
case //Do the rest with other tab
}
}
private void ResetTabColor(){
popular_tab.setBackgroundColor(Color.TRANSPARENT); // or white color
time_tab.setBackgroundColor(Color.TRANSPARENT);
duration_tab.setBackgroundColor(Color.TRANSPARENT);
price_tab.setBackgroundColor(Color.TRANSPARENT);
I'm looking at the documentation here:
http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents
And all their examples seem to overlook the developer's potential desire to totally alter the appearance and/or positioning of the OK | Cancel buttons.
I feel like the solution with the DialogFragment comes close, but still there's no obvious way for me to define what View in my activity should BE the OK button, so that I can easily attach the callback to it.
What am I missing? Or is it really not possible to manipulate the appearance and position of the buttons in anything that extends AlertDialog or DialogFragment?
You can use Dialog and add your own custom layout to it and control the appearance of every view in it. Here is an example how you can do this :
final Dialog alert = new Dialog(FingerPaintActivity.this, android.R.style.Theme_Light_Panel);
alert.requestWindowFeature(Window.FEATURE_NO_TITLE); // no title
alert.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation; // this is used for custom animation when dialog is showing and hiding
alert.setContentView(getLayoutInflater().inflate(R.layout.stamps, null)); // here is your custom layout
alert.getWindow().setLayout(width-50, (height-100)); // set height / width
and you can use set listeners to your views (for example a button) like this :
Button myOkBtn = (Button) alert.findViewById(R.id.myOkBtn);
myOkBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
I'm using a custom view expanded from a XML layout in a horizontal scroll view as a sort of horizontal image list but I'm not sure how to get them to appear clickable/tappable (ie they highlight when tapped) or how to capture these events. I think I've tried setOnClickHandler without it working. I'm also trying to get a simple TextView to do the same. I've also tried setting android:clickable="true" but that hasn't helped either. Any ideas?
To take care of the visual feedback use an xml Selector, and set it as the View's background.
To handle click events use
mView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//your code here
}
});
In my calculator app, I want an Inverse button, which when clicked changes the text of other buttons. Like sin to sin inverse etc. Is this possible?
You can just change the text of a button in that button onclick event.
Say For example
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(btn.getText().toString().trim().equals("sin")){
btn.setText("sin inverse");
}else if(btn.getText().toString().trim().equals("sin inverse")){
btn.setText("sin");
}
}
});
I think this will help you
While the other answers are completely right showing you to how rename the buttons, I suggest another solution with a cleaner design: Have different buttons for "sin" and "sin inverse", and just make them visible/invisible when clicking the "Inverse" button. That way you can write clean click handlers and don't have to use a lot of "if (isInverseMode()...)".
To make that work correctly, you just declare some additional buttons for the inverse operations in your XML layout file and set them to android:visibility="gone".
If you then set one the visible buttons to invisible and the next insivible button besides it to visible in the code, then the effect for the user looks like you exchanged one button by the other (so he only notices the text of the button changing).
It's possible.
Just re-set the button text in your onClick method for the button.
buttonId.setText("Your button text");
From what you're saying though it sounds like you want to change the buttons function as well as the text... in which case you need to put an if statement in your onClick method to handle the two button states.
Setting the text on a button would help anytime.
This code may help you..
final Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setText(new StringBuilder(btn.getText().toString().trim()).reverse());
}
});
In my Android application, I have an activity with three layouts: A left layout, a middle layout, and a right layout.
The right layout is the main one. I want the right layout to zoom into fullscreen when I click a button. If you have the specific code,it's better.
Thanks very much!
If I got your question right:
Your button's OnClickListener should look like something like this:
OnClickListener listener = OnClickListener() {
public void onClick(View v){
findViewById(R.id.left_layout).setVisibility(View.INVISIBLE);
findViewById(R.id.center_layout).setVisibility(View.INVISIBLE);
};
This by itself will not get your right layout to fullscreen. You'll need to add something the below to your right layout for it to "auto fullscreen":
android:layout_width = "0dp";
android:layout_weight = "1";
if you don't do that, you'll have to resize the right layout from the "listener" above.
Hmm, the question is vague, but if I understand correctly, what you want to do is hide the left and center layouts when a button is clicked, so that the right layout becomes full-screen?
Without more details, it's not easy to give you a more precise answer (please post your current layout), but I would do something like:
// this code in your Activity:
// this method is bound to button onCLick (android:onClick="clickButton")
public void clickButton(View view) {
findViewById(R.id.left_layout).setVisibility(View.INVISIBLE);
findViewById(R.id.center_layout).setVisibility(View.INVISIBLE);
}
And to revert back to a 3-layout display, you do the opposite (View.VISIBLE)