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);
}
}
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);
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
}
I am developing a android calculter and I want to add the number the user clicks when he clicks its button
i added the onclick like this
android:onClick="number(1)"
the number inside the brackets is the number that i want it to be appended inside the EditText
and the code of the main activity and the function is
public void number(int medo){
EditText result = (EditText) findViewById(R.id.result);
result.setText(result.getText() + " " + medo);
}
but when i click any button while testing the app it crashes so may anybody tell me how to fix this but fast please
Your method needs to have a View parameter, which will contain the button you clicked.
So, your code could look like this:
public void number(View v) {
EditText result = (EditText) findViewById(R.id.result);
// get the text from the button that was clicked and add it to the EditText
result.setText(result.getText() + " " + ((Button)v).getText().toString());
}
You can't set the argument in XML. It should look like this:
android:onClick="number"
Also i think you have to call toString.
result.getText().toString()
the onCLick should be a method that has a view parameter in it.
this View represents the View (in this case Button) was clicked.
you method should be like that:
public void number(View view)
to solve your problem i will suggest putting android:tag in the xml.
for the Button you showed you will do:
android:tag="1"
android:onClick="number"
and in the number function you will do something like that:
public void number(View view)
{
result.setText(result.getText() + " " + view.getTag().toString);
}
in addition, i will highly suggest to make result to a global variable and initialize it in the onCreate() instead, it will make the code better and efficient
I am a new android dev. I want to use a same TextView in all activity. But, i don't know how to declare TextView as global Variable & How can i use this to show text in activity. Please help me. The code is simple.
Thank for every one.
Write your XML code of text view with id as:
<TextText
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Than in your activity declare it before onCreate() method as:
public static TextView textview = (TextView) findViewByID(Your ID);
Than this will be accessible to all calsses.
You can use this everywhere:
TextView textview = (TextView) findViewByID(Your ID);
textview = (TextView) findViewByID(Your ID);
Make it a singleton. Or just keep one public static reference.
public class MyReference {
public static TextView myTextView = new TextView();
}
and then you can use it anywhere by calling MyReference.myTextView
I think this Link this is helpfull to
http://alinberce.wordpress.com/2012/02/20/android-edittext-with-custom-font-and-clear-button/
you have to create Editbox just One time & use anywhere in Application
weather use in Layout file or use Dynamically in java Class
I'm creating an EditText in onOptionsItemSelected() and trying to get it's information in onClick(). Here's the offending code:
onOptionItemSelected(MenuItem item){
...
EditText mealCalories = new EditText(context);
mealCalories.setId(MealCalId) //in this example it's just an integer 1.
...
}
onclick(View v){
EditText mealCaloriesInBox = (EditText)findViewById(mealCalId);
}
When I haven't selected an item from the menu (and thus haven't called onOptionItemSelected();) it doesn't crash when I click the button. However, when I actually have created the EditText and I click the button it crashes as it's trying to create the instance, giving me the aforementioned error. Any ideas on why it could be doing that?
EDIT
Here's more of my code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Context context = getApplicationContext();
switch(item.getItemId()) {
case R.id.addMeal:
trackMealItems++;
mealCalId++;
mealFatId++;
mealCarbId++;
mealProteinId++;
//the base layout
LinearLayout root = (LinearLayout)findViewById(R.id.linearLayout1);
//make the layout that holds the meal item and add it to the base layout
LinearLayout mealItem = new LinearLayout(context);
mealItem.setId(trackMealItems);
mealItem.setOrientation(LinearLayout.VERTICAL);
mealItem.setLayoutParams(mealItemParams);
root.addView(mealItem);
//make the TextView that holds the name of the meal and add it to the mealItem layout
EditText mealName = new EditText(context);
mealName.setLayoutParams(mealNameParams);
mealItem.addView(mealName);
//make the TextViews that hold the information about the meal and stick them in a
//horizontal LinearLayout
LinearLayout mealStats = new LinearLayout(context);
mealStats.setOrientation(LinearLayout.HORIZONTAL);
mealStats.setLayoutParams(mealStatParams);
mealItem.addView(mealStats);
EditText mealCalories = new EditText(context);
mealCalories.setId(mealCalId);
mealCalories.setLayoutParams(mealStatParams);
mealStats.addView(mealCalories);
EditText mealFat = new EditText(context);
mealFat.setId(mealFatId);
mealFat.setLayoutParams(mealStatParams);
mealStats.addView(mealFat);
EditText mealCarbs = new EditText(context);
mealCarbs.setId(mealCarbId);
mealCarbs.setLayoutParams(mealStatParams);
mealStats.addView(mealCarbs);
EditText mealProtein = new EditText(context);
mealProtein.setId(mealProteinId);
mealProtein.setLayoutParams(mealStatParams);
mealStats.addView(mealProtein);
return true;
case R.id.removeMeal:
LinearLayout removeMe = (LinearLayout)findViewById(trackMealItems);
removeMe.setVisibility(View.GONE);
trackMealItems--;
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v){
EditText mealCaloriesInTextBox = (EditText)findViewById(mealCalId);
}
You seem to be using two different values: MealCalId when you create your EditText and mealCalId when you call findViewById. That's one possible problem. The other is that if you have more than one view with the same id, findViewById will not necessarily return the one you want.
EDIT
At first glance, your code looks like it should work. I don't know what's going wrong, but I have a suggestion for a work-around. When you create the view, instead of assigning it an ID, assign it a tag:
mealCalories.setTag(mealCalId);
(The int value will be autoboxed to an Integer.) Then in your onClick handler, retrieve it by tag:
EditText mealCaloriesInTextBox =
(EditText) getContentView().findViewWithTag(mealCalId);
If there's any kind of funny interaction with view IDs, this technique will avoid them.
If that doesn't work (or if you prefer anyway) you can also try diagnosing the ID-based retrieval using the Hierarchy Viewer.
For those who are having this error for the same reason I did....
Just try cleaning your project and re-building.
Solved it for me.
i tried to run your code and what i found is that
when menu item is not clicked and button is clicked, the edit text is null.
So if you will call any method on this object, it will crash with NULLPointerException
When menu item is clicked and then button is clicked, the edit text is not null so you call any method on this object.