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.

5
  • Are you looking for LayoutInflater ( developer.android.com/intl/ja/reference/android/view/… ) ? Commented Aug 25, 2015 at 3:38
  • @innoSPG i check with LayoutInflater but it is not working as well. Commented Aug 25, 2015 at 3:39
  • @innoSPG Please see this link as this is what i am looking for. (thegamecontriver.com/2015/04/…) Commented Aug 25, 2015 at 3:40
  • @Mediasoft are you want to create this layout programmatically.. Commented Aug 25, 2015 at 5: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. Commented Aug 25, 2015 at 7:27

2 Answers 2

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(); 
Sign up to request clarification or add additional context in comments.

Comments

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)); 

4 Comments

@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.
Ah, of course. Have you seen this post? stackoverflow.com/questions/21236094/… . 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 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 ?
I don't think I understand enough about this to help you then. Looks the same to me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.