In android, how do I create a click-able text link (embedded in a dynamic layout) that produces a pop-up window. I want to put text in the pop-up window.
Is it like creating a hyperlink?
TextView link = (TextView) findViewById(R.id.textView3);
String linkText = "<a href='https://www.google.com'>linked text</a>";
link.setText(Html.fromHtml(linkText));
You are looking for ClickableSpan.
Usage:
TextView textView = (TextView) findViewById(R.id.textView3);
textView.setMovementMethod(LinkMovementMethod.getInstance());
SpannableString spannableString = new SpannableString("your text");
spannableString.setSpan(new ClickableSpan() {
#Override
public void onClick(View widget) {
// show your pop-up here
}
}, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
Related
I want to create this textview, and then display it after this button is clicked, but no textview is displayed.
I dont't want to use findViewById(), if possible, because I want to create a new textview every time the button is pressed. I've tried making a linear layout first, but I've seen a lot of websites say that you don't need to, and I would prefer not to. Any advice would be helpful. Thank you.
EditText name=layout.findViewById(R.id.enterName);
final String Name=name.getText().toString();
Button create=layout.findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView ProgrammaticallyTextView = new TextView(MainActivity.this);
ProgrammaticallyTextView.setText(Name);
ProgrammaticallyTextView.setTextSize(22);
popup.dismiss();
}
});
There are no error messages and the logcat doesn't say that anything is wrong.
Try like this :
private LinearLayout lLayout;
private EditText lEditText;
private Button lButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lLayout = (LinearLayout) findViewById(R.id.linearLayout);
lButton = (Button) findViewById(R.id.button);
lEditText = (EditText) findViewById(R.id.editText);
lButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText("Text New");
}
private OnClickListener onClick() {
return new OnClickListener() {
#Override
public void onClick(View v) {
lLayout.addView(createTextView(lEditText.getText().toString()));
}
};
}
private TextView createTextView(String text) {
final LayoutParams loutParams = new
LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(loutParams );
textView.setText("Text is " + text);
return textView;
}
Use the activity's findViewById() method to reference your layout views. For example, you can replace
EditText name=layout.findViewById(R.id.enterName);
Button create=layout.findViewById(R.id.create);
with
EditText name=getActivity().findViewById(R.id.enterName);
Button create=getActivity().layout.findViewById(R.id.create);
Note: if you are not using fragments then there is no need to use getActivity since findViewById() is a method of the superclass AppCompactActvity( or Activity).
I guess your code is not working because the Button View and Editext Views have not been reference when activity starts for the oncreate() method
I have no clue what I am doing wrong but when I print the text from the edit text (using getText().toString()), in the logcat it is always an empty string. I am wondering if it has to do with that it is being done inside the onClickfunction for the continue button. I'm putting the whole onCreate function because the code inside the onClick function seems to match exactly what countless tutorials have shown.
#Override
protected void onCreate(Bundle savedInstanceState)
{
Button add_test = new Button(this);
Button delete_test = new Button(this);
Button[] barray = new Button[100];
int trans_grey = Color.parseColor("#40000000");
final String[] test_types = new String[] {"Choose...","Terms", "Multiple choice", "Custom"};
final ArrayAdapter<String> type_adapter = new ArrayAdapter<String>(this,R.layout.spinner_item,test_types);
// Preliminary operations and display opening layouts
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
LinearLayout scroll_layout = (LinearLayout)findViewById(R.id.scroll_layout);
LayoutParams scroll_params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
// Add the add a dynamic button
for (int index = 0; index <= 4; index++)
{
barray[index] = new Button(this);
barray[index].setBackgroundColor(trans_grey);
barray[index].setText("Array buttons");
barray[index].setTextColor(Color.parseColor("#CCffffff"));
scroll_layout.addView(barray[index], scroll_params);
}
add_test.setTextColor(Color.parseColor("#CCffffff"));
add_test.setBackgroundColor(trans_grey);
add_test.setText("Add a Test");
scroll_layout.addView(add_test, scroll_params);
add_test.setOnClickListener(new View.OnClickListener()
{#Override
public void onClick(View v)
{
AlertDialog.Builder add_test_builder = new AlertDialog.Builder(TestActivity.this);
final View add_test_view = getLayoutInflater().inflate(R.layout.add_test_menu, null);
Spinner type_spinner = (Spinner) add_test_view.findViewById(R.id.type);
add_test_builder.setView(add_test_view);
final Button continue_button = (Button) add_test_view.findViewById(R.id.continue_button);
AlertDialog dialog = add_test_builder.create();
dialog.show();
dialog.getWindow().setLayout(950,900);
type_spinner.setAdapter(type_adapter);
continue_button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
View add_test_view = getLayoutInflater().
inflate(R.layout.add_test_menu, null);
// The view of the test attribute dialog
EditText test_title = (EditText) add_test_view.
findViewById(R.id.testTitle);
// Test title widget to hold title of test
String user_title = test_title.getText().toString();
Log.i(TAG, "onClick: " + user_title);
}
});
return;
}
});
// Add the delete a test button
delete_test.setTextColor(Color.parseColor("#CCffffff"));
delete_test.setBackgroundColor(trans_grey);
delete_test.setText("Delete a Test");
scroll_layout.addView(delete_test, scroll_params);
return;
}
}
Layout:
<EditText
android:id="#+id/testTitle"
android:maxLines="1"
android:lines="1"
android:inputType="textAutoCorrect"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#00000000"
android:hint="Test Title"/>
Don't inflate the view yiu are assigning to dialog multiple times as you are creating multiple instances of it you are writing to one view but trying to fetch the data from another....Try doing it like this...
AlertDialog.Builder add_test_builder = new AlertDialog.Builder(TestActivity.this);
final View add_test_view = getLayoutInflater().inflate(R.layout.add_test_menu, null);
final EditText test_title = (EditText) add_test_view. findViewById(R.id.testTitle);
continue_button.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
String user_title = test_title.getText().toString();
Log.i(TAG, "onClick: " + user_title); } });
I think you should define EditText before and outside of your onlick method. Or get string after your onlick method.
IMO you may call editText string outside of Click method like above
final EditText test_title;
continue_button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
View add_test_view = getLayoutInflater().
inflate(R.layout.add_test_menu, null);
// The view of the test attribute dialog
EditText test_title = (EditText) add_test_view.
findViewById(R.id.testTitle);
// Test title widget to hold title of test
}
});
String user_title = test_title.getText().toString();
Log.i(TAG, "onClick: " + user_title);
Here's where I display an AlertDialog:
public void showCenteredInfoDialog(TextView _title, TextView _message) {
_title.setGravity(Gravity.CENTER);
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton("OK", null);
builder.setCustomTitle(_title);
builder.setMessage(_message.getText());
AlertDialog dialog= builder.show();
TextView messageView = (TextView) dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);
}
I'd like the font to be monospace.
Do I need to modify an xml file or can Java alone handle this? (If Java CAN handle it, it would probably take API 21, yes?)
I wonder if adding messageView.setFontFeatureSettings("..."); or messageView.setTypeface(...) with some way to refer to monospace would work. I can't find syntax for the String argument for setFontFeatureSettings.
This should work for your problem. Add it in the TextView part:
TextView messageView = (TextView) dialog.findViewById(android.R.id.message);
messageView.setTypeface(Typeface.MONOSPACE);
messageView.setGravity(Gravity.CENTER);
Also is possible to change the font in XML, but i think this it's more confortable.
Hope this will help you! :)
Here's a nice look at what #rpfc made for me (and it doesn't require API 21):
Method to show AlertDialog with a TextView _title and _message in MONOSPACE font:
public void showCenteredInfoDialog(TextView _title, TextView _message) {
_title.setGravity(Gravity.CENTER);
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setPositiveButton("OK", null)
.setCustomTitle(_title)
.setMessage(_message.getText());
AlertDialog dialog= builder.show();
TextView messageView = (TextView) dialog.findViewById(android.R.id.message);
messageView.setTypeface(Typeface.MONOSPACE);
messageView.setGravity(Gravity.CENTER);
}
Right now I have an EditText with id "getUserName" and a button next to it (both in a linear view) with id "setName"
I want someone to be able to click setName, and have the EditText field disappear, the button disappear, and a TextView take it's place. Here's what I have thus far:
public void setName(View view){
EditText editText = (EditText) findViewById(R.id.getUserName);
Button button = (Button) findViewById(R.id.setName);
TextView textView = (TextView) findViewById(R.id.displayName);
String playerName = editText.getText().toString();
((ViewManager)editText.getParent()).removeView(editText);
((ViewManager)button.getParent()).removeView(button);
Log.d("ScoreKeeper", playerName);
}
So I am successfully removing the desired elements from the screen, but I don't know how to add the textView to take their place.
How can I do that? I'm brand new to Android, so forgive me if this seems ignorant. I've tried looking it up!
Thanks
OPSRCFTW
You can simply hide the EditText, Button and TextView using turn visibility on.
You can add textview in your xml file and keep it invisible..
On button click, just change its visibility...
So the code is on buton click like below:
textview.setVisibility(View.VISIBLE);
edittext.setVisibility(View.GONE);
button.setVisibility(View.GONE);
First -> make ur textview Gone,
textview..setVisibility(View.GONE)
when u click the button..
Second -> Make
`Make the EditText and Button GONE with` `edittext.setVisibility(View.GONE);` and make textview visible textview..setVisibility(View.VISIBLE)
What about starting with
textView.setVisibility(View.GONE);
and then set an OnClickListener to your button:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setVisibility(View.VISIBLE);
}
});
Write code onCreate method of your class
EditText editText = (EditText) findViewById(R.id.getUserName);
Button button = (Button) findViewById(R.id.setName);
TextView textView = (TextView) findViewById(R.id.displayName);
textView.setVisibility(View.GONE);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
editText.setVisibility(View.GONE);
button.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
}
});
Hope it will help you.
You can also dynamically create the text view , like textview view= new textview(context); set the height and width thru layout params; and then add this view to parent view or pare layout like parent view.addview(textview). Change the visibility of the button and edittext rather than totally removing them.
on startup
textView.setVisibility(View.GONE);
on button click
textview.setVisibility(View.VISIBLE);
edittext.setVisibility(View.GONE);
button.setVisibility(View.GONE);
I have an EditText, and when a button is clicked, I want the text to be displayed on the screen. And the EditText can be used again to add other text and on and on. Thanks!
Something like:
EditText et = (EditText)findviewById(R.id.et1);
TextView display = (TextView)findviewById(R.id.tv1);
plusBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
tv1.setText(tv.getText().toString()+"\n"+et.getText().toString());
//prints the text from edittext and concatinates it to the textview previous text
}
});
Please use below code for that, it will solve your problem.
EditText editText1 = (EditText)findviewById(R.id.editText1);
TextView textView1 = (TextView)findviewById(R.id.textView1);
plusBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
textView1.append("\n"+editText1.getText().toString());
// OR
textView1.setText(textView1.getText().toString()+"\n"+editText1.getText().toString());
}
});
You can add Views to your Layout by using its addView() Method. So you could add a TextView upon each button-click which contains the content of your TextField.