How to disable programmatically button by using xml button - android

i have 4 button create in programmatically button in for loop, is it possible to disable the onclicklistener function or ontouchlistener function by using button create in xml ? Sorry for my einglish, wish can be understand my question.
Sample Image
Code
public class Tab1 extends Fragment {
TextView textView, test1;
Button button, button2;
LinearLayout rl;
//Overriden method onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View h = inflater.inflate(R.layout.tab1, container, false);
//Returning the layout file after inflating
//Change R.layout.tab1 in you classest
textView = (TextView) h.findViewById(R.id.textView);
test1 = (TextView) h.findViewById(R.id.test1);
button = (Button) h.findViewById(R.id.button);
button2 = (Button) h.findViewById(R.id.button2);
rl = (LinearLayout) h.findViewById(R.id.tlayout);
for (int i = 1; i < 5; i++) {
final Button btnAddARoom = new Button(getActivity());
RelativeLayout.LayoutParams params;
params = new RelativeLayout.LayoutParams
(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
btnAddARoom.setText("Add");
btnAddARoom.setLayoutParams(params);
rl.addView(btnAddARoom);
btnAddARoom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
test1.setText("qweqweqweqwe");
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnAddARoom.setOnClickListener(null);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
test1.setText("111111111111111");
}
});
}
return h;
}
}

Add all the buttons you created from the for loop into an array.
ArrayList<Buttons> buttons = new ArrayList<Buttons>();
for(int i =1 ; i<5 ; i++) {
final Button btnAddARoom = new Button(getActivity());
RelativeLayout.LayoutParams params;
params = new RelativeLayout.LayoutParams
(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
btnAddARoom.setText("Add");
btnAddARoom.setLayoutParams(params);
rl.addView(btnAddARoom);
buttons.add(YourDynamicallyCreatedButton)
}
Now in your onClick of xml button:
onClick(){
for(Button button: buttons){
button.setEnable(false)
}
}
Note:
When we add a view(button or text..) via xml we give it an id. We can access these elements with this ID. Similarly, when we create buttons dynamically, we need to set id to the buttons/views.

Call setEnable(false) on your Button reference
Example:
Button b ;
..
..
b.setEnable(false);

Keep reference of your programatically added buttons:
Button[] buttons = new Buttons[5];
for(int i =0 ; i<buttons.length ; i++) {
buttons[i] = new Button(getActivity());
final Button btnAddARoom = buttons[i];
RelativeLayout.LayoutParams params;
params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
btnAddARoom.setText("Add");
btnAddARoom.setLayoutParams(params);
rl.addView(btnAddARoom);
btnAddARoom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
test1.setText("qweqweqweqwe");
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnAddARoom.setOnClickListener(null);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
test1.setText("111111111111111");
}
});
}
Now when you want to disable these buttons:
private disableButtons() {
for(int i=0;i<buttons.length;i++) {
buttons[i].setEnabled(false);
}
}
Hope it will help you!

If you have a dedicated parent for the four buttons then you can eliminate the Arralist or array.
LinearLayout ll = ..//your linear layout or any other parent
final int childcount = ll.getChildCount();
for (int i = 0; i < childcount; i++) {
View view = ll.getChildAt(i);
if (view instanceof Button) {
view.setEnabled(false);
}
}

Related

How can get dynamic button text from another dynamic button OnClickListener event

I want get second dynamic button text from another dynamic button OnClickListener event:
Here is define some dynamic buutons:
LinearLayout lv=(LinearLayout)findViewById(R.id.lv);
for (int k = 1; k <= str[0].length(); k++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);
btnTopEn = new Button(this);
btnTopEn.setId(k);
final int id_ = btnTopEn.getId();
btnTopEn.setText(" ");
lv.addView(btnTopEn, params);
btnTopEn = ((Button) findViewById(id_));
final Button finalBtnT = btnTopEn;
btnTopEn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finalBtnT.setText("");
}
});
}
Now I want text of second button from OnClickListener Event:
TableLayout layout = (TableLayout)findViewById(R.id.TableL);
String stt="RZCEADHPTAUJTSFR";
int l=0;
for (int f=0; f<=1; f++) {
TableRow tr = new TableRow(this);
for (int c=0; c<=7; c++) {
btnCEn = new Button (this);
String ss=(String.valueOf(stt.charAt(l)));
btnCEn.setText(ss);;
final Button finalBtnB = btnCEn;
btnCEn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int m=2;
btnTopEn = ((Button) findViewById(m));
final Button finalBtnT = btnTopEn;
if (finalBtnT.getText().equals("")) {
String stGetText=finalBtnB.getText().toString();
finalBtnT.setText(stGetText);
break;
}
}
}
});
TableRow.LayoutParams lp = new TableRow.LayoutParams(100,100);
tr.addView(btnCEn, lp);
}
layout.addView(tr);
}
I wrote some code in OnClickListener event but none happen!
What is the value of str in the first loop ?
Also you are setting the text to a space .
btnTopEn.setText(" ");
And while checking you check for empty :
if (finalBtnT.getText().equals("")){
}
Try changing to
if (finalBtnT.getText().toString().trim().equals("")){
}

