I've created a method which adds a number of TextViews to a LinearLayout in a while loop. I'm having trouble identifying those TextViews by click. If I click on them, it prints out that they are the ID of my linear layout. Is there any way I can access them?
while(i.hasNext()){
TextView x = new TextView(this);
linearLayout.addView(x);
}
EDIT:
I understand this was an absurdly dumb question.
This...
x.setOnClickListener(this);
...did the trick.
Simple, you will get the reference of clicked view in OnClickListener() callback. type cast that view into textview and call getText() method of textview.
Here is sample code:
while(i.hasNext()) {
TextView x = new TextView(this);
linearLayout.addView(x);
x.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TextView tv = (TextView) v;
v.getText();
}
});
}
if you'd be using a for loop you could do something like this x.setId(i+30); any number in place of 30. In while you can still take a variable i and do something like x.setId(x++ +30); . now if you want to access them from a loop its simple findViewById(i+30); or else findViewById(31);
Related
first sorry for my english and my knowledge, I'm a french beginner.
I would like to set the visibility of each of my four Textview according to four buttons (one button for one Textview). So I've the next code, but when I click on the button, the apps is closing. I'm watching the debug, but I don't understand what's not working.
Thank you for all your helps !
public void onClick(View v) {
TextView mWhoAreWeTextView1 = (TextView) v.findViewById(R.id.who_are_we_text_1);
TextView mOurSchoolTextView1 = v.findViewById(R.id.our_school_text_1);
TextView mWhatDoWeDoTextView1 = v.findViewById(R.id.what_do_we_do_text_1);
TextView mComeInTextView1 = v.findViewById(R.id.come_in_text_1);
switch (v.getId()){
case R.id.about_part_1:
if (mWhoAreWeTextView1.getVisibility() == View.VISIBLE){
mWhoAreWeTextView1.setVisibility(View.GONE);
}
else {
mWhoAreWeTextView1.setVisibility(View.VISIBLE);
}
case R.id.about_part_2:
break;
case R.id.about_part_3:
break;
case R.id.about_part_4:
break;
}```
You need to know that every initialize of View better to do in the onCreate function
Make sure you are implementing View.OnClickListener
You are using View.GONE that removing the textview from the screen and not only stop from displaying the textview
To do that you need to use View.INVISIVLE
Your code is ok (the code that check the visibility of the view) but you forgot to do the most important part and it's to initializing and calling the buttons like this for example
Button button1example = findViewById(R.id.button1);
button1example.setOnClickListener(this);
I have a ListView in my app.
Its items have a Button and an invisible text field; I want to make the text field visible when the Button is pressed.
How can I do it from the onClick(View v) method of OnClickListener's object?
Maybe it's something like "getParent().findViewById()", but getParent() doesn't return a View..
You could set the tag of the button view to the TextView.
Then you can use v.getTag() to get it back, then do what you want with it.
Otherwise, make the TextView final in your adapter getView method after finding it , then simply use it within the click event
If you want to visible the textView
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn:
yourtextView.setVisibility(View.VISIBLE);
break;
}
}
try this
View view = listview.getAdapter().getView(0,null,null);
TextView textView = view.findViewById(R.id.textView);
In the code below, the first TextView symbol cannot be resolved, and the findById method cannot be resolved. Can someone explain to me what the problem is and how I can fix it?
final TextView factLabel = (TextView) findViewById(R.id.factTextView);
Button showFactButton = (Button) findById(R.id.showFactButton);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
String fact = "";
// Randomly select a fact.
// Update the label with our dynamic fact
factLabel.setText(fact);
}
};
You need to give more background. Are you in a fragment, or an activity? How was the layout set? I'm guessing the answer is that you need to be calling findViewById() on the View of your layout - e.g. view.findViewById() but it depends on how your layout was set.
If you're in an activity, calling findViewById() on the Activity object will only work if the current Activity layout is set by setContentView. If your layout was set a different way, then you need to get the View object of the layout and call findViewById() on it. If you're in a fragment, and you're in onCreateView() then the view has been passed in for you and you just need to call view.findViewById()
I am asuming that you want some text to set on text view on click of button try this
final TextView factLabel = (TextView)findViewById(R.id.factTextView);
Button showFactButton = (Button)findViewById(R.id.showFactButton);
showFactButton.setOnClickListener(new View.onClickListener()
{
#Override
public void onClick(View v) {
String fact = "Your Text Here";
// Randomly select a fact.
// Update the label with our dynamic fact
factLabel.setText(fact);
});
Make sure you are importing all the packages you need. Also make sure all the dependancies are working. Lastly make sure that there is a text view in the XML for the activity. Hooe some of this is helpful.
I just started learning android programming and can't seem to work out how to permit the user to create a selected number (n) of EditText fields by clicking a button n times such that each would be uniquely identified, namely 1,2,3...,n as well as accessible programatically, from a different method (invoked by a different button click). I hope the question is clear enough as I don't really have that much code to provide.
Do something like this
// A list to keep reference to your created edit texts
List<EditText> mEditTexts = new ArrayList<EditText>();
// Get root view of your activity
ViewGroup viewGroup = (ViewGroup) ((ViewGroup)
findViewById(android.R.id.content)).getChildAt(0);
// Get the button and set a click listener to it
Button mButton = (Button) findViewById(R.id.button_id);
mButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
// Build edit text
EditText mEditText = new EditText(v.getContext());
// Pass two args (arg1/arg2); must be LayoutParams.MATCH_PARENT,
// LayoutParams.WRAP_CONTENT, or an integer pixel value.
mEditText.setLayoutParams(new LayoutParams(arg1, arg2));
// Add the edit text to your list
mEditTexts.add(mEditText);
// Add edit text to your root view
viewGroup.addView(mEditText);
}
}
To check your edit text fields you can then access them from the list
for(EditText editText : mEditTexts){
Log.d(TAG, editText.getEditableText().toString());
}
or explicitly
int specificPosition = (SOME_INT);
EditText specificEditText = mEditTexts.get(specificPosition);
Haven't been able to test it so it might need some modifying but it should be something along those lines. You can also use your layout directly if you don't want to use the viewGroup. Modify it to something like
LinearLayout mLayout = (LinearLayout) findViewById(R.id.layout_id);
....
mLayout.addView(mEditText);
Right now I have an EditText with id "getUserName" and a button next to it (both in a linear view) with id "setName"
I want someone to be able to click setName, and have the EditText field disappear, the button disappear, and a TextView take it's place. Here's what I have thus far:
public void setName(View view){
EditText editText = (EditText) findViewById(R.id.getUserName);
Button button = (Button) findViewById(R.id.setName);
TextView textView = (TextView) findViewById(R.id.displayName);
String playerName = editText.getText().toString();
((ViewManager)editText.getParent()).removeView(editText);
((ViewManager)button.getParent()).removeView(button);
Log.d("ScoreKeeper", playerName);
}
So I am successfully removing the desired elements from the screen, but I don't know how to add the textView to take their place.
How can I do that? I'm brand new to Android, so forgive me if this seems ignorant. I've tried looking it up!
Thanks
OPSRCFTW
You can simply hide the EditText, Button and TextView using turn visibility on.
You can add textview in your xml file and keep it invisible..
On button click, just change its visibility...
So the code is on buton click like below:
textview.setVisibility(View.VISIBLE);
edittext.setVisibility(View.GONE);
button.setVisibility(View.GONE);
First -> make ur textview Gone,
textview..setVisibility(View.GONE)
when u click the button..
Second -> Make
`Make the EditText and Button GONE with` `edittext.setVisibility(View.GONE);` and make textview visible textview..setVisibility(View.VISIBLE)
What about starting with
textView.setVisibility(View.GONE);
and then set an OnClickListener to your button:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setVisibility(View.VISIBLE);
}
});
Write code onCreate method of your class
EditText editText = (EditText) findViewById(R.id.getUserName);
Button button = (Button) findViewById(R.id.setName);
TextView textView = (TextView) findViewById(R.id.displayName);
textView.setVisibility(View.GONE);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
editText.setVisibility(View.GONE);
button.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
}
});
Hope it will help you.
You can also dynamically create the text view , like textview view= new textview(context); set the height and width thru layout params; and then add this view to parent view or pare layout like parent view.addview(textview). Change the visibility of the button and edittext rather than totally removing them.
on startup
textView.setVisibility(View.GONE);
on button click
textview.setVisibility(View.VISIBLE);
edittext.setVisibility(View.GONE);
button.setVisibility(View.GONE);