Add TextView, when button is pushed Kotlin - android

I don't understand why it isn't just as simple as this...
// Add field
val LineLayoutInfo = findViewById<View>(R.id.LineLayoutInfo) as LinearLayout
val Add_Field = findViewById<View>(R.id.buttonAddField) as Button
Add_Field.setOnClickListener{
val tv1 = TextView(this#MainActivity)
tv1.text = "Show Up"
val t = TextView(applicationContext)
LineLayoutInfo.addView(tv1)
}
All I want to do is create a new TextView inside my current LinearLayout. This part of the code is written in Kotlin and stored in the OnCreate(). Is it maybe because I need to refresh the activity?
The output I get when I push the button, looks like this.
D/HwAppInnerBoostImpl: asyncReportData com.xxx.httpposter,2,1,1,0 interval=83
I/ViewRootImpl: jank_removeInvalidNode all the node in jank list is out of time
V/AudioManager: playSoundEffect effectType: 0
V/AudioManager: querySoundEffectsEnabled...
D/HwAppInnerBoostImpl: asyncReportData com.xxx.httpposter,2,1,2,0 interval=353

You can try something like this
private LinearLayout mLayout;
private Button mButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLayout = (LinearLayout) findViewById(R.id.linearLayout);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText("New text");
}
private OnClickListener onClick() {
return new OnClickListener() {
#Override
public void onClick(View v) {
mLayout.addView(createNewTextView("New Text"));
}
};
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText("New text: " + text);
return textView;
}

Here, you could just create the TextView and set it's Visibility to INVISIBLE on MainActivity after the onCreate() of the program, then on button click set it to VISIBLE. You could do this infinitely!

Related

How do I keep data in my Deque in Android Studio on Buttonclick?

I am trying to save data to a Deque on buttonclick and display the data in a tablelayout. My issue is that every time I trigger the onclick, the Deque loses all previous data. I tried to instantiate the Deque outside the onCreate method and it did not work. I am using a Deque as a stack because I need to display the data LIFO. Any help on this would be very much appreciated. Here is what I've tried so far:
public class MainActivity extends AppCompatActivity {
private Button buttonAdd;
private EditText editText1;
private Deque<String> input;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText1 = (EditText)findViewById(R.id.edit_text_1);
final Deque<String> input = new ArrayDeque<String>();
buttonAdd = (Button) findViewById(R.id.button_add);
buttonAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
String data = editText1.getText().toString();
input.addFirst(data);
Deque<String> inputData = input;
while (!inputData.isEmpty()) {
String s = inputData.removeFirst();
TableRow row = new TableRow(MainActivity.this);
TextView textViewData = new TextView(MainActivity.this);
textViewData.setText(s);
textViewData.setGravity(Gravity.CENTER);
row.addView(textViewData);
table.addView(row);
}
}
});
}
}
The problem is this line:
String s = inputData.removeFirst();
With this operation, you remove (delete) the next entry and since you do it in a loop:
while (!inputData.isEmpty()) {
it simply empties the queue.
What you want is to iterate over the queue without removing anything:
for (String element : inputData) {
textViewData.setText(s);
}

How to add onclicklistener to dynamically generated textViews?

I write code to generate dynamic TextViews as a user what he looked like and any time
, But the problem each text view has only one click,
And any other click on textView does not reach onClick
I found some answers but not work with my code... Please help me.
This is my code
RelativeLayout mainBackground = findViewById(R.id.mainBackground);
private void AddTextView() {
RelativeLayout.LayoutParams lparams = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
final TextView tv = new TextView(this);
tv.setId(textCount);
tv.setLayoutParams(lparams);
tv.setText(text);
tv.setTextColor(colorChooser);
tv.setTextSize(size);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("textID",view.getId()+"");
}
});
this.mainBackground.addView(tv);
}

How to get data from EditText when it is using in linearlayout

