Move programatically added TextView back and forth between layouts onClick - android

I have this FlowLayout where I have a set of TextView's which I build programatically. After getting the wanted names, I create a TextView for each name inside the layout.
What I want to do, if I click on the TextView, I want to move it into another layout. I manage to do that but I also want to move it back. I could also do that until I program it to, but I can't program it to be like a infinite loop.
This is a piece of code which will make you understand better what I'm talking about hopefully.
TextView tv = new TextView(new ContextThemeWrapper(getActivity(), R.style.FlowLayoutTextView));
tv.setText("Test");
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tvSelected = new TextView(new ContextThemeWrapper(getActivity(), R.style.FlowLayoutTextView));
tvSelected.setText(tv.getText().toString());
tvSelected.setLayoutParams(params);
tv.setVisibility(View.GONE);
filteredLayout.addView(tvSelected);
}
});
unfilteredLayout.addView(tv);
Is it possible to make it work? Thanks.
LE: As you can see in the onClickListener event of the TextView, I create the other TextView I add in the other layout, to move it back I could also add an onClickListener event to this TextView but this is not the solution.

Try following:
boolean isInFilterLayout = false; //Class variable
TextView tv = new TextView(new ContextThemeWrapper(getActivity(), R.style.FlowLayoutTextView));
tv.setText("Test");
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isInFilterLayout){
filteredLayout.remove(tv);
unfilteredLayout.addView(tv);
isInFilterLayout = false;
}else{
unfilteredLayout.remove(tv);
filteredLayout.addView(tv);
isInFilterLayout = true;
}
}
});
unfilteredLayout.addView(tv);

Related

Change button text over other button android

I create an application in android studio and I need advice, I got one button, and I need to change the text on the second button clicks through to the first. I have a code that changes only TextView but not the text on the button.
NewText = (TextView)findViewById(R.id.textView1);
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
final TextView finalNewText1 = NewText;
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Set Text on button click via this function.
finalNewText1.setText(" (Frohe Weihnachten) ");
}
});
Same concept as you did for textView
Button SecondButton,ChangeText; // declaring the buttons
SecondButton = (Button)findViewById(R.id.button2);
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//This changes the text on the second button
SecondButton.setText("New Text Here");
}
});
SecondButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do anything
}
});
Button ChangeText;
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//part to change the button text
Button tmp_button = (Button)findViewById(R.id.ch_txt_ger);
tmp_button.setText("Frohe Weihnachten");
//part to change the textview text
TextView NewText
NewText = (TextView)findViewById(R.id.textView1);
finalNewText1.setText(" (Frohe Weihnachten) ");
}
});
After Clicking outlooking
Here you go: You can define a temporary button variable and make the change on it if setting the same button on its own clicking is causing problems.
And if the text will not change according to user, and if you know it like On/OFF, Red/Green you can also code it with a selector file which would make the java code look more clean.
A tiny advise: Defining the TextViews and Buttons that will get affected should all be written in the same function and close to the place where they are being changed for you to keep track of where you coded them.
I would add one thing, in case if you want to save the new button name when you close and reopen your app, you could use Shared Preferences: https://developer.android.com/training/basics/data-storage/shared-preferences.html

Android realtime add button

I need a button that, when clicked, would add a TextView below it. Also, the TextView cannot be made invisible and appear on click in this case, it must add the TextView. Was looking for this and couldn't find anything that suits my needs.
Set a listener on the button and for each click, create a new TextView and add it to the layout.
final LinearLayout theLayout = (LinearLayout)findViewById(R.id.thelayout);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = new TextView(MainActivity.this);
tv.setText("Hello");
tv.setTextColor(Color.BLACK);
theLayout.addView(tv);
}
});
Update: Added text color above

How can I change Text of Textview from other funcion?

this is my first question so I hope to make it clear.
I have one textView with some numerical text and next to it one button with one click listener and what I want is that when you click on the button the numerical value (>=0) of the TextView decrements in one.
Here is part of my code:
TextView Counter = new TextView(this);
if (intSeries != 0)
Counter.setText(Integer.toString(intSeries));
else
Counter.setText("0");
Counter.setId(4);
tablaContador.addView(Counter,Tr);
Button Done = new Button(this);
Done.setText("-1");
if (intSeries != 0)
Done.setVisibility(View.VISIBLE);
else
Done.setVisibility(View.GONE);
Done.setId(6);
Done.setOnClickListener(this);
And this is the onClick funcion (part of it):
#Override
public void onClick(final View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case 6:{
TextView text = (TextView)findViewById(4);
int series = Integer.parseInt(text.getText().toString());
series--;
text.setText(series);
if (series==0){
Button boton = (Button)findViewById(6);
boton.setVisibility(View.GONE);
}
}
}
}
The error is when I try to make the setText inside the onClick function, I hope it can be fixed or maybe recieve other idea to do it.
Thank you so much.
I would avoid all this hardcoding of Ids, use resources instead.
Your call to
text.setText(series)
is passing an int. The only valid setText(int resId) overload expects a resource associated with the int value, i.e. a string resource.
Convert your series value to a string.
Something like:
text.setText(Integer.toString(series));
You should setup series as an integer. And increase/descrease it as you wish. When you want to change the button's text convert the int to String.
Instead of:
text.setText(series);
use:
text.setText(String.valueOf(series));
Variablenames in java can't start with a capital letter. That is reserved for classnames.
Counter -> counter
Done -> done
I tried this and it worked:
//Create onClickListener
OnClickListener pickChoice = new OnClickListener()
{
public void onClick(View v)
{
TextView txt = (TextView) findViewById(4);
int number = Integer.valueOf(txt.getText().toString());
txt.setText(String.valueOf(number -1));
}
};
//Create layout
LinearLayout lnLayout = new LinearLayout(this);
lnLayout.setOrientation(LinearLayout.VERTICAL);
TextView txt = new TextView(this);
txt.setId(4);
txt.setText("0");
lnLayout.addView(txt);
Button Done = new Button(this);
Done.setText("-1");
Done.setId(6);
Done.setOnClickListener(pickChoice);
lnLayout.addView(Done);
setContentView(lnLayout);
Where are you creating your button inside? an activity? the part where you pass the onClickListener to the button doesn't make sense, maybe the button is getting a wrong listener and gets you an error every time you press the button ?
The code should be easy to understand, if there is anything you need me to explain please ask :)

how to implement onclicklistener to a dynamically creating textview?

Currently i am having some problem with implementing onclicklistener to a dynamically creating textview. I will explain the problem more detailed. What i need to do is, i need to create textviews when i click a button in an activity and when i click on that textview it should get removed. but i am not able to set onclicklistener to each textview. Since, set onclicklistener of textviews are written inside the onclick function of the above said button(button used for creating the textview), its scope get over when it exits from onclick function of the button(i think this is the problem). So i tried using visible and invisible feature, which will create the textviews before hand and make them invisible and they are made visible only when the button(button used for creating the textview)is clicked. But here even though it is invisible the space will be allocated(ie, blank space will be availabe).
Here is my code
This button addphone will dynamically create textview by inserting the value present in the edittext phoneno
addphone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(phoneno.getText().toString().length() > 0 && counter < MAX)
{
addphoneno[counter] = phoneno.getText().toString();
phoneno.setText("");
final TextView mybox = new TextView(getApplicationContext());
mybox.setText(addphoneno[counter]);
mybox.setPadding(5, 5, 5, 5);
mybox.setBackgroundColor(Color.rgb(99, 99, 99));
contactbox[counter] = mybox;
contactbox[counter].setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
contactbox[counter].setId(100+counter);
contactbox[counter].setText(addphoneno[counter]+" "+"X");
contactbox[counter].setClickable(true);
contactbox[counter].setOnClickListener(this); //This doesn't work!!!!!
counter = counter+1;
}
}
});
But the setOnClickListener in the above line is not working
So can anyone pls help me with this problem. I hope you are clear with my question.
Thank You!
You can try this:
private OnClickListener phoneViewClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
// your code
}
};
and use that listener in your TextViews:
contactbox[counter].setOnClickListener(phoneViewClickListener);
You will have to actually define a onClickListener instead of simply setting it as a boolean value.
contactbox[counter].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//this is where you would handle your click event
}
});
Good luck!
If your button was defined on the xml layout you can do that.
In your xml layout you can define which method will be called when a user click on your button:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/add_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="addTextView" /> // This is most imporant line
Your activity must have a method with the same name with a View parameter, like that:
/** Called when the user touches the button */
public void addTextView(View view) {
// Do something in response to button click
if(phoneno.getText().toString().length() > 0 && counter < MAX)
{
addphoneno[counter] = phoneno.getText().toString();
phoneno.setText("");
final TextView mybox = new TextView(getApplicationContext());
mybox.setText(addphoneno[counter]);
mybox.setPadding(5, 5, 5, 5);
mybox.setBackgroundColor(Color.rgb(99, 99, 99));
contactbox[counter] = mybox;
contactbox[counter].setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
contactbox[counter].setId(100+counter);
contactbox[counter].setText(addphoneno[counter]+" "+"X");
contactbox[counter].setClickable(true);
contactbox[counter].setOnClickListener(this); //This will work \o/
counter = counter+1;
}
}
}
On this method you should put your code to addViews.
As the behavior of all added textview must to be the same( i understood in that way), be removed when a user clicked on it, you can make your activity implements onClickListener and with that you just need to implement correctly the onClick method of your activity.

How to write onclick actions for dynamically generated textviews in android?

I have to display the textviews dynamically in android and need to write onclick action for each textview. I am able to display the textviews dynamically but I didnt get
how to write the onclick action for each textview. Please help me regarding this...Will be thankful to you..
You should check this code. Create an onclicklistener and then use setOnClickListener(); method.
private TextView textview1, textview2;
//initialize them
OnClickListener customTextviewOnClicklistener = new OnClickListener()
{
#Override
public void onClick(View v)
{
if (v == textview1)
{
// Here your code for textview1
Log.i("Clicked Item", "textview1");
}
else if (v == textview2)
{
// Here your code for textview2
Log.i("Clicked Item", "textview2");
}
else
{
//Here your code for others
}
}
};
textview1.setOnClickListener(customTextviewOnClicklistener);
textview2.setOnClickListener(customTextviewOnClicklistener);
i hope this may help you.
Create the views dinamically however you want to do and add the listener right then.
TextView tv = new TextView(ActivityName.this);
tv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(ActivityName.this, "tv text: " + ((TextView) v).getText().toString()).show();
//Do whatever you want to do here.
}
});
layout.addView(tv); //layout added on the xml for example, or by an inflater.

Categories

Resources