Only click Checkbox in clickable Relative Layout - java

I'm trying to design a layout with a checkbox inside of a relative layout. The relative layout is clickable and works as it is supposed to, but when the checkbox is click, it runs the checkbox code, and the relative view code.
Is there anyway to ignore the relative layout's OnClickListener while running the checkbox?
RelativeLayout holder = new RelativeLayout(context);
contactHolder.setId(View.generateViewId());
TextView name = new TextView(context);
final CheckBox checkBox = new CheckBox(context);
checkBox.setId(View.generateViewId());
name.setText(contact.getName());
name.setId(View.generateViewId());
holder.addView(name);
holder.addView(checkBox);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!checkBox.isChecked()) {
checkBox.setChecked(true);
}
else if (checkBox.isChecked()){
checkBox.setChecked(false);
}
}
});
holder.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!checkBox.isChecked()) {
checkBox.setChecked(true);
}
else if (checkBox.isChecked()){
checkBox.setChecked(false);
}
}
});
userView.addView(holder);
}

use this in Relativelayout
android:clickable="true"

Remove the listener from the check box. Then change the logic in RelativeLayout's listener to act based on the state of the check box.

Related

I want to create a checkbox when button is clicked

I want to make it so that every time someone types something in the editText and clicks the button, the text comes up. It does it in my code, but I also want to add a checkbox next to it. How can I add the checkbox every time the button is pressed?
This is how it looks in the emulator right now after I typed test in the editText:
https://gyazo.com/6b9a050976ecd2c4b509220263bbdce1
The second problem is: when I write a second todo, it overwrites the last one so right now I can only have 1 todo. Why?
Code:
final TextView textViewPrint;
Button btn_print;
final EditText editTextType;
textViewPrint = findViewById(R.id.print_text);
btn_print = findViewById(R.id.button_add);
editTextType = findViewById(R.id.test_textEdit);
btn_print.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textViewPrint.setText(editTextType.getText().toString()) ;
editTextType.setText("");
}
});
You can add to your layout programmatically after it has been inflated. Add an id to your LinearLayout:
android:id ="#+id/layout"
Then in OnCreate (after your existing code), get a reference to it and add controls as you wish. And add these lines in OnCreate, For example:
LinearLayout l = (LinearLayout)findViewById(R.id.layout);
CheckBox cb = new CheckBox(this);
int lHeight = LinearLayout.LayoutParams.MATCH_PARENT;
int lWidth = LinearLayout.LayoutParams.WRAP_CONTENT;
l.addView(cb, new LinearLayout.LayoutParams(lHeight, lWidth));
setContentView(l);
First You need add to LinearLayout layout
LinearLayout l = (LinearLayout)findViewById(R.id.layout);
btn_print.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CheckBox cb = new CheckBox(this);
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
cb.setLayoutParams(lparams);
l.addView(cb);
}
});

How to display a spinner when a radio button clicked

I want to display a spinner on the screen only if a radio button is clicked but I couldn't find a solution. Here is my current code.
RadioGroup radio_grp=(RadioGroup) rootView.findViewById(R.id.radio_grp);
RadioButton rb1 = (RadioButton) rootView.findViewById(R.id.radioButton1);
RadioButton rb2 = (RadioButton) rootView.findViewById(R.id.radioButton2);
View.OnClickListener button1Listener = new View.OnClickListener() {
public void onClick(View v) {
// Toast message etc.
}
};
View.OnClickListener button2Listener = new View.OnClickListener() {
public void onClick(View v) {
// I want to show a spinner if they click the second radio button
}
};
rb1.setOnClickListener(button1Listener);
rb2.setOnClickListener(button2Listener);
return rootView;
First initialize the spinner like:
Spinner mSpinner = (Spinner) rootView.findViewById(R.id.mSpinner);
Of course, before that, add a spinner in your layout somewhere, and set it's android:visibility="invisible" if the first selected radio button is 1, else set it to visible. Then add listeners:
View.OnClickListener button2Listener = new View.OnClickListener() {
public void onClick(View v) {
// I want to show a spinner if they click the second radio button
if (v.getId() == R.id.radioButton1) mSpinner.setVisibility(View.INVISIBLE);
if (v.getId() == R.id.radioButton2) mSpinner.setVisibility(View.VISIBLE);
}
};
If you don't want it to occupy any space once it's not on the screen, use View.GONEinstead of View.INVISIBLE. Refer here for the difference.
Put ((Spinner) findViewById(R.id.mySpinner)).performClick(); in second radiobutton click
View.OnClickListener button2Listener = new View.OnClickListener() {
public void onClick(View v) {
((Spinner) findViewById(R.id.mySpinner)).performClick();
}
};

