layout Inflator (XML & JAVA 2)

 layout Inflator (XML & JAVA 2) 


Layout XML File 👇

Layout 👉New 👉XML 👉Layout XML File


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Rakib Shah"
android:textSize="40sp"
android:textStyle="bold"
android:textColor="#000000"
/>

<ImageView
android:id="@+id/imageDis"
android:layout_width="match_parent"
android:layout_height="120sp"
android:src="@drawable/image"
/>

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="āĻŦাংāϞাāĻĻেāĻļ āϏংāĻŦাāĻĻেāĻļে āĻŦা āĻ—āĻŖāĻĒ্āϰāϜাāϤāύ্āϤ্āϰী āĻŦাংāϞাāĻĻেāĻļ āĻšāϞো āĻāĻ•āϟি āĻĻāĻ•্āώিāĻŖ āĻāĻļিāϝ়াāϰ āĻĻেāĻļ। āĻāϟি āĻŦāĻ™্āĻ—োāĻĒāϏাāĻ—āϰেāϰ āωāϤ্āϤāϰ āϤীāϰে āĻ…āĻŦāϏ্āĻĨিāϤ। āĻĒāĻļ্āϚিāĻŽে, āωāϤ্āϤāϰে āĻāĻŦং āĻĒূāϰ্āĻŦে āĻ­াāϰāϤ āĻāĻŦং āĻĻāĻ•্āώিāĻŖ-āĻĒূāϰ্āĻŦে āĻŽাāϝ়াāύāĻŽাāϰ (āĻŦাāϰ্āĻŽা) āĻĻেāĻļāϟিāϰ āϏীāĻŽাāύা āϰেāĻ–েāĻ›ে। āϰাāϜāϧাāύী āĻāĻŦং āĻŦৃāĻšāϤ্āϤāĻŽ āĻļāĻšāϰ āĻĸাāĻ•া।"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center"
/>



</LinearLayout>

</ScrollView>


</LinearLayout>
Main XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="#FBB29D"
android:id="@+id/mainLayout"
>

<Button
android:id="@+id/bAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Layout"
/>

<Button
android:id="@+id/bRemove"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Remove Layout"
/>

<LinearLayout
android:id="@+id/myLay"
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical"
android:background="#182FAE"
android:padding="10dp"
>

</LinearLayout>

</LinearLayout>


JAVA //////////

package com.rakibshah.class_237;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

Button bAdd,bRemove;
LinearLayout mainLayout, myLay;
LayoutInflater layoutInflater;
ImageView imageDis;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mainLayout = findViewById(R.id.mainLayout);
bAdd = findViewById(R.id.bAdd);
bRemove = findViewById(R.id.bRemove);
myLay = findViewById(R.id.myLay);

layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View myView = layoutInflater.inflate(R.layout.new_layout, myLay);

imageDis = myView.findViewById(R.id.imageDis);

imageDis.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

bAdd.setText("Hello World");

}
});

bRemove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

myLay.removeAllViews();

}
});



}
}



No comments: