activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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="#c7b8b0" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="♦ First item\n♦ Second item\n♦ Third item" android:background="#e68a88" android:textSize="18dp" android:layout_margin="10dp" android:padding="10dp" /> <TextView android:id="@+id/tv_second" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bulleted_list" android:background="#8dce92" android:textSize="18dp" android:layout_margin="10dp" android:padding="10dp" android:layout_toRightOf="@id/tv" android:layout_toEndOf="@id/tv" /> <TextView android:id="@+id/tv_third" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click the button to programmatically display a bulleted list here." android:background="#a3e4ea" android:textSize="18dp" android:layout_margin="10dp" android:padding="10dp" android:layout_below="@id/tv" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show a bulleted list in third TextView" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" /> </RelativeLayout>
res/values/strings.xml
<resources> <string name="app_name">Android Example</string> <string name="title_activity_settings">Settings</string> <string name="title_activity_main">Android Example - Bulleted List In A TextView</string> <string name="bulleted_list"> <li>First item</li>\n <li>Second item</li>\n <li>Third item</li> </string> </resources>
MainActivity.java
package com.cfsuman.me.androidcodesnippets; import android.app.Activity; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.Html; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private Context mContext; private Activity mActivity; private RelativeLayout mRelativeLayout; private TextView mTextView; private TextView mTextViewSecond; private TextView mTextViewThird; private Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get the application context mContext = getApplicationContext(); // Get the activity mActivity = MainActivity.this; // Get the widgets reference from XML layout mRelativeLayout = (RelativeLayout) findViewById(R.id.rl); mTextView = (TextView) findViewById(R.id.tv); mTextViewSecond = (TextView) findViewById(R.id.tv_second); mTextViewThird = (TextView) findViewById(R.id.tv_third); mButton = (Button) findViewById(R.id.btn); // Set a click listener for button mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // √ is the html entry, which we uses here as a bullet // Programmatically display the bulleted list on TextView String htmlString = "√ First item<br/>"+ "√ Second item<br/>"+ "√ Third item"; mTextViewThird.setText(Html.fromHtml(htmlString)); } }); } }
Komentar
Posting Komentar