Find array position using Button onclick in android - android

my array
String array1[] = menuname.toArray(new String[menuname.size()]);
array2 = menuid.toArray(new String[menuid.size()]);
my dynamic button coding
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.l);
List<Button> list = new ArrayList<Button>();
for (int i = 1; i < array1.length; i++) {
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.HORIZONTAL);
button.setText(String.valueOf(i));
button.setText(" " + array1[i]);
button.setId(i);
button.setWidth(180);
button.setHeight(60);
button.setTextColor(Color.BLUE);
button.setTypeface(Typeface.SERIF, Typeface.BOLD);
l.addView(button);
linearLayout.addView(l);// if you want you can layout params
// linearlayout
list.add(button);
}
array1[] contains the names of the button...and array2[] contains id of the button ....i want to set id and name for the dynamic button..how to do this?

You can set id for button in your code using
button.setId(array2[i]);

Maybe use button.setText(array2[i] + " " + array1[i]);? Is it something else you want?

You can set id to your button at runtime and set onClickListener on that, you will get that id when the button is clicked.
if(nameArray.length()==idArray.length())
{
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.menuLayout);
List<Button> buttonList = new ArrayList<Button>();
for (int i = 1; i < nameArray.length; i++) {
btnMenu.setText(String.valueOf(i));
btnMenu.setText(" " + nameArray[i]);
btnMenu.setId(idArray[i]);
btnMenu.setWidth(180);
btnMenu.setHeight(60);
btnMenu.setTextColor(Color.BLUE);
btnMenu.setTypeface(Typeface.SERIF, Typeface.BOLD);
btnMenu.setOnClickListener(this);
linearLayout.addView(btnMenu);
list.add(button);
}
}
public void onClick(View view) {
super.onClick(view);
switch (view.getId()) {
case idArray[0]:
// button with id "idArray[0]" is clicked
break;
}
}

Related

How to get values from dynamic edittext and radio group?

