I have to develop an Activity to show a list of items. In portrait I have to display 3 items in a single row in landscape 4 items.
What should be the best way to implement this solution?
I have to develop an Activity to show a list of items. In portrait I have to display 3 items in a single row in landscape 4 items.
What should be the best way to implement this solution?
You have to create one more layout
like res/layout-land
and have to create same xml
in that folder
for respective design in landscape mode
. You can put views
as per your requirement in each xml
file. So on orientation change
as per requirement it will pick your respective design from respective folder.
Check The image for more details
Note: here frgtpw.xml
is present in both Layout
Folder .
For more put your doubts here :)
Provide tow layouts with a same name but one with 3 elements and the other with 4 elements. Put one of them in the layout-port
and another in layout-land
. Then in your adapter find 4 elements and set the values for those but for the fourth item just check if it is null or not, if it is null that means it is portrait else it means landscape.
in your adapter implementation you can define as
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
if(isPortrait){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.portrait_layout, parent, false);
}else{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.lanscape_layout, parent, false);
}
}
}