I have created a custom view for my Toast Messages.
I want my toast messages to take up the width of the screen with say 30dp margin in left and right.
But it ends up, filling up the entire screen, if the text/content of the Toast message is long.(and wraps up a lot , if the content is less). I want it to remain the same size in Width ( i.e. entire screen width minus margin at left and right) , and height to wrap(increase/decrease) as per the content in it.
This is my current Toast Xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_root"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/jtoast_bg"
android:layout_gravity="center"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
<ImageView
android:id="@+id/toast_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:scaleType="fitStart"
android:src="@drawable/menu_info">
</ImageView>
<TextView
android:id="@+id/toast_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingLeft="10dp"
android:textSize="14sp"
android:gravity="center"
android:text="toast text"
android:textColor="#FFF">
</TextView>
and it is being populated as :
View layout = inflater.inflate(R.layout.jtoast,(ViewGroup)activity.findViewById(R.id.toast_root));
..
toast.setGravity(Gravity.BOTTOM, 40, 40);
..
Try this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#990099"
android:layout_gravity="center"
>
<View
android:layout_width="25dp"
android:layout_height="fill_parent"
/>
<LinearLayout
android:id="@+id/toast_root"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#999900"
android:layout_gravity="center"
android:layout_weight="1"
android:padding="5dp"
>
<ImageView
android:id="@+id/toast_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:scaleType="fitStart"
android:gravity="center"
android:src="@android:drawable/ic_btn_search"
>
</ImageView>
<TextView
android:id="@+id/toast_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingLeft="10dp"
android:textSize="14sp"
android:text="toast text"
android:textColor="#FFF"
android:layout_weight="1"
android:gravity="center"
android:background="#ff00"
/>
</LinearLayout>
<View
android:layout_width="25dp"
android:layout_height="fill_parent"
/>
</LinearLayout>