I want to set the layout background programatically, depending on the genre of the song to be played in my application.
I tried this:
public class AnswerActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.e("TRANSITION", "TRANSITIONED TO ANSWER 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 data = 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 it doesn't work, it's throwing a Null Pointer Exception in the root.setBackgroundResource lines, whenever anyone of these take place.
Any ideas?
Thanks in advance
EDIT: I do have R.drawable.wprock/pop/hiphop, plus I ruled out that possiblity bevause I tried to use a color instead with the setBackgroundColor mehtod and I had the 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 the error because you root is a new layout that has not been in your Activity :
LinearLayout root = new LinearLayout(this);
Remove the above code and give an android:id to the outer layout in R.layout.play, and use findViewbyId instead.
First you check the root layout id of your xml if it is correct then follow the code
protected void onCreate(Bundle savedInstanceState) {
Log.e("TRANSITION", "TRANSITIONED TO ANSWER 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 data = 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);
}
then check
Bundle data = getIntent().getExtras();
data in bundle is null or some thing else
Your xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ctr1"
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>
Related
I have implemented a custom title to my activity :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#3a5894" >
<LinearLayout
android:layout_width="50dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:id="#+id/left_button_title_bar_main_content_fragment"
android:background="#90C3D4"
android:onClick="onClickTitleBar">
<ImageView android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#90C3D4"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:id="#+id/center_textview_title_bar"
android:onClick="onClickTitleBar">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="15dp"
android:text="Quick Notes"
android:textColor="#ffffff"/>
</LinearLayout>
</RelativeLayout>
My activity implementation :
public class MainActivity extends Activity implements OnClickListener {
private LinearLayout leftButtonInTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// custom title
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
// full screen activity
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
// custom title bar
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
leftButtonInTitle = (LinearLayout) findViewById(R.id.left_button_title_bar_main_content_fragment);
and the method where I would like to handle click
public void onClickTitleBar(View v) {
int id = v.getId();
Log.e("","id : "+id);
Log.e("","button id: " + R.id.left_button_title_bar_main_content_fragment);
}
and I have logged out the v.getId and the linearlayout actual id.
And I have realized the ids are not the same.
here is the log :
E/﹕ id 2131493001
E/﹕ button id: 2131493000
As you can see the numbers are not the same.
Any idea why this is happening ?
Any help is appreciated.
You have set the onClick on the LinearLayout with the ID R.id.center_textview_title_bar as well. Because it is in a RelativeLayout, and no constraints are set, and it is "higher up" in the order of the children, it will be displayed on top of the LinearLayout with the ID R.id.left_button_title_bar_main_content_fragment and it will pick up all clicks in place of the title bar.
To fix this, remove the line
android:onClick="onClickTitleBar"
From the LinearLayout with the ID R.id.center_textview_title_bar
In your xml file, value of onClick attribute of both LinearLayout is same.
May be you are getting different IDs because of that.
Try giving different values of onClick for both LinearLayouts.
Here's the problem with setting text in TextView in Activity. I guess the problem was that I used the same id for each fragment XMLin another activity. I used the same XML code for relative layout, but all worked well in fragments. The problem with TextView is in another activity. Please have a look at my code :
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map_main);
TextView townNameTxt = (TextView)findViewById(R.id.town_name_txt);
TextView tradeTxt = (TextView)findViewById(R.id.trade_txt);
String townName1 = "London";
String trade1 = "buy sum";
townNameTxt.setText(townName1);
tradeTxt.setText(trade1);
}
and xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/action_bar_title_map"
android:layout_width="match_parent"
android:layout_height="#dimen/actionbar_height"
android:background="#null"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/town_name_txt"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:ellipsize="end"
android:textSize="#dimen/actionbar_title2"
android:paddingLeft="#dimen/side_padding"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/trade_txt"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:ellipsize="end"
android:textSize="#dimen/actionbar_title2"
android:paddingLeft="#dimen/side_padding"/>
</RelativeLayout>
<org.osmdroid.bonuspack.mapsforge.GenericMapView
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/action_bar_title_map"/>
</RelativeLayout>
first what I do to fix this: I have change id of TextView. But it hasn't effect.
I tried to delete relative layout in XML, and it deletes from screen. No contact to XML file from Java file.
Sorry for bothering and thanks to all answers. The problem was a dupplicate setcontentView() method:
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map_main);
TextView townNameTxt = (TextView)findViewById(R.id.town_name_txt);
TextView tradeTxt = (TextView)findViewById(R.id.trade_txt);
String townName1 = "London";
String trade1 = "buy sum";
townNameTxt.setText(townName1);
tradeTxt.setText(trade1);
setContentView(R.layout.map_main);
}
p.s espacially Thanks to Sabya
It is weird that you don't get an exception in this code.
Because there is no method settext(). There is method called setText().
Change the method name from settext() to setText()..
Case Sensitive problem with method settext() to setText().
Use your onCreate method from this one:
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map_main);
TextView townNameTxt = (TextView)findViewById(R.id.town_name_txt);
TextView tradeTxt = (TextView)findViewById(R.id.trade_txt);
String townName1 = "London";
String trade1 = "buy sum";
townNameTxt.setText(townName1);
tradeTxt.setText(trade1);
}
Clean and Build project and run it again.
Hope, it will help you
If you are running it on Android Lollipop version, Uninstall and re-install the application and check.
I'm a new user of PanesLibrary but I'don't know how to show a personal layout on the background when the app starts. The code is like the example on the github without auto generated fragmentes and Example Fragment:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setPaneSizer(new ExamplePaneSizer()); //inner class like on the github example
// Lets setup a menu and a first pane!
if (savedInstanceState == null) {
Fragment menu = new CatFragment(); //show list of categories
setMenuFragment(menu);
}
}
And this is the layout of ExampleActivity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SandboxActivity" >
<com.mapsaurus.paneslayout.PanesLayout
android:id="#+id/panes"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Some Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</com.mapsaurus.paneslayout.PanesLayout>
The proble is that the TextView is not showed. Thanks!
I resolved. Simply add a new Fragment
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setPaneSizer(new ExamplePaneSizer());
// Lets setup a menu and a first pane!
if (savedInstanceState == null) {
Fragment menu = new CatFragment();
Fragment instr = new InstructionFragment();
setMenuFragment(menu);
addFragment(menu, instr);
I have a listview in my code and I want to set adapter on it.
But the issue is, even after initializing the listview, it is still null resulting into nullPointerException. (I checked it by logging and debugging)
I'm not able to access any view from that xml.
What am I missing? Any help appreciated.
xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:scrollbars="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/lvToday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#FFF"
android:dividerHeight="2dp" />
<ListView
android:id="#+id/lvTomorrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/lvToday"
android:divider="#FFF"
android:dividerHeight="2dp" />
</RelativeLayout>
</ScrollView>
Activity file
public class DashboardActivity extends Activity {
ListView lvToday, lvTomorrow;
TextView lblCall;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
init();
}
private void init() {
lvToday = (ListView) findViewById(R.id.lvToday);
lvTomorrow = (ListView) findViewById(R.id.lvTomorrow);
TodayAppAdapter adapter = new TodayAppAdapter(DashboardActivity.this,
DashboardActivity.this);
// This line is giving NULL
lvToday.setAdapter(adapter);
TomorrowAppAdapter adapter1 = new TomorrowAppAdapter(
DashboardActivity.this, DashboardActivity.this);
// This line is giving NULL
lvTomorrow.setAdapter(adapter1);
}
}
1) test eclipse menu: Project -> Clean...
2) if you have more than one version for your xml layout (example layout-large, layout-xlarge,...), check if all of them have your view.
You are missing a TextView in your layout
Put a String []
along with name of your Adapter in your
OnDraw()
Method
friends,
i have created custom title bar using following titlebar.xml file with code
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTitle"
android:text="This is my new title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#color/titletextcolor"
android:layout_marginLeft="25px"
android:paddingTop="3px"
/>
and java code to display custom title bar on each activity.
#Override
public void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
now i want to set textview value dynamically in each activity can any one guide me how can i achieve this?
using findviewbyid here i dont get reference of that textview to set value because
main layout does not contains any textbox with such a name but mytitle.
any help would be appriciated.
This is the way to set the custom title:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if ( customTitleSupported ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
}
final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
if ( myTitleText != null ) {
myTitleText.setText("========= NEW TITLE ==========");
myTitleText.setBackgroundColor(Color.GREEN);
}
}
SDK 2.1+
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTitle("========= NEW TITLE ==========");
}
Have you tried the setTitle() method of your Activity?
my_title.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/header"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:background="#d4e9a9">
<ImageView android:src="#drawable/jetpack"
android:layout_width="wrap_content" android:layout_alignParentLeft="true"
android:layout_centerVertical="true" android:id="#+id/back"
android:layout_height="wrap_content" android:layout_alignParentTop="true" />
<TextView android:id="#+id/title" android:layout_width="wrap_content"
android:gravity="center_vertical" android:textSize="20px"
android:textColor="#ffffff" android:layout_alignParentRight="true"
android:text="New Title" android:background="#a5c639"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:padding="9dip" android:layout_margin="5dip" />
</RelativeLayout>
Code:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);
((TextView)findViewById(R.id.title)).setText("gradient shadow");
findViewById(R.id.back).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
((TextView)findViewById(R.id.title)).setText("loce");
}
});
Because custom title default is fixed you should write yourself a theme:
<application android:icon="#drawable/icon" android:label="#string/app_name" android:theme="#style/MyTheme">
Try the following:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
TextView mytitletext = (TextView) findViewById(R.id.myTitle);
mytitletext.setText("========= NEW TITLE ==========");
}
You can use the hierarchyviewer to see if your view is accessible (you will see its id in the hierarchy graph)