I would like to be able to for loop in my layout and add text to the textviews dynamically, this does not error out but I get the lost row in the display for example
Tw04 One4
I would like to be able to display
One1 Two1
One2 Two2
etc...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rel_layout);
for (int i = 0; i < 5; i++) {
TextView woTxt = (TextView) findViewById(R.id.ticker_price);
woTxt.setText("One"+i);
TextView cusTxt = (TextView) findViewById(R.id.ticker_symbol);
cusTxt.setText("Two"+i);
}
}
You may add TextViews programmatically to your layout as below :
TextView [] txt1 =new TextView[5];
for(int i=0;i<5;i++)
{
txt1[i]=new TextView(YourActivity.this);
txt1[i].setText("One"+i);
txt1[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
linear.addView(txt1[i]);
}
where linear is a LinearLayout from your layout.
You have only two text views, where in fact you need 10 on your example, one text view for each item you want to display. I suggest that you make a ListView instead, where each line of the list will be a couple of text views.
Related
LinearLayout x=(LinearLayout)findViewById(R.id.english_no);
Why this code is wrong-
TextView wordview=new TextView(this);
for(int i=0;i<english.size();i++)
{
wordview.setText(english.get(i));
x.addView(wordview);
}
and this one is correct-
for(int i=0;i<english.size();i++)
{
TextView wordview=new TextView(this);
wordview.setText(english.get(i));
x.addView(wordview);
}
I couldn't understand the difference.
here in the first example you are just referring to the first TextView that you created and changing its value and adding it to the view, eventually the x (hoping a Linearlayout) will have english.size() number of views where the content of every view would be same and that is the last content of english
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 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]);
}
}
I have added multiple TextViews dynamically in a layout,
for(int x=4;x<result.length();x++)
{
JSONObject collegeData = result.getJSONObject(x);
Log.i("Classlist",""+x);
TextView tv = new TextView(this);
Animation animation = AnimationUtils.loadAnimation(student_profile.this, android.R.anim.slide_in_left);
tv.startAnimation(animation);
tv.setTag(tag);
tv.setLayoutParams(lparams);
tv.setText(collegeData.getString("date") + " " + collegeData.getString("day_name"));
tv.setTextSize(17);
this.linearLayout_top5classes.addView(tv);
}
This loop adds textViews according to the data received by the url,Now i want to remove the textviews which were created in this loop and i cant find a proper method to do so....I only want to remove these textviews and not all the textviews
UPDATE
First i used
int prv=0;
then
String tag ="textView_"+x;
prv++;
in the first loop to generate multiple tags
then i removed them with
for(int x=4;x<prv;x++)
{
View view = this.linearLayout_top5classes.findViewWithTag("textView_"+x);
this.linearLayout_top5classes.removeView(view);
Log.i("prv value",prv+"");
}
Of course there is a way. Just look for child views with tag:
View view = this.linearLayout_top5classes.findViewWithTag(tag);
this.linearLayout_top5classes.removeView(view);
If you add ID's to child views, then:
View view = this.linearLayout_top5classes.findViewById(id);
this.linearLayout_top5classes.removeView(view);
If possible try using different layout for the dynamically added textviews. and then remove views from second layout only. By using,:
linearLayout_top5classes.removeAllViews();
To remove particular view with tag
View view = this.linearLayout_top5classes.findViewWithTag(tag);
this.linearLayout_top5classes.removeView(view)
To remove view from particular position
this.linearLayout_top5classes.removeViewAt(position);
Use following logic to remove any view from layout.
this.linearLayout_top5classes..post(new Runnable() {
public void run() {
this.linearLayout_top5classes..removeView(view);
}
});
I have table layout. We should be able to add rows that containing 3 textviews and 4 imageviews to that tablelayout dynamically when onclick of the "+" button. We can able to remove a particular selected row when onclick of "-" button. When we select a particular row, we should be able to add images onclick of camera button to that particular row. How to do it. Can anyone please help me..
You can either use a LayoutInflater to add rows on clicking + button. Or you can add the Views using code.
This is a sample code to add an ImageView to a Table layout dynamically.
private void addImageView()
{
ImageView mImageView = new ImageView(this);
TableLayout mTableLayout = (TableLayout) findViewById(R.id.tablelayout1);
mImageView.setId(getRndId());
TableRow mTableRow = new TableRow(this);
mTableRow.setId(getRndId());
mTableRow.addView(mImageView);
mTableLayout.addView(mTableRow);
}
/**
* Gets a random no and checks if its already used in R.java
* */
protected int getRndId()
{
Random rnd = new Random();
int possible_id = rnd.nextInt();
while(true)
{
// Log.d(TAG, "possible_id=" + possible_id);
View temp = findViewById(possible_id);
if ((possible_id>0) && (temp==null))
{
return possible_id;
}
else
{
possible_id = rnd.nextInt();
}
}
}