I have an Android test app (because I am new to Android development) that has a couple of onClick secrets in it that changes the screen, in particular 2 that won't work. You have to find and click the first one to gain access to the other. Before I added this system, both of the secrets worked and the changes were successfully made with no problems.
My goal is to prevent people triggering the second method before triggering the first one. When the first one is triggered, the app allows the second method to be triggered.
The relevant Java code:
private boolean colorChangable = false;
public void changeSecret(View v) {
Button btn = (Button) findViewById(R.id.button);
btn.setText("Your mind has been blown!");
btn.setTextColor(Color.BLUE);
colorChangable = true;
}
public void changeColor(View v) {
if (colorChangable){
TextView tw = (TextView) findViewById(R.id.textView);
tw.setTextColor(Color.RED);
tw.setText("Again, your mind has been blown.");
}
}
And my relevant XML code:
<TextView
android:id="#+id/textView"
android:onClick="changeColor" />
<Button
android:id="#+id/button"
android:onClick="changeSecret" />
What is wrong with my code and is there anything that I can improve?
P.S. I cut some useless parts in the code, if there was something else
important needed to answer the question, please notify me.
P.P.S. This is different from the other questions about onClick not firing because in this problem, the onClick fires WITHOUT the boolean confirmation
Second method changeColor is being executed without needing of executing first method changeSecret. But note the if (colorChangable). This line avoids the rest of the method to be executed when colorChangeable variable is false by default:
private boolean colorChangable = false;
So will be false until first method changeSecret is executed...
I would recommend to add an else to see what is happening or explain what you want to achieve in each case:
public void changeColor(View v) {
TextView tw = (TextView) findViewById(R.id.textView);
if (colorChangable){
tw.setTextColor(Color.RED);
tw.setText("Again, your mind has been blown.");
} else {
tw.setText("You cannot change color.");
}
just add one line in your code
if (colorChangable){
TextView tw = (TextView) findViewById(R.id.textView);
tw.setTextColor(Color.RED);
tw.setText("Again, your mind has been blown.");
colorChangable=false //add this line
}
Related
This is my first time with android programming and I got stuck.
Now I'm trying to add view dynamically which contains toggle buttons, and edittext. However, whenever I select toggle button, options I created only works on last created view.
Options are simple. There are two toggle buttons and they can be clicked mutually exclusive
example
which means whenever I add new views such as B and C in above, the options are only worked on C while not in B. How can I make it to work on every view?
public void onAddField(View v){
LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView=inflater.inflate(R.layout.data_gledger_add_new,null);
tbg_add=(ToggleButton)rowView.findViewById(R.id.add_toggle_gledger);
tbc_add=(ToggleButton)rowView.findViewById(R.id.add_toggle_credit);
if(create_box<4){
csl.addView(rowView,csl.getChildCount()-1);
Log.d("create_box",String.valueOf(create_box));
create_box++;
}
else{
Log.d("create_box","full");
create_box=4;
}
tbg_add.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(tbg_add.isChecked()){
get_add_cla="menu1";
tbg_add.setTextColor(getResources().getColor(R.color.color_white));
tbc_add.setChecked(false);
tbc_add.setTextColor(getResources().getColor(R.color.color_black));
}
else{
get_add_cla="";
tbg_add.setTextColor(getResources().getColor(R.color.color_black));
}
}
});
//대변 선택
tbc_add.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(tbc_add.isChecked()){
get_add_cla="menu2";
tbc_add.setTextColor(getResources().getColor(R.color.color_white));
tbg_add.setChecked(false);
tbg_add.setTextColor(getResources().getColor(R.color.color_black));
}
else{
get_add_cla="";
tbc_add.setTextColor(getResources().getColor(R.color.color_black));
}
}
});
}
I forgot to mention that views are added by clicking button.
android:onClick="onAddField"
The problem almost certainly stems from the fact that you are re-using instance fields (tbg_add and tbc_add) as add new views dynamically.
tbg_add=(ToggleButton)rowView.findViewById(R.id.add_toggle_gledger);
tbc_add=(ToggleButton)rowView.findViewById(R.id.add_toggle_credit);
Because you are re-assigning these fields and also referencing them from the click listeners, you'll always be referencing the most recently created toggle buttons.
Change these to be local variables and everything should work fine.
ToggleButton ledger=(ToggleButton)rowView.findViewById(R.id.add_toggle_gledger);
ToggleButton credit=(ToggleButton)rowView.findViewById(R.id.add_toggle_credit);
Unrelated to your problem, but also something you should fix, is the fact that you're passing null as the second parameter to your inflate() call:
final View rowView=inflater.inflate(R.layout.data_gledger_add_new,null);
When you pass null in this manner, the system won't have any ability to correctly handle the LayoutParams (anything starting with android:layout_ in the xml file) for the newly-inflated view.
You know that you're going to wind up adding the rowView to your csl view, so you should pass that as the second parameter. Once you do that, you also have to pass false as a third parameter to make sure that the inflate() call actually returns the rowView and not its new parent (csl).
final View rowView=inflater.inflate(R.layout.data_gledger_add_new, csl, false);
What I need is for the textview to be added onto a linear layout where it will look organised and nice. Whenever the button is clicked again, it should replace the old textview and update it again.
At the moment, the button onclick listener will produce a textview, but I don't like it because it looks messy and unorganised.
I tried doing this:
varlinear.addView(varkebabTotal);
but it caused an error.
I have looked at other examples but they didn't explain how it will work with an onlick listener.
Here's the code for what happens when the button is clicked:
varKebab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = "Kebab Wrap";
if (menu.containsKey(name)) {
kebabquantity = list.get(name);
if (kebabquantity == null) {
kebabquantity = 0;
}
kebabquantity++;
list.put(name, kebabquantity);
//kebab qty calc
double kebabTotal = kebabquantity * menu.get(name);
overallTotal = overallTotal + menu.get(name);
varkebabTotal.setText(String.format("%s %s # £%s = £%.2f", kebabquantity.toString(), name, menu.get(name), kebabTotal));
varTotalPrice.setText(String.format("Total = £%.2f", overallTotal));
varlinear.addView(varkebabTotal); // this line caused an error
}
}
});
Edit:
The error I received is when I tested the app, and when I click the button, the app stops. It shuts itself down due to that one line: varlinear.addView(varkebabTotal);
The variables are as follows:
varkebabTotal = (TextView) findViewById(R.id.kebabTotal);
varTotal = (TextView) findViewById(R.id.TotalPrice);
varlinear = (LinearLayout) findViewById(R.id.myLinear);
one method you can try is to set varkebabTotal.setVisibility(View.GONE); when you initialize the TextView, and then on your onClick event, you can change it to varkebabTotal.setVisibility(View.VISIBLE);
i am developing/designing a quiz application in android, on the testActivity i have
a TextView which loads the questions and four answer(alternatives) buttons.
btnAnswer1, btnAnswer2, btnAnswer3, and btnAnswer4.
Problem:When i click on btnAnser1 as my answer, it should remain in selected state, however it should also be possible to unselect(normal state) it if i doubt the answer. and lastly if its selected, and i decide to go for btnAnswer2 whiles btnAnswer1 is selected, immediately i select btnAnswer2, btnAnswer1 should be unselected(normal) whiles btnAnswer2 is selected.
i hope my question is clear and it sounds quiet easy but am still new in android programming so will appreciate the help.
thanks.
optionone = (Button) findViewById(R.id.optionone);
optionone.setTag(1);
optionone.setId(i);
//optionone.setBackgroundColor(Color.parseColor("#F1F1F1"));
optionone.setBackgroundColor(Color.parseColor("#F1F1F1"));
optionone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setSelected(true);
checkAnswer();
final int change = (Integer) v.getTag();
if (change == 1) {
optionone.setBackgroundColor(Color.parseColor("#B2B2B2"));
back.setVisibility(View.VISIBLE);
next.setVisibility(View.VISIBLE);
v.setTag(0);
} else {
optionone.setBackgroundColor(Color.parseColor("#F1F1F1"));
back.setVisibility(View.GONE);
next.setVisibility(View.GONE);
v.setTag(1);
}
}
});
Try to use toggle buttons instead of buttons, or just change background color of buttons when they pressed, not state.
I have several strings of text on a screen that are set to invisible when the application starts. When a button is clicked on another screen, I want a specific string to become visible. Ultimately I want to have a few strings, of the several, become visible as a result of clicking this button.
public void buttona0Click(View view){
setContentView(R.layout.report_screen);
buttonClicked2 = 1;
if(buttonClicked1==1){
setVisibility(R.id.textView2.VISIBLE);
}
}
I am primarily looking for guidance on this line
setVisibility(R.id.textView2.VISIBLE);
I am new to programming in general, so I don't know if what I've said makes sense to most of you. Is .setText an alternative?
Write your own algo:
Use one boolean
view VISIBLE and GONE based own above boolean variable
You need to instantiate your TextView first. So the easiest way to begin is to declare them before onCreate() and outside of any other method so they are member variables
public class MyActivity extends Activity
{
TextView tv1, tv2, etc...;
public void onCreate(...)
{
super.onCreate(...);
setContentView(R.layout.my_layout);
tv1 = (TextView) findViewById(R.id.textView1);
...
}
Then in your onClick() change the Visibility which takes an int value
public void buttona0Click(View view){
buttonClicked2 = 1;
if(buttonClicked1==1){
tv1.setVisibility(View.VISIBLE);
}
}
Note: please don't include the "..." above in your code. That is just omitted code that I assume you know how to handle already. Also, I took out setContentView() from the onClick() method because typically this should only be done once in onCreate().
I'm not sure about the logic inside there because I don't know what the buttonClicked1 variables are for but that is how to do the Visibility.
setVisibility() Docs
Is .setText an alternative?
This will simply set the text so if you have it already set in your xml then you don't need to...you can just change the Visibility as you are trying to do. If you haven't set it then you will need to with something like
tv1.setText("Hello World"); // input your own String or String resource
Make sure you call setVisibility(View.GONE) for all your TextViews in onCreate() after setContentView(). Then in your if clause set the visibility of selected textview to View.VISIBLE - textview.setVisibility(View.VISIBLE)
First findViewById of the TextView and in onClick function of your button set visibility of that textView as VISIBLE
TextView textView = (TextView) findViewById(R.id.textView1);
public void buttona0Click(View view){
buttonClicked2 = 1;
if(buttonClicked1==1){
textView.setVisibility(view.VISIBLE);
}
}
I have an EditText field that represents an ID number. That field can either be filled programmatically, using IDField.setText(String) based on the results of a card swipe, or it can be filled manually using the keyboard.
Once the text is filled both methods (auto login--based on swipe, or manual--based on button click) both run the same sign in script. However when I go to grab the contents of the EditText field, if I edited the text manually I get an empty string returned. If the text was set programmatically then it works perfectly.
This doesn't make any sense to me. Is there a reason that editText.getText().toString() would not return the content that is visibly shown in the textbox?
XML:
<Button
android:id="#+id/btn_swipeCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/signInID"
android:layout_toLeftOf="#+id/textView1"
android:onClick="SignInStudent"
android:text="Swipe ID" />
Button Initialization:
IDField = (EditText) layout.findViewById (R.id.signInID);
LoginButton = (Button) findViewById(R.id.button1);
LoginButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { SignInStudent(); } } );
Card Swipe:
// displays data from card swiping
private Runnable doUpdateTVS = new Runnable()
{
public void run()
{
try
{
//Grab ID Number
String[] splitMSG = strMsrData.split("=");
//txtIDNumber.setText(splitMSG[2]);
IDField.setText(splitMSG[2]);
StringBuffer hexString = new StringBuffer();
hexString.append("<");
String fix = null;
for (int i = 0; i < msrData.length; i++) {
fix = Integer.toHexString(0xFF & msrData[i]);
if(fix.length()==1)
fix = "0"+fix;
hexString.append(fix);
if((i+1)%4==0&&i!=(msrData.length-1))
hexString.append(' ');
}
hexString.append(">");
myUniMagReader.WriteLogIntoFile(hexString.toString());
SignInStudent();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
};
Sign In Logic:
public void SignInStudent()
{
String temp = "http://wwww.tempUrl.com/signIn?ID="+ IDField.getText().toString() + "&DeviceId="+KEY;
webView.loadUrl(temp);
}
The layout is only updated during the onCreate phase of the loop. This is fired when an onResume event is called as well which explains why the fields update after you lock and unlock the device. There are a few workarounds for this such as doing more background processing and then creating a new view with correct values, or using a surfaceView that allows drawing to occur while the program is in its normal execute cycle.
For my application I either do background processing and then move to a new view, or have a view that just keeps calling itself to get the onCreate events to fire again. The solution depends on the application, but that's why the problem occurs.