Saturday, 15 January 2011

android - How to set layout Background programatically -



android - How to set layout Background programatically -

i want set layout background programatically, depending on genre of song played in application. tried this:

public class answeractivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { log.e("transition", "transitioned reply activity"); super.oncreate(savedinstancestate); setcontentview(r.layout.play); android.support.v7.app.actionbar actionbar = getsupportactionbar(); actionbar.hide(); linearlayout root = (linearlayout) findviewbyid(r.id.ctr1); bundle info = getintent().getextras(); if(data.getstring("genre").equals("rock")){ root.setbackgroundresource(r.drawable.wprock); } else if(data.getstring("genre").equals("pop")){ root.setbackgroundresource(r.drawable.wppop); } else if(data.getstring("genre").equals("hiphop")){ root.setbackgroundresource(r.drawable.wphiphop); } }

but doesn't work, it's throwing null pointer exception in root.setbackgroundresource lines, whenever of these take place. ideas? in advance edit: have r.drawable.wprock/pop/hiphop, plus ruled out possiblity bevause tried utilize color instead setbackgroundcolor mehtod , had same exception.

xml:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#1d1d1d" > <imageview android:id="@+id/res" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:layout_margintop="130dp" android:onclick="go" android:background="@drawable/playbtn" /> </linearlayout>

you got error because root new layout has not been in activity :

linearlayout root = new linearlayout(this);

remove above code , give android:id outer layout in r.layout.play, , utilize findviewbyid instead.

android android-intent background bundle

No comments:

Post a Comment