I am working on a Android apps using Android Studio and just a new to it. I want to make outer main layout on currentscreenwidth + 300dp and height should be 100% means match_parent. In that main outer layout, I am creating two layouts. Inner first layout will have a fix 300dp width and the other layout should have screenwidth+300dp. For that I have to main layout width currentscreenwidth + 300dp becasue one of my inner layout have fixed 300dp and other one have match_parent and placed in a row. For this I am using below codes but its not working. When I manually write width about 1000dp to outer layout then my inner layout work fine as I want but I want the fix calculation of currentscreenwidth + 300dp. How Can I Do This?
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/mainbody" android:layout_width="match_parent" //currentscreenwidth + 300dp android:layout_height="match_parent" tools:context="com.domain.project.MainActivity"> <RelativeLayout android:id="@+id/menubar" android:layout_width="300dp" //300dp android:layout_height="fill_parent"> </RelativeLayout> <RelativeLayout android:layout_toRightOf="@+id/menubar" android:layout_width="match_parent" //currentscreenwidth android:layout_height="fill_parent"> </RelativeLayout> </RelativeLayout> MainActivity.java:
package com.domain.project; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.Display; import android.widget.Button; import android.widget.RelativeLayout; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get Width And Height Of Screen On Page Load Display display = getWindowManager().getDefaultDisplay(); DisplayMetrics outMetrics = new DisplayMetrics(); display.getMetrics(outMetrics); float density = getResources().getDisplayMetrics().density; float dpHeight = outMetrics.heightPixels / density; float dpWidth = outMetrics.widthPixels / density; int width = Math.round(dpWidth); RelativeLayout rl = (RelativeLayout) findViewById(R.id.mainbody); //rl.getLayoutParams().height = 100; rl.getLayoutParams().width = width + 300; } }
No comments:
Post a Comment