Android bangla tutorial for beginners Part 2, Textview | অ্যান্ড্রয়েড বাংলা টিউটোরিয়াল

  • 23 April 20
  • Posted By : Admin
  • 51 Hits
  • 10 Comment

You will learn how to create textview and how to add action on it to do somthing. Styling text and changing color and changing font size etc.

}

Java code

After creating a project

You will get MainActivity.java paste below code

package com.salam.textview; import androidx.appcompat.app.AppCompatActivity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { public TextView textView1,textView2; public Button btn1,btn2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1 = findViewById(R.id.text1); textView2 = findViewById(R.id.text2); btn1 = findViewById(R.id.btn1); btn2 = findViewById(R.id.btn2); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { textView1.setText("This text has changed"); textView1.setTextSize(25f); textView1.setTextColor(Color.rgb(255,0,0)); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { textView2.setText("This is second text"); textView2.setTextSize(20f); textView2.setTextColor(Color.rgb(0,0,255)); } }); } }

Create xml file

when you create a project

By default you create a MainActivity.java fille and activity_main.xml file also. Below activity_main.xml code

<?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:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text1" android:text="text 1" android:textColor="#000"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text2" android:text="text 2" android:textColor="#000"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn1" android:text="Button 1" android:layout_marginTop="30dp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn2" android:text="Button 2"/> </LinearLayout>