Can a TextView inside LinearLayout container be collapsed and expand on Button click?

I can dynamically add the text fields in my LinearLayout container.
Now I need to collapse those added fields in click of the Button those added fields should get collapse with any label.
Can it be done? Below is the code that i can add the EditText dynamically.
txtHeading = (EditText)findViewById(R.id.heading);
buttonAdd = (Button)findViewById(R.id.add);
container = (LinearLayout)findViewById(R.id.container);
buttonAdd.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0)
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.list_view, null);
LinearLayout linearLayout=(LinearLayout)findViewById(R.id.layout_icon_select);
TextView textOut = (TextView)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
textIn.setText(null);
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}
});
container.addView(addView);
}}});
btnsave = (Button) findViewById(R.id.btn_save);
btnsave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//What to insert here?
on click to save button in need to collapse all the textView Elements.
let us consider Heading is the label and it is named as Gender so if i added the options as male and female then after clicking the save button this should get collapsed under the Heading.
Based on what I'm kind of guessing that you want... I would create a custom view class based on a linear layout. Each having two edit texts.
Create and add this class to dynamically when the add button is pressed. When the save button is pressed, you can loop over the parent LinearLayout's children and then call a helper method to collapse the second edit text element.
To Loop over the parent and collapse children. Now the CustomElement and the collapseOptionField() call is custom code you must write.
LinearLayout parentLayout = findViewById(...);
for ( int i = 0; i < parentLayout.getChildCount(); i++ ) {
CustomElement ce = parentLayout.getChildAt(i);
ce.collapseOption();
}
Update
Custom class with collapse-able option:
public class TextWithOption extends LinearLayout {
EditText edit0;
EditText edit1;
public TextWithOption(Context context, AttributeSet attrs) {
this(context);
}
public TextWithOption(Context context) {
super(context);
View view = LayoutInflater.from(context).inflate(R.layout.edit_text_option, null);
edit0 = (EditText) view.findViewById(R.id.edit_text0);
edit1 = (EditText) view.findViewById(R.id.edit_text1);
addView(view);
}
public void collapseOption() {
edit1.setVisibility(View.GONE);
}
public void showOption() {
edit1.setVisibility(View.VISIBLE);
}
}
Activity code that will add new options and then collapse them:
final LinearLayout optionsLayout = (LinearLayout) findViewById(R.id.outer_layout);
Button addButton = (Button) findViewById(R.id.button_add);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextWithOption to = new TextWithOption(context);
optionsLayout.addView(to);
}
});
((Button) findViewById(R.id.button_save)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for ( int i = 0; i < optionsLayout.getChildCount(); i++ ) {
((TextWithOption)optionsLayout.getChildAt(i)).collapseOption();
}
}
});
public static void collapse(final View v) {
ScaleAnimation anim = new ScaleAnimation(1, 1, 1, 0);
v.startAnimation(anim);
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
v.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
onclick of button write view.collapse
view equals to whatever view you want collapse

Adding onClick to buttons that are created programmatically

A TextView and Button are programmatically created and added to a pre-existing vertical layout making it look like a vertical list of views. These views are only created based off the user entering data into an edittext and that data being saved into an ArrayList.
How can I add an onClick function to my 'trash' buttons that are programmatically created that would allow them to remove the view they are associated with.
public static ArrayList<String> deckNameArray = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainLayout);
for(int i = 0; i < deckNameArray.size(); i++)
{
LinearLayout layout = new LinearLayout(this);
if ((i % 2) == 0) {
layout.setBackgroundColor(Color.CYAN);
} else {
layout.setBackgroundColor(Color.WHITE);
}
layout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
layout.setPadding(10, 5, 10, 5);
layout.setWeightSum(5);
mainLayout.addView(layout);
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 4f);
TextView deckName = new TextView(this);
deckName.setText(deckNameArray.get(i));
deckName.setTextColor(Color.BLACK);
deckName.setTextSize(18);
deckName.setGravity(Gravity.CENTER_VERTICAL);
deckName.setLayoutParams(textViewParams);
layout.addView(deckName);
LinearLayout.LayoutParams imageButtonParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 1f);
ImageButton remove = new ImageButton(this);
remove.setImageResource(R.mipmap.trash);
if ((i % 2) == 0) {
remove.setBackgroundColor(Color.CYAN);
} else {
remove.setBackgroundColor(Color.WHITE);
}
remove.setLayoutParams(imageButtonParams);
layout.addView(remove);
}
remove.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
See http://developer.android.com/reference/android/widget/Button.html
You can set a click listener on any View, using code similar to the sample below:
View myView;
myView.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
// TODO your logic here
}
}