How to use Onclick event when using <include> tag

I have two java class and two layout for both the class.
Each layout is having one button in it.
Both classes are extending Activity.
Now in first layout I used include tag like this
<include
android:id="#+id/clicked"
layout="#layout/activity_main" />
I can now see two buttons but the second button is not working.
First You have to declare and initialise the include view and then decalre and initialise both buttons using view.findViewById() method as follows:
View includeView = (View)findViewById(R.id.clicked);
Button button1 = (Button)includeView.findViewById(R.id.button1ID); //decalre button like this
Button button2 = (Button)includeView.findViewById(R.id.button2ID);
And then set their onClickListeners
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//code whatever you want to do here
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//code whatever you want to do here
}
});
** EDIT **
Fixed the typo. Should be includeView on the findViewById.
Good explanation though!

How to make a textview show after the button click how to hide the textview again by clicking on the same button in android java

How to make a textview show after the button click how to hide the textview again by clicking on the same button in android java.Should i have to try if.else structure?
You can use a FLAG of some sort to make this happen
int i = 0;
yourButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(i%2==0){
textView.setVisibility(View.INVISIBLE);
}
else{
textView.setVisibility(View.VISIBLE);
}
i++;
}
}
Initially make the textview disable after clicking the button make it enable,again after clicking the button disable the button.
Use textview.setVisibility() to achieve this.
In button.setOnClickListner() method put this code:
if(textview.getVisibility() == View.GONE){
textview.setVisibility(View.Visible);
}else{
textview.setVisibility(View.GONE);
}
and in layout xml set default visibility of textview as you desire that can be visible or gone or invisible
yourButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if(textview.getVisibility() == View.GONE)
{
textview.setVisibility(View.Visible);
}
else
{
textview.setVisibility(View.GONE);
}
}
}

ImageButton changing an Image of another ImageButton causes unintended cycle

Aftermath of this: Image for ImageButton doesn't change as intended
When ImageButton of "ibChamp" is clicked, it opens up champions.xml. In that layout, there is an Imagebutton called "ibAnnie". What I want that to do when clicked is, to change the image of "ibChamp", and to switch to that layout. What happened here is that after "ibAnnie" is clicked, it switches to "create_build_page.xml", and in that is a changed picture of "ibChamp". But that only happens for a split second before changing back to the original, unchanged picture, "create_build_page.xml" layout. When I click the back button, it goes to the "create_build_page.xml" layout with the changed picture, but the buttons do not work. I would gladly provide any additional information required.
I'm still not entirely clear on what you're trying to achieve... but if my understanding is correct, this should get you closer:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.champions);
ImageButton ibAnnie = (ImageButton) findViewById(R.id.ibAnnie);
ibAnnie.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
RelativeLayout test = (RelativeLayout) findViewById(R.id.layoutChampions);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View createView = layoutInflater.inflate(R.layout.create_build_page, null);
test.addView(createView);
ImageButton img = (ImageButton) view.findViewById(R.id.ibChamp);
img.setImageResource(R.drawable.ic_launcher);
img.setTag(R.drawable.ic_launcher); // so you can retrieve it later
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// this will set the imageView of ibAnnie
ibAnnie.setImageView((Integer)view.getTag());
// this will remove the selection view from your layout
((RelativeLayout) findViewById(R.id.layoutChampions)).removeChild(createView);
}
});
// this opens another Activity... you don't want that, at least not for what you seem to be trying to do.
/*try {
Intent open = new Intent("android.intent.action.CREATE");
startActivity(open);
} catch (Exception e) {
}*/
}
});
}

Categories