I am trying to choose between two different layouts based on a value of a certain parameter and set that layout for my class.Both the layouts are having the same ids of all the views.If we dynamically allocate ids to the views of the two layout wont there be a ambiguity?When i tried practically I am getting a null pointer exception.Is this null pointer because of this ambiguity only??Can anybody please help.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// condition can be from getIntent() or from SharedPreferences or whatever
if(condition) {
setContentView(R.layout.activity_main);
} else {
setContentView(R.layout.activity_main_2);
}
// If they have the same components, but different layouts only
// You can use it as usual with findViewById(R.id.view_id);
Button btnExample = findViewById(R.id.btn_example);
}
I could't understand well what you asked. If i'm correct you asking if you can use the same id for a view on different layouts.
Yes you can. Unless you try to inflate those two layouts at the same time. Then you will have a problem.
You need to narrow you error.
if(condition) {
setContentView(R.layout.activity_main);
} else {
setContentView(R.layout.activity_main_2);
}
When you set different views, it may cause null pointer exception when any of the view is missing between this layouts.
So you should make sure that your findViewById returns not null. OR just add check when you try to use this views in the code.
Advanced....
Anyway if you already know all these things and have enough experience with all these stuffs, Try the belw listed libraries it will help you a lot
https://github.com/JakeWharton/butterknife
https://github.com/roboguice/roboguice
You can avoid "repetitive, cumbersome boilerplate coding parts. Indeed, we should focus on logic, not on meta programming".
These will help you to avoid all these findViewById...stuffs and you can slim your code...less bugs!
Related
I know there are a lot of similar questions, but suggested solutions don't work in my case.
What is my problem.
I have fragment nested inside activity.
Fragment layout includes ViewPager.
What is my idea ? As far as my application won't support landscape (this can cause some additional changes), my ViewPager is loading images from the network.Images can be quietly large and heavy, so I have written server side API which helps to convert image to the desired size on the server and return resized image url.
This will help to get rid of OutOfMemory errors.
But the problem is that I need to send ViewPager's height and width in my request.
I am finding it by id in onViewCreated
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mBannerSlider = (AutoScrollViewPager) view.findViewById(R.id.banner_slider);
mBannerSlider.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener(){
#Override
public void onGlobalLayout() {
mSliderHeight = mBannerSlider.getHeight();
mSliderWidth = mBannerSlider.getWidth();
Toast.makeText(getActivity(),"View " + mSliderHeight + " " + mSliderWidth,Toast.LENGTH_SHORT).show();
}
requestBannerImages(mSliderHeight........);
});
The problem here is that this method is called on every page change.
Of course I can use some helper variable to determine if it is first time or not.
Also there is way to send callback from activity to the fragment using this activitiy's method.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
//Here you can get the size!
}
What is the best solution in this way, maybe I can do this better in another way.
Thanks in advance.
EDIT
I have a plenty of view where I need to get dimensions, so I need to add listener to every view and that after first measurement remove it ?
I would do it with the tree observer. To avoid repeated calls I would remove the listeners once i get the value. This assures you that you will get the dimensions and it wont get call anymore.
if (Build.VERSION.SDK_INT < 16) {
v.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
} else {
v.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
}
I took the code from: https://stackoverflow.com/a/16190337/2051397
I understand your dilemma, mainly because I have implemented a multimedia app. However setting dimensions in every view or many UI elements related to icons/images is time consuming. If not so, then you end up with lots of code doing the same stuff.
In my opinion, you don't have to set dimensions for every view, for the sake of looks. You may set max height of a view/UI or/and set setAdjustViewBound to true, let the UI framework handle the images. There is a cost of performance though if the image is large, in your case probably not.
You may refer to Google notes on setMaxHeight of ImageView. Let me know of your view on this, interested to know.
I am currently working on a app and it might be used on all android devices. I find it really challenging to adjust my XML layout files according to various screen sizes. I have surfed a lot over this topic and found a useful doc at Developer site. The document is decent and provides enough information on what should be done for screen compatibility.
Questions :
1.If I have two different layouts in folders like res/layout-sw600dp and res/layout-sw720dp, will the app automatically decides which one of these layouts is to be used ?
2.Assuming that I prefer a ListView for handsets and GridView for Tabs as a Home Page display, how will I define my layouts and how will I refer them for UI ?
Any ideas on how I can pull off the 2nd question's feature will be highly appreciated. Thanks in advance.
You can defferenciate it in java by checking "hasHoneycomb", tabs will give tru as return value. One way is : you can set different layout from setcontentview() according to condition.
if(hasHoneycomb()) {
setcontentView(layout_for_tabs);
} else {
setcontentView(layout_for_phones);
}
1) Yes, it will, based on the screen size.
2) Give them different IDs and see which one is visible:
ListView mList = (ListView) findViewById( R.id.homeList );
GridView mGrid = (GridView) findViewById( R.id.homeGrid );
if( mList != null ) {
// set list adapter
} else if( mGrid != null ) {
// set grid adapter
} else {
// neither view exists...
}
Yes, app automatically decide which layout file to use for current device screen.
Give the ListView and GridView different ids. In code use findViewById() method when creating views.
If ListView found (findViewById(R.id.list) returned View) app is running on handset, otherwise (findViewById(R.id.list) returned null and findViewById(R.id.grid) returned View) app is running on tablet.
For question 1, Yes Android will automatically choose and decide the most suitable amongst the two.
For 2, here is link - Determine if the device is a smartphone or tablet?
Hope it helps. :)
i have a row of buttins created like this
i want to change the background colour at runtime in code.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout track1 = (LinearLayout)findViewById(R.id.my_toggle_container);
for (int i = 0; i<32; i++) {
ToggleButton tgl = new ToggleButton(this);
tgl.setId(i);
...
track1.addView(tgl);
this names the id of the togglebuttons 1, 2, 3... (i presume?)
i have an int variable called 'xBtn' that changes 1, 2,..
this is how i get a reference to the button using xBtn
String buttonID = ""+xBtn;
int resID = getResources().getIdentifier(buttonID, "id", "com.thing");
//find the button
ToggleButton tb = (ToggleButton) findViewById(resID);
//change its colour
tb.setBackgroundColor(Color.BLUE);
it crashes on the setBackgroundColor line.
it may be obvious to someone whats wrong and thats what im hoping
any help would be totaly ace ta
thanks
main.xml
<LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:id="#+id/my_toggle_container" android:orientation="vertical">
The id of your togglebuttons is gonna be a number from 1 to 32... However, trying to find the toggle button by id will return null because simply instantiating a new toggle button and giving an id wont help you. findViewById looks in the parent view for a child view with the specified id. If you havent added that toggle button with that id to the view, then findViewById will return null. I am 99.99% sure even without looking at the log, that it crashes because you are calling setBackgroundColor on a null object.
In other words, the id that you set a view to is only relevant once the view is actually added to a parent view. In your case you are probably trying to add these toggle buttons to your main content view, in which case you need grab hold of that view that you used for setContentView and call addView on that view and pass in each new toggle button. Note that this will probably not look right unless you also specify layoutparams for the togglebuttons.
EDIT
If thats your entire main.xml, then you've got other issues. Post the full xml file. In any event, you still are going to have to do what I've said, which is to grab hold of the view or a child view of that view and then add the toggle buttons to it via addView (after giving the togglebuttons their proper ids). Once the button has been added, then you can find it. Note though that if you're gonna add the toggle buttons to a child view of your main view, then you'll likely have to grab hold of that child view and call findViewById on THAT.
For example, you can do a nested call like this. findViewById(1) <--- gets you the LinearLayout or whatever inside of your main content view, then once you have that you can call addView on it. So LinearLayout ll = (LinearLayout)findViewById(someNumber); ll.addView(tb);
Try to use the method setTag() , and then you can get all your ToggleButton by using : findViewByTag();
Perhaps tb is null? Could you check that out?
To expand on what LuxuryMode said... What gets an ID INTO your java is inflating it via setContentView and setting it as content. That's why it's ok to have overlapping (duplicate) IDs in different layouts. You can have #+id/submit_button in layout1.xml and in layout2.xml and the Activity will get you the object via findViewById(R.id.submit_button) based on which one you have loaded into setContentView() at any given moment.
So, we're all guessing that you're probably not setting the content view and hoping that the code will find your object in your non inflated XML, which it won't. Which would lead (as everyone has guessed) to you now dealing with a null object, which you obviously can't set a background color on.
I know it gets confusing cause you have the XML RIGHT THERE!!! But the reality is that the xml isn't "alive". It's just stuff for you to look at until you have tasked the Application with inflating it and converting all of it into Android objects of some kind. A lot of the time this is done mostly transparently to you, so, it's easy to forget that none of these things really exist.
It's very likely that tb is null, because findViewById() didn't go as you expected.
You can verify this by surrounding the erroneous line with try.. catch block:
try {
tb.setBackgroundColor(Color.BLUE);
} catch (Exception e){
}
and watch for the message of e. It's likely to be null pointer exception.
In fact, I think you should not use getResources().getIdentifier(buttonID, "id", "com.thing") in the first place. It seems to me that all these resources are continuously numbered in R file, thus you should simply get the first id (as an integer), and then increment on that.
That is, you should do things like:
// The following code is not tested; I just wrote it here on SO.
for (int resID = R.id.button1; resID <= 32; resID++) {
ToggleButton tb = (ToggleButton) findViewById(resID);
tb.setBackgroundColor(Color.BLUE);
}
this should make all 32 buttons blue.
Well basicly I have a textview and when the application is created it sets a string as the textviews text not hard, but I get a force close error when I run the app on my phone.
TextView sdcard=(TextView)findViewById(R.id.sd_textview);
sdcard.setText(R.string.not_mounted);
Then I have a error on a togglebutton also
ToggleButton silent=(ToggleButton)findViewById(R.id.silentbutton);
silent.setChecked(false);
And I have errors for all my other buttons/textviews can anyone help, please?!
EDIT:
I cant post pics because I am a new member, :(
Link to imgshack http://imageshack.us/photo/my-images/849/unledggp.png/
If code for the whole textview snippet.
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_UNMOUNTED)) {
TextView sdcard=(TextView)findViewById(R.id.sd_textview);
sdcard.setText(R.string.not_mounted);
}
OnCreate Function
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkSD();
checkRing();
checkWifi();
checkBt();
}
Look for all instances of sd_textview and make sure the one that you're trying to reference is a TextView. If you want more clarity you can debug your code and see what object is actually being returned by not casting into a TextView:
View sdcard = findViewById(R.id.sd_textview); //debug this
//you can also log the View object to see the type
Log.d("Test", "" + sdcard);
Looking at your error log (assuming its the right error log) you have a ClassCastException in the checkWifi method. Edit your question and include ALL of the onCreate method and all of the checkWifi method, but I expect you are using the same id for multiple views.
Two things I can think of (although seeing more code would help).
Make sure you have called setContentView(R.layout.main) (or whatever your layout file is called). Do this BEFORE any attempt to use findViewById(...).
Secondly sdcard.setText(R.string.not_mounted); in this statement R.string.not_mounted is a resource ID (int) and not a string. You would need to use...
sdcard.setText(getString(R.string.not_mounted));
I am trying to register a context menu in a skeleton app's OnCreate():
/** Called with the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate our UI from its XML layout description.
setContentView(R.layout.skeleton_activity);
View v = findViewById(R.layout.skeleton_activity);
registerForContextMenu(v);
// Find the text editor view inside the layout, because we
// want to do various programmatic things with it.
mEditor = (EditText) findViewById(R.id.editor);
// Hook up button presses to the appropriate event handler.
((Button) findViewById(R.id.back)).setOnClickListener(mBackListener);
((Button) findViewById(R.id.clear)).setOnClickListener(mClearListener);
mEditor.setText(getText(R.string.main_label));
}
The debugger tells me that findViewById(R.layout.skeleton_activity) returns null.
#CommonsWare solution to a similar post is to Wait until onFinishInflate(). However, in the sample project he provides, it doesn't seem that he waits until onFinishInflate.
My questions:
Can registerForContextMenu() wait
until onFinishInflate()?
If so, how do I do so?
This line is not correct its asking for id and you are providing layout
View v = findViewById(R.layout.skeleton_activity);
Instead if you want to have object of your root layout element then provide it some id and then try something like this
View v = findViewById(R.id.root_element);
I think you should use
View v = findViewById(R.id.skeleton_activity);
instead.
For the 2nd question, sorry, I 've no idea. Hope to see someone else's answer.
You shouldn't need to wait for the content to inflate in an Activity.
One problem is that findViewById takes an ID (R.id....) when you provide it with a layout (R.layout...). Can you try the following instead, to reference the Activity's root view?
setContentView(R.layout.skeleton_activity);
View content = findViewById(android.R.id.content);
registerForContextMenu(content);
i think the code you have shown is very confusing. Here is a good article http://blog.sptechnolab.com/2011/02/10/android/android-contextmenu-submenu/. In my case it works, i hope you can solve your problem.