Add Views Programatically with loop, First Element is not shown - java

i loop through an array of objects and add a custom view with the objects name to a horizontalscrollview for each. My problem is, that the first one always disappears.
Here is my Activity Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="lp.german.bischofshofpresenter.app.PraesentationsActivity">
<TextView
android:text="Hier wird das pdf gezeigt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"/>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:id="#+id/file_scroll_view"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/file_scroll_view_linear_layout"
android:orientation="horizontal"
android:layout_gravity="center">
</LinearLayout>
</HorizontalScrollView>
And I fill the LinearLayout with the following Code:
for(int i = 0; i<files.length; i++)
{
LayoutInflater vi = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.file_template, null);
TextView textView = (TextView)v.findViewById(R.id.file_template_text);
textView.setText(files[i].Name());
Log.v("FILENAME:",files[i].Name());
View fileList = findViewById(R.id.file_scroll_view_linear_layout);
((ViewGroup)fileList).addView(v, ((ViewGroup)fileList).getChildCount());
}
I get the following result:
First Objects name is File0, so this one is missing.
The really strange thing is, that there is an empty space on the right side that matches exactly the size of one of my elements.
See here:
I already checked with the Log output, that it loops through all 10 elements and its there, so I can
t explain that behaviour.
What might be the fault is the position I set in ((ViewGroup)fileList).addView(v, ((ViewGroup)fileList).getChildCount()); but i don't know whats wrong here.
Thanks in forward ;)

Related

How to make FOR LOOP for changing color of text that has been allready through loop?

After searching for hours, I still cant understand how to make this working.
I have some numbers: https://prnt.sc/qaul4u
and a lop that is increasing number size as you can see on a picture.
I want to make another for lop that is turning PAST numbers in GREEN color, so when that number is over and loop goes to a next, past number turns out to a GREEN color, like a checking algorithm.
MainActivity.java (FOR LOOP)
int[] counts;
int currentCount;
TextView countText;
private void createCountsView() {
countsList.removeAllViews();
for(int i = 0; i<counts.length; i++){
int res = currentCount == i ? R.layout.workout_count_current : R.layout.workout_count;
View row = inflater.inflate(res,null);
TextView count = (TextView)row.findViewById(R.id.w_count_value);
count.setText(String.valueOf(counts[i]));
countsList.addView(row);
}
}
Workout_count.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/w_count_value"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="5sp"
android:layout_marginEnd="296dp"
android:textColor="#ff0000"
android:textSize="25dp" />
</LinearLayout>
Workout_count_current.xml - increasing text size and changing color to white
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/w_count_value"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="5sp"
android:layout_marginEnd="296dp"
android:textColor="#ffffff"
android:textSize="35dp"
android:textStyle="bold" />
</LinearLayout>

How add first two character in Textdrawable library?

