Creating a TextView once checkbox is clicked - android

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));
}
}
});

Related

How to set editText visible/invisible?

In my program I want to be able to hide the edit text when a radio button is check and then reappear when the user clicks the other radio button.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.waist2height); {
final EditText h = (EditText)findViewById(R.id.editText2);
final RadioButton rCM = (RadioButton) findViewById(R.id.radioCM);
final RadioButton rFT = (RadioButton) findViewById(R.id.radioFT);
if(rCM.isChecked()){
h.setVisibility(View.VISIBLE);
}
else if(rFT.isChecked()){
h.setVisibility(View.INVISIBLE);
}}
The code below is where i have the problem i only added in the relevant part of my code instead of the entire thing
if(rCM.isChecked()){
h.setVisibility(View.VISIBLE);
}
else if(rFT.isChecked()){
h.setVisibility(View.INVISIBLE);
}
Unfortunately I can't seem to get it to work.
Am I missing anything? Or have I got it all wrong altogether?
I have tried h.setVisibility(View.GONE); however it just ruines the format of the xml.
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==0){
h.setVisibility(View.VISIBLE);
}
else if(checkedId==1){
h.setVisibility(View.INVISIBLE);
}
}
});
Try this :
rCM.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
h.setVisibility(View.VISIBLE);
}
});
rFT.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
h.setVisibility(View.INVISIBLE);
}
});
I've made a sample and it works for me just take a look at this code :
I've declared those variables as global
RadioButton rb1,rb2;
TextView tbHideOrNot;
Then in my onCreate() I've got this :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rb1 = (RadioButton)findViewById(R.id.rb1);
rb2 = (RadioButton)findViewById(R.id.rb2);
tbHideOrNot = (TextView)findViewById(R.id.textView);
rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
tbHideOrNot.setVisibility(View.INVISIBLE);
rb2.setChecked(false);
}
}
});
rb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
tbHideOrNot.setVisibility(View.VISIBLE);
rb1.setChecked(false);
}
}
});
}
In my opinion, that's not a good idea use a RadioButton for the use that you want, I recommend you to use CheckBox cause you can only click ONE and this you have to check if the first RadioButton is clicked and you press the second one you'll have to uncheck the first, etc... well It's your code I'm only giving to you an advice, this code is working for me, let me know if it works for you :)
By the way if you want to use this code :
if(rCM.isChecked()){
h.setVisibility(View.VISIBLE);
}
else if(rFT.isChecked()){
h.setVisibility(View.INVISIBLE);
}
It's fine but you only will hide or not the TextView when you start the Activity and you'll have to put any RadioButton (if you want) android:checked="true" or false if you want to be showed or not the TextView, but with this code you won't get the click event on the RadioButtons.

android TextView selected option

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.

show/hide an listView on a buttonClick

final ImageView patientAllergyImage = (ImageView) findViewById(R.id.image);
patientAllergyImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
patientAllergyImage.setImageDrawable(getResources().
getDrawable(R.drawable.nav_down_green));
List.setVisibility(View.GONE);
}
});
I am making my List to Hide, but how do i show it when i click on the same button. I am not able to keep a boolean to check whether its clicked or not as it saying... The final local variable clicked cannot be assigned, since it is defined in an enclosing type neither an non final variable
Try this,
public void onClick(View V){
patientAllergyImage.setImageDrawable(getResources().
getDrawable(R.drawable.nav_down_green));
List.setVisibility(List.isShown() ? View.GONE : View.VISIBLE);
}
Instead of typical button, you can use a toogle Button to achieve this
ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// hide the listview
} else {
// show the listview
}
}
});
Try something like this:
public void onClick(View v) {
// TODO Auto-generated method stub
patientAllergyImage.setImageDrawable(getResources().
getDrawable(R.drawable.nav_down_green));
if(List.getVisibility()==View.VISIBLE){
List.setVisibility(View.INVISIBLE)
}else{
List.setVisibility(View.VISIBLE)
}
}
Replace INVISIBLE by GONE if needed. Hope this helped.
Remove final for boolean variable or
try this
try this
if(List.getVisibility()==View.GONE)
{
List.setVisibility(View.VISIBLE);
}
if(List.getVisibility()==View.VISIBLE)
{
List.setVisibility(View.GONE);
}
Seems that there are a few issues here.
First of all you shouldn't call your listview "List" this is masking the real class called List.
Best to use "listView" with a lowercase "l" if you are stuck for a decent variable name.
You don't need to use final everywhere.
Use setImageResource instead to keep you code clean and readable.
Use the ?true:false syntax when it is readable
ImageView patientAllergyImage = (ImageView) findViewById(R.id.image);
patientAllergyImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//see if the list view is visible
bool bVisible = listView.getVisibility();
//select the image resource
int iImageRes = bVisible?R.drawable.nav_down_green:R.drawable.nav_up_green;
//Toggle Image
(ImageView)v.setImageResource(iImageRes);
//Toggle List Visibility
listView.setVisibility(bVisible?View.GONE:View.VISIBLE);
}
});

