how to create loops in xml file in android - android

I'm new to Android Programming.
I want to implement p: data table tag functionality as of prime faces in android.
I want a table layout in XML through which I can iterate over a list of values from bean along with a checkbox for each row.
Using checkbox listener I want that particular row to be editable and vice versa. and that row should contain text views which allow the user to edit the text and also add row functionality which add an empty row to table to allow a user to enter values and save it.
can anyone suggest how can I proceed?
Thank you.

You have to manage all this in your Java class.
Here is a triggerless example for dynamically added checkboxes.
for(int i = 0; i < 20; i++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText("I'm dynamic!");
ll.addView(cb);
}
Here is a simple example. Simply put the event listener on the desired checkbox and do the processing according to what you want.
public class MainActivity extends AppCompatActivity {
private CheckBox checkBox1;
private TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv_1);
tv1.setText("This is an example");
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(checkBox1.isChecked()){
tv1.setText("Hi i am check");
tv1.setTextColor(Color.RED);
}else {
tv1.setText("Hi i am not checked");
tv1.setTextColor(Color.BLUE);
}
}
});
}
}

Related

Move programatically added TextView back and forth between layouts onClick

I have this FlowLayout where I have a set of TextView's which I build programatically. After getting the wanted names, I create a TextView for each name inside the layout.
What I want to do, if I click on the TextView, I want to move it into another layout. I manage to do that but I also want to move it back. I could also do that until I program it to, but I can't program it to be like a infinite loop.
This is a piece of code which will make you understand better what I'm talking about hopefully.
TextView tv = new TextView(new ContextThemeWrapper(getActivity(), R.style.FlowLayoutTextView));
tv.setText("Test");
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tvSelected = new TextView(new ContextThemeWrapper(getActivity(), R.style.FlowLayoutTextView));
tvSelected.setText(tv.getText().toString());
tvSelected.setLayoutParams(params);
tv.setVisibility(View.GONE);
filteredLayout.addView(tvSelected);
}
});
unfilteredLayout.addView(tv);
Is it possible to make it work? Thanks.
LE: As you can see in the onClickListener event of the TextView, I create the other TextView I add in the other layout, to move it back I could also add an onClickListener event to this TextView but this is not the solution.
Try following:
boolean isInFilterLayout = false; //Class variable
TextView tv = new TextView(new ContextThemeWrapper(getActivity(), R.style.FlowLayoutTextView));
tv.setText("Test");
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isInFilterLayout){
filteredLayout.remove(tv);
unfilteredLayout.addView(tv);
isInFilterLayout = false;
}else{
unfilteredLayout.remove(tv);
filteredLayout.addView(tv);
isInFilterLayout = true;
}
}
});
unfilteredLayout.addView(tv);

How can I get the selected value of the checked checkboxes written programmatically in android?

I have a CheckBox written programmatically in android, it is programmatically written because the value of the CheckBox will be based on online content, so basically, the program has no idea on how many CheckBox will going to print. I use for loops to print checkboxes, and it was successful, but among those checkboxes, I want to get the value of the CheckBox that will going to be selected.
How can I do that?
String ans = _jsonObject.optString("choices").toString();
String[] ansar = ans.split("`");
cb = new CheckBox[ansar.length];
for (int z = 0; z<ansar.length; z++){
CheckBox cb = new CheckBox(survey);
cb.setText(ansar[z]);
choiceslayouts.addView(cb);
}
You need to create a boolean array for saving the state of all checkbox
Then add setOnCheckedChangeListener for each CheckBox
String ans = _jsonObject.optString("choices").toString();
String[] ansar = ans.split("`");
boolean[] array = new boolean[ansar.length];
for (int z = 0; z<ansar.length; z++){
...
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
array[z] = isCheck;
}
});
}
OR
You can set the tag for each CheckBox to check the state of CheckBox
1.Implement onClickListener for your Activity
public YourActivity extends Activity implements View.OnClickListener
2.Then set the tag for each CheckBox
for (int z = 0; z<ansar.length; z++){
...
cb.setTag("tag"+z);
cb.setOnClickListener(this);
}
3.Finnaly, implement onClick
public void onClick(View v) {
switch (v.getTag()) {
case "tag0":
if(((CheckBox) v).isChecked()) {
// your checkbox 0 is checked
}else{
// your checkbox 0 is not checked
}
break;
case "tag1":
// do the same
break;
...
}
}
Hope this help
I think you should use a ListView or a RecyclerView with custom layout and custom adapter, in order to optimize performance. Anyway it should be possible to achieve what you're look for without a major change to your code.
Your problem is that in the for loop you recreate a new checkbox with the exact same properties as the previous one, so even if you add it to your parent view, you are basically adding the same checkbox over and over again, and you can't listen to the event in the "single" checkboxes.
You should try something like this:
String ans = _jsonObject.optString("choices").toString();
String[] ansar = ans.split("`");
final boolean[] cbValue = new boolean[ansar.length];
cb = new CheckBox[ansar.length];
for (int z = 0; z<ansar.length; z++){
cb[z] = new CheckBox(survey);
cb[z].setText(ansar[z]);
cb[z].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cbValue[z] = !cbValue[z];
}
});
choiceslayouts.addView(cb[z]);
}
cbValue[] should hold the values of your checkboxes.
This is without error-checking, but it should give you the idea.

how to get multiple edittext value from child view

