I have 1 top layout and 3 bottom layouts, a user drags items from top layout to one of the 3 below.
The code below:
puts sequentially every image in every bottom layout into a list
removes items from each bottom layout
puts them back into top layout
public void onClick(View view) {
LinearLayout bottomLinearLayout1 = (LinearLayout) findViewById(R.id.bottom1);
LinearLayout bottomLinearLayout2 = (LinearLayout) findViewById(R.id.bottom2);
LinearLayout bottomLinearLayout3 = (LinearLayout) findViewById(R.id.bottom3);
LinearLayout topLinearLayout = (LinearLayout) findViewById(R.id.topLinear);
int total_num1 = bottomLinearLayout1.getChildCount();
int total_num2 = bottomLinearLayout2.getChildCount();
int total_num3 = bottomLinearLayout3.getChildCount();
View current_image = null;
List<View> listOfkids = new ArrayList<>() ;
//************ REPEATS **************
for(int i = 0 ; i < total_num1 ; i++){
current_image = bottomLinearLayout1.getChildAt(i);
listOfkids.add(current_image);
}
bottomLinearLayout1.removeAllViews();
for(int i = 0 ; i < listOfkids.size();i++){
topLinearLayout.addView( listOfkids.get(i));
}
listOfkids.clear();
//************ REPEATS **************
for(int i = 0 ; i < total_num2 ; i++){
current_image = bottomLinearLayout2.getChildAt(i);
listOfkids.add(current_image);
}
bottomLinearLayout2.removeAllViews();
for(int i = 0 ; i < listOfkids.size();i++){
topLinearLayout.addView( listOfkids.get(i));
}
listOfkids.clear();
//************ REPEATS **************
for(int i = 0 ; i < total_num3 ; i++){
current_image = bottomLinearLayout3.getChildAt(i);
listOfkids.add(current_image);
}
bottomLinearLayout3.removeAllViews();
for(int i = 0 ; i < listOfkids.size();i++){
topLinearLayout.addView( listOfkids.get(i));
}
Basically, the only difference between these loops is the last digit in "bottomLinearLayout"; the other code just copies itself!
Can i do this:
if(id == R.id.bottom1){
String current_layout = "bottomLinearLayout" + 1 ;
}
else if( id == R.id.bottom2){
current_layout = "bottomLinearLayout" + 2 ;
}
else if( id == R.id.bottom3){
current_layout = "bottomLinearLayout" + 3 ;
}
and then add this string as a command right into the java source code?
is this possible?
Assuming that you want to refactor the onClick method, you could create an array of the layout ids and iterate through them as follows:
int bottomLayoutIds[] = {R.id.bottom1, R.id.bottom2, R.id.bottom3};
LinearLayout topLinearLayout = (LinearLayout) findViewById(R.id.topLinear);
for (int layoutId : bottomLayoutIds){
LinearLayout bottomLinearLayout = (LinearLayout) findViewById(layoutId);
int childCount = bottomLinearLayout.getChildCount();
List<View> listOfKids = new ArrayList<>() ;
for(int i = 0 ; i < childCount ; i++){
View currentImage = bottomLinearLayout.getChildAt(i);
listOfKids.add(currentImage);
}
bottomLinearLayout.removeAllViews();
for(int i = 0 ; i < listOfKids.size();i++){
topLinearLayout.addView( listOfKids.get(i));
}
}
Related
How to Set Text for Textview for Particular Id which are generated dynamically in Linear Layout???
else if(j==4)
{
tvprodpcs_tot = new TextView(this);
tvprodpcs_tot.setInputType(InputType.TYPE_CLASS_NUMBER);
tvprodpcs_tot.setId(+i );
int totid=tvprodpcs_tot.getId();
tvprodpcs_tot.setBackgroundResource(R.drawable.edittext);
tvprodpcs_tot.setHeight(60);
tvprodpcs_tot.setPadding(0, 15,0, 15);
tvprodpcs_tot.setTextColor(Color.parseColor("#0000E6"));
tvprodpcs_tot.setBackgroundColor(Color.parseColor("#A2FF74"));
// tvprodpcs_tot.setVisibility(View.INVISIBLE);
// tvprodpcs_tot.setText("a");
tvprodpcs_tot.setLayoutParams(new LinearLayout.LayoutParams(0,90, 20));
tvprodpcs_tot.setGravity(Gravity.RIGHT);
linearLayout.addView(tvprodpcs_tot);
}
** I need to set text at child layout**
LinearLayout pLayout= (LinearLayout) findViewById(R.id.LinLayStkSub);
if (pLayout == null)
{
return;
}
TotClo=0; TotCloKg=0;TotPcs=0;
int rows=pLayout.getChildCount();
for(int i = id; i < pcount ; i++)
{
if ((pLayout.getChildAt(i) instanceof LinearLayout) )
{
LinearLayout SubLayout = (LinearLayout) pLayout.getChildAt(i);
for(int j = 0; j < SubLayout.getChildCount(); j++)
{ }
Try this,
TextView txtResMemberAge = new TextView(RegistrationPatientActivity.this);
txtResMemberAge.setLayoutParams(lparams);
txtResMemberAge.setText("Member Age: " +familyMemberModel.getStrMemberAge());
txtResMemberAge.setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf"));
txtResMemberAge.setTextSize(14);
txtResMemberAge.setTextColor(Color.parseColor("#9C9C9C"));
linearLayout.addView(txtResMemberAge);
lparams.setMargins(50, 10, 0, 0);
I'm making an layout with 400 imageView. The question is how I can set R.id.imageView>>i<<
for(int i = 1; i < 401; i++){
ImageView imageView = (ImageView)findViewById(R.id.imageView-i-);
}
for (int i = 1; i <= 400; i++) {
String s = "imageview" + i;
int id = getResources.getIdentifier(s, "id", getPackageName ());
ImageView imageview = (ImageView)findViewById (id);
}
This should work
My goal is to add a number of buttons (in a 4 column grid) to a RelativeLayout depending on how many "items" are in the database. When I was first learning how to add buttons to a RelativeLayout I just created 6 static buttons and added them the following way (ItemButton is simple class that extends Button):
private void loadItemButtons2(){
itemButtonLayout = (RelativeLayout)findViewById(R.id.itemButtonLayout);
itemButtonLayout.removeAllViews();
ArrayList<Item> items = db.getAllActiveItems();
ItemButton b1, b2, b3, b4, b5, b6;
b1 = new ItemButton(this, items.get(0));
b2 = new ItemButton(this, items.get(1));
b3 = new ItemButton(this, items.get(2));
b4 = new ItemButton(this, items.get(3));
b5 = new ItemButton(this, items.get(4));
b6 = new ItemButton(this, items.get(5));
RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams)b1.getLayoutParams();
params1.addRule(RelativeLayout.ALIGN_PARENT_START);
b1.setId(111);
b1.setLayoutParams(params1);
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams)b2.getLayoutParams();
params2.addRule(RelativeLayout.RIGHT_OF, 111);
b2.setId(222);
b2.setLayoutParams(params2);
RelativeLayout.LayoutParams params3 = (RelativeLayout.LayoutParams)b3.getLayoutParams();
params3.addRule(RelativeLayout.RIGHT_OF, 222);
b3.setId(333);
b3.setLayoutParams(params3);
RelativeLayout.LayoutParams params4 = (RelativeLayout.LayoutParams)b4.getLayoutParams();
params4.addRule(RelativeLayout.RIGHT_OF, 333);
b4.setId(444);
b4.setLayoutParams(params4);
RelativeLayout.LayoutParams params5 = (RelativeLayout.LayoutParams)b5.getLayoutParams();
params5.addRule(RelativeLayout.ALIGN_PARENT_START);
params5.addRule(RelativeLayout.BELOW, 111);
b5.setId(555);
b5.setLayoutParams(params5);
RelativeLayout.LayoutParams params6 = (RelativeLayout.LayoutParams)b6.getLayoutParams();
params6.addRule(RelativeLayout.RIGHT_OF, 555);
params6.addRule(RelativeLayout.BELOW, 222);
b6.setId(666);
b6.setLayoutParams(params6);
itemButtonLayout.addView(b1);
itemButtonLayout.addView(b2);
itemButtonLayout.addView(b3);
itemButtonLayout.addView(b4);
itemButtonLayout.addView(b5);
itemButtonLayout.addView(b6);
}
And this gives me a perfect result:
But this is the dynamic solution I came up with, and to me it looks like it is doing the exact same thing but the results come out super wonky:
private void loadItemButtons(){
itemButtonLayout = (RelativeLayout)findViewById(R.id.itemButtonLayout);
itemButtonLayout.removeAllViews();
ArrayList<Item> items = db.getAllActiveItems();
int colCount = 0;
int rowCount = 0;
int i = 0;
while(i < items.size()-1){
ItemButton newItemButton = new ItemButton(this, items.get(i));
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)newItemButton.getLayoutParams();
newItemButton.setId(i);
if(colCount == 0){
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
}else if(colCount == 1){
layoutParams.addRule(RelativeLayout.RIGHT_OF, i-1);
}else if(colCount == 2){
layoutParams.addRule(RelativeLayout.RIGHT_OF, i-1);
}else if(colCount == 3){
layoutParams.addRule(RelativeLayout.RIGHT_OF, i-1);
}
//If we are in any row except the top row, place in reference to the button above it
if(rowCount != 0){
layoutParams.addRule(RelativeLayout.BELOW, i-4);
}
newItemButton.setLayoutParams(layoutParams);
itemButtonLayout.addView(newItemButton);
if(colCount == 3){
colCount = 0;
rowCount += 1;
}else{
colCount += 1;
}
i++;
}
}
Is anyone able to see what I am doing incorrectly or different from the first example??? Any advice is much appreciated!
The reason this is failing for you is because of the IDs you are using. Android uses "reserved" ids for things like the general content area of the app.
Using your code, I was able to add 1000 to each ID and generate the intended result.
Do note my cleanup below:
private void loadItemButtons(){
itemButtonLayout = (RelativeLayout)findViewById(R.id.itemButtonLayout);
itemButtonLayout.removeAllViews();
List<String> items = itemList;
int colCount = 0;
int rowCount = 0;
// # of items per column
int colSpan = 4;
final int itemListSize = itemList.size();
for (int i = 0; i < itemListSize; i++) {
int id = 1000 + i;
ItemButton newItemButton = new ItemButton(this, items.get(i));
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
newItemButton.setId(id);
if(colCount == 0)
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
else
layoutParams.addRule(RelativeLayout.RIGHT_OF, id-1);
//If we are in any row except the top row, place in reference to the button above it
if(rowCount != 0)
layoutParams.addRule(RelativeLayout.BELOW, id-colSpan);
newItemButton.setLayoutParams(layoutParams);
itemButtonLayout.addView(newItemButton);
if(colCount == colSpan - 1)
rowCount += 1;
colCount = (colCount + 1) % colSpan;
}
}
Also, as mentioned in my comment on the original post, you really should use a gridview for this, it's what it was built for.
I can dig a hole with a pitchfork, but I bet a shovel would work better.
I didn't test the code:
private void loadItemButtons(){
itemButtonLayout = (RelativeLayout)findViewById(R.id.itemButtonLayout);
itemButtonLayout.removeAllViews();
ArrayList<Item> items = db.getAllActiveItems();
int colCount = 0;
int rowCount = 0;
int i = 0;
while(i < items.size())
{
ItemButton newItemButton = new ItemButton(this, items.get(i));
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)newItemButton.getLayoutParams();
newItemButton.setId(i);
if(colCount == 0)
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
else
layoutParams.addRule(RelativeLayout.RIGHT_OF, i-1);
//If we are in any row except the top row, place in reference to the button above it
if(rowCount != 0){
layoutParams.addRule(RelativeLayout.BELOW, i-4);
}
itemButtonLayout.addView(newItemButton, i, layoutParams);
if(colCount == 3){
colCount = 0;
rowCount += 1;
}else{
colCount += 1;
}
i++;
}
}
I have problem with creating header of column and row with respective cell values. Only show last position value in both column and row.
ScrollView sv = new ScrollView(this);
TableLayout ll = new TableLayout(this);
HorizontalScrollView hsv = new HorizontalScrollView(this);
String[] row = { "ROW1", "ROW2", "Row3", "Row4", "Row 5", "Row 6", "Row 7" };
String[] column = { "COLUMN1", "COLUMN2", "COLUMN3", "COLUMN4", "COLUMN5", "COLUMN6" };
int nor = row.length;
int noc = column.length;
TextView tv = new TextView(this);
tv.setText("Matrix Implemention Test");
for (int i = 0; i < nor; i++)
{ // for rows
TableRow tbrow = new TableRow(this);
for (int j = 0; j <= noc; j++)
{ // for columns
TextView tv1 = new TextView(this);
String s1 = Integer.toString(i);
String s2 = Integer.toString(j);
String s3 = s1 + s2;
int id = Integer.parseInt(s3);
// tv1[i][j].setId(id);
if (i == 0 & j==0) {
tv1.setText("0=0");
Log.d("TAH", "Display00!!!");
} else if (i == 0) {
for (int r = 0; r < nor; r++) {
tv1.setText(row[r]);
Log.d("TAG", "i==0->"+row[r]);
}
} else if (j == 0) {
for (int c = 0; c < noc; c++) {
tv1.setText(column[c]);
Log.d("TAG", "j==0->"+row[c]);
}
} else {
tv1.setText("Table Cell No=>> " + id);
}
tbrow.addView(tv1);
}
ll.addView(tbrow);
}
hsv.addView(ll);
sv.addView(hsv);
setContentView(sv);
Output like this: I want of respective header of both column and row. first row 1, row 2.... and column 1, column 2....and so on. but only last position value display above code.
change your code to:
for (int i = 0; i < nor; i++) { // for row
TableRow tbrow = new TableRow(this);
for (int j = 0; j <= noc; j++) { // for column
TextView tv1 = new TextView(this);
String s1 = Integer.toString(i);
String s2 = Integer.toString(j);
String s3 = s1 + s2;
int id = Integer.parseInt(s3);
// tv1[i][j].setId(id);
if (i == 0 & j==0) {
tv1.setText("0=0");
Log.d("TAH", "Display00!!!");
} else if (i == 0) {
tv1.setText(column[j]);
} else if (j == 0) {
tv1.setText(row[i]);
} else {
tv1.setText("Table Cell No=>> " + id);
}
tbrow.addView(tv1);
}
ll.addView(tbrow);
}
First thing you seems to have confused columns with rows so i corrected that.
And second notice this for loop:
for (int r = 0; r < nor; r++) {
tv1.setText(row[r]);
Log.d("TAG", "i==0->"+row[r]);
}
is missing.
Here you are first setting row[0] value to tv1 and then row[1] ... so on.
And in the end tv1 has row[6] as the last value.
i wanted to make an application in which i have to randomaly generate images but in order....
like :- APPLE.... i want to generate it as A_PP_E....
but i also want them uniqualy every time
final int[] imageViews = {
R.id.imageView2, R.id.imageView10, R.id.imageView3,
R.id.imageView4, R.id.imageView5, R.id.imageView6, R.id.imageView8 };
int[] photos={R.drawable.aa, R.drawable.pp
,R.drawable.ee,
R.drawable.pp_blue,R.drawable.ll};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// for randomizing the view
Random rng = new Random();
List<Integer> generated = new ArrayList<Integer>();
for (int i = 0; i < 5; i++)
{
while(true)
{
Integer next = rng.nextInt(5) ;
if (!generated.contains(next))
{
generated.add(next);
ImageView iv = (ImageView)findViewById(imageViews[i]);
iv.setImageResource(photos[next]);
break;
}
}
}
Step 1): put the images in the array in correct order as follows:
int[] photos={R.drawable.aa, R.drawable.pp, R.drawable.pp_blue, R.drawable.ll, R.drawable.ee}; // correct order APPLE
Step 2): Update your code as below:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Random rng = new Random();
List<Integer> generated = new ArrayList<Integer>();
// select `n` places which will be "BLANK"
int n = 2; // example `n=2`
for (int i = 0; i < n; i++)
{
while(true)
{
Integer next = rng.nextInt(5) ;
if (!generated.contains(next))
{
generated.add(next);
break;
}
}
}
// now `generated` has `n` random positions
// set these `n` positions as "BLANK" rest as "FILLED"
for (int i = 0; i < 5; i++)
{
ImageView iv = (ImageView)findViewById(imageViews[i]);
if(generated.contains(i)) {
// this was a random selected position
// set it blank or empty
iv.setImageBitmap(null);
}
else {
// set this image as the correct alphabet
iv.setImageResource(photos[i]);
}
}
}
i m doing this for getting blank images in another image view array....and its giving correct output
int n = 2; // example `n=2`
for (int i = 0; i < n; i++)
{
while(true)
{
Integer next = rng.nextInt(5) ;
Log.i("test","value:-"+next);
if (!generated.contains(next))
{
generated.add(next);
break;
}
}
}
for (int i1 = 0; i1 < 2; i1++)
{
ImageView ne = (ImageView)findViewById(nextimages[i1]);
ne.setImageResource(photos[generated.get(i1)]);Log.i("test","value:-"+photos[generated.get(i1)]);
}