I want to create a layout depend on size of device screen. This is my MainActivity
public class LoginActivity extends Activity {
int width_device;
int height_device;
LinearLayout layout_login_content;
LinearLayout.LayoutParams param;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Get screen size
width_device = Utility.getDeviceWidth(this);
height_device = Utility.getDeviceHeight(this);
// Get widget from layout
layout_login_content = (LinearLayout) findViewById(R.id.layout_login_content);
// Set up layout size
param = (LinearLayout.LayoutParams) layout_login_content
.getLayoutParams();
param.width = width_device * 60 / 100;
param.height = height_device * 60 / 100;
layout_login_content.setLayoutParams(param);
}
Don't worry about how to get screen size. I did it and it worked.
This is my main_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_login_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
tools:context="login.LoginActivity" >
</LinearLayout>
And this is error message
java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
I dont know what happen here. I declare LinearLayout.LayoutParams
, get params from LinearLayout
in xml. But why it can't cast correctly?
Sorry about my english. I hope everyone know what I mean :( Thank you so much!