I have created the dynamic view. That view contains two edittext and one radio group. when I click the add button the view is added to the layout. Now I got a confusion, how to get values from these type of dynamic views. I tried but it doesn't work. when I add the two or more views, the loop does not find the next views values. I want to add that values to ArrayList. This is code:
private void addDynamicViews() {
EditText name = new EditText(this);
EditText mobile = new EditText(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
p.setMargins(10, 10, 5, 5);
name.setLayoutParams(p);
name.setBackgroundResource(R.drawable.edittext_box);
name.setHint("Enter Name");
studentslayout.addView(name);
mobile.setLayoutParams(p);
mobile.setBackgroundResource(R.drawable.edittext_box);
mobile.setHint("Enter Mobile No");
studentslayout.addView(mobile);
/* radioGroup - Radio Group
maleButton,femaleButton - Radio Buttons
studentslayout - Linear Layout */
radioGroup = new RadioGroup(this);
radioGroup.setOrientation(RadioGroup.VERTICAL);
maleButton = new RadioButton(this);
maleButton.setText("Male");
radioGroup.addView(maleButton);
femaleButton = new RadioButton(this);
radioGroup.addView(femaleButton);
femaleButton.setText("Female");
studentslayout.addView(radioGroup);
}
How to take all dynamic edittext and radio group values ?
I tried this code But unfortunately it stopped.
#Override
public void onClick(View v) {
String[] array = new String[studentslayout.getChildCount()];
int count = studentslayout.getChildCount();
for (int i=0; i < studentslayout.getChildCount(); i++){
editText = (EditText)studentslayout.getChildAt(i);
array[i] = editText.getText().toString();
RadioButton radValues = (RadioButton) studentslayout.getChildAt(i);
array[i] = radValues.getText().toString();
}
}
RadioButton radValues = (RadioButton) studentslayout.getChildAt(i);
You have added radioGroup and are expecting radiobutton. Also since you are looping, you should check the type of the view.
You can try something like this:
int childCount = studentslayout.getChildCount();
for (int i = 0; i < childCount; i++) {
View childView = studentslayout.getChildAt(i);
if (childView instanceof EditText) {
EditText editText = (EditText) childView;
String text = editText.getText().toString();
//use text
} else if (childView instanceof RadioGroup) {
RadioGroup radioGroup = (RadioGroup) childView;
int radioCount = radioGroup.getChildCount();
for (int j = 0; j < radioCount; j++) {
RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
//use radioButton.
}
}
}

How to add buttons dynamically?

I want to add buttons dynamically. I am adding multiple buttons dynamically but I want to add the buttons in following pattern:
[BUTTON1] [BUTTON2]
[BUTTON3] [BUTTON4]
[BUTTON5] [BUTTON6]
That means I want to add only 2 buttons in a row that is dynamically.
I have tried many options.
one of them is:
LinearLayout ll = (LinearLayout) findViewById(R.id.buttonlayout);
Button[][] buttonArray = new Button[count][count];
TableLayout table = new TableLayout(this);
for (int row = 0; row < count; row++) {
TableRow currentRow = new TableRow(this);
for (int button = 0; button < row; button++) {
Button currentButton = new Button(this);
// you could initialize them here
currentButton.setOnClickListener(this);
// you can store them
buttonArray[row][button] = currentButton;
// and you have to add them to the TableRow
currentRow.addView(currentButton);
}
// a new row has been constructed -> add to table
table.addView(currentRow);
}
and finally takes that new table and add it to your layout. ll.addView(table);
Note: count of button could be random.
How can I do that?
Use vertical LinearLayout in XML. Then programmatically create horizontal LinearLayout and add buttons in horizontal layout. For each line, create and add new horizontal layout.
XML:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/buttonlayout">
</LinearLayout>
ACTIVITY:
public class dynamicButtons extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int numberOfRows = 3;
int numberOfButtonsPerRow = 2;
int buttonIdNumber = 0;
final LinearLayout verticalLayout= LinearLayout)findViewById(R.id.buttonlayout);
for(int i=0;i<numberOfRows;i++){
LinearLayout newLine = new LinearLayout(this);
newLine.setLayoutParams(params);
newLine.setOrientation(LinearLayout.HORIZONTAL);
for(int j=0;j<numberOfButtonsPerRow;j++){
Button button=new Button(this);
// You can set button parameters here:
button.setWidth(20);
button.setId(buttonIdNumber);
button.setLayoutParams(params);
button.setText("Button Name");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent is = new Intent(getApplicationContext(), someOtherApplication.class);
is.putExtra("buttonVariable", buttonIdNumber);
startActivity(is);
}
});
newLine.addView(button);
buttonIdNumber++;
}
verticalLayout.addView(newLine);
}
}
}
try this code:
TableLayout layout = new TableLayout (this);
layout.setLayoutParams( new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
for (int i=0; i<2; i++) { //number of rows
TableRow tr = new TableRow(this);
for (int j=0; j<2; j++) { //number of columns
Button b = new Button (this);
b.setText("Button:"+i+j);
b.setTextSize(10.0f);
b.setOnClickListener(this);
tr.addView(b, 30,30);
}
layout.addView(tr);
}
As your number of button is random u can use:
int total = 20; //random number of buttons
int column = 3; //specify the column number
int row = total / column;
now use the column and row value to dynamically show buttons
Do something like this:
LinearLayout ll_Main = new LinearLayout(getActivity());
LinearLayout ll_Row01 = new LinearLayout(getActivity());
LinearLayout ll_Row02 = new LinearLayout(getActivity());
ll_Main.setOrientation(LinearLayout.VERTICAL);
ll_Row01.setOrientation(LinearLayout.HORIZONTAL);
ll_Row02.setOrientation(LinearLayout.HORIZONTAL);
final Button button01 = new Button(getActivity());
final Button button02 = new Button(getActivity());
final Button button03 = new Button(getActivity());
final Button button04 = new Button(getActivity());
ll_Row01.addView(button01);
ll_Row01.addView(button02);
ll_Row02.addView(button03);
ll_Row02.addView(button04);
ll_Main.addView(ll_Row01);
ll_Main.addView(ll_Row02);
button04.setVisibility(View.INVISIBLE);
button04.setVisibility(View.VISIBLE);
Create a listview/recyclerview with custom item that hold 2 button as you mentioned in your question, than populate that listView with the buttons (inside the adapter if item index % 2 == 0 it will take the left position otherwise the right position).

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("")){
}

