Reputation: 269
<RelativeLayout
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:background="#b4d3d3">
<TextView
android:id="@+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#5d737e"
android:padding="20dp"
android:text="First" />
<TextView
android:id="@+id/world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/hello"
android:padding="20dp"
android:text="Second" />
</RelativeLayout>
this is my xml i want set two textview inside relative layout equal part horzontally but using this xml left textview is coming only few part while right one take much space please suggest me what i am doing wrong .
Upvotes: 0
Views: 3353
Reputation: 1106
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:background="#b4d3d3">
<TextView
android:id="@+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#5d737e"
android:padding="20dp"
android:text="First" />
<TextView
android:id="@+id/world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/hello"
android:padding="20dp"
android:text="Second" />
You can set programmatically width child view like below code and manage different device size easily:
private TextView hello, world;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hello = findViewById(R.id.hello);
world = findViewById(R.id.world);
RelativeLayout.LayoutParams relativeParam = (RelativeLayout.LayoutParams) hello.getLayoutParams();
relativeParam.width = getDeviceWidth(MainActivity.this) * 50 / 100;
hello.setLayoutParams(relativeParam);
RelativeLayout.LayoutParams relativeParamWorld = (RelativeLayout.LayoutParams) world.getLayoutParams();
relativeParamWorld.width = getDeviceWidth(MainActivity.this) * 50 / 100;
world.setLayoutParams(relativeParamWorld);
}
public int getDeviceWidth(Context context) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return metrics.widthPixels;
}
Upvotes: 2
Reputation: 9692
Try this You can achive that using LinearLayout
just set same Weight
to Your both TextView
like below code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="@+id/hello"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#5d737e"
android:layout_weight="1"
android:padding="20dp"
android:text="First" />
<TextView
android:id="@+id/world"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="Second" />
</LinearLayout>
if You want to use RelativeLayout
than try this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center">
<TextView
android:id="@+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#5d737e"
android:padding="20dp"
android:text="First" />
<TextView
android:id="@+id/world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/hello"
android:padding="20dp"
android:text="Second" />
</RelativeLayout>
Upvotes: 1
Reputation: 11
Use Perfect weight Concept
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="@+id/hello"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#5d737e"
android:layout_weight="1"
android:padding="20dp"
android:text="First" />
<TextView
android:id="@+id/world"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="Second" />
</LinearLayout>
Upvotes: 0
Reputation: 3100
try below code
<RelativeLayout
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:background="#b4d3d3"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_weight="1"
android:id="@+id/hello"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#5d737e"
android:padding="20dp"
android:text="First" />
<TextView
android:layout_weight="1"
android:id="@+id/world"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/hello"
android:padding="20dp"
android:text="Second" />
</LinearLayout>
</RelativeLayout>
Upvotes: 0
Reputation: 1646
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:background="#b4d3d3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#5d737e"
android:padding="20dp"
android:layout_weight="1"
android:text="First" />
<TextView
android:id="@+id/world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/hello"
android:padding="20dp"
android:layout_weight="1"
android:text="Second" />
</LinearLayout>
</RelativeLayout>
Upvotes: 0