set background color for imageview doesn't work

I am trying to change ImageView background when clicked(like Duolingo)
Here is my code from fragment :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.frag_repeat, container, false);
final int[] a1 = {0};
final int[] a2 = {0};
final int[] a3 = {0};
final int[] a4 = {0};
TypedArray itemsIcon = getResources().obtainTypedArray(R.array.nav_drawer_icons);
ImageView wer1 = (ImageView) rootView.findViewById(R.id.imageView);
ImageView wer2 = (ImageView) rootView.findViewById(R.id.imageView2);
ImageView wer3 = (ImageView) rootView.findViewById(R.id.imageView23);
ImageView wer4 = (ImageView) rootView.findViewById(R.id.imageView43);
TextView textView1 = (TextView) rootView.findViewById(R.id.textView711);
textView1.setText(ss[i]);
wer1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a1[0] = 1;
a2[0] = 0;
a3[0] = 0;
a4[0] = 0;
}
});
wer2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a2[0] = 1;
a1[0] = 0;
a3[0] = 0;
a4[0] = 0;
}
});
wer3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a3[0] = 1;
a2[0] = 0;
a1[0] = 0;
a4[0] = 0;
}
});
wer4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a4[0] = 1;
a2[0] = 0;
a3[0] = 0;
a1[0] = 0;
}
});
if(a1[0] > 0){
wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer1.setBackgroundColor(Color.parseColor("#42A5F5"));
wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
} else if(a2[0] > 0){
wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer2.setBackgroundColor(Color.parseColor("#42A5F5"));
wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
}else if(a3[0] > 0){
wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer3.setBackgroundColor(Color.parseColor("#42A5F5"));
wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
} else if(a4[0] > 0){
wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer4.setBackgroundColor(Color.parseColor("#42A5F5"));
wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
if(i1 == 1){
wer1.setImageResource(itemsIcon.getResourceId(i, -1));
} else if(i1 == 2){
wer2.setImageResource(itemsIcon.getResourceId(i, -1));
} else if(i1 == 3){
wer3.setImageResource(itemsIcon.getResourceId(i, -1));
} else if(i1 == 4){
wer4.setImageResource(itemsIcon.getResourceId(i, -1));
}
return rootView;
}
But ImageView background doesn't change! I have CardViews inside RelativeLayout and inside RelativeLayout I have ImageView.
Please share xml file. Maybe you don't set background for ImageView in xml
Each of your onClick listeners is just toggling values in an array. I suspect what you really want is to invoke the setBackground color code inside each handler.
From your code, I think what you are trying to do is this:
Whichever button was clicked - change it's background color from white to #42A5F5.
For the other three buttons not clicked, change the background color back to white.
I don't know what the name of your class is (I'll refer to it as "YourClass"). But make your 4 ImageViews member variables.
class YourClass extends Fragment // I'm guessing this is the declaration, it doesn't matter - use whatever you have now
{
ImageView _wer1;
ImageView _wer2;
ImageView _wer3;
ImageView _wer4;
Then inside onCreateView, assign each one of these member variables exactly where you assign the local variables:
_wer1 = (ImageView) rootView.findViewById(R.id.imageView);
_wer2 = (ImageView) rootView.findViewById(R.id.imageView2);
_wer3 = (ImageView) rootView.findViewById(R.id.imageView23);
_wer4 = (ImageView) rootView.findViewById(R.id.imageView43);
Now set each of your click listeners to invoke a "ToggleBackgroundColors". Notice that there's a different index value passed to this function in each callback handler.
_wer1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourClass.this.ToggleBackgroundColors(0);
}
});
_wer2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourClass.this.ToggleBackgroundColors(1);
}
});
_wer3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourClass.this.ToggleBackgroundColors(2);
}
});
_wer4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourClass.this.ToggleBackgroundColors(3);
}
});
Then implement ToggleBackgroundColors:
void ToggleBackgroundColors(int which)
{
int highlight = Color.parseColor("#42A5F5");
ImageView [] views = {_wer1, _wer2, _wer3, _wer4};
for (int x = 0; x < views.length; x++)
{
int background = (which == x) ? highlight : Color.WHITE;
views[x].setBackgroundColor(background);
}
}
In your onClickListener, you can also set colorFilter on your imageView when you onClick it.
Consider using the onTouchListener instead though as you would like the imageView to change color when you touch the button and not when you click on it.
ImageView iv = (ImageView)findViewById(resIdOfImageToFilter);
iv.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);

