LayoutParams not working - java

I am programming an app which generates all of its layout programmatically. I am looking to place an array of ImageView[] depending on their index. But it looks like the LayoutParams doesn't do anything since all the ImageViews are being superposed. Here is the code I am using:
for (int i=0; i<tiles; i++){
Log.i("Setting up tile", "#" + i);
tileParams.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
tileParams.removeRule(RelativeLayout.ALIGN_PARENT_START);
tileParams.removeRule(RelativeLayout.BELOW);
tileParams.removeRule(RelativeLayout.RIGHT_OF);
if (((double)i/columns) == Math.round(i/columns)){
if (i == 0){
tileParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
tileParams.addRule(RelativeLayout.ALIGN_PARENT_START);
} else {
tileParams.addRule(RelativeLayout.BELOW, tileView[i-columns].getId());
tileParams.addRule(RelativeLayout.ALIGN_PARENT_START);
}
} else {
tileParams.addRule(RelativeLayout.RIGHT_OF, tileView[i-1].getId());
}
tileView[i].setLayoutParams(tileParams);
grid.addView(tileView[i]);
}
Here, "grid" is a RelativeLayout and "tileParams" is a RelativeLayout.LayoutParams.
PS: I know that the if
methods are used correctly at the required time, and the ImageViews have their ids set using iv.setId(View.generateViewId()).

Related

Recycle-view different column number

