activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".MainActivity" android:background="@android:color/white" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sample TextView..." android:background="#ffd1fcff" android:padding="25dp" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Set TextView Font Size" android:layout_below="@id/tv" /> </RelativeLayout>
MainActivity.java
package com.cfsuman.me.androidcodesnippets; import android.os.Bundle; import android.app.Activity; import android.util.TypedValue; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get the widgets reference from XML layout RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl); tanggapan TextView tv = (TextView) findViewById(R.id.tv); Button btn = (Button) findViewById(R.id.btn); // Set a click listener for Button widget btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Set TextView font/text size to 25 dp tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25); // Set TextView font/text size to 25 sp //tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25); // Set TextView font/text size to 25 pixels //tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 25); // Set TextView font/text size to 25 points //tv.setTextSize(TypedValue.COMPLEX_UNIT_PT, 25); } }); } }
- How to create a TextView programmatically
- How to programmatically bold TextView text
- How to change TextView width programmatically
- How to add padding to TextView programmatically
- How to set TextView text style italic programmatically
- How to add a hyperlink programmatically in a TextView
- How to display html formatted text in a TextView
- How to place a view at the center of a RelativeLayout
- How to set RelativeLayout border radius
- How to remove all items from ListView
Komentar
Posting Komentar