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)
}
Related
In an activity java file, we created radio buttons dynamically in a for loop to display when the activity is run. However, because they are dynamically created, we are am unable to edit their properties (such as color, text size, etc) as in a typical xml file. How can we set such properties (color, size, etc...) programmatically?
public void PrintGames(ArrayList<String> games,String league ) {
setContentView(R.layout.activity_api_connection_test);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
RadioGroup rg2 = new RadioGroup(this);
rg2.setOrientation(LinearLayout.VERTICAL);
//ArrayList<RadioButton> buttons = new ArrayList<RadioButton>();
for (int i = 0; i < games.size(); i++) {
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(LinearLayout.VERTICAL);
RadioButton button = new RadioButton(this);
RadioButton button2 = new RadioButton(this);
button.setText("Home");
button2.setText("Away");
button.setId(View.generateViewId());
button2.setId(View.generateViewId());
TextView textbutton = new TextView(this);
rg.addView(textbutton);
rg.addView(button);
rg.addView(button2);
textbutton.setText("" + games.get(i));
rg2.addView(rg);
}
layout.addView(rg2);
if (games.isEmpty()) {
TextView nogames = new TextView(this);
nogames.setText("No Games today");
layout.addView(nogames);
} else {
Button submit = new Button(this);
submit.setText("Submit");
layout.addView(submit);
//on click listner
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Store(rg2,league);
}
});
}
}
I found some threads saying its not possible for radio buttons and there was suggestions to create vector asset drawables, but many of those threads are outdated (2011), and I have a feeling its possible now with the advanced studio we have.
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);
}
});
I have a method that creates layouts vertically each with 3 buttons aligned horizontally with onClick creating one at a time with each click to a max of 5 layouts. How do I remove those layouts with a button that was created.
public void addTroop(Editable name){
LinearLayout mainPage = (LinearLayout) findViewById(R.id.manageTroopsMain);
if (count <= 5)
{
//CREATE NEW LINEAR LAYOUT
LinearLayout addTroopLayout = new LinearLayout(this);
//CREATE LAYOUT PARAMS FOR LAYOUT
LinearLayout.LayoutParams newLayout = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
newLayout.bottomMargin = 10;
//STYLE NEW LINEAR LAYOUT
addTroopLayout.setTag("addTroopLayout" + count);
addTroopLayout.setLayoutParams(newLayout);
addTroopLayout.setOrientation(LinearLayout.HORIZONTAL);
//CREATE NEW BUTTONS
Button newTroop = new Button(this);
Button remove = new Button(this);
Button change = new Button(this);
//CREATE LAYOUT PARAMS FOR BUTTONS
LinearLayout.LayoutParams newTroopParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 20f);
LinearLayout.LayoutParams rmvBtnParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, .5f);
LinearLayout.LayoutParams chngNameParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, .5f);
//STYLE NEW BUTTONS
newTroop.setText(name);
newTroop.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
newTroop.setLayoutParams(newTroopParam);
remove.setTag("rmvBtn" + count);
remove.setText("-");
remove.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
remove.setLayoutParams(rmvBtnParam);
change.setText("...");
change.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
change.setLayoutParams(chngNameParam);
//ADD VIEWS TO NEW LAYOUT
addTroopLayout.addView(newTroop);
addTroopLayout.addView(remove);
addTroopLayout.addView(change);
//ADD NEW LAYOUT TO mainPage LAYOUT
mainPage.addView(addTroopLayout);
//Increment Counter
count++;
}
}
Each time a layout is created it set with the following so that each layout will be named addTroopLayout1, addTroopLayout2, etc etc.
addTroopLayout.setTag("addTroopLayout" + count);
I did the same thing with the remove button
remove.setTag("rmvBtn" + count);
So now how do I access/edit/remove these? Could I do something like
Button rmvBtn1 = (Button) findViewByTag(R.id.rmvBtn1);
rmvBtn1.setOnClickListener(new view.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//REMOVE CREATED LAYOUT
//DECREMENT ALL LAYOUT TAGS BY 1 IF NOT LAST LAYOUT
//DECREMENT count VAR BY ONE
//ERASE ALL VARIABLES ASSOCIATED WITH THIS VIEW
}
});
This code is obviously giving me errors and im sure this is something simple. Thanks.
////////////////////EDIT/////////////////////
Sorry if I wasent clear on what I wanted. So I have created/styled a button using my if statement that is called from a method using an onClick:
Button newTroop = new Button(this);
remove.setTag("rmvBtn" + count);
Count starts at 1 and is incremented in when a button is pressed. Now I want to findViewByTag for that button to use it with something like:enter code here
Button rmvBtn1 = (Button) findViewByTag(rmvBtn1);
rmvBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//REMOVE CREATED LAYOUT
//DECREMENT ALL LAYOUT TAGS BY 1 IF NOT LAST LAYOUT
//DECREMENT count VAR BY ONE
//ERASE ALL VARIABLES ASSOCIATED WITH THIS VIEW
}
});
So specifically how to I use a button that was created with my if statement? Do I still have to define it by
Button rmvBtn1 = (Button) findViewByTag(rmvBtn1);
or is there another way I should be doing this?
keep view in remove button, and keep view's parent int view's tag
addTroopLayout.setTag(mainPage);
remove.setTag(addTroopLayout);
and then use it like this
rmvBtn1.setOnClickListener(new view.OnClickListener() {
#Override
public void onClick(View v) {
View willRemove = (View)v.getTag();
LinearLayout mainPage = (LinearLayout)willRemove.getTag();
mainPage.removeView(willRemove);
}
});
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....
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);
}
});