-
-
Save AVINASH21AK/5c8f8bbefd882b0092f31e50790e9188 to your computer and use it in GitHub Desktop.
Dynamically add a gridlayout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.testapp; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.CardView; | |
import android.support.v7.widget.GridLayout; | |
import android.support.v7.widget.Toolbar; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.ViewGroup; | |
import android.widget.ImageView; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
ViewGroup root = (ViewGroup) findViewById(R.id.content_main); | |
addCustomView(root); | |
} | |
private void addCustomView(ViewGroup root) { | |
GridLayout gridLayout = new GridLayout(this); | |
GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams(); | |
layoutParams.height = GridLayout.LayoutParams.MATCH_PARENT; | |
layoutParams.width = GridLayout.LayoutParams.MATCH_PARENT; | |
layoutParams.setMargins(10, 10, 10, 10); | |
gridLayout.setLayoutParams(layoutParams); | |
gridLayout.setColumnCount(3); | |
gridLayout.setRowCount(5); | |
int[] arr = {3, 4, 3, 2}; | |
int r = 0, c = 0; | |
for (int i=0; i<4; i++) { | |
for(int j=0; j<arr[i]; j++) { | |
CardView cardView = createChild(); | |
if( c == 3 ) { | |
c = 0; | |
r++; | |
} | |
ImageView imageView = new ImageView(this); | |
imageView.setImageResource(R.mipmap.ic_launcher); | |
imageView.setLayoutParams(new ViewGroup.LayoutParams(150, 150)); | |
GridLayout.Spec rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 1, 1f); | |
GridLayout.Spec colSpan = GridLayout.spec(GridLayout.UNDEFINED, 1, 1f); | |
// if(r == 0 && c == 0) { | |
// colSpan = GridLayout.spec(GridLayout.UNDEFINED, 1); | |
// rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 2); | |
// } | |
GridLayout.LayoutParams gridParam = new GridLayout.LayoutParams( | |
rowSpan, colSpan | |
); | |
cardView.addView(imageView); | |
gridLayout.addView(cardView, gridParam); | |
r++; c++; | |
} | |
} | |
root.addView(gridLayout); | |
} | |
private CardView createChild() { | |
CardView cardView = new CardView(this); | |
cardView.setCardElevation(2); | |
cardView.setUseCompatPadding(true); | |
ViewGroup.LayoutParams cvLayoutParams = new ViewGroup.LayoutParams( | |
ViewGroup.LayoutParams.WRAP_CONTENT, | |
ViewGroup.LayoutParams.WRAP_CONTENT | |
); | |
cardView.setLayoutParams(cvLayoutParams); | |
return cardView; | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment