<?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">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Only I can change my life. No one can do it for me."
android:textSize="200px"
android:typeface="serif"/>
</LinearLayout>
JAVA
package com.example.a07_29;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
registerForContextMenu(tv1);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("컨텍스트메뉴");
menu.add(0,1,0,"배경색: RED");
menu.add(0,2,0,"배경색: GREEN");
menu.add(0,3,0,"배경색: BLUE");
}
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == 1) {
tv1.setBackgroundColor(Color.RED);
return true;
} else if (id == 2) {
tv1.setBackgroundColor(Color.GREEN);
return true;
} else if (id == 3) {
tv1.setBackgroundColor(Color.BLUE);
return true;
}
return super.onContextItemSelected(item);
}
}