How to apply TextView linear layout (Java, Android Studio, development)
This example shows how to dynamically add a TextView to a LinearLayout on Android.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project, and fill in all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
<?xml version="1.0" encoding="utf-8"?>
android:id="@+id/rlMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical" />
Step 3 − Add the following code to src/MainActivity.java package
package Asbury Park Press NJ | Jersey Shore & New Jersey News.sample;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout llMain = findViewById(http://R.id.rlMain);
TextView textView = new TextView(this);
textView.setText("I am added dynamically to the view");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
textView.setLayoutParams(params);
llMain.addView(textView);
}
}
Step 4 − Add the following code to androidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from the android studio, open one of your project's activity files and click the Run icon from the toolbar. Select your mobile device as an option and then check your mobile device that will display your default screen −
Articoli simili
- Cosa è meglio progettare, un layout relativo o un layout lineare in Android?
- Why does Android button shape changes but not color after applying background drawable (Android, button, Android drawable, development)?
- How to apply a gradient color on my navbar through materialize CSS
- How to apply for driving license online