Currently I am displaying two texts in the activity.
e.g.: "Group 1" and "Group 2".
I let user select the text (just like a button).
I need to find out whether the above displayed text is selected or not and then change the background color.
Here is the code I use for that. tv.isSelected() always evaluates to 'false'. Can any body tell me what I am doing wrong.
Is the "isSelected()" method used for a different purpose than how I use it?
TextView textView = new TextView(this);
textView.setText("Group 1");
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView tv = (TextView) view;
if (tv.isSelected()) {
tv.setBackgroundColor(Color.BLUE);
} else {
tv.setBackgroundColor(Color.WHITE);
}
}
});
linearLayout.addView(textView);
Try to do the following:
if (tv.getId() == textBox1Id) {
firstSelected = true;
} else { firstSelected = false; }
Try this
OnCreate()
{
TextView textView = new TextView(this);
textView.setText("Group 1");
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView.setSelected(true);
}
});
if (textView.isSelected()) {
textView.setBackgroundColor(Color.BLUE);
} else {
textView.setBackgroundColor(Color.WHITE);
}
linearLayout.addView(textView);
}
You could use a boolean variable to handle this:
private boolean tvSelected = false;
Your onClick method would change:
TextView textView = new TextView(this);
textView.setText("Group 1");
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (tvSelected) {
textView.setBackgroundColor(Color.WHITE);
tvSelected = false;
else {
tv.setBackgroundColor(Color.BLUE);
tvSelected = true;
}
});
linearLayout.addView(textView);
In terms of isSelected() usage, according to the API documentation:
A view can be selected or not. Note that selection is not the same as
focus. Views are typically selected in the context of an AdapterView
like ListView or GridView; the selected view is the view that is
highlighted.
Related
I have a dynamic Android form with dynamic fields behaviour.
Example: If user fills field A, show field B and hides field C.
I'm using methods view.setVisibility(View.VISIBLE) and view.setEnabled(boolean) for this purpose. This approach works for RadioGroup and EditText, but don't work for Spinner component.
The spinner is set to visible, but is always disabled. The method .setEnable(true) don't cause any effect.
I need to show the spinner enabled.
If the spinner was visible, the setEnabled() works. But if the spinner was invisible, this 2 commands together don't work.
spinner.setVisibility(View.VISIBLE)
spinner.setEnabled(Boolean.TRUE)
[]'s
Please provide more details. The following code shows you how you can show/hide or enable/disable the spinner.
boolean visibility = true;
boolean enabled = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
final Spinner spinner = new Spinner(this);
String[] animals = {"cats","dogs","lines","rats"};
spinner.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,animals));
linearLayout.addView(spinner);
Button visiblity = new Button(this);
visiblity.setText("VisibilityButton");
linearLayout.addView(visiblity);
setContentView(linearLayout);
visiblity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(visibility){
spinner.setVisibility(View.INVISIBLE);
visibility = false;
}
else{
spinner.setVisibility(View.VISIBLE);
visibility = true;
}
}
});
Button enabledButton = new Button(this);
enabledButton.setText("EnabledButton");
linearLayout.addView(enabledButton);
enabledButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(enabled){
spinner.setEnabled(false);
enabled = false;
}
else{
spinner.setEnabled(true);
enabled = true;
}
}
});
}
I have a TextView and I put a OnClickListener on this TextView. I use this action to load custom view onto a LinearLayout.
But when I click on this TextView twice, custom view is repeating on the LinearLayout. I clear all custom views on this LinearLayout before I load new custom views on to this LinearLaout.
This is my OnClickListener on TextView,
TextView rejectedTitleTextView = (TextView) findViewById(R.id.roster_menu_rejected_title);
rejectedTitleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
rejectedTitleTextView.setBackgroundColor(getResources().getColor(R.color.acceptedPurpleColour));
newTitleTextView.setBackgroundColor(getResources().getColor(R.color.defaultBlack));
acceptedTitleTextView.setBackgroundColor(getResources().getColor(R.color.defaultBlack));
locationLinearLayout.removeAllViews();
rosterBottomLayout.setVisibility(View.GONE);
Log.d("CHECK_ACTION"," REJECTED_TEXT_VIEW ");
InternetConnectivity internetConnectivity = new InternetConnectivity();
final boolean isConnectedToInternet = internetConnectivity.isConnectedToInternet(context);
if(isConnectedToInternet==true) {
try {
Thread.sleep(1300);
} catch (Exception e) {
e.printStackTrace();
}
getDataFromServer("REJECTED");
}else{
Snackbar.make(mainView, "No Internet Connection", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}
});
getDataFromServer("REJECTED");
is the method which I used to load custom view onto this LinearLayout.
How can I prevent this issue ?
Have any ideas ?
Inside onclickListener put
rejectedTitleTextView.setClickable(false);
and once finish your functionality make it as true because u need to click for next time .
rejectedTitleTextView.setClickable(true);
Inside setOnclickListener try below code:-
textView.setClickable(false);
Try this
rejectedTitleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mSpinner.setEnabled(false);
mSpinner.postDelayed(new Runnable() { #Override public
void run() {
mSpinner.setEnabled(true); }
}
// do your stuff here
});
You can maintain boolean value like this
boolean isClick=false;
rejectedTitleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!isClick)
{
//do your Stuff on onCLick
isClick=true;
}else
{
//leave it blank if you do not want to do anything second time
}
}
});
I am working with android LinearLayout and i am adding a TextView programmicataly and then on TextView click add an EditText in View. Now I want to get data at then end on button click from all added EditTexts in layout. here is my coding through which i am working.
TextView addMoreText = new TextView(this);
addMoreText.setText("Add More Ingredients");
addMoreText.setGravity(Gravity.CENTER);
addMoreText.setPadding(20, 20, 20, 20);
addMoreText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.add, 0);
addMoreText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final EditText editTextItem = new EditText(SearchRecipe.this);
editTextItem.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.cross, 0);
editTextItem.setPadding(20, 20, 20, 20);
editTextItem.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
parentLayout.removeView(editTextItem);
return true;
}
});
parentLayout.addView(editTextItem, 0);
}
});
parentLayout.addView(addMoreText);
searchRecipe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
i want to get all the data from EditText on searchRecipe click
Assign the id to the EditText:
editTextItem .setId({yourId});
Then inside searchRecipe onClick method, invoke:
EditText editTextItem = (EditText) parentLayout.findViewById({yourId});
And then use it as you want.
Add an arraylist and put the edittexts in it to keep track of them.
ArrayList<EditText> etList = new ArrayList();
In onClick of textview just add to it at the end.
etList.add(editTextItem);
In searchReciepe go through your list.
for(Edittext et : etList){
et.getText();
[...]
}
Edit: Obviously remember to remove it from the List on remove.
private int EDITTEXT_ID = 1;
private List<EditText> editTextList = new ArrayList<EditText>();
// Add Edittext programmatically in your view
private void addView() {
EditText editText = new EditText(this);
editText.setId(EDITTEXT_ID);
EDITTEXT_ID++;
editText.setText("Hello there " + EDITTEXT_ID);
editTextList.add(editText);
lnvEditText.addView(editText); // lnvEdittext is my LinearLayout which is added in XML file
}
// Get values of every Edittext on click of button
for(int i=0; i<editTextList.size(); i++) {
Log.e("All Values=", editTextList.get(i).getText().toString());
}
I have dynamically created Textviews in my application. I want to give On click event to this textviews.. When I click on the textview, I need to get the id of the textview..
You try following code.
TextView text = new TextView(this);
text.setText("text here");
ll.addView(text);
text.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "ID : "+arg0.getId(), Toast.LENGTH_SHORT).show();
}
});
in that "ll" is Layout which add textview and after adding put clickListener() for click event.
hope this is useful for you.
Have a look at setOnClickListener and getId.
TextView textView = new TextView(this);
// Set up your text view
textView.setId(1); // Any number is ok
textView.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
int id = v.getId(); // This is the id you want
// Do whatever you want here
}
});
If you are creating TextViews dynamically than just implement OnClickListener:
import android.view.View;
import android.view.View.OnClickListener;
public class MyClickListener implements OnClickListener {
#Override
public void onClick(View v) {
int vId = v.getId();
}
}
and set it to dynamically created view like this:
private TextView createTextView(int vId) {
TextView textView = new TextView(this);
textView.setId(vId);
textView.setOnClickListener(myClickListener);
return textView;
}
P.S.: Don't forget to create & initilialize myClickListener in your Activity.
In my activity I would like a TextView to appear below a checkbox once the checkbox has been clicked. How should I do this. Do I need to create a new activity that will display the new TextView below the checkbox. Or can I just use the same activity as before to accomplish this.
Thanks!
Add the TextView to your layout and set android:visibility="gone".
In your onCheckboxClicked() set the visibility of the TextView to VISIBLE
you can do it in same activity
in the xml create the TextView below CheckBox and yourTextView.visibility=gone
in your class write the following code :
yourCheckBox.setonClickListener=new onClickListener(){
#Override
public void onClick(View v) {
if(checkBox.isChecked())
yourTextView.setVisibility(View.VISIBLE);
else
yourTextView.setVisibility(View.GONE);
}
}
no need to create new activity .Just add textview in your layout file and keep it invisible..when you check the checkbox just make that Textview visible.
You have to create textbox in onChecked event .
you can refer this :
http://www.mysamplecode.com/2011/10/android-programmatically-generate.html
Try this:
TextView tv;
CheckBox cbS;
OnClickListener checkBoxListener;
checkBoxListener =new OnClickListener() {
#Override
public void onClick(View v) {
tv=(TextView)findViewById(R.id.tvDetails);
//by default keep textview visibility as invisible in xml file;
tv.setVisibility(View.GONE)
};
cbS.setOnClickListener(checkBoxListener);
This should help you. try this..
checkBxAutomaticLogin
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
ViewGroup automaticLoginLyt = (ViewGroup) findViewById(R.id.yourlayout);
LayoutInflater.from(SomeActivity.this).inflate(
R.layout.your_layout,
automaticLoginLyt, true);
editTxtUsername = (EditText) findViewById(R.id.edit_txt_user_name);
editTxtPassword = (EditText) findViewById(R.id.edit_txt_password);
} else {
ViewGroup automaticLoginLyt = (ViewGroup) findViewById(R.id.your_layout);
View v = automaticLoginLyt
.findViewById(R.id.your_layout);
if (v != null) {
automaticLoginLyt
.removeView(v);
}
}
}
});
You can try this
holder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (holder.checkBox.isChecked()==true){
holder.title.setPaintFlags(holder.title.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
else {
holder.title.setPaintFlags(holder.title.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
}
}
});
I think this can help you...
holder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (holder.checkBox.isChecked()==true){
holder.title.setPaintFlags(holder.title.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
else {
holder.title.setPaintFlags(holder.title.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
}
}
});