I am trying to make a custom view using XML then retrieve its properties and edit them. This custom view is added into the test2 view several times with different id’s for each. I have tried to retrieve the id for one of these custom views (player_1) then get the TextView (player_name), and edits its properties and no other views like the player_2 id view.
But I am not sure that this is this even possible? It renders fine, but I get an error every time I try to do this in code. I’m really not sure how to approach this, I can’t seem to get inflate to work either to test that instead. Any ideaS?
Thanks!
Game.java
setContentView(R.layout.test2);
View player1 = (View) findViewById(R.id.player_1);
TextView player1Name = (TextView) player1.findViewById(R.id.player_name);
player1Name.setText("John");
test2.xml
<include
layout="@layout/player_view"
android:id="@+id/player_1"
/>
<include
layout="@layout/player_view"
android:id="@+id/player_2"
/>
player_view.xml
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:player="http://schemas.android.com/apk/res/com.example.android.merge">
<LinearLayout
android:id="@+id/top_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
>
<TextView
android:id="@+id/player_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
>
</TextView>
</LinearLayout>
</merge>
This is a cut down version of player_view.xml, I can post more if needed.
Also I have not worked out if it even prints the error message for this in eclipse, it goes into debug mode and does not explain any further what the issue is.
Your player_view.xml has unknown type, so the include is confused what kind of View is it. Try to do the next:
Use or create new PlayerView class, mergeable with player_view.xml.
Edit test2.xml to:
then inflate it in your code as following:
Do the same for player2.