I am working with android LinearLayout and i am adding a TextView programmicataly and then on TextView click add an EditText in View. Now I want to get data at then end on button click from all added EditTexts in layout. here is my coding through which i am working.
TextView addMoreText = new TextView(this);
addMoreText.setText("Add More Ingredients");
addMoreText.setGravity(Gravity.CENTER);
addMoreText.setPadding(20, 20, 20, 20);
addMoreText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.add, 0);
addMoreText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final EditText editTextItem = new EditText(SearchRecipe.this);
editTextItem.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.cross, 0);
editTextItem.setPadding(20, 20, 20, 20);
editTextItem.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
parentLayout.removeView(editTextItem);
return true;
}
});
parentLayout.addView(editTextItem, 0);
}
});
parentLayout.addView(addMoreText);
searchRecipe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
i want to get all the data from EditText on searchRecipe click
Assign the id to the EditText:
editTextItem .setId({yourId});
Then inside searchRecipe onClick method, invoke:
EditText editTextItem = (EditText) parentLayout.findViewById({yourId});
And then use it as you want.
Add an arraylist and put the edittexts in it to keep track of them.
ArrayList<EditText> etList = new ArrayList();
In onClick of textview just add to it at the end.
etList.add(editTextItem);
In searchReciepe go through your list.
for(Edittext et : etList){
et.getText();
[...]
}
Edit: Obviously remember to remove it from the List on remove.
private int EDITTEXT_ID = 1;
private List<EditText> editTextList = new ArrayList<EditText>();
// Add Edittext programmatically in your view
private void addView() {
EditText editText = new EditText(this);
editText.setId(EDITTEXT_ID);
EDITTEXT_ID++;
editText.setText("Hello there " + EDITTEXT_ID);
editTextList.add(editText);
lnvEditText.addView(editText); // lnvEdittext is my LinearLayout which is added in XML file
}
// Get values of every Edittext on click of button
for(int i=0; i<editTextList.size(); i++) {
Log.e("All Values=", editTextList.get(i).getText().toString());
}

i made check box dynamically but i can not see them

i have an app that make check box dynamically.
A button and edit text,i insert some thing in the edit text then click button after that a check box with that text appear.
but i have problem with that.
i am completely confuse, i cannot see them!!!! and i do not have any error!!!!????
can some one help me ? :(
...
public int i2 = 0;
...
final EditText et1 = (EditText) findViewById(R.id.editText1);
final Button bnext = (Button) findViewById(R.id.button1);
//
final LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
//
bnext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//getting text from edit text
String str_et1 = et1.getText().toString();
do {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setId(i2);//set id to each checkbox
cb.setText(str_et1);//set text to each checkbox
ll.addView(cb);
} while (bnext.isDirty());//add when ever i clicked
i2++;//this the counter
}
});
Try this code -
final EditText et1 = (EditText) findViewById(R.id.editText1);
final Button bnext = (Button) findViewById(R.id.button1);
//
final LinearLayout ll = new LinearLayout(this);
LayoutParams params =
new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(params);
//
bnext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//getting text from edit text
String str_et1 = et1.getText().toString();
do {
CheckBox cb = new CheckBox(getApplicationContext());
cb .setLayoutParams(params);
cb.setId(i2);//set id to each checkbox
cb.setText(str_et1);//set text to each checkbox
ll.addView(cb);
} while (bnext.isDirty());//add when ever i clicked
i2++;//this the counter
}
});
Hope this code helps you!!
if its not working please let me know i will try to help more.
A check box alone is not a visible component. You will need to have a LayoutParam, then attach the checkbox to it to make it visible.

Android : How to set TextViews positions depending on other ones programatically?

I want to put elements in my view depending on the position of the other ones. Let me explain : First, I have two textviews and I've put successfully the second (villeOffre) one under the first one (titreOffre) :
titreOffre = (TextView) findViewById(R.id.offreTitre);
titreOffre.setText(capitalizedString(offre.getTitle()));
villeOffre = (TextView) findViewById(R.id.offreVille);
villeOffre.setText(offre.getLocality());
infos = (TextView) findViewById(R.id.infos);
ViewTreeObserver vto = titreOffre.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(
{
#Override
public void onGlobalLayout()
{
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) villeOffre.getLayoutParams();
params.setMargins(15,titreOffre.getBottom()+12,15,0);
villeOffre.setLayoutParams(params);
titreOffre.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
This works perfectly, but now I want to put a third textview (infos) which is depending on villeOffre position. How to do that ?
Thanks a lot.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rr = new RelativeLayout(this);
b1 = new Button(this);
b1.setId(R.id.Button01);
recent = b1;
b1.setText("Click me");
rr.addView(b1);
setContentView(rr);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1 = new TextView(can.this);
tv1.setId((int)System.currentTimeMillis());
lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, recent.getId());
tv1.setText("Time: "+System.currentTimeMillis());
rr.addView(tv1, lp);
recent = tv1;
}
});
}

Categories

Resources