Skip to content Skip to sidebar Skip to footer

How To Change Background Style/color Of Just One Item In Galleryview/gridview?

I have a GalleryView widget in my android app and I would like to change the background style/color of the central item in the widget, so that it stands out from the rest. Even bet

Solution 1:

Cerate a selector and set it as background of gallery items . in selector, set BackgroundColor and other features for selected state .

selector.xml

<itemandroid:state_focused="true"android:color="#000000" /><!-- focused --></selector>

gallery_item.xml :

<LinearLayout backgrond=selector.xml >

----

</LinearLayout >

Solution 2:

If you see the Android SDK source code, You should find the following directories and files:

/opt/android-sdk-linux/platforms/android-10/data/res/drawable-hdpi/gallery_*
/opt/android-sdk-linux/platforms/android-10/data/res/drawable/gallery_item_background.xml

You can copy these files to your project and create an adapter like this:

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

publicclassImageAdapterextendsArrayAdapter<ImageItem> {

    private Context context;
    privatefinalImageDownloaderimageDownloader=newImageDownloader();

    publicImageAdapter(Context context, int layoutItem,List<ImageItem> objects) {
        super(context, layoutItem, objects);
        this.context=context;
    }

    @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
        ImageViewimageView=newImageView(context);
        if (convertView == null) {  
            // if it's not recycled, initialize some attributes
            imageView = newImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setBackgroundResource(R.drawable.gallery_item_background);
        } else {
            imageView = (ImageView) convertView;
        }
        imageDownloader.download(getItem(position).thumbnail, imageView); // or similar function to set imageView.return imageView;
    }

}

Then edit xml and png files in resources directory like this:

<selectorxmlns:android="http://schemas.android.com/apk/res/android"><!-- When the window does not have focus. --><itemandroid:drawable="@drawable/gallery_selected_default"android:state_selected="true"android:state_window_focused="false"/><itemandroid:drawable="@drawable/gallery_unselected_default"android:state_selected="false"android:state_window_focused="false"/><!-- When the window does have focus. --><itemandroid:drawable="@drawable/gallery_selected_pressed"android:state_selected="true"android:state_pressed="true"/><itemandroid:drawable="@drawable/gallery_selected_focused"android:state_selected="true"android:state_focused="true"/><itemandroid:drawable="@drawable/gallery_selected_default"android:state_selected="true"/><itemandroid:drawable="@drawable/gallery_unselected_pressed"android:state_selected="false"android:state_pressed="true"/><itemandroid:drawable="@drawable/gallery_unselected_default"/></selector>

Solution 3:

You can set the backgorund colour (ur choice) in the specific layout for example you have 5 edit box 2 above 2 below and 1 in center whom u wanna change the background color u can do the following

<?xml version="1.0" encoding="utf-8"?>

<LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1.0"android:background="#000000"android:orientation="vertical"android:padding="5px" ><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><EditTextandroid:id="@+id/id1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0" /></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><EditTextandroid:id="@+id/id2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0" /></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:background="#435232"android:orientation="horizontal" ><EditTextandroid:id="@+id/id3"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0"android:digits="0123456789"android:maxLength="6" /></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><EditTextandroid:id="@+id/id4"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0" /></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><EditTextandroid:id="@+id/id5"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0"android:digits="0123456789"android:maxLength="11" /></LinearLayout></LinearLayout>

As you can see i hav changed the background colour of 3rd edit text in the same way u can also do that

Post a Comment for "How To Change Background Style/color Of Just One Item In Galleryview/gridview?"