First Create xml file
By default : – activity_main
Small Phones : – activity_main(sw360)
Large Phones : – activity_main(sw480)
Extra large Phones/Tablets : – activity_main(sw600)
I want follow this condition but some error?????
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Configuration config = getResources().getConfiguration();
if (config.smallestScreenWidthDp >= 320 && config.smallestScreenWidthDp <= 480) {
setContentView(R.layout.activity_main(sw320));
} else if (config.smallestScreenWidthDp >= 481 && config.smallestScreenWidthDp <= 600) {
setContentView(R.layout.activity_main(sw480));
} else(config.smallestScreenWidthDp >= 601{
setContentView(R.layout.activity_main(sw600));
}
}
2
Answers
You don’t have to program it, when you use the correct folder and naming structure the system will determine the correct layout or use the default (which is in the
layout
folder).See this link which tells more about the folder structure.
Eg:
So in your case it would be:
Your Activity can then use this OnCreate method:
use wrap content and match-parent instead of giving hard dimension to views.