I have the following code:
for (int i = 0; i < dataStrings.length; i++) {
TextView tv1 = new TextView(this);
tv1.setTextSize(TEXT_SIZE);
tv1.setTypeface(Typeface.MONOSPACE);
tv1.setText(dataStrings[i]);
dynamicLL.addView(tv1);
}
Basically, i am creating some amount of TextView's dynamically (the amount is unknown). I want each TextView to have its own OnLongClickListener. My question is how do I give each TextView its own unique OnLongClickListener when the TextViews are generated in this way, and how do I handle the clicks once the listener has been created?
You can just set them in your for loop, like this
OnLongClickListener longClickListener = new OnLongClickListener () {
#Override
public void onLongClick (View v) {
switch (v.getId()) ...
}
};
for (int i = 0; i < dataStrings.length; i++) {
TextView tv1 = new TextView(this);
tv1.setId(i * 1000);
tv1.setOnLongClickListener(longClickListener);
}
In your switch in a listener you can then check what TextView was pressed and do according action.
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.
This question already has answers here:
How to Programmatically Add Views to Views
(7 answers)
Closed 3 years ago.
I have made some buttons and stored them into an ArrayList. I need to add these buttons to my main activity layout.
I have tried to create a linearlayout and make that my main layout, however the buttons do not show.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// createButtons();setContentView(new CircleView(this));
}
public void createButtons() {
ArrayList<Button> buttons = new ArrayList<>();
int n = 0; // the number of buttons circumferencing the semicircle
for(int i = 0; i < 6; i ++) {
n = 7;
Button button = new Button(this);
double Xval = 500* Math.cos(i * Math.PI / n);
double Yval = 500* Math.sin(i * Math.PI / n);
button.setX((float)(Xval));
button.setY((float)(Yval));
buttons.add(button);
}
}
}
I expect to have my buttons appear in my main activity layout.
I Found some solutions for your case, but not the right solution, you must change it to your own implementation.
One way to do this, is create a layout and get the layout in code by id and in that you insert your buttons. This is one way to do this:
Button myButton = new Button(this);
myButton.setText("Push Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
Inserting mutiple buttons with an event, even if I do prefer to implement OnClickListener on the class:
for (int i = 1; i <= 20; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText("button " + id_);
btn.setBackgroundColor(Color.rgb(70, 80, 90));
linear.addView(btn, params);
btn1 = ((Button) findViewById(id_));
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(view.getContext(),
"Button clicked index = " + id_, Toast.LENGTH_SHORT)
.show();
}
});
}
You can find many samples of android native in the link Android Samples
Source
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'm trying to make a TextView programmatically in a LinearLayout. The program includes a checking system to check if its been added already and the prompt for creating the textview is an option in a spinner. Here is the full onClick method for the spinner
public void onClick(String Ingredient, int i) {
Toast.makeText(Kitchen.super.getContext(), "Selected "+Ingredient, Toast.LENGTH_SHORT).show();
if(Ingredient.equals(tomatoSauce.name)) {
if (tomatoSauce.init == 0){
tomatoSauce.init = 1;
TextView one = new TextView(getContext());
one.setText(Ingredient);
mainll.addView(one);
}
} else if(Ingredient.equals(chicken.name)) {
chicken.init = 1;
} else if(Ingredient.equals(olives.name)){
olives.init = 1;
}
}
The Linear layout is identified from the xml layout when the app is started in a separate method.
final LinearLayout mainll = (LinearLayout) getActivity().findViewById(R.id.main);
The app crashes upon selecting Tomato Sauce from the menu despite the lack of identified coding errors. Any help with this issue would be greatly appreciated.
Try to add below lines of code:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout_id);
TextView tv = new TextView(this);
tv.setText("hallo hallo");
tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
linearLayout.addView(tv);
I have a tableLayout in Android and each row in the table has an ImageButton, a TextView and another ImageButton. The no. of rows is variable and I am generating the table programmatically at runtime.
I am setting id of the buttons to be same as row number by using ImageButton.setId(i).
When the user clicks on any ImageButton, I want to be able to access its id in the onClickListener. Is there a way to do it? All answers on stackoverflow which I have seen regarding this involve a fixed no. of buttons and use a switch statement to find out the id. But since the no. of buttons is dynamic for me, this won't work for me.
Here's the code:
TableLayout tbl_layout = (TableLayout) findViewById(R.id.tbl_layout);
for (int i=0; i<num_rows; i++) {
TableRow tbl_row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
tbl_row.setLayoutParams(lp);
tbl_row.setPadding(0, 5, 0, 5);
//tbl_row.setBackground(R.drawable.border);
if(i == 3)
tbl_row.setBackgroundColor(Color.GREEN);
else
tbl_row.setBackgroundColor(Color.parseColor("#ffffd1"));
tbl_row.setGravity(Gravity.CENTER);
TextView tv = new TextView(this);
tv.setGravity(Gravity.CENTER);
tv.setText("Some text goes here\New line.");
ImageButton infoBtn = new ImageButton(this);
infoBtn.setImageResource(R.drawable.ic_menu_info);
infoBtn.setAdjustViewBounds(true);
infoBtn.setScaleType(ImageView.ScaleType.FIT_XY);
infoBtn.setMaxHeight(120);
infoBtn.setMaxWidth(120);
infoBtn.setId(i);
//infoBtn.setBackgroundColor(Color.BLUE);
ImageButton cameraBtn = new ImageButton(this);
cameraBtn.setImageResource(R.drawable.ic_menu_camera);
cameraBtn.setAdjustViewBounds(true);
cameraBtn.setScaleType(ImageView.ScaleType.FIT_XY);
cameraBtn.setMaxHeight(120);
cameraBtn.setMaxWidth(120);
cameraBtn.setId(i);
tbl_row.addView(infoBtn);
tbl_row.addView(tv);
tbl_row.addView(cameraBtn);
tbl_layout.addView(tbl_row, i);
}`
for (int i=0; i<num_rows; i++) {
TableRow tbl_row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
tbl_row.setLayoutParams(lp);
tbl_row.setPadding(0, 5, 0, 5);
//tbl_row.setBackground(R.drawable.border);
if(i == 3)
tbl_row.setBackgroundColor(Color.GREEN);
else
tbl_row.setBackgroundColor(Color.parseColor("#ffffd1"));
tbl_row.setGravity(Gravity.CENTER);
TextView tv = new TextView(this);
tv.setGravity(Gravity.CENTER);
tv.setText("Some text goes here\New line.");
ImageButton infoBtn = new ImageButton(this);
infoBtn.setImageResource(R.drawable.ic_menu_info);
infoBtn.setAdjustViewBounds(true);
infoBtn.setScaleType(ImageView.ScaleType.FIT_XY);
infoBtn.setMaxHeight(120);
infoBtn.setMaxWidth(120);
//infoBtn.setId(i);
//infoBtn.setBackgroundColor(Color.BLUE);
infoBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
ImageButton cameraBtn = new ImageButton(this);
cameraBtn.setImageResource(R.drawable.ic_menu_camera);
cameraBtn.setAdjustViewBounds(true);
cameraBtn.setScaleType(ImageView.ScaleType.FIT_XY);
cameraBtn.setMaxHeight(120);
cameraBtn.setMaxWidth(120);
//cameraBtn.setId(i);
cameraBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
tbl_row.addView(infoBtn);
tbl_row.addView(tv);
tbl_row.addView(cameraBtn);
tbl_layout.addView(tbl_row, i);
}`