In my fragment's onCreateView method I'm trying to programatically inflate viewstubs to layouts under each other, but the first two takes place inside each other, like this
Here is my fragment's onCreateView method
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mBaseLayout = (PercentRelativeLayout) inflater.inflate(R.layout.fragment_calender, container, false);
findViews();
mBundle = new Bundle();
mFragments = new ArrayList<>();
mDaysContainer = (RelativeLayout) mBaseLayout.findViewById(R.id.daysContainer);
mStartPeriod = DateAndTime.getFirstDayFirstWeekOfMonthAsString("yyyy-MM-dd");
mEndPeriod = DateAndTime.getLastDayLastWeekOfMonthAsString("yyyy-MM-dd");
mPeriodTextView.setText(mStartPeriod + " - " + mEndPeriod);
createDayFragments(); // Create the fragments
findDayViewIds(); // Get access to the views
return mBaseLayout;
}
I use this method to create the views and put them into the container
private void createDayFragments() {
for (int i = 0; i < 30; i++) {
mFragments.add(new ViewStub(getActivity()));
mFragments.get(i).setId(i);
mDaysContainer.addView(mFragments.get(i));
ViewStub viewStub = (ViewStub) mBaseLayout.findViewById(i);
viewStub.setInflatedId(i);
viewStub.setLayoutResource(R.layout.fragment_day);
Log.d("ID", mFragments.get(i).getId() + "");
Log.d("i = ", i + "");
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if(i >= 1){
p.addRule(RelativeLayout.BELOW, i - 1);
Log.d("Below ID", i - 1 + "");
}
viewStub.setLayoutParams(p);
Log.d("", "");
viewStub.inflate();
}
}
And I use this method to get access to all the views after creating them
private void findDayViewIds(){
for(int i = 0; i < 30; i++) {
RelativeLayout tmpRel = (RelativeLayout) mDaysContainer.findViewById(i);
for(int j = 0; j < tmpRel.getChildCount(); ++j){
if(tmpRel.getChildAt(j) instanceof PercentRelativeLayout) {
PercentRelativeLayout percentTmp = (PercentRelativeLayout) tmpRel.getChildAt(j);
percentTmp.setId(100 + i);
for(int h = 0; h < percentTmp.getChildCount(); ++h){
if(percentTmp.getChildAt(h) instanceof TextView){
TextView textViewTmp = (TextView) percentTmp.getChildAt(h);
textViewTmp.setId(300 + i);
textViewTmp.setText("Day " + i + " ID: " + tmpRel.getId());
}
}
} else if (tmpRel.getChildAt(j) instanceof ViewStub){
tmpRel.getChildAt(j).setId(200 + i);
}
}
}
}
Here is the XML for the fragment that should contain the other fragments
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/weekDisplayed">
<TextView
android:text="Placeholder"
android:layout_alignParentTop="true"
android:gravity="center"
app:layout_heightPercent="10%"
app:layout_widthPercent="94%"
android:layout_centerHorizontal="true"
android:background="#drawable/datefrom_dateto_bottom_border"
android:id="#+id/periodTextView"/>
<ScrollView
android:id="#+id/scrollView"
app:layout_widthPercent="94%"
android:layout_below="#+id/periodTextView"
android:layout_centerHorizontal="true"
app:layout_marginBottomPercent="4%"
android:layout_width="match_parent">
<RelativeLayout <!-- This is where i want the fragments -->
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/daysContainer">
</RelativeLayout>
</ScrollView>
</android.support.percent.PercentRelativeLayout>
And here is the xml for the fragment I want to inflate into my daysContainer relative layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<android.support.percent.PercentRelativeLayout
android:gravity="center_vertical"
android:id="#+id/dayContainerOne"
android:layout_width="match_parent"
android:layout_height="35dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
app:layout_marginLeftPercent="2%"
android:text="placeholder"
android:id="#+id/weekOneTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="monospace"/>
</android.support.percent.PercentRelativeLayout>
<ViewStub layout="#layout/fragment_replace"
android:id="#+id/replaceOne"
android:layout_below="#id/dayContainerOne"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</ViewStub>
</RelativeLayout>
Try shifting ids up by 1. So your first id is not equal to 0. My guess is that BELOW doesn't work if given id equals 0.
Update to my comment.
Found the following line in RelativeLayout sources:
rules[BELOW] = a.getResourceId(attr, 0);
Where 0 is the default value, therefore it is ignored.
Related
I have seen some explanations on how to add two views next to each other in LinearLayout by using XML. How can this be done programatically?
I have the following two methods:
private void createListOfInstalledApplications() {
installedApplicationNames = this.getApplicationNames();
installedPackages = this.getPackageNames();
for(int i = 0; i < installedApplicationNames.size(); i++){
TextView scrollAppsView = new TextView(this);
scrollAppsView.setId(i);
scrollAppsView.setText(installedApplicationNames.get(i) + "\n ");
layout.addView(scrollAppsView);
}
}
This method adds the names of installed applications on the Android phone to the LinearLayout. On the right side of these applications, I would like to have a drop down menu where some settings can be chosen. For now the following methods adds the Spinners to the LinearLayout:
private void createDropDownMenuesForApplications() {
List<String> vibPatternNames = new ArrayList<String>();
VibrationPatternManager vibPatManager = VibrationPatternManager.getInstance();
List<VibrationPattern> vibrationPatterns = vibPatManager.getAllAvailablePatterns();
for(VibrationPattern vibrationPattern : vibrationPatterns){
vibPatternNames.add(vibrationPattern.getName());
}
for(int i = 0; i < installedApplicationNames.size(); i++){
Spinner dropDownMenu = new Spinner(this);
dropDownMenu.setId(i);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, vibPatternNames);
dropDownMenu.setAdapter(dataAdapter);
layout.addView(dropDownMenu);
}
}
The drop-down menus now appear below the application names. However, I would like them to appear to the right. So
"ApplicationName1 drop-down-menu1"
instead of
ApplicationName1
ApplicationName2
Drop-down-menu1
Drop-down-menu2
How do I do this?
Just do
layout.setOrientation(Horizontal)
I have done similar task as required by you, please have a look at my answer:
https://stackoverflow.com/a/33339420/5479863
in JSON response image URLs were provided so, at runtime I have to add views dynamically adjacent to each other.
Look at the answer to know more...
I hope that will help you...
Happy coding :)
Try the following:
1) MActivity.class--------------
public class MActivity extends AppCompatActivity {
private LinearLayout layout_horizontal;
private LinearLayout layout_vertical;
private LayoutInflater layoutInflater;
private FrameLayout fl;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
// Frame layout generic container --- root.
// Frame layout has as a direct child layout_vertical (Linear layout with orientation vertical).
// for each item inside installedApplicationNames, re-create a linear layout with orientation horizontal (layout_horizontal),
// with a TextView and Spinner and then add it as a direct child of the linear layout with orientation vertical.
fl = (FrameLayout) findViewById(R.id.fl);
layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.layout_vertical , null);
fl.addView(view);
layout_vertical = (LinearLayout) view.findViewById(R.id.ll_v);
populate();
}
private void populate() {
layout_vertical.removeAllViews();
List<String> installedApplicationNames = new ArrayList<String>();
for (int k = 0; k < 30; k++) {
installedApplicationNames.add("N" + k);
}
for (int i = 0; i < installedApplicationNames.size(); i++) {
View view1 = layoutInflater.inflate(R.layout.layout_horizontal , null);
layout_horizontal = (LinearLayout) view1.findViewById(R.id.ll_h);
TextView scrollAppsView = new TextView(this);
scrollAppsView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.WRAP_CONTENT));
scrollAppsView.setId(i);
scrollAppsView.setText(installedApplicationNames.get(i) + "\n ");
layout_horizontal.addView(scrollAppsView);
// TODO: use a certain method to get vibPatternNames for the current installedApplicationName
List<String> vibPatternNames = new ArrayList<String>();
for (int j = 0; j < 4; j++) {
vibPatternNames.add("S" + j);
}
Spinner dropDownMenu = new Spinner(this);
dropDownMenu.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
dropDownMenu.setId(i);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, vibPatternNames);
dropDownMenu.setAdapter(dataAdapter);
layout_horizontal.addView(dropDownMenu);
layout_vertical.addView(layout_horizontal);
}
}
}
2) layout.xml-----------
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_marginTop="5dp"
android:id="#+id/fl">
</FrameLayout>
</ScrollView>
3) layout_vertical.xml-----------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ll_v"
android:layout_gravity="center"
android:orientation="vertical">
</LinearLayout>
4) layout_horizontal.xml---------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/ll_h"
android:layout_gravity="center"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
how to create more TextView in relativelayout like
my code not showing
not like that :
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 10, 5, 10);
params.setMarginStart(40);
params.setMarginEnd(40);
for (int a = 0; a < alquran.getindokata().length; a++) {
textView = new TextView(context);
textView.setLayoutParams(params);
textView.setId(a);
textView.setGravity(Gravity.CENTER);
textView.setPadding(10, 5, 10, 5);
textView.setBackgroundColor(context.getResources().getColor(R.color.hitam_pudar));
textView.setText(alquran.getarabkata()[a] + "\n" + alquran.getindokata()[a]);
Log.d(TAG, "VerseID " + id_surat +
" getKata " + alquran.getindokata()[a]
);
holder.terjemahankata.addView(textView);
}
From the image you supplied, I suggest you a non-official Google library FlexBoxLayout for this. You got variety of options to choose from:
flexDirection
flexWrap
justifyContent
alignItems
alignContent
and many more!
You can dynamically add elements (TextView, in your case), in FlexBoxLayout, and it will get arranged as you need.
To add more textview to single layout you can use library “FlexboxLayout”.
Following is the code to implement this library to your project:
Add this to your build.gradle file
compile 'com.google.android:flexbox:0.2.6'
Add this to main layout file
<com.google.android.flexbox.FlexboxLayout
android:id="#+id/flexBoxLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:alignContent="flex_start"
app:alignItems="flex_start"
app:flexDirection="row"
app:flexWrap="wrap"
app:justifyContent="flex_start" />
Another textview file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/tvTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Test"
android:background="#drawable/rounded_corner_green"
android:padding="10dp"
android:textColor="#android:color/white"
android:textStyle="bold"
android:textSize="12sp" />
</LinearLayout>
Java Code
public class MainActivity extends AppCompatActivity {
private FlexboxLayout flexBoxLayout;
private List<String> List = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.flex_activity);
flexBoxLayout = (FlexboxLayout) findViewById(R.id.flexBoxLayout);
List.add("Test Content");
List.add("Content");
List.add("Test Content");
List.add("Test");
List.add("Test Content");
List.add("Content");
List.add("Test Content");
List.add("Test");
setView();
}
public void setView() {
flexBoxLayout.removeAllViews();
for (int i = 0; i < List.size(); i++) {
final int pos = i;
final View tagView = this.getLayoutInflater().inflate(R.layout.item_select_intrest_tag, null);
final TextView tvTag = (TextView) tagView.findViewById(R.id.tvTag);
tvTag.setText(List.get(i));
tvTag.measure(0, 0);
tvTag.getMeasuredWidth();
tvTag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Test Clicked", Toast.LENGTH_SHORT).show();
}
});
flexBoxLayout.addView(tagView);
}
}
}
I am trying to count all the views in any layout being displayed to the user. To achieve it, the code posted below was used, but as you see in the layout, it contains
Button
LinearLayout
Button
Button
TextView
LinearLayout
when the code is executed, it says that the layout has only 4 widgets "Button, LinearLayout, TextView, LinearLayout", while I expected the code to return 6 widgets.
would you please tell me how to get the total widgets contains in a layout including the sub-widgets/views?
code:
public class ActMain extends AppCompatActivity {
private static final String TAG = ActMain.class.getSimpleName();
private LinearLayout mMainContainer = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_main);
this.mMainContainer = (LinearLayout) findViewById(R.id.actMain_mainContainer);
int totalWidgets = this.mMainContainer.getChildCount();
Log.d(TAG, "totalWidgets:" + totalWidgets);
for (int i = 0; i < this.mMainContainer.getChildCount(); i++) {
View view = mMainContainer.getChildAt(i);
}
}
}
layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/actMain_mainContainer"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.alten.test_traversethroughaview_1.ActMain">
<Button
android:id="#+id/actMain_btn_change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/decision_change" />
<LinearLayout
android:id="#+id/actMain_linlay_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/actMain_btn_yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/decision_yes" />
<Button
android:id="#+id/actMain_btn_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/decision_no" />
</LinearLayout>
<TextView
android:id="#+id/actMain_tv_display"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/actMain_linlay_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
use this method:
public int getChildrenViews(ViewGroup parent){
int count = parent.getChildCount();
for (int i=0;i<parent.getChildCount();i++){
if (parent.getChildAt(i) instanceof ViewGroup){
count+=getChildrenViews((ViewGroup) parent.getChildAt(i));
}
}
return count;
}
anywhere you want add this part:
if (getWindow().getDecorView().getRootView() instanceof ViewGroup){
getChildrenViews((ViewGroup) getWindow().getDecorView().getRootView());
}
This function will probably help:
private int countChild(View view) {
if (!(view instanceof ViewGroup))
return 1;
int counter = 0;
ViewGroup viewGroup = (ViewGroup) view;
for (int i=0; i<viewGroup.getChildCount(); i++) {
counter += countChild(viewGroup.getChildAt(i));
}
return counter;
}
It only counts the leaves (ignores the ViewGroups). But if you want to count also them if is straightforward: just substitute counter = 0 with counter = 1.
Your code only counting its parent child, not all the child in the layout. That means you are counting Grandfather's child not Grandfather's Grandchild. You have to traverse all view to count all child. To do that you can use a recursive function to count all child.
getChildCount(View)
count = 1;
check the view is instance of ViewGroup
if it is ViewGroup
for each child
count+= getChildCount(child)
return count
Call this with root layout
totalChild = getChildCount(root)
Hope you can count all child this way.
I have a horizontal scroll view containing a linear layout.
Something like this:
<HorizontalScrollView
android:fillViewport="true"
android:id="#+id/product_hsv"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:measureAllChildren="false"
android:scrollbars="none">
<LinearLayout
android:id="#+id/product_container_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal" />
</HorizontalScrollView>
What I want to do is to add n number of the same layout to display
product collection in a Horizontal scroll view.
The picture below will give you an idea of what I have done so far.
For this, I have added product collection view to R.id.product_container_layout.
String[] productCollectionArr = new String[]{"Shirt", "Jewellary", "Moto X Play", "Ellis Flat"};
for (int i = 0; i < productCollectionArr.length; i++) {
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.product_container_layout);
View child = getLayoutInflater().inflate(R.layout.item_product, null);//child.xml
parentLayout.addView(child);
}
for (int i = 0; i < productCollectionArr.length; i++) {
LinearLayout eachProductLayout = (LinearLayout) findViewById(R.id.product_container_layout);
((TextView) eachProductLayout.findViewById(R.id.product_name_tv)).setText(productCollectionArr[i]);
}
But the problem is passing values. Since each view added to product_container_layout has the same ids. Therefore only first element gets the value from the product collection array.
What I can do is generate view id for each view and its element and map these ids to certain name so that I could access them by id.
Is this the correct methodology or should I do something else?.
You need to add this first :
<HorizontalScrollView
android:id="#+id/galleryImages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/chim_image_view_divider"
android:layout_marginTop="#dimen/twenty"
android:focusable="false"
android:layout_centerInParent="true"
android:focusableInTouchMode="false"
android:scrollbars="none" >
<LinearLayout
android:id="#+id/Images_scroller_view_linear_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
ImageScrollerViewLinearLayout = (LinearLayout) findViewById(R.id.Images_scroller_view_linear_layout);
ImageScrollerViewLinearLayout.removeAllViews();
- addImagesTo list:
void addImages() {
ImageScrollerViewLinearLayout.removeAllViews();
int i = 0;
final float scale = this.getResources().getDisplayMetrics().density;
int pixels = 0;
if (myImagesList != null && myImagesList.size() > 0) {
for (i = 0; i < myImagesList.size(); i++) {
ImageView imageView = new ImageView(this);
pixels = (int) (60 * scale + 0.5f);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(pixels, pixels);
params.setMargins(5, 0, 0, 0);
imageView.setLayoutParams(params);
imageView.setScaleType(ScaleType.CENTER_CROP);
imageView.setOnClickListener(new SelectNewSingleImageClickListener());
imageView.setImageResource(myImagesList.get(i));
ImageScrollerViewLinearLayout.addView(imageView, i);
}
}
}
So, you need to populate your view before adding to container or save links to all items. It will looks like this:
LinearLayout parentLayout=(LinearLayout)findViewById(R.id.linearLayout1);
for (int i = 0; i < items.size(); i ++) {
View child = getLayoutInflater().inflate(R.layout.item_grid, null);//child.xml
((TextView) child.findViewById(R.id.name)).setText(items.get(i).getName(); // where R.id.name is a textView in your child.xml layout.
parentLayout.addView(child);
}
after all, you can access to your elements by index, if you want to change it:
parentLayout.getChildAt(index)
But if you have a lot of items, it's not good idea. In this case you will not recycle your views and will consume a lot of memory.
I suggest to use recyclerview from support library.
add code in xml file
<org.lucasr.twowayview.TwoWayView
android:id="#+id/lvItems"
style="#style/TwoWayView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_toLeftOf="#+id/linearLayout3"
android:cacheColorHint="#android:color/transparent"
android:divider="#android:color/transparent"
android:drawSelectorOnTop="false"
android:padding="10dp" >
</org.lucasr.twowayview.TwoWayView>
You can use **DevSmart** library for horizontal listview which is similar to **ListView**.
https://github.com/dinocore1/DevsmartLib-Android
This is Relative Layout view which is created dynamically like this :
...
So my question is how to get the values from those dynamic elements and perform some calculation to show result on textview which is also dynamically created.
Thanks in advance guys!!
public void goButtonClicked(View view) {
maalContainer.removeAllViews();
int numberofPlayersTolayout = (Integer) Integer.parseInt((String) numberOfPlayers.getSelectedItem());
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < numberofPlayersTolayout; i++) {
View dynamicEntryView = inflater.inflate(R.layout.player_entry_item, null);
maalContainer.addView(dynamicEntryView, params);
}
}
}
for (int i = 0; i < maalContainer.getChildCount(); i++) {
View view = maalContainer.getChildAt(i);
EditText ed_item = (EditText) view
.findViewById(R.id.edittext1);
EditText ed_value = (EditText) view
.findViewById(R.id.edittext2);
EditText ed_value1 = (EditText) view
.findViewById(R.id.edittext3);
ed_item.getText().toString().trim();
ed_value.getText().toString().trim();
ed_value1.getText().toString().trim();
}
No need to assign/get ID of any View but the best way is to iterate through the child views of LinearLayout:
int childcount = myLinearLayout.getChildCount();
for (int i=0; i < childcount; i++){
View v = myLinearLayout.getChildAt(i);
// do whatever you would want to do with this View
}
A few things here:
You shouldn't be using the Application Context to get the LayoutInflater. You should get that from the Activity Context, otherwise your themes and styles will not be respected.
You shouldn't inflate with null as the second parameter (except in special instances where you don't know the View's parent). Just put the android:layout_width="match_parent" and android:layout_height="wrap_content" attributes in player_entry_item.xml and they will be respected upon inflation.
Store the EditText objects upon inflation as a private array.
With that said, my suggested revision:
private EditText[] mEditTextPlayers;
public void goButtonClicked(View view) {
maalContainer.removeAllViews();
int numPlayers = Integer.parseInt((String) numberOfPlayers.getSelectedItem());
LayoutInflater inflater = LayoutInflater.from(view.getContext());
mEditTextPlayers = new EditText[numPlayers];
for (int i = 0; i < numPlayers; i++) {
//Pass the parent as the second parameter to retain layout attributes
mEditTextPlayers[i] = inflater.inflate(R.layout.player_entry_item, maalContainer, false);
maalContainer.addView(dynamicEntryView);
}
}
public void goButtonClicked(View view) {
int numberofPlayersTolayout = Integer.parseInt(numberOfPlayers.getSelectedItem().toString());
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
maalContainer.removeAllViews();
maalText = new EditText[numberofPlayersTolayout];
points = new EditText[numberofPlayersTolayout];
result = new TextView[numberofPlayersTolayout];
for (int i = 0; i < numberofPlayersTolayout; i++) {
View dynamicEntryView = inflater.inflate(R.layout.player_entry_item, null);
maalContainer.addView(dynamicEntryView, params);
TextView playerName = (TextView) dynamicEntryView.findViewById(R.id.player_name_textview);
playerName.setText("Player :" + (i + 1));
maalText[i] = (EditText) dynamicEntryView.findViewById(R.id.player_item_edittext_maal);
points[i] = (EditText) dynamicEntryView.findViewById(R.id.player_item_edittext_point);
result[i] = (TextView) dynamicEntryView.findViewById(R.id.player_item_textview_result);
}
You can do it by using getId() and setId() methods of the View.
dynamicEntryView.setId(i);
and access the view as below by getting the
dynamicEntryView.getId(i);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/player_name_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10sp"
android:layout_marginTop="10sp"
android:text="Player name"
android:textColor="#202020"
android:textSize="18sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<EditText
android:id="#+id/player_item_edittext_point"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Points"
android:inputType="number"
android:textColor="#202020" />
<EditText
android:id="#+id/player_item_edittext_maal"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Maal"
android:inputType="number"
android:textColor="#202020" />
<TextView
android:id="#+id/player_item_textview_result"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0.00$"
android:textSize="22sp"
android:typeface="serif" />
</LinearLayout>
</LinearLayout>
Only one id is used. It will loop through according to number of players. Hope it will help you. #user2731584