android onclicklistener GUI change - java

I am having a silly issue with onClickListeners modifying GUI elements. Below I have a radioGroup holding two buttons, an EditText and a Spinner. When the choose_old radio button is selected, I simply wanted to toggle enable/disable between the Spinner and EditText. The variables select_run and new_run_name are both instance variables and are
obtained from my XML files.
select_run = (Spinner)runDialoglayout.findViewById(R.id.run_choice_spinner);
new_run_name = (EditText)runDialoglayout.findViewById(R.id.new_run_name);
RadioButton choose_old = (RadioButton)runDialoglayout.findViewById(R.id.select_old);
RadioButton choose_new = (RadioButton)runDialoglayout.findViewById(R.id.select_new);
choose_old.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
Log.e("ERROR","GOT CLICK");
select_run.setEnabled(true);
new_run_name.setEnabled(false);
}
});
The 'GOT CLICK' message prints but I do not get the desired changes.
Note:
In trying solutions, I had wrapped the GUI changes in a handler.post with no effect.
If I set to enabled outside the onClickListener, it successfully changes it.
I've no doubt this is something dumb but can't seem to figure out why this is happening

Try putting this code in the listener.
boolean b = select_run.isEnabled();
select_run.setEnabled(!b);
new_run_name.setEnabled(b);

I tried the following code seems to work fine for me, please make sure views accessed in onClick are declared final
final Button button2 = (Button)findViewById(R.id.button2);
final EditText et = new EditText(this);
et.setText("Hi There");
RelativeLayout vg = (RelativeLayout)findViewById(R.id.relativeLayout1);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.BELOW, button2.getId());
vg.addView(et, lp);
final RadioButton rb = new RadioButton(this);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp1.addRule(RelativeLayout.RIGHT_OF ,button2.getId());
vg.addView(rb, lp1);
rb.setSelected(false);
rb.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
et.setEnabled(false);
}
});

Related

Unable to display textview on button click in android

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

Android: how to listen dynamically added button's click?

I have the following code that upon image capture, places a RelativeLayout container, which contains ImageView of the image and Button that is supposed to be the X (Remove) button.
private void addImage(Bitmap photo) {
RelativeLayout rLayout = new RelativeLayout(getApplicationContext()); // Any difference between 'this' and 'getApplicationContext()'?
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParas(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rLayout.setLayoutParams(params);
ImageView imageView = new ImageView(getApplicationContext());
imageView.setLayoutParams(new LayoutParams(100, 100));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(photo);
RelativeLayout.LayoutParams bParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
bParams.addRule(RelativeLayout.ALIGN_RIGHT, imageView.getId());
bParams.addRule(RelativeLayout.ALIGN_TOP, imageView.getId());
bParams.setMargins(0, -10, -10, 0);
Button closeButton = new Button(getApplicationContext());
closeButton.setText("X"); // This will be an icon later
closeButton.setBackgroundColor(Color.RED); // For visualization purposes
closeButton.setLayoutParams(bParams);
rLayout.addView(imageView);
rLayout.addView(closeButton);
parentLinearLayout = (LinearLayout) findViewById(R.id.horizontal_scrolling_linear_layout);
parentLinearLayout.addView(rLayout);
}
The ideal scenario is having a close/delete button on top-right corner of the image, that upon click will remove the parent RelativeLayout from it's parent. Some styling and placements in the code might not make sense, I am JUST starting Android Development so any help will be appreciated. I am also including small questions in comments that would help me clear things up in Android Dev.
Question is: How can I capture button (X) click and remove the parent relative layout from the code?
Any help is appreciated, thank you!
Try this:
closeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
((ViewGroup) rLayout.getParent()).removeView(rLayout);
}
});
I'd start by moving the following line into your Activity's onCreate():
parentLinearLayout = (LinearLayout) findViewById(R.id.horizontal_scrolling_linear_layout);
This helps to reduce unnecessary reassignment as well as clutter. Once that's done, the following should suit your needs.
Button closeButton = new Button(getApplicationContext());
closeButton.setText("X"); // This will be an icon later
closeButton.setBackgroundColor(Color.RED); // For visualization purposes
closeButton.setLayoutParams(bParams);
closeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (parentLinearLayout != null) {
parentLinearLayout.removeView(rLayout);
}
}
});
Just use
closeButton.setOnClickListener(new OnClickListener(){
//override method
public void onClick(View v) {
//do something
}
});
In addition,you can't remove the parent RelativeLayout ,because any view must be included by viewGroup.
And the difference between "this" and "getApplicationContext()" as you mentioned at the beginning is:
this == yourActivity.this ,it is activity's instance. However,getApplicationContext() will get Application's instance.
// Try this way,hope this will help you to solve your problem...
private void addImage(Bitmap photo) {
RelativeLayout rLayout = new RelativeLayout(getApplicationContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rLayout.setLayoutParams(params);
ImageView imageView = new ImageView(getApplicationContext());
imageView.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(photo);
RelativeLayout.LayoutParams bParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
bParams.addRule(RelativeLayout.ALIGN_RIGHT, imageView.getId());
bParams.addRule(RelativeLayout.ALIGN_TOP, imageView.getId());
bParams.setMargins(0, -10, -10, 0);
Button closeButton = new Button(getApplicationContext());
closeButton.setText("X"); // This will be an icon later
closeButton.setBackgroundColor(Color.RED); // For visualization purposes
closeButton.setLayoutParams(bParams);
closeButton.setTag(0,parentLinearLayout);
closeButton.setTag(1,rLayout);
closeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
LinearLayout parentLinearLayout = (LinearLayout)view.getTag(0);
RelativeLayout rLayout = (RelativeLayout)view.getTag(1);
parentLinearLayout.removeView(rLayout);
}
});
rLayout.addView(imageView);
rLayout.addView(closeButton);
parentLinearLayout = (LinearLayout) findViewById(R.id.horizontal_scrolling_linear_layout);
parentLinearLayout.addView(rLayout);
}

How to create buttons on pressing a button?

I am using following code to create buttons in a horizontal layout using array of button names:
LinearLayout tabView = (LinearLayout) findViewById(R.id.tabView);
tabView.setOrientation(LinearLayout.HORIZONTAL); //Can also be done in xml by android:orientation="vertical"
for (int i = 0; i < tabButtonNames.length; i++) {
Button btnTag = new Button(this);
btnTag.setText(tabButtonNames[i]);
btnTag.setWidth(50);
btnTag.setHeight(14);
btnTag.setTextSize(8);
btnTag.setId(i);
btnTag.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
---the code TODO
});
tabView.addView(btnTag);
}
They are created but I cannot change the height and the width of the buttons using setWidth, setHeight or LayoutParam. Then on pressing a button, I want to create a list of more buttons in my vertical layout using an array of button names. I used the same code as above in onClick method, but application crashes on pressing button. Also Button btn=new Button(this) cannot be used in onClick.
I have done this in i-Pad app easily,but here I am having trouble.
Use
Button btn = new Button(getApplicationContext());
OR
Button btn = new Button(ActivityName.this);
instead of
Button btn = new Button(this);
As Button requires context. And in OnClick, context of Activity is not accessible.
Button btn=new Button(this) is actually referring your clicklistiner, you have to refer your class, Button btn=new Button(classname.this) or create a simple function outside clickListener.
Just pass the context in new Button() and set layout params instead of height and width
for (int i = 0; i < tabButtonNames.length; i++) {
Button btnTag = new Button(<-Context->);//You need to pass context just write <ActivityName>.this
btnTag.setText(tabButtonNames[i]);
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(<width>,<height>);(50,40)
//btnTag.setWidth(50);
//btnTag.setHeight(14);
btnTag.setTextSize(8);
btnTag.setId(i);
btnTag.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
}
});
tabView.addView(btnTag);
btnTag.setLayoutParams(params)
}

Android: Create EditText on Runtime

I'm trying to create a view where the user can click a "plus" button, and have additional EditTexts be created. The goal is to have a base of 2 EditTexts, and each time the user clicks the button, add another 2 EditTexts.
How can I do this? I can add EditTexts from Java, but I can't figure out how to add and handle a list of them dynamically.
I was hoping to take however many pairs of EditTexts, and push it into a key/value HashMap or something.
Any ideas of how to do this? Thanks!
public class MyActivity extends Activity {
private LinearLayout main;
private int id = 0;
private List<EditText> editTexts = new ArrayList<EditText>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
main = new LinearLayout(this);
main.setOrientation(LinearLayout.VERTICAL);
Button addButton = new Button(this);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
addEditText();
}
});
Button submit = new Button(this);
submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
for (EditText editText : editTexts) {
editText.getText().toString();
// whatever u want to do with the strings
}
}
});
main.addView(addButton);
main.addView(submit);
setContentView(main);
}
private void addEditText() {
LinearLayout editTextLayout = new LinearLayout(this);
editTextLayout.setOrientation(LinearLayout.VERTICAL);
main.addView(editTextLayout);
EditText editText1 = new EditText(this);
editText1.setId(id++);
editTextLayout.addView(editText1);
editTexts.add(editText1);
EditText editText2 = new EditText(this);
editText2.setId(id++);
editTextLayout.addView(editText2);
editTexts.add(editText2);
}
Do it in a ListView.
Then you can just add them to a ListAdapter.
And then use adapter.notifyDatasetChanged()
May be I am not clear but Instead of adding Individual edit text you can add as Group View like Linear layout here you can use any flag values to add dynamic name conversions also.
That view you can update into List View like inflating rows in the List View....

Categories