I have created dynamically a LinearLayout and inside a HorizontalScrollView and I have placed insite a TableLayout (my table is too big an I need to scroll). When first created my table has a row as a header and a row for the user to input data. Outside the HorizontalScrollView but inside the LinearLayout I want to add 2 buttons. One for adding a row and one for deleting rows. I setup onclickListener but it did not work. My code is :
LinearLayout linearLayout = new LinearLayout(findViewById(R.id.internal_nodes_layout).getContext());
HorizontalScrollView scrollView1 = new HorizontalScrollView(findViewById(R.id.opaque_scroll_view).getContext());
TableLayout in_opaque_table = new TableLayout(findViewById(R.id.opaque_table).getContext());
LinearLayout opaquebuttonsLayout = new LinearLayout(findViewById(R.id.opaque_buttons_layout).getContext());
TableRow in_opaque_table_HeaderRow = new TableRow(this);
in_opaque_table_HeaderRow.setLayoutParams(tlparams);
//.............. code for header...............
createRowInOpaqueTable(in_opaque_table, in_opaque_table_num_of_rows, tlparams);
scrollView1.addView(in_opaque_table);
opaquebuttonsLayout.addView(addInOpaque);
opaquebuttonsLayout.addView(removeInOpaque);
linearLayout.addView(scrollView1);
linearLayout.addView(opaquebuttonsLayout);
//setup buttons and onClickListeners
Button addInOpaque = new Button(this);
addInOpaque.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
addInOpaqueRow(view);
}
});
addInOpaque.setText(R.string.add_line);
addInOpaque.setAllCaps(false);
Button removeInOpaque = new Button(this);
removeInOpaque.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
removeInOpaqueRow(view);
}
});
public void addInOpaqueRow(View view) {
createRowInOpaqueTable(in_opaque_table, in_opaque_table_num_of_rows, tlparams);
in_opaque_table_num_of_rows++;
}
public void removeInOpaqueRow(View view) {
if (in_opaque_table_num_of_rows > 0) {
in_opaque_table.removeViewAt(in_opaque_table_num_of_rows);
in_opaque_table_num_of_rows--;
} else {
Toast.makeText(this, "Δεν υπάρχουν γραμμές στον πίνακα", Toast.LENGTH_SHORT).show();
}
}
Although it appears the way I want to the add and remove buttons won't work.
I get this message:
E/libfingersense_wrapper: libfingersense.so was not loaded
but I see that rows count correct when I use add and remove buttons. Any ideas??
Related
I have a LinearLayout ("ll") that is already created in xml and the app dynamically creates another LinearLayout inside of it and creates an EditText and a Button inside of that view. The button makes the whole LinearLayout destroy itself along with the EditText and Button inside it (the whole system is a player name entering activity). Anyway, I am trying to find a way to get the text from all of the EditTexts. I have tried using a for loop on "ll" and using ll.getChildAt() but I can't seem to use .getChildAt() on whatever ll.getChildAt() generates because getChildAt() generates a "View" not a "LinearLayout." I basically just need a way to search two children in, rather than just one. Also, if there is just a better way I should be doing this, let me know. I'm open to suggestions.
Here's my code if it will help:
NewGameCreate.java
public class NewGameCreate extends Activity {
int numOfPlayers = 0;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_game_create);
}
public void newPlayer(View view) {
numOfPlayers++;
final LinearLayout ll = findViewById(R.id.playerView);
final LinearLayout llNew = new LinearLayout(getApplicationContext());
llNew.setOrientation(LinearLayout.HORIZONTAL);
llNew.setId(numOfPlayers);
ll.addView(llNew);
EditText newName = new EditText(this);
newName.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
newName.setHint("Enter Player Name");
newName.setId(numOfPlayers);
newName.setWidth(0);
llNew.addView(newName);
final Button delete = new Button(this);
delete.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0));
delete.setText("Delete");
delete.setId(numOfPlayers);
delete.setWidth(0);
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int id = delete.getId();
ll.removeViewInLayout(findViewById(id));
Drawable back = ll.getBackground();
ll.setBackgroundColor(00000000);
ll.setBackground(back);
ll.invalidate();
}
});
llNew.addView(delete);
}
public void startGame(View view){
LinearLayout ll = findViewById(R.id.playerView);
List text = new ArrayList();
for(int loop = 0; loop < ll.getChildCount(); loop++) {
//this is the code in question and where I want to get the text from
//all my EditTexts
LinearLayout inner = ll.getChildAt(loop);
}
}
}
I think I found the answer to it. You need to change a little bit of code in the startGame() method I m providing the code for startGame below.
public void startGame(View view) {
LinearLayout ll = findViewById(R.id.playerView);
List text = new ArrayList();
for (int loop = 0; loop < ll.getChildCount(); loop++) {
//this is the code in question and where I want to get the text from
//all my EditTexts
LinearLayout inner = (LinearLayout) ll.getChildAt(loop);
for (int j = 0; j < inner.getChildCount(); j++) {
if (inner.getChildAt(j) instanceof EditText) {
EditText textET = (EditText) inner.getChildAt(j);
Log.d("TAG",textET.getText().toString());
}
}
}
}
In the above code you were able to get the first child only but as you have added a linearLayout with orientation Horizontal in a parent LinearLayout with orientation Vertical, you have written code for the child of parent layout i.e playerView. I have modified the code to get the elements of the child Linear layout and Log prints all the text from the EditText.
Hope that helps!!
I have a Linearlayout called "resultView". I then dynamically added many textViews in it after clicking a button.
I want to remove all the textviews that I just created when I click the button again.
btn_search.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View view) {
resultView.removeAllViews();
String strFileName = et_fileName.getText().toString();
searchFiles(strFileName);
}
});
public void searchFiles(String strFileName){
....
for (int i = 0; i < fileList.size(); i++) {
TextView textView = new TextView(this);
textView.setText(fileList.get(i).getName());
textView.setPadding(5, 5, 5, 5);
if (fileList.get(i).isFile())
resultView.addView(textView);
}
}
App Screenshot, All the listed results are dynamically created textViews.
My XML, the id "view" is my resultView.
But resultView.removeAllViews(); doesn't work. The results are still appened.
Calling resultView.invalidate(); after that doesn't work either.
What should I do to make the layout refresh?
Use .invalidate() to upddate UI.
I have one activity and inside it I have a Linear layout (lets name it the "main linear layout") and I am adding dynamically created view inside it (text views, linear layouts, edit texts, ...etc).
In the bottom of the screen there are two button (next and back).
If the user clicked on next button I should save the current "main linear layout " in a list for example and then I am generating a new views and I add it inside the "main linear layout" then .
And if the user clicked on back button I should restore the "main linear layout" and show all its views.
I don't know how I can do it.
I hope my description is clear :)
Take a look at onSaveInstance for preserving the view on the main screen when leaving to a different activity or screen orientation.
You can also try and use the buttons to hide the layouts using Visibility instead of trying to navigate through them.
This may help you. but concrete code need you write it yourself.
public class TestActivity extends Activity{
Stack<LinearLayout> layouts = new Stack<>();
FrameLayout container;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btnPre = new Button(this);
Button btnNext = new Button(this);
btnPre.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
container.addView(layouts.pop());
}
});
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout linearLayout = (LinearLayout) container.getChildAt(0);
layouts.push(linearLayout);
container.removeAllViews();
container.addView(createNextView());
}
});
}
private LinearLayout createNextView(){
LinearLayout linearLayout = new LinearLayout(this);
return linearLayout;
}
}
Thank you all for your comments.
I haven't test Cyrus answer. I will post my answer as well in case someone has the same problem in Xamarin.Android since I am using it.
I fixed it like this:
I added this in next button click event and previous button click event:
AddQuestionsLinearLayoutToPagesLinearLayoutsList();
And the AddQuestionsLinearLayoutToPagesLinearLayoutsList() body is, of course you can change it the way you want:
private void AddQuestionsLinearLayoutToPagesLinearLayoutsList()
{
//Create a new linear layout to add views into it and saved it in _mPagesLinearLayouts list at index _mPageIndex
LinearLayout savedLinearLayout = new LinearLayout(this);
//List of _mQuestionsLinearLayout views
List<View> views = new List<View>();
//Add all _mQuestionsLinearLayout to views list
for (int i = 0; i < _mQuestionsLinearLayout.ChildCount; i++)
{
views.Add(_mQuestionsLinearLayout.GetChildAt(i));
}
//Remove views from main questions linear layout
_mQuestionsLinearLayout.RemoveAllViews();
//Add all views from the views list to savedLinearLayout
for (int i = 0; i < views.Count; i++)
{
savedLinearLayout.AddView(views[i]);
}
//Add savedLinearLayout to _mPagesLinearLayouts list at index _mPageIndex
_mPagesLinearLayouts.Insert(_mPageIndex, savedLinearLayout);
}
In creating views method:
if(NeedToCreateViews())
{
//create views here and add it to _mQuestionsLinearLayout
}
//Get all saved views and add it to _mQuestionsLinearLayout
else
{
//List of _mPagesLinearLayouts[_mPageIndex] LinearLayout views
List<View> views = new List<View>();
//Add all _mPagesLinearLayouts[_mPageIndex] LinearLayout to views list
for (int i = 0; i < _mPagesLinearLayouts[_mPageIndex].ChildCount; i++)
{
views.Add(_mPagesLinearLayouts[_mPageIndex].GetChildAt(i));
}
//Remove all views from _mPagesLinearLayouts[_mPageIndex] linear layout
_mPagesLinearLayouts[_mPageIndex].RemoveAllViews();
//Remove the linear layout at index _mPageIndex
_mPagesLinearLayouts.RemoveAt(_mPageIndex);
//Add all views from views list to _mQuestionsLinearLayout
for (int i = 0; i < views.Count; i++)
{
_mQuestionsLinearLayout.AddView(views[i]);
}
}
public void Add_text() {
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView product = new TextView(getActivity());
product.setText(" Product" + 5 + " ");
ll.addView(product);
EditText qty = new EditText(getActivity());
qty.setText(i + "");
qty.setId(i);
qty.setWidth(120);
ll.addView(qty);
Button btn = new Button(getActivity());
ll.addView(btn);
btn.setLayoutParams(params);
btn.setOnClickListener(o);
ly.addView(ll);
i++;
}
I wrote the above code to create the textfields and buttons dynamically; But now I need to remove 2 textfields and a button when the button is clicked. How do I do that?
Try following code.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout linearParent = (LinearLayout) v.getParent().getParent();
LinearLayout linearChild = (LinearLayout) v.getParent();
linearParent.removeView(linearChild);
}
});
Explanation
Here first take "GrandParent" of any view.
Then take its "Parent" view
With reference to "GrandParent" remove that "Parent" view.
this will remove all views which that "Parent" holds. As per your code, your "ll" will be "linearChild" here. And "ly" will be "linearParent" here. So whole "ll" will be removed from "ly" which you have added dynamically.
If you want to permanently remove the views you created.
OnClick(View view){
ly.removeAllViews()
}
If you do not want to permanently remove the views you created.
OnClick(View view){
ly.setVisibility(View.GONE); //This will hide the all views
qty.setVisibility(View.GONE);//This will hide the EditText qty
product .setVisibility(View.GONE);//This will hide the TextView product
}
So use appropriate code line which you want.
EDIT:
Use this code for your situation:
public void Add_text() {
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView product = new TextView(getActivity());
product.setText(" Product" + 5 + " ");
ll.addView(product);
EditText qty = new EditText(getActivity());
qty.setText(i + "");
qty.setId(i);
qty.setWidth(120);
ll.addView(qty);
Button btn = new Button(this);
ll.addView(btn);
btn.setLayoutParams(params);
ly.addView(ll);
i++;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View button) {
qty.setVisibility(View.GONE);//This will hide the EditText qty
product .setVisibility(View.GONE);//This will hide the TextView product
}
});
}
i think you got to use this method on your LinearLayout :
public void removeView (View view)
first you call :
EditText et = (EditText)linearLayout.findViewById(yourEditText.getId());
then call the remove view method :
linearLayout.removeView (et) ;
and to remove all of the Views that are in the LinearLayout do the following :
public void removeAllViews ()
like the following :
linearLayout.removeAllViews()
and give me some feedback
Hope that Helps .
you can simply use qty.setVisibility(View.GONE) on the onClickListener() of the Button of your choice. like this.
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
qty.setVisibility(View.GONE); //for temporary purpose
//or u can also do this
layout.removeView(qty); //removes permanently
}
});
The benefit of using View.GONE is that you can get the View back if you want but layout.removeView(qty) will remove the view permanently and you have to re add the view again.
[EDIT] 1. changed to View.GONE instead of View.INVISIBLE because of reasons explained here
Hope I answered your question. :)
just use index for which you want to remove your view from linear layout
Linearlayout.removeViewAt();
if you want again that view then you can call addViewAt()in same way.
I hope it will help you.
Add this line
mLayout.removeViewAt(mLayout.getChildCount()-1);
I m very new to Android.
I want to create a dynamic OnClick button functionality.
OnClick of "+" above , it should create a other layer , like this below.
My confusion , my entire design UI is in layout.xml.
How we can we include another layer in our UI on OnClick of "+" button.
Any input would be helpful.
Thanks !!!
You could do this programatically. XML is for static layouts.
Excuse my pseudo Android:
private LinearLayout root;
public void onCreate(Bundle b){
LinearLayout root = new LinearLayout(this);
root.addChild(createGlucoseReadingView());
setContentView(root);
}
private View createGlucoseReadingView() {
LinearLayout glucoseRoot = new LinearLayout(this);
glucoseRoot.addChild(new TextView(this));
return glucoseRoot;
}
public void onPlusClick(View button){
root.addChild(createGlucoseReadingView());
}
Something along those lines, I've obviosuly left out formatting and adding the layout params to the views, but you get the idea.
In your XML have one Vertical Linear Layout to add and remove EditTexts at runtime, Here I have shown you code I have used in my demos. To handle and maintain the usage.
Onclick of your Add and Minus button click
public void onClick(View view) {
super.onClick(view);
switch (view.getId()) {
case R.id.btnadd:
createTextview(counter);
counter++;
if (counter > 3) {
btnAdd.setVisibility(View.GONE);
btnRemove.setVisibility(View.VISIBLE);
}
break;
case R.id.btnremove:
removeView(counter);
txtoption[counter - 1] = null;
counter--;
if (counter < 3) {
btnAdd.setVisibility(View.VISIBLE);
btnRemove.setVisibility(View.GONE);
}
break;
}
}
Functions to create and remove view
private void createTextview(int index) {
txtoption[index] = new EditText(this);
txtoption[index].setSingleLine(true);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
param.bottomMargin = 10;
txtoption[index].setLayoutParams(param);
txtoption[index].setBackgroundResource(R.drawable.textbox);
txtoption[index].setTypeface(ttfDroidSherif);
lnpolloptions.addView(txtoption[index]);
}
private void removeView(int index) {
lnpolloptions.removeView(txtoption[index - 1]);
}
Your vertical LinearLayout to contain all the edittext childs
LinearLayout lnpolloptions = (LinearLayout) findViewById(R.id.lnpolloptions);
Arrays of edittext to be created of removed at runtime
private EditText[] txtoption = new EditText[4];
Onclick of submit to get value from each textbox
int length = txtoption.length;
for (int i = 0; i < length; i++) {
if (txtoption[i] != null) {
Log.i("Value",""+txtoption[i].getText());
}
}