Search code examples
androidxmlbitmapdrawerlayout

adding an icon for each row of listView into drawer


i want to be very clear on explain everything,as title says, how i can insert icon next to each row,but an image not taken from drawable but by URL

i know to achieve this task with a drawable, but i'm not figuring out on how to do this with an URL image like:

URL url = new URL(stringURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
overlay = BitmapFactory.decodeStream(input);
connection.disconnect();

so it's a bitMap, not a drawable eg.(.png,.jpg...),

my activity:

public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ListView mDrawerListR;
private ActionBarDrawerToggle mDrawerToggle;
private LinearLayout rightDrawerLinearLayout;
private String[] drawerItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    drawerItems = getResources().getStringArray(R.array.array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, drawerItems));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    rightDrawerLinearLayout = (LinearLayout) findViewById(R.id.right_drawer_ll);
    mDrawerListR = (ListView) findViewById(R.id.right_drawer);
    mDrawerListR.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, drawerItems));
    mDrawerListR.setOnItemClickListener(new DrawerItemClickListener());
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);
    SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar1);
    seekBar.setOnTouchListener(this);
    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open,R.string.drawer_close );
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}
[...]

activity_main.xml

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#123456"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

    <LinearLayout
        android:id="@+id/right_drawer_ll"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:background="#123456"
        android:orientation="vertical"
        android:visibility="visible" >

        <SeekBar
            android:id="@+id/seekBar1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="14dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="10dp"
            android:max="5"
            android:progress="0" />

        <ListView
            android:id="@+id/right_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:background="#123456"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />
    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

drawer_list_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="#fff" />

thanks for any help.


Solution

  • If you want to load a image from a URL, you can use a library like ...

    https://github.com/nostra13/Android-Universal-Image-Loader

    You can just load the image asynchronously via the adapter..