0

I want to create Buttons dynamically and place them on the layout, But it is giving me some error. I am doing this first time, so have very less idea about the things.

My Xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:id="@+id/parentRelativeLayout"

            <LinearLayout
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_height="fill_parent"
                    android:layout_width="fill_parent"
                    android:orientation="vertical"
                    android:id="@+id/deptKeyslayout">
     <com.hellovx100.in.HorizontalPager
        android:id="@+id/horizontal_pager"
        android:layout_width="fill_parent"
        android:layout_height="0px"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="310dp"
        android:layout_weight="1">
        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/keyslayout1">
            </RelativeLayout>

        </ScrollView>
        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/keyslayout2">
            </RelativeLayout>

        </ScrollView>
        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/keyslayout3">
            </RelativeLayout>

        </ScrollView>
        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/keyslayout4">
            </RelativeLayout>

        </ScrollView>
    </com.hellovx100.in.HorizontalPager>
        </LinearLayout>


   <!--  <include layout="@layout/keys_layout"
                 android:layout_toRightOf="@+id/subtotalbox"/> -->

</RelativeLayout>

Below is the class in which I am getting the list of records from the DB and Placing those buttons on the layout.My MainClass.java

package com.hellovx100.in;

import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.view.ViewPager.LayoutParams;
import android.util.Log;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.DigitalClock;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

public class MainSaleScreen extends Activity{
    private HorizontalPager mPager;
    private RadioGroup mRadioGroup;
    Cursor mCursor;
    DatabaseHandler db;
    RelativeLayout parentLayout;
    RelativeLayout keysLayout1;
    RelativeLayout keysLayout2;
    RelativeLayout keysLayout3;
    RelativeLayout keysLayout4;
    com.hellovx100.in.HorizontalPager horizontalPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_sale_screen);
        parentLayout=(RelativeLayout)findViewById(R.id.parentRelativeLayout);
        keysLayout1=(RelativeLayout)findViewById(R.id.keyslayout1);
        keysLayout2=(RelativeLayout)findViewById(R.id.keyslayout2);
        keysLayout3=(RelativeLayout)findViewById(R.id.keyslayout3);
        keysLayout4=(RelativeLayout)findViewById(R.id.keyslayout4);
        DigitalClock dc = (DigitalClock) findViewById(R.id.digitalClock1);
         mPager = (HorizontalPager) findViewById(R.id.horizontal_pager);
         mPager.setOnScreenSwitchListener(onScreenSwitchListener);
         db=new DatabaseHandler(this);
         try{
             // Reading all contacts
            Log.d("Reading: ", "Reading all contacts..");
            List<Contact> contacts = db.getAllContacts();  
          final Button dept[]=new Button[100];
         RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
            for (Contact cn : contacts) {
                int id=cn.getID();
                String deptName=cn.getName();
                if(id==1){
                    dept[id]=new Button(this);
                    dept[id].setText(deptName);
                    dept[id].setId(id);
                    keysLayout1.setLayoutParams(params);
                      keysLayout1.addView(dept[id]);
                                        }
                else if(id>=2 && id<=7 ){
                    dept[id]=new Button(this);
                    dept[id].setText(deptName);
                    dept[id].setId(id);
                    params.addRule(RelativeLayout.RIGHT_OF,dept[id].getId()-1);
                    keysLayout1.setLayoutParams(params);
                    keysLayout1.addView(dept[id]);
        }
                else if(id==8){
                    dept[id]=new Button(this);
                    dept[id].setText(deptName);
                    dept[id].setId(id);
                    keysLayout2.addView(dept[id]);
        }
                else if(id>=9 && id<=14){
                    dept[id]=new Button(this);
                    dept[id].setText(deptName);
                    dept[id].setId(id);
                    params.addRule(RelativeLayout.RIGHT_OF,dept[id].getId()-1);
                    keysLayout2.setLayoutParams(params);
                    keysLayout2.addView(dept[id]);
        }
                else if(id==15){
                    dept[id]=new Button(this);
                    dept[id].setText(deptName);
                    dept[id].setId(id);
                    keysLayout3.addView(dept[id]);
                    Log.d("I am in 5th if", ""+deptName);
                }
                else if(id>=16 && id<=21){
                    dept[id]=new Button(this);
                    dept[id].setText(deptName);
                    dept[id].setId(id);
                    keysLayout3.setLayoutParams(params);
                    params.addRule(RelativeLayout.RIGHT_OF,dept[id].getId()-1);
                    keysLayout3.addView(dept[id]);
                    Log.d("I am in 6th if", ""+deptName);
                }
                else{
                    Log.d("I am in else", ""+id);
                    String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber();
                    // Writing Contacts to log
                    Log.d("Name: ", log);
                }


            }

         }
         catch(Exception e){
            e.printStackTrace();
         }


     }
     private final HorizontalPager.OnScreenSwitchListener onScreenSwitchListener =
         new HorizontalPager.OnScreenSwitchListener() {
             @Override
             public void onScreenSwitched(final int screen) {
                 // Check the appropriate button when the user swipes screens.
                 switch (screen) {
                     case 0:
                        mPager.setCurrentScreen(0, true);
                         break;
                     case 1:
                        mPager.setCurrentScreen(1, true);
                       break;
                     case 2:
                        mPager.setCurrentScreen(2, true);
                        break;
                     default:
                         break;
                 }
             }
         };

     }