Android is there ClickListener for linearLayout using addView?

I'm trying to create pinterest like layout. I find a way to achieve this: Android heterogeneous gridview like pinterest?!
However the problem is: I want to view item details after clicking each picture. But as I am using LinearLayout.addView() to add all the ImageViews, I'm not sure how I can get it clickable?
Is there anyway to be able to click each item on the view?
You can do this pretty easily by adding tag information to your image view that can be displayed when clicked.
Adding your image view would look like:
ImageView iv = new ImageView(context);
iv.setOnClickListener(your_listener);
iv.setTag("Item information");
linearLayout.addView(iv);
Then in your click listener:
public void onClick(View v) {
if(v instanceof ImageView) {
String info = (String)v.getTag();
/* Show information here */
}
}
Use this :
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// ADD your action here
}
});
or make your class implement the OnClickListner Interface and override the onClick() method
Use:
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do magic
}
});
And in your layout file mark the ImageView as clickable:
<ImageView
...
android:clickable="true">
...
</ImageView>
Building on the code that you linked, you can add a listener to each image view as you create it:
linear1 = (LinearLayout) findViewById(R.id.linear1);
linear2 = (LinearLayout) findViewById(R.id.linear2);
linear3 = (LinearLayout) findViewById(R.id.linear3);
for(int i=0;i<n;i++)
{
ImageView iv = new ImageView(this);
iv.setImageResource(R.id.icon);
iv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Your click code
}
}
int j = count % 3; <----
if(j==0)
linear1.addView(iv);
else if(j==1)
linear2.addView(iv);
else
linear3.addView(iv);
}

why parent view and child view are receiving onclickevent

I have a parent view LinearLayout i have added a child view TextView, But whenever i click on childview why parentview is also clicked. What i want is to differentiate whether only parent has been clicked and only childview is clicked.
LinearLayout rlmain = (LinearLayout)findViewById(R.id.linearLayout1);
rlmain.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
}
});
TextView tv = (TextView )findViewById(R.id.TextViewLayout1);
tv.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
}
});
then try to implement View.OnClickListener in yout Activity first, then try to override
#Override
public void onClick(View v){
if(v.getId().equals(R.id.linearLayout1) {
//do ur stuffs
} else {
//do your stuffs
}
Hope it will help you. :)
In android, ViewGroups (i.e. all layouts by inheritance) use the property (and corresponding xml attribute ) : addStatesFromChildren
If set, the parent will just be set in the very same state as one of his children view.
try this code...
LinearLayout rlmain = (LinearLayout)findViewById(R.id.linearLayout1);
rlmain.setOnClickListener(this);
TextView tv = (TextView )findViewById(R.id.TextViewLayout1);
tv.setOnClickListener(this)
#Override
public void onClick(View v){
switch(v.getId()) {
case R.id.TextViewLayout1:
//do ur stuffs
break;
case R.id.linearLayout1:
//do ur stuffs
break;
}
}

Categories

Resources