user2707674
user2707674

Reputation: 333

multiple elements within same drawable

I'm trying to add several items within a single Drawable which would be used in a GridView: - text aligned at the center of the drawable - image at the background - a programmatically variable number (0-3) of images (stars) ALIGNED horizontally at the bottom of the drawable.

How could I accomplish this please? At the moment I'm trying to use a custom ImageView and set the background image using setImageResource. An alternative would be to use a TextView, however I still have no clue about the stars at the bottom.

Any help would be appreciated!

George.

Upvotes: 0

Views: 400

Answers (1)

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28541

You don't mean Drawable, you mean to say a View. You simply have to find a decent tutorial about making custom views for AdapterView (ListView, GridView, ...).

One such tutorial to get you started is http://www.learn-android-easily.com/2013/09/android-custom-gridview-example.html

Quick dump of the code:

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

   <GridView
        android:id="@+id/gridViewCustom"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:columnWidth="80dp"
        android:gravity="center"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth" />


</RelativeLayout>

grid_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginRight="10dp"
        android:src="@drawable/ic_launcher" >
    </ImageView>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:textSize="15sp" >
    </TextView>

</LinearLayout>

CustomGridViewMainActivity.java

public class CustomGridViewMainActivity extends Activity 
{


            GridView gridView;
            GridViewCustomAdapter grisViewCustomeAdapter;


            @Override
            protected void onCreate(Bundle savedInstanceState)
            {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);


                    gridView=(GridView)findViewById(R.id.gridViewCustom);
                    // Create the Custom Adapter Object
                    grisViewCustomeAdapter = new GridViewCustomAdapter(this);
                    // Set the Adapter to GridView
                    gridView.setAdapter(grisViewCustomeAdapter);

                    // Handling touch/click Event on GridView Item
                      gridView.setOnItemClickListener(new OnItemClickListener() {

                       @Override
                       public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
                           String selectedItem;
                           if(position%2==0)
                               selectedItem="Facebook";
                           else
                               selectedItem="Twitter";
                        Toast.makeText(getApplicationContext(),"Selected Item: "+selectedItem, Toast.LENGTH_SHORT).show();

                       }
                      });


               }

}

GridViewCustomAdapter.java

public class GridViewCustomAdapter extends ArrayAdapter
{
         Context context;



     public GridViewCustomAdapter(Context context) 
     {
             super(context, 0);
             this.context=context;

     }

     public int getCount() 
        {
                     return 24;
        }

     @Override
     public View getView(int position, View convertView, ViewGroup parent) 
     {
             View row = convertView;

             if (row == null) 
             {
                     LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                     row = inflater.inflate(R.layout.grid_row, parent, false);


                     TextView textViewTitle = (TextView) row.findViewById(R.id.textView);
                     ImageView imageViewIte = (ImageView) row.findViewById(R.id.imageView);

                     if(position%2==0)
                     {
                             textViewTitle.setText("Facebook");
                             imageViewIte.setImageResource(R.drawable.facebook);
                     }
                     else
                     {
                             textViewTitle.setText("Twitter");
                             imageViewIte.setImageResource(R.drawable.twitter);
                     }
             } 



      return row;

     }

}

Upvotes: 1

Related Questions