Now the Logcat is below

01-30 15:08:51.970: E/AndroidRuntime(274): FATAL EXCEPTION: main
01-30 15:08:51.970: E/AndroidRuntime(274): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.FrameLayout.onLayout(FrameLayout.java:288)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.ScrollView.onLayout(ScrollView.java:1205)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.view.View.layout(View.java:7035)
01-30 15:08:51.970: E/AndroidRuntime(274):  at com.hellovx100.in.HorizontalPager.onLayout(HorizontalPager.java:198)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.view.View.layout(View.java:7035)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.view.View.layout(View.java:7035)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.view.View.layout(View.java:7035)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.view.View.layout(View.java:7035)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.view.View.layout(View.java:7035)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.view.View.layout(View.java:7035)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.os.Looper.loop(Looper.java:123)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-30 15:08:51.970: E/AndroidRuntime(274):  at java.lang.reflect.Method.invokeNative(Native Method)
01-30 15:08:51.970: E/AndroidRuntime(274):  at java.lang.reflect.Method.invoke(Method.java:521)
01-30 15:08:51.970: E/AndroidRuntime(274):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-30 15:08:51.970: E/AndroidRuntime(274):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-30 15:08:51.970: E/AndroidRuntime(274):  at dalvik.system.NativeStart.main(Native Method)

Guide me that where I am doing wrong. I am getting the correct data from the db and it's working fine. Any help is appreciated.

2
  • 1
    why do you add layout params to keylayout 1...n when you have defined them in XML? it makes no sense. And also, I see your last exception is java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams Commented Jan 30, 2013 at 10:28
  • @Cristi you mean to say that if I have created the keylayout1 in xml file then no need to add the params into it. In that case how I apply the addRules on that layout ? Commented Jan 30, 2013 at 10:31

2 Answers 2

2

If u add button to relative layout in one place all the buttons will create. I mean second button will create on 1st button. take LinearLayout instead of relativelayout. then it will create one by one ..or.. side by side. based on your orientation.

Sign up to request clarification or add additional context in comments.

1 Comment

that's good idea but why that error occurred. It also seems to work.
2

When setting LayoutParams for a View, you need to define the LayoutParams object for its parent view. In this case, you are setting LayoutParams for keyslayout1 whose parent is a ScrollView. So you should use ViewGroup.LayoutParams(or LayoutParams object that works for ScrollView, whichever is correct) instead of RelativeLayout.LayoutParams.

1 Comment

This should definitely work. If your problem is solved, please don't hesitate to mark it as answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.