Sandip Armal Patil
Sandip Armal Patil

Reputation: 5905

How to set text to button in scrollview by programmatically?

I created one Linear layout inside scroll view (Horizontal) in xml with number of button.
I also have one table "TABLE_SUBJECT" in database.

I need to set the subject to button from database but I don't understand how to do that. I searched a lot of but didn't find anything helpful.

Please provide me some hint or reference.

Here's my code:

Main.xml

<HorizontalScrollView android:layout_width="fill_parent"
 android:id="@+id/horizontalScrollView1"
  android:background="@color/myMaroonColor" 
  android:layout_height="wrap_content">
    <LinearLayout android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent"
     android:layout_height="62dp" >
     <!--  android:background="@drawable/qaapti"-->

        <Button android:textSize="15px" android:id="@+id/button9" android:gravity="center|bottom" android:textColor="@color/myWhiteColor" android:drawableTop="@drawable/math" android:text="@string/HomePage_Math" android:background="@color/myMaroonColor" android:layout_width="54dp" android:layout_height="wrap_content" android:clickable="true"></Button>
        <Button android:textSize="15px" android:id="@+id/button11" android:gravity="center|bottom" android:layout_width="54dp" android:textColor="@color/myWhiteColor" android:drawableTop="@drawable/stat" android:text="@string/HomePage_Statstics" android:background="@color/myMaroonColor" android:layout_height="wrap_content"></Button>
        <Button android:textSize="15px" android:id="@+id/button10" android:gravity="center|bottom" android:textColor="@color/myWhiteColor" android:drawableTop="@drawable/biology" android:text="@string/HomePage_Biology" android:background="@color/myMaroonColor" android:layout_width="55dp" android:layout_height="wrap_content"></Button>
        <Button></Button>
                -
                -
                -
                -
         <Button></Button>
        <Button android:textSize="15px" android:id="@+id/button7" android:gravity="center|bottom" android:textColor="@color/myWhiteColor" android:drawableTop="@drawable/physics" android:text="@string/HomePage_Physics" android:background="@color/myMaroonColor" android:layout_width="50dp" android:layout_height="wrap_content"></Button>
        <Button android:textSize="15px" android:id="@+id/button6" android:gravity="center|bottom" android:textColor="@color/myWhiteColor" android:text="@string/HomePage_Computer" android:background="@color/myMaroonColor" android:layout_width="58dp" android:layout_height="46dp" android:drawableTop="@drawable/computer"></Button>                  
      </LinearLayout>
</HorizontalScrollView>

MySQLiteHelper.java

public List<ObjectiveWiseQuestion>getAllSubject()
 {
     List<ObjectiveWiseQuestion>LocwiseProfileList=new ArrayList<ObjectiveWiseQuestion>();
     String selectQuery= "SELECT  * FROM " + TABLE_SUBJECT;
     SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst())
        {
            do {
                ObjectiveWiseQuestion owq= new ObjectiveWiseQuestion();
                owq.setSubjectName(cursor.getString(1));
                LocwiseProfileList.add(owq);
              }
            while(cursor.moveToNext());
            db.close();
        }
        return LocwiseProfileList;       
 }

Upvotes: 1

Views: 1189

Answers (1)

waqaslam
waqaslam

Reputation: 68177

you need to use button's method setText():

btn.setText("whatever_your_text");

Update

in your scenario, this how you can do it dynamically:

    List<ObjectiveWiseQuestion> tmp = getAllSubject();
    int b = 1; //set the starting value as per your layout
    for (ObjectiveWiseQuestion i : tmp) {
        int btId = getApplicationContext().getResources()
                            .getIdentifier("button" + b, "id", getPackageName());

        Button btn = (Button) findViewById(btId);
        btn.setText(i.getSubjectName() + b);

        b++;
    }

Upvotes: 4

Related Questions