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));
}
}
CodePudding user response:
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:
res/layout/main_activity.xml # For handsets
res/layout-land/main_activity.xml # For handsets in landscape
res/layout-sw600dp/main_activity.xml # For 7” tablets
res/layout-sw600dp-land/main_activity.xml # For 7” tablets in landscape
So in your case it would be:
res/layout/activity_main.xml # default (smaller than 360dp available width)
res/layout-sw360dp/activity_main.xml # Small phones (360dp wide and bigger)
res/layout-sw480dp/activity_main.xml # Large phones (480dp wide and bigger)
res/layout-sw600dp/activity_main.xml # XLarge phones (600dp wide and bigger)
Your Activity can then use this OnCreate method:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
CodePudding user response:
use wrap content and match-parent instead of giving hard dimension to views.