Hello friend I am using compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1' this library in my project i want add first two character inTextDrawableicon if anyone know about this library please help how add first two character of my text inTextDrawableicon here is my code my it show only first character of myTextViewinTextDrawable` icon but I want it show first two character please help me?
//get first letter of each String item
String firstLetter = String.valueOf(getItem(position).charAt(0));
ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
// generate random color
int color = generator.getColor(getItem(position));
TextDrawable drawable = TextDrawable.builder().buildRound(firstLetter,//radius in px color);
My 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="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:id="#+id/image_view"
android:src="#drawable/ic_home_black_24dp"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:id="#+id/text"
android:paddingLeft="20sp"
android:textSize="22sp"
android:fontFamily="sans-serif"
android:textColor="#color/darkcolor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="John" />
</LinearLayout>
Use substring() instead of chaAt()
String firstTwoChars = getItem(position).subString(0,2);

Android list view wrap content until a certain size based on footer size

I am working on an android application.
In an XML Layout I need to do the following:
I have a list view at the top (listViewProducts) , and another Relative view under it (receiptSection).
The list view should take as much space as it has items. And the rest is taken by the receiptSection.
So for example if I have 2 items in the listViewProducts:
The list view is as big as the 2 items and the rest is taken by the receiptView.
If I add another item, the list view now take more space and push the receiptView lower:
However if I add a lot more items, I want the list view height to stop growing to leave a minimum height for the receiptView that cannot go smaller:
As you see in the picture, the receiptVIew has a minimum height of 50dp. once the receipt view get to that height, it should stop shrinking and now the list view has a fixed size based on the remaining of the space. The rest will be scrollable.
What I have tried
I created a list view. I have android:layout_alignParentTop="true" and android:layout_height="wrap_content".
This will make it grow with its content and its at the top of the view
<ListView
android:id="#+id/listViewProducts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
</ListView>
THen I created a RelativeLayout that will hold the checkout_receipt_view that is in a seperate xml layout file.
For this view I have android:layout_alignParentBottom="true" and android:layout_below="#id/listViewProducts" that will make it go under the list view and align with the bottom of the view.
I also used android:minHeight="50d" in order to set the minimum height of the receiptSection.
<RelativeLayout
android:id="#+id/receiptSection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="#id/listViewProducts"
android:minHeight="50dp" >
<include layout="#layout/checkout_receipt_view" />
</RelativeLayout>
The listViewProducts is growing with the items, and the receiptView is taking the remaining space correctly.
The problem
however the minimum height did not work. The list view keeps on growing infinitely and the receiptSection will be pushed out of the view.
Is there a way I can make the listView stop growing when the receiptView reaches 50dp?
Thanks a lot for any help.
Unfortunately I think your best bet is to do this by making a custom view that extends ListView and overrides onMeasure.
public class CustomView extends ListView {
#Override
int maxHeight = 0;
View parentView = (RelativeLayout) (or whatever) getParent();
if (parentView != null){
maxHeight = parentView.getHeight() - 50;
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
Try swapping the 'layout_below'.
What you are actually saying is the following: please put my relativelayout BELOW the listview. If you want your listview to respect the height of the relativelayout, you'll have to say in the listview:
<ListView
android:id="#+id/listViewProducts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/receiptSection"
android:layout_alignParentTop="true" >
</ListView>
And your relativelayout:
<RelativeLayout
android:id="#+id/receiptSection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:minHeight="50dp" >
<include layout="#layout/checkout_receipt_view" />
</RelativeLayout>

How do i make a 4x4 grid of imageviews?

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:src="#drawable/music" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:src="#drawable/music" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:src="#drawable/music" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:src="#drawable/music" />
</TableLayout>
This is the original XML, i just need to add more. i have an imageView that's big , so it needs to be shrunk down and copied 16 times into a 4x4 grid. I can only get it to go 4 images in one column
I don't understand the problem with the large image, but I will tell you my suggestion:
there are multiple possible solutions:
Since you have 16 imageViews that you wish to create, you can use a GridView together with a BaseAdapter . If it's important for you to see it in the xml, use isInEditMode for a custo GridView, and set the adapter there to be your adapter with fake items. You should be aware of problems with the sizes of the columns/rows on the gridView , especially when changing orientations.
Another alternative could be the GridLayout
If you insist on using the TableLayout, you can have 4 TableRow instances, each has a weight of 1 . in each of them , add 4 imageViews and there each has a weight of 1.
Add 4 TableRow's and put your ImageView's into them.
Or you can create this grid from code. Like this:
LinearLayout container = null;
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1);
for (int i = 0; i < 16; ++i) {
if (i % 4 == 0) {
container = new LinearLayout(getActivity());
container.setOrientation(LinearLayout.HORIZONTAL);
mGrid.addView(container);
}
LinearLayout view = (LinearLayout) mLayoutInflater
.inflate(R.layout.view_item, container, false);
//populate the view in loop
view.setLayoutParams(params);
container.addView(view);
}

Change ImageView size programmatically

I have a relative layout with 3 ImageViews. The first one is a square image, the second is just used for spacing, and the third one is another square image.
Here is the xml code for that layout:
<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/radioSV"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/radioLayout"
android:orientation="vertical"
android:gravity="center">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:id="#+id/sssImageView"
android:src="#drawable/radio_sss_400_r"
android:layout_width="10sp"
android:layout_height="10sp"
android:layout_gravity="center"
android:scaleType="centerCrop">
</ImageView>
<!-- spacing -->
<ImageView
android:id="#+id/spacing1"
android:background="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="10dip"
android:layout_below="#id/sssImageView">
</ImageView>
<ImageView
android:id="#+id/gaImageView"
android:src="#drawable/radio_ga_400_r"
android:layout_width="10sp"
android:layout_height="10sp"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:layout_below="#id/spacing1">
</ImageView>
</RelativeLayout>
</LinearLayout>
</ScrollView>
Now, I want my app to be useful for various screen densities, so therefore, in the java file, I ask for the density and use a switch-case statement afterwards. In this statement, the size (width, height) of the 2 ImageViews (sssImageView and gaImageView) have to be changed.
For instance, if the density of the screen is high, I want the width and height of the ImageViews to be 200sp. I've just put 10sp in the xml as a 'standard' value.
This is the part of the use-case statement for high screen density:
case DisplayMetrics.DENSITY_HIGH:
layoutParams = new RelativeLayout.LayoutParams(val_high, val_high);
sssImageView.setLayoutParams(layoutParams);
sssImageView.setMaxHeight(val_high);
sssImageView.setMaxWidth(val_high);
gaImageView.setLayoutParams(layoutParams);
gaImageView.setMaxHeight(val_high);
gaImageView.setMaxWidth(val_high);
break;
Also, val_high is 200sp:
int val_high = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 200, this.getResources().getDisplayMetrics());
Now, the part with the ImageView sssImageView works perfectly. It correctly enlarges the image from 10sp to 200sp (don't worry - the image is not 10sp originally!).
The problem is the fact that the other ImageView, gaImageView, puts itself on top of sssImageView and destroys the layout. If I comment out the 3 lines with gaImageView, it is on its correct place in the layout, but is still small (10sp) of course.
I have also tried the opposite: commenting out the 3 lines with sssImageView and only manipulating gaImageView. Now the top ImageView (sssImageView) is on its correct place and small as expected. However, the bottom ImageView, gaImageView, positions itself on top of sssImageView, and not below as written in the xml layout file.
What is wrong here?
I managed to solve this problem.
Here is the code for the DENSITY_HIGH case statement:
case DisplayMetrics.DENSITY_HIGH:
display = getWindowManager().getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
// 0.36 is a scaling factor
int val_high = (int) (height*0.36);
sssImageView.setId(1);
gaImageView.setId(2);
spacing.setId(3);
layoutParams = new RelativeLayout.LayoutParams(val_high, val_high);
layoutParams2 = new RelativeLayout.LayoutParams(val_high, val_high);
layoutParams3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
(int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 10, this.getResources().getDisplayMetrics()));
sssImageView.setMaxHeight(val_high);
sssImageView.setMaxWidth(val_high);
relativeLayout.updateViewLayout(sssImageView, layoutParams);
layoutParams3.addRule(RelativeLayout.BELOW, sssImageView.getId());
relativeLayout.updateViewLayout(spacing, layoutParams3);
layoutParams2.addRule(RelativeLayout.BELOW, spacing.getId());
gaImageView.setMaxHeight(val_high);
gaImageView.setMaxWidth(val_high);
relativeLayout.updateViewLayout(gaImageView, layoutParams2);
break;
So generally, it looks like I had to "re-create" the xml file programmatically.

Categories

Resources