Remove the View from Layout of ScrollView

I have a ScrollView with LinearLayout into it.After adding the Layout into my LinearLayout, I wanted to remove the view but it removes only the first view.
I have tried parent.removeView(myView)
My Code for Layout adding :
LayoutInflater inflater = getLayoutInflater();
final View view_top = inflater.inflate(R.layout.layout_top, linear_layout,false);
final View view_bottom = inflater.inflate(R.layout.layout_bottom,linear_layout,false);
final RelativeLayout rel_layout_bottom = (RelativeLayout) view_bottom.findViewById(R.id.relative_bottom);
Button btn_update = (Button) rel_layout_bottom.findViewById(R.id.lst_todo_update);
ImageButton btn_remove = (ImageButton) rel_layout_bottom.findViewById(R.id.lst_btn_delete);
ImageButton btn_Color = (ImageButton) rel_layout_bottom.findViewById(R.id.lst_btn_color);
final RelativeLayout rel_layout_top = (RelativeLayout) view_top.findViewById(R.id.relative_top );
final TextView note_Title = (TextView) rel_layout_top.findViewById(R.id.lst_title );
final TextView note_color = (TextView) rel_layout_top.findViewById(R.id.lst_color_type );
note_Title.setText(note_title);
linear_layout.addView(rel_layout_bottom, 0);
JSONArray jsonArray=queryResult.getResultObjects().get(count).getJSONArray("todo_item");
for (int i = 0; i < jsonArray.length(); i++) {
final View view_middle = inflater.inflate(R.layout.layout_middle, linear_layout, false);
final RelativeLayout rel_layout_middle = (RelativeLayout) view_middle.findViewById(R.id.relative_middle);
final CheckBox note_check ;
final TextView note_content ;
note_content = (TextView) rel_layout_middle.findViewById(R.id.lst_content);
note_check = (CheckBox) rel_layout_middle.findViewById(R.id.lst_check);
btn_remove.setOnClickListener(null);
try {
//Getting data correctly here
linear_layout.addView(view_middle,0);
btn_remove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
linear_layout.removeView(view_middle); //Not able to remove the view here i.e view_middle
linear_layout.removeView(view_top);
linear_layout.removeView(view_bottom);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
linear_layout.addView(rel_layout_top , 0);
}
Any answer Appreciated...Thks
Something like below will solve your problem.
final View[] view_middleArr;
for (int i = 0; i < jsonArray.length(); i++) {
view_middleArr = new View[jsonArray.length()];
view_middleArr[i] = inflater.inflate(R.layout.layout_middle, linear_layout, false);
// ...
btn_remove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (View view_middle : view_middleArr) {
linear_layout.removeView(view_middle);
}
linear_layout.removeView(view_top);
linear_layout.removeView(view_bottom);
}
});
}
Edit: You are using same view_middle reference for all your view_middle istances inside your loop, so you can only remove last view_middle instance with your code.
The code
btn_remove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
linear_layout.removeView(view_middle); //Not able to remove the view here i.e view_middle
linear_layout.removeView(view_top);
linear_layout.removeView(view_bottom);
}
});
is in a for loop.
It means the click of the button will only remove the view_middle that was lastly added to the LinearLayout, as you are using the same object name.

Categories

Resources