I have file FirstLevel where user goes from the main file(mainActivity). I need to add LinearLayouts to the screen in FirstLevel (these LinearLayouts contain two TextViews and are realised in separate XML-file), when user touches the screen. But, actually, nothing happens, I don't see any added LinearLayouts.
public class First_level extends AppCompatActivity implements View.OnTouchListener {
ImageButton firstButton;
//boolean flag = true;
String[] replics = {...}; //filled massives
String[] who = {...};
int i = 0;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_level);
LinearLayout gameWindow = findViewById(R.id.mesWin);
gameWindow.setOnTouchListener(this);
}
void npcsay(String text, String hero)
{
LinearLayout gameWindow = findViewById(R.id.mesWin);
LinearLayout view = (LinearLayout) getLayoutInflater().inflate(R.layout.message, gameWindow, false);
TextView username = view.findViewById(R.id.name);
username.setText(hero);
TextView ttxt = view.findViewById(R.id.point);
ttxt.setText(text);
gameWindow.addView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
npcsay(replics[i], who[i]);
i++;
return true;
}
return false;
}
}
First_level.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/gameWindow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/fillback"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/mesWin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
message.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="25dp"
android:layout_marginRight="25dp"
android:background="@drawable/grey_background"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/neucha"
android:padding="5dp"
android:text="User"
android:textColor="@color/white"
android:textSize="20sp" />
<TextView
android:id="@+id/point"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/neucha"
android:padding="5dp"
android:text="hi"
android:textColor="@color/black"
android:textSize="21sp" />
</LinearLayout>
That is how first_level XML looks like.