How can we create dynamic textview?

how to create the textview in code not in xml file. It is because number of textviews will be changing in my application according to some integer.
This is the code to create TextView Dynamically
LinearLayout layout = (LinearLayout ) findViewById(R.id.llayout);
for (int i = 0; i < 3; i++) {
TextView dynamicTextView = new TextView(this);
dynamicTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
dynamicTextView.setText("NewYork");
layout.addView(tstate);
}
Maybe thats what you need:
LinearLayout lin = (LinearLayout) findViewById(R.id.myLinear);
for (int i = 0; i <= 10 ; i++)
{
TextView myText = new TextView(this);
myText.setText("textview# "+ i);
lin.addView(myText);
}
Something like the following should be what you need:
final int N = 10; // total number of textviews to add
final TextView[] myTextViews = new TextView[N]; // create an empty array;
for (int i = 0; i < N; i++) {
// create a new textview
final TextView rowTextView = new TextView(this);
// set some properties of rowTextView or something
rowTextView.setText("This is TextView #" + i);
// add the textview to the linearlayout
myLinearLayout.addView(rowTextView);
// save a reference to the textview for later
myTextViews[i] = rowTextView;
}
private LinearLayout ll;
private TextView tv;
// in oncreate()
onCreate()
{
int WrapWidth = LinearLayout.LayoutParams.WRAP_CONTENT;
int WrapHeight = LinearLayout.LayoutParams.WRAP_CONTENT;
tv = new TextView(this);
ll.addView(tv,WrapWidth,WrapHeight);
}
Code is here
final int c = 12;
final TextView[] mtext = new TextView[c];
for (int i = 0; i < c; i++) {
TextView rowtxt = new TextView(this);
rowtxt.setText("Hello" + i);
myLinearLayout.addView(rowtxt);
myTextViews[i] = rowtxt;
myTextViews[i].setOnClickListener(onclicklistener);//textview click
}
OnClickListeners code is here
OnClickListener onclicklistener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == myTextViews[0]){
//do whatever you want....
}
}
};
Hope it is helpful for you
You use TextView textView = new TextView(CurrentActivity.this);
and then you add setter arguments that come with the TextView class

Dynamically creating ImageButtons and adding them to layout with OnClickListeners

I'm trying to dynamically and programmatically create ImageButtons and add them to my Scrolling LinearLayout. I've been able to add them, but when I try to add onClickListeners to them, all their view ID's are -1; hence not being able to find out which button was clicked.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OnClickListener imageClickListener;
imageClickListener = new OnClickListener(){
#Override
public void onClick(View v) {
System.out.println("id clicked: " + v.getId());
}
};
for (int i = 0; i<images.length; i++)
{
LinearLayout il = new LinearLayout(this);
il.setOrientation(LinearLayout.HORIZONTAL);
il.setMinimumHeight(LayoutParams.WRAP_CONTENT);
il.setMinimumWidth(LayoutParams.WRAP_CONTENT);
int imageid = 0;
ImageButton ib;
BitmapDrawable imagebd;
imageid = getResources().getIdentifier("drawable/" + images[i], null, getPackageName());
imagebd = resizeImage(imageid);
ib = new ImageButton(this);
ib.setClickable(true);
ib.setOnClickListener(imageClickListener);
ib.setImageDrawable(imagebd);
ib.setMinimumHeight(size);
ib.setMinimumWidth(size);
ib.setMaxHeight(size);
ib.setMaxWidth(size);
imageButtons.add(ib);
il.addView(ib);
System.out.println("id: " + ib.getId());
ll.addView(il);
}
this.setContentView(sv);
}
Why not call ib.setId(i) ?
If you want an id, you have to define it when you create the image button!
ImageButton ib = new ImageButton(this);
ib.setId(i);
Try this

Categories

Resources