I have one main layout with one LinearLayout. In this LinearLayout I inflates xml layouts that contain a form with multiple edittext fields(4 EditText) in it. every time i click on add button xml layouts that contain a form with multiple edittext fields(4 EditText) add in linearlayout.
On first attempt I show only one form. There is button for adding more forms. Suppose If user clicks on "Add" button two times then user have three total forms. I successfully get all these three forms.
i am set Tag child view so easy to get one bye one edittext value
but my problem is when i click on save button all edittext data
get and add in array ..
Button buttonAdd = (Button) findViewById(R.id.pdBtnAddDoc);
buttonAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myRoot = (LinearLayout) findViewById(R.id.linearLayoutAdd);
LayoutInflater inflater = (LayoutInflater)PrimaryDoctorFragment.this.getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
childView = inflater.inflate(R.layout.fragment_primary_doc_add,myRoot);
// childView.setId(tag++);
childView.setTag(tag++);
}
});
here is my save button:-
btnSave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for (int i = 0; i < tag; i++) {
System.out.println("i:- " + i);
View aView = myRoot.findViewById(i);
EditText name = (EditText) aView
.findViewById(R.id.childEtName);
Log.d("Gettext :- ", name.getText().toString());
}
}
});
on save button all four text field data get and add in array..

How to change a sting Variable name dynamically and programatically, this is challenging

I have MainText1 displays a text, when you click on button 3 in the Menu MainText1 displays text3 which is coming from the super class Text. What I want is that dynamically when you click on any button it reads the number and displayed the respective text, that's all about. ;)
I want to get rid of switch case in my activity, so I'm trying now for 2days :( to change the name of the string variable dynamically, but I think I'm using a wrong code as string variable is different from resources (confused), here's my code, this really challenging me this weekend:
public class MainText1 extends Text {
String
tx1=text1,tx2=text2,tx3=text3,
tx,stringReceived;//text1,text2...textn strings coming from the Super class Text
num = Integer.parseInt(getIntent().getStringExtra("somekey1")); // this data is coming from the menu, it depends on which button is clicked
tx="text"+num; // text is the name of the string variable, it should be in format like that : text1,text2,...textn which have predefined string content
stringId = getApplicationContext().getResources().getIdentifier(tx, "string", getPackageName());
if (stringId > 0) {
stringReceived=getApplicationContext().getResources().getString(stringId);
I guess what you're trying to do is to change a TextView contents. So, you could do the following:
public YourActivity extends Activity {
//Here you will declare your layout items
private Button button1;
private Button button2;
private Button button3;
private TextView txtView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//Here you will get you layout elements
button1 = (Button) findViewById(R.id.button1_id);
button2 = (Button) findViewById(R.id.button2_id);
button3 = (Button) findViewById(R.id.button3_id);
txtView = (TextView) findViewById(R.id.txtview_id);
//Now you will have to set the onClickListeners
button1.addOnCLickListener(new OnClickListener() {
#Override
private onClick() {
//Set the text in the text view to the string related
//to button1
txtView.setText(getResources()
.getString(R.string.button1_string);)
}
});
button2.addOnCLickListener(new OnClickListener() {
#Override
private onClick() {
//Set the text in the text view to the string related
//to button2
txtView.setText(getResources()
.getString(R.string.button2_string);)
}
});
button3.addOnCLickListener(new OnClickListener() {
#Override
private onClick() {
//Set the text in the text view to the string related
//to button3
txtView.setText(getResources()
.getString(R.string.button3_string);)
}
});
}
}
This should do the trick. Although, like the other guys suggested it, you should take a look at some tutorials before coding on

How to change TextView multiple times on multiple Button clicks?

I'm kind of new when it comes to Android Application development and I'm developing an app at the moment. I'm trying to get my TextView change every time the user clicks the Button(NEXT) and when another Button (PREVIOUS) gets clicked on I want it to change back to the original TextView. So basically I'd like to set up a certain amount of TextViews and be able to browse through them with the two Buttons I mentioned.
So far I only know how to make the the TextView change one time on a Button(NEXT) click. I'm using this piece of code for that:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton Next = (ImageButton) findViewById(R.id.Next);
Next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView Text1= (TextView) findViewById(R.id.Text1);
Text1.setText("New Text");
}
});
NOTE: The Button "PREVIOUS" isn't included yet because I didn't know what to do with it yet.
I'm getting the feeling this code is only used when you want the TextView to change one time and you need a whole different method to make it change multiple times.
I hope I provided you with enough information and you are willing to help me out here.
Thanks in advance!
public class MyActivity extends Activity implements View.OnClickListener {
int stringIdList[] = {R.string.text1, R.string.text2, R.string.text3, R.string.text4}
int stringListCounter = 0;
TextView text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton next = (ImageButton) findViewById(R.id.Next);
ImageButton previous = (ImageButton) findViewById(R.id.Previous);
text1 = (TextView) findViewById(R.id.Text1);
Next.setOnClickListener(this);
previous.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int id = v.getId();
if(id == R.id.Next && stringListCounter < stringIdList.length - 1) {
stringListCounter++;
} else if (id == R.id.Previous && counter > 0) {
stringListCounter--;
}
Text1.setText(stringIdList[stringListCounter]);
}
What this does is assigns your Activity to an OnClickListener to handle the click events. If Next was pressed and the counter is within the range of the array list, it will increase the counter. The same for previous. At the end of the click, it will set the text to whatever the ID is. This assumes your strings are in a strings.xml file which is recommended in the Android spec and is static.
I think you can store history as List and have all states of textview in each moment.
Only thing that you have to do is to take previous value from this history stack after pressing previous button.

Categories

Resources