I have a spinner with the values = {4,5,6,7} , initially spinner selected value is set to 4 , therefore 4 TextViews and 4 EditTexts showing in the activity.
Now I want if the user select value 5 from the spinner then these previous 4 TextViews and EditTexts will overwrite with new 5 TextViews and 5 EditTexts.
How can I do so ? any help?
For this you can manage through loop like...
for( int i = 0; i < spinner.getSelectedItem(); i++)
{
TextView textView = new TextView(this);
textView.setText(textArray[i]);
linearLayout.addView(textView);
}
For create view one after another create programmatically layout like this:
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL)
Like this you can manage programmatically as per your need.
You can write your TextView and EditText as an item of RecyclerView and every time you can have as much items as you want.
OR
every time add TextView and EditText programmatically from the java code.
Related
I am creating an app where I will be loading some images and data from a database, it should look like this:
Image______Name of user
Image______Name of user
Image______Name of user
etc..
I tried to create it just with a dummy image and some text to figure out how it works.
I create a LinearLayout, ImageView and a TextView, I add those two to the LinearLayout, and than I add that LinearLayout to a RelativeLayout.
The problem is, that all the images and text are placed in the same place, on top of each other. How can I change it so it is in the format I need?
relativeLayout = (RelativeLayout) findViewById(R.id.rel);
for(int i = 0; i< 30; i++)
{
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
TextView hello = new TextView(this);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.mipmap.ic_launcher);
String hi = "Hey";
if(i == 0){hi = "Hello0";}
if(i == 2){hi = "Hello2";}
if(i == 3){hi = "Hello3";}
if(i == 4){hi = "Hello4";}
hello.setText(hi);
layout.addView(imageView);
layout.addView(hello);
relativeLayout.addView(layout);
}
I am using a for to loop it a few times just for test.
Instead of RecyclerView, add the items in a LinearLayout. You can also set position where to add the new item in the LinearLayout.
I would suggest you do instead is:
create a model object for the user details(name and picture)
Use ListView or RecyclerView with a simple adapter add items to an
ArrayList of model object and notify the adapter when data is
changed.
This way you'll be reusing the views, and that'll improve the performance much better.
for examples, you can take a look at these sample projects.
https://github.com/lokeshsaini94/SimpleAndroidExamples/tree/master/ListView
https://github.com/lokeshsaini94/SimpleAndroidExamples/tree/master/RecyclerView
How to create multiple text views at run time in multiple rows and column? I have inflated a linear layout and created the text views using for loop. Text Views were created successfully, but i'm facing the issue that all created text views are only in single row. I tried to set it with the LayoutParams also, but cant fixed it. How to fix this? Below is my code
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
appointmentSlotList = appointmentSlot.getAppointmentSlots();
if(appointmentSlotList != null && appointmentSlotList.size()>0){
for(int i = 0; i<appointmentSlotList.size(); i++){
View appointmentInflater = layoutInflater.inflate(R.layout.appointment_time, null);
TextView lblDate = (TextView) appointmentInflater.findViewById(R.id.appointmentTime);
lblDate.setText(appointmentSlotList.get(i));
lblDate.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
//lblDate.setLayoutParams(lparams);
lblDate.setOnClickListener(onclickTime);
try {
//if(previousSelected!=i)
lnrDateContainer.addView(appointmentInflater);
}catch (Exception e){
e.printStackTrace();
}
}
}
and my output is shown below:
Output for the inflated text views shown below the date picker
Set the textview android layout gravity to fill
android:layout_gravity="fill"
If above wont work then refer to this question : Android multi line linearlayout
As you want to add TextView in rows and columns, you can use TableLayout and add TextView in it pragmatically
Check this answer : https://stackoverflow.com/a/16939325/5345482
To achieve these I think FlowLayout library might be fit to your requirements.
With this lib you can create your TextViews in a single line and when doesn't have space then auto inserts the view in the next line.
The link: https://github.com/ApmeM/android-flowlayout
I hope helps you!
Specify the orientation in your Linear Layout as VERTICAL
<LinearLayout
----
android:orientation="vertical"
---- />
I've got a layout which is called activity_main.xml which is my parent layout, and I am then inserting a child layout (activity_main_card.xml) within a for loop.
I also have an array, and what I am doing is using the length of the array to determine how many child elements it should create. All of my data to be used in the child element is stored in the array, so the idea is to loop through the array, create a child element for each loop and populate the data.
Instead, what is currently happening is that it is generating the 3 (length of array) child elements, but it is only populating the first one with the latest content in the array. This is because it keeps the variables the same.
What I need to do is somehow set dynamic variables that change as the loop iterates.
The code for my method is as follows:
for (int i = 0; i < cardArray.length; i++) {
LinearLayout item = (LinearLayout)findViewById(R.id.card_holder);
View child = getLayoutInflater().inflate(R.layout.activity_main_card, item, false);
item.addView(child);
//Set objects from array to variables
String cardTitle = cardArray[i][0];
String cardContent = cardArray[i][1];
String cardImage = cardArray[i][2];
//Set XML elements to variables
TextView title = (TextView) findViewById(R.id.card_title);
TextView content = (TextView) findViewById(R.id.card_content);
ImageView image = (ImageView) findViewById(R.id.card_image);
//Load variable content into card layout
title.setText(cardTitle);
content.setText(cardContent);
image.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("drawable/" + cardImage, "drawable", getPackageName())));
}
What I thought I could do was to set the views like:
TextView title[i] = (TextView) findViewById(R.id.card_title);
however this doesn't work. Does anyone know how I can acheive this?
You need to get a reference to the current View you just created and then populate the TextView elements on that View. This is achievable by doing this:
TextView title = (TextView) child.findViewById(R.id.card_title);
TextView content = (TextView) child.findViewById(R.id.card_content);
ImageView image = (ImageView) child.findViewById(R.id.card_image);
You might be better off approaching this problem differently however, as Jonathan suggests a ListView and adapter might suit better for this situation.
You aren't using the View created, you are using the root layout of the activity in every loop because you apply the findViewById in the parent view:
Change:
TextView title = (TextView) findViewById(R.id.card_title);
By;
TextView title = (TextView) child.findViewById(R.id.card_title);
I have an android app which asks a question followed by x number of options. Each option contains a textview, ImageView and a radio button.
The value of x (i.e. the number of options) is not constant. I want to dynamically add UI content to satisfy this requirement.
This I am doing using the following code
LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayoutThis = (LinearLayout)findViewById(R.id.lladrList);
for (int i =0 ;i< options.length; i++){
LinearLayout lladroption=(LinearLayout)inflater.inflate(R.layout.adroption, null);
//get view id and set values
ImageView iv = (ImageView) lladroption.findViewById(R.id.ivadroption);
iv.setId(OPTIONIMAGE+i);
iv.setImageBitmap(downloadFile(options[i].getOptionData().getDataURL()));
((ViewGroup) iv.getParent()).removeView(iv);
iv.setOnClickListener(this);
linearLayoutThis.addView(iv);
RadioButton rb = (RadioButton) lladroption.findViewById(R.id.rbadroption);
rb.setOnClickListener(this);
rb.setId(OPTIONRADIOBUTTON+i);
rb.setText(options[i].getOptionString());
((ViewGroup) rb.getParent()).removeView(rb);
linearLayoutThis.addView(rb);
// rg.addView(rb);
}
I want the radiobuttons to belong to a particular radiogroup. But if I do a
rg.addView(rb) instead of linearLayoutThis.addView(rb); in the above code where rg is the radiogroup view ... all the radiobuttons are display in one go. Instead of displaying text,image,radiobutton,text,image,radiobutton,text,image,radiobutton I get text,image,text,image,text,image,radiobutton,radiobutton,radiobutton.
Does anyone know how to resolve this?
You dont have to add those dynamically. create a view group or layout with the textview image and the radio button you require. use listview to get the x number of options. If this is your requirment you can reply so that i can help you with it.
I am having a dynamic table in which I have created lots of textview dynamically... actually I having one row in which I have 3 textview... in this one of them contains unique id but other's textview value repeats around the table... therefore on the click of other textview I need text from unique textview.... so please suggest some thing like getting text from the neighbor textview... line through which I am able to get text from the textview is following.
((TextView)v).getText().toString();
When creating dynamic TextViews assign a reference to the unique TextView as a tag using setTag to each of them.
TextView uniqueTextView = (TextView)findViewById(R.id.unique_id);
TextView neighbourView = new TextView();
neighbourView.setTag(uniqueTextView);
You can later get the reference back using getTag
public void onTextViewClick(TextView view) {
TextView uniqueTextView = (TextView)view.getTag();
String text = uniqueTextView.getText().toString();
}