Hello Can i make like this image with recycle-view layout manger?
and this image show what i want to do thank in advance.
Use ReycyleView using GridLayoutManager
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
if (position == 0) {
return 2; // Splitting into 2 columns
} else {
return 3; // Splitting into 3 columns
}
}
});
Yes u can ...in recycler_adapter check if position == 0 then make the two cardsview visible and set them... and in else {cardview4 ,cardview5,cardview6 visible and set them and make cardview1 and cardview2 Gone the inflate layout will contain a relative lat
yout with two horizontal linearlayouts just hide make the layouts gone and visible at proper position in onBind()
for achieving this view i think GridLayoutManager with it's setSpanSizeLookup() method is the best choice. you can find complete explanation in this answer. In case you face any problem while implementing you can ask.

Change Layout position from code

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

Remove space between childrens in a LinearLayout

I'm developing an Android game for fun and I can't remove the space between my ImageViews in my LinearLayouts.
There isn't a defined number of ImageViews in these layouts, that's why I don't deal with XML files.
So I've created every ImageViews in my Java code and I've added them in my Layout.
But there is blank spaces between each of them : Spaces between ImageViews (Blue/red/green squares are ImageViews).
I started with a GridLayout to do that but I can't remove that blank space so I try with some LinearLayouts (Horizontal) into other LinearLayout (Vertical).
I've tried a lot of thing like setMargin and Padding to 0, create a LayoutParams to remove them, etc .. but it didn't work.
Thank you !
EDIT : here's my code
iv = new ImageView[cm.getNbRow()][cm.getNbColumn()]; //cm is the map object
LinearLayout[] linearTab = new LinearLayout[cm.getNbColumn()];
for(int i=0; i<cm.getNbRow(); i++) {
linearTab[i] = new LinearLayout(this);
linearTab[i].setOrientation(LinearLayout.HORIZONTAL);
}
for(int i=0; i<cm.getNbRow(); i++) {
for(int ii = 0; ii < cm.getNbColumn(); ii++) {
if(cm.getMap()[i][ii] == 1) {
iv[i][ii] = new ImageView(this);
iv[i][ii].setImageResource(R.drawable.wall);
} else if(cm.getMap()[i][ii] == 2) {
p = new Player(i, ii, this, cm);
iv[i][ii] = new ImageView(this);
iv[i][ii].setImageResource(R.drawable.player);
} else if(cm.getMap()[i][ii] == 3) {
iv[i][ii] = new ImageView(this);
iv[i][ii].setImageResource(R.drawable.stop);
} else {
iv[i][ii] = new ImageView(this);
iv[i][ii].setImageResource(R.drawable.path);
}
linearTab[i].addView(iv[i][ii]); //Horizontal LinearLayout
}
ll.addView(linearTab[i]); //Vertical LinearLayout
}
EDIT 2 : Android Studio adds transparents borders to the ImageViews (don't know why tho) but I've solved it by editing the generated image and coloring the transparents parts.
When you import an image, you need to make sure that the option "Trim" is set to "yes" and "Padding" is set to 0%. That should ensure that the image doesn't have a transparent region around it.
Have you tried with android:adjustViewBounds="true"?

bug z-index ViewPager with PageTransformer doesn't work with getChildDrawingOrder

I'm looking for a solution since too long now and I feel stuck. So I decided to write my first post on stackoverflow :)
I'm trying to do a CoverFlow for my app in android 4.0 and higher, after some research I decided to do that with a ViewPager, to increase the OffscreenPageLimit, and to put a negative margin and in bonus a little PageTransformer for the effect.
So my bug is about the z-index when the margin is too negative one image overlay an other, because of the drawing order, the image on the left is on the top and it's not pretty at all.
See bug here --> http://img33.imageshack.us/img33/2448/r1l1.jpg
So after some research the solution appear to be to overwrite the getChildDrawingOrder method in my ViewPager class. I did it like that :
private void setChildDrawingOrder() {
int count = getChildCount();
positions = new SparseIntArray(count);
for (int i=0, j=0 ; i<count ; i++) {
if (i == position) {
positions.put(count-1, i);
} else {
j++;
positions.put(count-1-j, i);
}
}
mNeedsToSetChildDrawingOrder = false;
}
#Override
protected int getChildDrawingOrder(int childCount, int i) {
if(i == 0 && mNeedsToSetChildDrawingOrder) setChildDrawingOrder();
return positions.get(i);
}
Where positions is an SparseIntArray, where the item on the top got the higher value (childCount-1). I fill it in the onPageSelected (method implement with OnPageChangeListener on my ViewPager) to get the position of the item that i want it to be on the top.
#Override
public void onPageSelected(int position) {
if (lastPosition != position) {
lastPosition = position;
ViewPager viewPager = ((MainActivity)mAdapterContext).getViewPager();
viewPager.setPosition(position);
}
}
I can't see where is my mistake! I also tried something with bringChildToFront in the onPageSelected without any success, probably because the PageTransformer redraw everything after the onPageSelected is called.
Any idea ?
Thanks
EDIT 15/10/13 :
I edit my code because I didn't catch what the method do very weel! It's "to get the index of the child to draw for that iteration." and not "when should I draw child i?" (thx to this post)
I also had some NullPointerException` because the count where not everytime the same so I move it into my ViewPager.
It's way better but still have some drawing bug! My positions SparseIntArray look good so I don't understand. It seems to appear kind of randomly...

Android - Loading a different Layout depending on value selected from a Spinner

I have a spinner on my home page and when I select a value from the spinner and hit submit my next page loads up with several text boxes.
However, how can I get it so that differnt layouts are displayed according to the value selected from the spinner. I am trying to currently use if statements, however this only works for the first selection, any other causes the app to error out (The application has stopped unexpectedly ... Force close:
String RefType = getIntent().getStringExtra("REFTYPE");
if (RefType.equals("spinner_value_1"))
{
setContentView(R.layout.layoutvalue1);
}
else if (RefType.equals("spinner_value_2"))
{
setContentView(R.layout.layoutvalue2);
}
Any help would be much appeciated.
Thanks
I imagine the error would be because you'd be trying to set the contentView twice, so you could try this:Add int layoutToLoad = 0;*(change here) to the top of your class, this will help us determine what to load. Then in your onCreate:
if (layoutToLoad == 0)
{
setContentView(R.layout.choiceLayout); //whatever the layout with the spinner is
//alternatively you can make the spinner via code
}
else if (layoutToLoad == 1))
{
setContentView(R.layout.layout1);
}
else if (layoutToLoad == 2))
{
setContentView(R.layout.layout2);
}
//etc
then where you're handling your onOptionSelected:
String RefType = getIntent().getStringExtra("REFTYPE");
if (RefType.equals("spinner_value_1"))
{
layoutToLoad = 1;
onCreate(null);
}
else if (RefType.equals("spinner_value_2"))
{
layoutToLoad = 2;
onCreate(null);
}
//etc

Categories

Resources