What I am trying to do is create N (in this case 9) copies of the ImageView object R.id.tile, place each of them at different coordinates on the layout, and give each its own unique identifier.
board_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/backgroundcolor"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/topbar"
... >
<ImageButton
android:id="#+id/imagebutton"
... />
</LinearLayout>
<RelativeLayout
android:id="#+id/board"
android:layout_width="match_parent"
android:layout_height="345dp"
android:layout_centerVertical="true"
android:background="#drawable/wwfboard" >
<view class="languid.legend.xsolver.DrawView"
android:id="#+id/myView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ImageView
android:id="#+id/tile"
android:layout_width="21dp"
android:layout_height="21dp"
android:src="#drawable/tile" />
</RelativeLayout>
</RelativeLayout>
BoardLayout.class:
#Override
protected void onCreate(Bundle savedInstance)
{ super.onCreate(savedInstance);
setContentView(R.layout.board_layout);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View[] tiles = new ImageView[9];
entire_layout = (RelativeLayout)findViewById(R.layout.board_layout);
for(int i = 0; i<tiles.length; i++){
tiles[i] = (ImageView) inflater.inflate(R.layout.board_layout, null);
tiles[i].setId(1000+i);
params = new RelativeLayout.LayoutParams(-1, 345);
params.leftMargin = 32*2*i;
params.topMargin = 34*2*i;
entire_layout.addView(tiles[i]);
}
...
However, I keep getting "Source not found" errors on the last line: layout.addView(tiles[i]);..
Any ideas why?
(Side question: is RelativeLayout better than AbsoluteLayout if you're working with coordinates?)
Edit:
First create a layout with one ImageView with the properties you like
Example:
create file in res/layout/singleimage.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_launcher"
android:contentDescription="Sample"/>
And then inflate the ImageView to get the copies of it like this
View[] tiles = new ImageView[9];
// get reference to LayoutInflater
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for(int i = 0; i<tiles.length; i++) {
//Creating copy of imageview by inflating it
ImageView image = (ImageView) inflater.inflate(R.layout.singleimage, null);
tiles[i] = image;
tiles[i].setId(i);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(-1, 345);
params.leftMargin = 32*2*3;
params.topMargin = 34*2*3;
layout.addView(tiles[i]);
}
You have set id to i value here
tiles[i].setId(i);
But you are (incorrectly) trying to get it from your other resource (the one you wanted to 'clone'):
layout.addView(tiles[i] = (ImageView) findViewById(R.id.tile)); // INCORRECT
Instead, set the id manually as above, and then:
layout.addView(tiles[i]);
And there is no need to call findViewById()
Related
I have an XML layout looking like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_weight="0.1"
android:text="1"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/child"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<!-- Define these into code
<TextView android:id="#+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="1"/>
<TextView android:id="#+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/first"
android:layout_marginLeft="5dp"
android:textSize="20sp"
android:text="1"/>-->
</RelativeLayout>
<TextView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_weight="0.1"
android:text="1"/>
</LinearLayout>
The TextViews inside LinearLayout are not meant to be changed. RelativeLayout also consists of TextViews which are aligned 5dp away from each other. Unlike the parent layout's ones, they need to be defined inside my Java activity. This is what I have tried:
private View getGeneratedView(String[] texts) {
LayoutInflater inflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View parentLayout = inflater.inflate(R.layout.parent, null);
RelativeLayout childLayout = (RelativeLayout) parentLayout.findViewById(R.id.child);
Integer lastViewId = null;
for (String text: texts) {
TextView displayedText = new TextView(this);
displayedText.setText(text);
displayedText.setTextSize(20);
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (lastViewId != null) {
params.addRule(RelativeLayout.RIGHT_OF, lastViewId);
params.leftMargin = 5;
}
displayedText.setLayoutParams(params);
lastViewId = displayedText.getId();
childLayout.addView(displayedText);
}
return parentLayout;
}
But what the code above does is putting all of the TextViews in the center, the one on top of the other like so:
(parameter texts was new String[] {"1", "1", "1", "1"})
And this is what I am trying to get:
Can anyone point out what I am doing wrong?
Even though you are generating a new TextView for each string in texts, you do not generate an id for it. As a result, displayedText.getId() will always be -1 (View.NO_ID) and lastViewId will never be other than -1.
To correct the code, you need to generate a new id for each generated TextView. With API 17+, you can use View.generateViewId() to do this.
private View getGeneratedView(String[] texts) {
LayoutInflater inflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View parentLayout = inflater.inflate(R.layout.parent, null);
RelativeLayout childLayout = (RelativeLayout) parentLayout.findViewById(R.id.child);
Integer lastViewId = null;
for (String text : texts) {
TextView displayedText = new TextView(this);
// Need API 17+ for generateViewId
displayedText.setId(View.generateViewId());
displayedText.setText(text);
displayedText.setTextSize(20);
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (lastViewId != null) {
params.addRule(RelativeLayout.RIGHT_OF, lastViewId);
params.leftMargin = 5;
}
displayedText.setLayoutParams(params);
lastViewId = displayedText.getId();
childLayout.addView(displayedText);
}
return parentLayout;
}
I am developing an Android App. In my app, I am adding view dynamically and setting its weight in code as well. But it is not working.
This is my XML for container of dynamic views:
<ScrollView>
.
.
.
<LinearLayout
android:orientation="vertical"
android:id="#+id/id_related_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"></LinearLayout>
.
.
.
</ScrollView>
This is how I am adding controls dynamically to it.
private void addRelatedItemViews(final ArrayList<Item> relatedItems)
{
LinearLayout subContainer= new LinearLayout(this);
LinearLayout.LayoutParams initialParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
subContainer.setLayoutParams(initialParam);
relatedItemsContainer.addView(subContainer);
for(int i=0;i<relatedItems.size();i++)
{
if(i>0 && i%2==0)
{
//initialize sub container
subContainer = new LinearLayout(this);
LinearLayout.LayoutParams subContainerParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
subContainer.setLayoutParams(subContainerParam);
relatedItemsContainer.addView(subContainer);
}
final int index = i;
View view = layoutInflater.inflate(R.layout.realated_item_view,null);
ImageView imageView = (ImageView)view.findViewById(R.id.iv_related_item_image);
TextView tvName = (TextView)view.findViewById(R.id.tv_related_item_name);
TextView tvPrice = (TextView)view.findViewById(R.id.tv_related_item_price);
TextView tvLikeCount = (TextView)view.findViewById(R.id.tv_related_item_like_count);
Picasso.with(getBaseContext()).load(relatedItems.get(i).getMediumImageUrl()).into(imageView);
tvName.setText(relatedItems.get(i).getName());
tvPrice.setText(CommonHelper.formatCurrency(relatedItems.get(i).getPrice()));
tvLikeCount.setText(CommonHelper.formatLikeCount(relatedItems.get(i).getLikeCount()) + " like");
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openItemActivity(relatedItems.get(index).getId());
}
});
LinearLayout itemContainer = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,1);
itemContainer.setLayoutParams(params);
itemContainer.addView(view);
subContainer.addView(itemContainer);
}
}
This is my realated_item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_weight="1"
android:padding="3dp"
android:layout_height="wrap_content">
<LinearLayout
android:background="#drawable/item_bg"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/iv_related_item_image"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:padding="5dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:textSize="17dp"
android:id="#+id/tv_related_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="3dp"
android:textSize="13dp"
android:id="#+id/tv_related_item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="3dp"
android:textSize="10dp"
android:id="#+id/tv_related_item_like_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
But it is not settings its weight properly. Actually, each row has two column and each column must be half of screen width. But it is not working.
This is the screenshot:
As you can see, it not taking screen half by half. How can I fix it?
Try this below in your realated_item_view.xml (wrap_content to fill_parent)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_weight="1"
android:padding="3dp"
And change your Java code:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,1);
itemContainer.setLayoutParams(params);
itemContainer.addView(view);
subContainer.addView(itemContainer);
But maybe your image will be stretched by the width. But it will fill half of the width as you want.
UPDATE:
I dont know why you tried failed with above solution. So I write a small demo for referencing. It uses ScrollView as you want. But I strongly recommend you should use GridView instead for better perfomance!!!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
LinearLayout containerLayout = (LinearLayout) findViewById(R.id.container_layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LayoutInflater inflater = getLayoutInflater();
for (int i = 0; i < 4; i ++){
LinearLayout subLayout = new LinearLayout(this);
subLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams subParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
for (int j = 0; j < 3; j ++){
View v = inflater.inflate(R.layout.test_item_layout, null, false);
if (j == 1){
ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.icon);
}
subLayout.addView(v, subParams);
}
containerLayout.addView(subLayout, params);
}
}
test_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:src="#mipmap/ic_launcher"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="100dip" />
<TextView
android:text="Test label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
test_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/container_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
</LinearLayout>
Screenshot:
Instead of ViewGroup.LayoutParams try it with LinearLayout.LayoutParams and also set layout width to 0
LinearLayout itemContainer = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1);
itemContainer.setLayoutParams(params);
It might work.
try this
use straggerdgridlayout manager
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
gaggeredGridLayoutManager = new StaggeredGridLayoutManager(column, 1);
recyclerView.setLayoutManager(gaggeredGridLayoutManager);
column replaces your requirement of column
LinearLayout lay1 = (LinearLayout) findViewById(R.id.lay1);
for(int i=0;i<list1.size();i++) {
View child = lay1.getChildAt(i);
LinearLayout lay3 = (LinearLayout) child.findViewById(R.id.lay3);
for (j = 0; j <list2.size(); j++) {
View child1 = lay3.getChildAt(j);
// View hiddenInfo11 = getActivity().getLayoutInflater().inflate(R.layout.add_inner_item, lay3 , false);
final TextView name = (TextView) child1.findViewById(R.id.name);
name.setText("new");
}
}
XML Layout:
<LinearLayout
android:id="#+id/lay1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="#+id/lay2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:background="#color/white"
android:orientation="horizontal">
</LinearLayout>
add_item.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:orientation="horizontal">
<LinearLayout
android:id="#+id/lay3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
add_inner_item.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:orientation="horizontal"
>
<TextView android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
Added View Dynamically using the following method.
for(int i=0;i<list.size();i++) {
View hiddenInfo = getActivity().getLayoutInflater().inflate(R.layout.add_item, lay, false);
LinearLayout lay3 = (LinearLayout) hiddenInfo.findViewById(R.id.lay3);
lay1.addView(hiddenInfo);
for (j = 0; j < list2.size(); j++) {
View hiddenInfo1 = getActivity().getLayoutInflater().inflate(R.layout.add_inner_item, lay3, false);
final TextView name = (TextView) hiddenInfo1.findViewById(R.id.name);
name.setText(list2.get(j).get("Name"));
lay3.addView(hiddenInfo1);
}
I have one Layout with creating dynamically and I want to update the child textview from outside of the button click view.I have tried this code.But nothing is changed.Can anyone give suggestion to this sample.
You are looking for "lay2" inside the "lay1" container. According to your XML layout, they are not nested, so you won't be able to find it this way. Change it to this:
LinearLayout lay2 = (LinearLayout) findViewById(R.id.lay2);
You should get the actual lay2 container. I'm not sure whether the text views are there though since I have seen your code for the views' creation.
EDIT:
I've not specified a parent - you should use the Activity for this. Something like:
LinearLayout lay2 = (LinearLayout) MyActivity.this.findViewById(R.id.lay2);
i have an ArrayList of usernames for each username i want to add a xml layout (layout_one_user.xml) and put the username String into each TextView (name_user) present in the layout inflated.
my code in the main Activity is this:
ll_counts = (LinearLayout) findViewById(R.id.ll_counts);
lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutInflater inflater_one_user = MainScreen.this.getLayoutInflater();
View layout_one_user = inflater_one_user.inflate(R.layout.layout_one_user, null);
TextView txt_counts = (TextView) layout_one_user.findViewById(R.id.name_user);
ImageView delete_icon = (ImageView) layout_one_user.findViewById(R.id.icon_delete);
ImageView edit_icon = (ImageView) layout_one_user.findViewById(R.id.icon_edit);
for (int i = 0; i < usernames.size(); i++) {
layout_one_user = new View(this);
txt_counts.setText( usernames.get(i) );
ll_counts.addView(layout_one_user);
}
the layout_one_user is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
>
<ImageView
android:id="#+id/icon_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:src="#drawable/ic_edit_black_24dp" />
<ImageView
android:id="#+id/icon_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_toLeftOf="#+id/icon_edit"
android:src="#drawable/ic_delete_black_24dp" />
<TextView
android:id="#+id/name_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/icon_delete"
android:layout_centerHorizontal="true"
android:text="text"
/>
and the layout in my main is:
<LinearLayout
android:id="#+id/ll_counts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/action_bar"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
</LinearLayout>
But it doesn't appear anything on the screen.
You inflate your custom layout to layout_one_user:
View layout_one_user = inflater_one_user.inflate(R.layout.layout_one_user, null);
but then, on the very first iteration of your loop, you overwrite this variable with an empty View:
layout_one_user = new View(this);
Thus when you add layout_one_user to your layout, you're adding an "empty view" and you see no result.
It seems to me that you need to inflate a new layout for each user:
for (int i = 0; i < usernames.size(); i++) {
View layout = getLayoutInflater().inflate(R.layout.layout_one_user, ll_counts, false);
TextView textView = (TextView) layout.findViewById(R.id.name_user);
textView.setText(usernames.get(i));
ll_counts.addView(layout);
}
I have an xml file (option_element.xml) that contains a ImageView and a TextView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="-18dp" >
<ImageView
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/option_button" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button"
android:layout_alignLeft="#+id/button"
android:layout_alignRight="#+id/button"
android:layout_alignTop="#+id/button"
android:layout_centerHorizontal="true"
android:gravity="center" />
</RelativeLayout>
I should fill a LinearLayout with this View, based on a content of an array
<?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="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/options_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" />
<!-- other layout elements -->
</LinearLayout>
I'm adding it like
LinearLayout options_layout = (LinearLayout) findViewById(R.id.options_list);
String[] options = getActivity().getResources().getStringArray(R.array.options);
for (int i = 0; i < options.length; i++) {
View to_add = inflater.inflate(R.layout.options_element,
options_layout);
TextView text = (TextView) to_add.findViewById(R.id.text);
text.setText(options[i]);
text.setTypeface(FontSelector.getBold(getActivity()));
}
But something gone wrong. I'm expecting options.length ImageView with relative TextView filled with options[i] text, but i obtain options.length ImageView, but only first one has text (and text it's not options[0] element, but last one).
For example, if option contains {"one", "two", "three"} inside first imageView i get "three", and other ones are empty.
How can put each string inside each TextView?
The inflate(int resource, ViewGroup root) method return root if root is not null, thus to_add.findViewById() equals options_layout.findViewById() and it always return the Views in the first position.
Change as following should help:
LinearLayout options_layout = (LinearLayout) findViewById(R.id.options_list);
String[] options = getActivity().getResources().getStringArray(R.array.options);
for (int i = 0; i < options.length; i++) {
View to_add = inflater.inflate(R.layout.options_element,
options_layout);
TextView text = (TextView) to_add.findViewById(R.id.text);
text.setText(options[i]);
text.setTypeface(FontSelector.getBold(getActivity()));
}
to:
LinearLayout options_layout = (LinearLayout) findViewById(R.id.options_list);
String[] options = getActivity().getResources().getStringArray(R.array.options);
for (int i = 0; i < options.length; i++) {
View to_add = inflater.inflate(R.layout.options_element,
options_layout,false);
TextView text = (TextView) to_add.findViewById(R.id.text);
text.setText(options[i]);
text.setTypeface(FontSelector.getBold(getActivity()));
options_layout.addView(to_add);
}