Puneet Sharma Puneet Sharma - 5 months ago 23
Android Question

Button Component Cuts off my custom View

My button component takes space of my custom view class,if i put more it takes up more space,any reason why is that

My oncreate method :

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_control);



MyCustomPanel view = new MyCustomPanel(this);
DisplayMetrics metrics;
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = getDPI(500, metrics);
int width = getDPI(800,metrics);
layout = (LinearLayout) findViewById(R.id.r2) ;
layout1 = (LinearLayout) findViewById(R.id.l2);

ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width,
height);
layout.addView(view,params);
}


This is my xml code :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/r2"
android:gravity="bottom|start"
android:layout_gravity="bottom|start"
tools:context="company.ciso.com.wheelchair.Control">

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="top|end"
android:layout_gravity="top|end"
android:id="@+id/l2"
>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="S1" />

</LinearLayout>

</LinearLayout>


these are the screen shots
enter image description here
enter image description here

Additionally is there anyway to set gravity of my custom view.??

Answer

Update your layout with this code.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/l2"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_gravity="top|end"
        android:gravity="top|end"
        android:orientation="horizontal">

        <Button
            android:id="@+id/b1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="S1" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/r2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="horizontal"></LinearLayout>
</RelativeLayout>

Comments