1

i want to Call Android Layout(XML) Programmatically in a function. Layout is already Created in XML. i am asking this question because i don't want to use this in Android Native app, actually i will call these layouts in Unity3D which is a Gaming Engine. So Let say that i have a layout. For Example look at below code:

    <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/useful_nums_item_name"/>

        <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/useful_nums_item_value"/>
    </LinearLayout>

   <ImageButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:src="@drawable/call"
            android:id="@+id/call_btn"
            android:onClick="callNumber"/>

</LinearLayout>

Now i want to create a function which can call this layout. But But i don't want to use below code as i am not going to use in Android Native App.

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

i will directly use the Unity3D class as below:

public class MainActivity extends UnityPlayerActivity {

}

So i need to call my layout in that class but in form of Some Function. For Example:

public class MainActivity extends UnityPlayerActivity {

        public void ShowLayout(){
             enter code here
        }
}

So i need you people help to solve this problem.

Any help will be appreciated.

Mediasoft
  • 271
  • 2
  • 7
  • 18
  • Are you looking for LayoutInflater ( http://developer.android.com/intl/ja/reference/android/view/LayoutInflater.html ) ? – innoSPG Aug 25 '15 at 03:38
  • **@innoSPG** i check with LayoutInflater but it is not working as well. – Mediasoft Aug 25 '15 at 03:39
  • **@innoSPG** Please see this link as this is what i am looking for. (http://www.thegamecontriver.com/2015/04/android-plugin-unity-android-studio.html) – Mediasoft Aug 25 '15 at 03:40
  • @Mediasoft are you want to create this layout programmatically.. – Vishwa Aug 25 '15 at 05:14
  • **@Vishwa** No, i don't want to create Layout Programmatically. Layout i create using XML and somehow i need to call it in a function. As you read above Description that i am not going to use in Android Native App.I am going to use in Game which is using Unity3d Engine. So from that game i need to call Native Layouts. – Mediasoft Aug 25 '15 at 07:27

2 Answers2

4

Take your layout with the use of inflator and then bring it on front.

LayoutInflater inflater = LayoutInflater.from(context);
View yourView = inflater.inflate(R.layout.popup_layout, null, false);

// then bring it to front
yourView.bringToFront();
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
2

Define your view / layout without XML instead. In your case, you could define it in the ShowLayout() method. This code should point you in the right direction:

    LinearLayout layout = new LinearLayout(getContext());
    layout.setWeightSum(1);
    LinearLayout childLayout = new LinearLayout(getContext());
    LinearLayout.LayoutParams childParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 0.8f);
    childLayout.setLayoutParams(childParams);

    LinearLayout.LayoutParams fieldParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    TextView name = new TextView(getContext());
    name.setLayoutParams(fieldParams);
    TextView value = new TextView(getContext());
    name.setLayoutParams(fieldParams);

    LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 0.2f);
    ImageButton call = new ImageButton(getContext());
    call.setLayoutParams(buttonParams);
    call.setImageResource(getResources().getDrawable(R.id.call));
Joakim
  • 3,224
  • 3
  • 29
  • 53
  • **@Joakim** Yes, i know this way but the problem is if i want to create Complex Layout and it will be very difficult to draw using Programmatically .So Create using XML is quite easy. – Mediasoft Aug 27 '15 at 09:55
  • Ah, of course. Have you seen this post? http://stackoverflow.com/questions/21236094/integrate-unity3d-view-into-android-activity . Especially the part about `View rootView = findViewById(android.R.id.content);` where `content` would be the id you set on the root linear layout? – Joakim Aug 27 '15 at 10:01
  • **@Joakim** actually this is reverse case. they care calling Unity3d into Android but in my case i need to call Android into Unity3d. which is different ? – Mediasoft Aug 27 '15 at 10:11
  • I don't think I understand enough about this to help you then. Looks the same to me. – Joakim Aug 27 '15 at 10:59