i want to integrate horizonal scrollview with custom linear layout inflater so i set below code
for (int i=0;i<mArrayListPersonDataLists.size();i++)
{
View child = getLayoutInflater().inflate(R.layout.row_pager_layout, null);
TextView mTextView1=(TextView)child.findViewById(R.id.row_txt_name);
TextView mTextView2=(TextView)child.findViewById(R.id.row_txt_email);
TextView mTextView3=(TextView)child.findViewById(R.id.row_txt_gender);
mLinearLayout.removeAllViews();
mTextView1.setText(mArrayListPersonDataLists.get(i).getName());
mTextView2.setText(mArrayListPersonDataLists.get(i).getEmail());
mTextView3.setText(mArrayListPersonDataLists.get(i).getGender());
mLinearLayout.addView(child);
}
in above my array list include 9 data but in screen it is display only one data so any idea how can i solve this ?
You should write this function like this
mLinearLayout.removeAllViews();
for (int i=0;i<mArrayListPersonDataLists.size();i++)
{
View child = getLayoutInflater().inflate(R.layout.row_pager_layout, null);
TextView mTextView1=(TextView)child.findViewById(R.id.row_txt_name);
TextView mTextView2=(TextView)child.findViewById(R.id.row_txt_email);
TextView mTextView3=(TextView)child.findViewById(R.id.row_txt_gender);
mTextView1.setText(mArrayListPersonDataLists.get(i).getName());
mTextView2.setText(mArrayListPersonDataLists.get(i).getEmail());
mTextView3.setText(mArrayListPersonDataLists.get(i).getGender());
mLinearLayout.addView(child);
}
try this one...
for (int i=0;i<mArrayListPersonDataLists.size();i++)
{
View child = getLayoutInflater().inflate(R.layout.row_pager_layout, null);
TextView mTextView1=(TextView)child.findViewById(R.id.row_txt_name);
TextView mTextView2=(TextView)child.findViewById(R.id.row_txt_email);
TextView mTextView3=(TextView)child.findViewById(R.id.row_txt_gender);
mTextView1.setText(mArrayListPersonDataLists.get(i).getName());
mTextView2.setText(mArrayListPersonDataLists.get(i).getEmail());
mTextView3.setText(mArrayListPersonDataLists.get(i).getGender());
mLinearLayout.addView(child);
child.setId(i+1)
child.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//your code here
int position=child.getId()-1;
Toast.makeText(HorizontalAcivity.this, mArrayListPersonDataLists.get(position).getName(), Toast.LENGTH_SHORT).show();
}
});
}
Remove a line with mLinearLayout.removeAllViews(); :)
Related
I have a LinearLayout ("ll") that is already created in xml and the app dynamically creates another LinearLayout inside of it and creates an EditText and a Button inside of that view. The button makes the whole LinearLayout destroy itself along with the EditText and Button inside it (the whole system is a player name entering activity). Anyway, I am trying to find a way to get the text from all of the EditTexts. I have tried using a for loop on "ll" and using ll.getChildAt() but I can't seem to use .getChildAt() on whatever ll.getChildAt() generates because getChildAt() generates a "View" not a "LinearLayout." I basically just need a way to search two children in, rather than just one. Also, if there is just a better way I should be doing this, let me know. I'm open to suggestions.
Here's my code if it will help:
NewGameCreate.java
public class NewGameCreate extends Activity {
int numOfPlayers = 0;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_game_create);
}
public void newPlayer(View view) {
numOfPlayers++;
final LinearLayout ll = findViewById(R.id.playerView);
final LinearLayout llNew = new LinearLayout(getApplicationContext());
llNew.setOrientation(LinearLayout.HORIZONTAL);
llNew.setId(numOfPlayers);
ll.addView(llNew);
EditText newName = new EditText(this);
newName.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
newName.setHint("Enter Player Name");
newName.setId(numOfPlayers);
newName.setWidth(0);
llNew.addView(newName);
final Button delete = new Button(this);
delete.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0));
delete.setText("Delete");
delete.setId(numOfPlayers);
delete.setWidth(0);
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int id = delete.getId();
ll.removeViewInLayout(findViewById(id));
Drawable back = ll.getBackground();
ll.setBackgroundColor(00000000);
ll.setBackground(back);
ll.invalidate();
}
});
llNew.addView(delete);
}
public void startGame(View view){
LinearLayout ll = findViewById(R.id.playerView);
List text = new ArrayList();
for(int loop = 0; loop < ll.getChildCount(); loop++) {
//this is the code in question and where I want to get the text from
//all my EditTexts
LinearLayout inner = ll.getChildAt(loop);
}
}
}
I think I found the answer to it. You need to change a little bit of code in the startGame() method I m providing the code for startGame below.
public void startGame(View view) {
LinearLayout ll = findViewById(R.id.playerView);
List text = new ArrayList();
for (int loop = 0; loop < ll.getChildCount(); loop++) {
//this is the code in question and where I want to get the text from
//all my EditTexts
LinearLayout inner = (LinearLayout) ll.getChildAt(loop);
for (int j = 0; j < inner.getChildCount(); j++) {
if (inner.getChildAt(j) instanceof EditText) {
EditText textET = (EditText) inner.getChildAt(j);
Log.d("TAG",textET.getText().toString());
}
}
}
}
In the above code you were able to get the first child only but as you have added a linearLayout with orientation Horizontal in a parent LinearLayout with orientation Vertical, you have written code for the child of parent layout i.e playerView. I have modified the code to get the elements of the child Linear layout and Log prints all the text from the EditText.
Hope that helps!!
I am doing bubble display of selected contacts. Each telephone bubble is a LinearLayout, which contains ImageView and TextView. These bubbles are then displayed in another LinearLayout which is child of HorizontalScrollView.
It child/parent tree looks like this:
- HorizontalScrollView
|- LinearLayout (id="#+id/telField")
|- LinearLayout (id="#+id/telBox") <- is programmatically added to parent
|- TextView (id="#+id/telNumber")
|- ImageView (id="#+id/delNumber")
In my .java class I call this method to display "telBox" LinearLayout in "telField" LinearLayout:
public void createAdapter() {
telList = new ArrayAdapter<>(this, R.layout.text_buble, R.id.telNumber, telNumList);
telField = (LinearLayout) findViewById(R.id.telField);
telField.removeAllViews();
final int adapterCount = telNumList.size();
for (ik = 0; ik < adapterCount; ik++) {
final View item = telList.getView(ik, null, null);
telField.addView(item);
item.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
telField.removeView(item);
telNumList.remove(ik-1);
telList.notifyDataSetChanged();
refresh();
}
});
}
}
Method refresh(); – is custom method which helps to "reload" Activity: it gets App values, refreshes warning ImageViews and cals createAdapter() method.
Big button "SELECT" calls an Intent which returns a selected phone number from a contacts book. I call this code to update my LinearLayout with id "telField":
telNumList.add(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)));
createAdapter();
Problem I face is:
After I click on LinearLayout with id "telNumber" it one by one deletes every bubble (no matter which I clicked) until it reaches first added bubble. It also crashes 50/50 when reaches first added element, I have not figured out a dependency. The error it returns is "out of bounds error", so I think it is connected with ik - 1 line.
My question is: How do I better construct my ArrayAdapter?
In your code you are trying to remove the view by ik which is getting change continuously because of which your coding is removing last view I have modified your code as given below
for (ik = 0; ik < adapterCount; ik++) {
final int position=ik;
final View item = telList.getView(ik, null, null);
telField.addView(item);
item.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
telField.removeView(item);
telNumList.remove(position);
telList.notifyDataSetChanged();
//refresh();
createAdapter();
}
});
}
Here position will help you to remove the particular view which you want to remove. I hope this is what you are asking for.
Inside your for loop, write these line of code:
for (ik = 0; ik < adapterCount; ik++) {
final View item = telList.getView(ik, null, null);
item.setTag(ik);
telField.addView(item);
item.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int index = Integer.parseInt(v.getTag().toString());
telNumList.remove(index);
refresh();
}
});
}
Hope it will help you out.
So in my application i have a linear layout, to which i'm adding programmatically some CardViews (android L cardview) like this :
//This is my LinearLayout
LinearLayout myLayout = (LinearLayout) findViewById(R.id.accounts_layout);
//Here i create my CardView from a prepared xml layout and inflate it to the LinearLayout
View card = View.inflate(getApplicationContext(), R.layout.account_card, myLayout);
//Now i change the 'text' value of the Card's text views
TextView cardTitle = (TextView) card.findViewById(R.id.text_card_title);
cardTitle.setText("Title1");
TextView cardDecription = (TextView) card.findViewById(R.id.text_card_description);
cardDecription.setText("Description1");
//...
//Now i do the same thing for another card
View card2 = View.inflate(getApplicationContext(), R.layout.account_card, myLayout);
TextView cardTitle2 = (TextView) card2.findViewById(R.id.text_card_title);
cardTitle2.setText("Title2");
TextView cardDecription2 = (TextView) card2.findViewById(R.id.text_card_description);
cardDecription2.setText("Description2");
//...
The two cards are displayed properly, but what happens is than the first card displayed has "Title2" and "Description2" written in the textViews, while the second card has the default values defined in the xml.
It seems to me that by calling card.findViewById() or card2.findViewById() i get always the TextView of the first card.
So my question is : how do i manage to differentiate the cards i create programmatically and then correclty access the view within them?
Try this way,hope this will help you to solve your problem.
LinearLayout myLayout = (LinearLayout) findViewById(R.id.accounts_layout);
for (int i=1;i<=2;i++){
View card = View.inflate(getApplicationContext(), R.layout.account_card, null);
TextView cardTitle = (TextView) card.findViewById(R.id.text_card_title);
cardTitle.setText("Title"+i);
TextView cardDecription = (TextView) card.findViewById(R.id.text_card_description);
cardDecription.setText("Description"+i);
card.setTag(i);
card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
Toast.makeText(context,pos,Toast.LENGTH_SHORT).show();
}
});
myLayout.addView(card);
}
public void onClick_addContact(View v)
{
LinearLayout layout = (LinearLayout) findViewById(R.id.layoutLinear);
layout.addView(linearlayout(_intMyLineCount));
_intMyLineCount++;
}
private EditText editText(int _intID) {
EditText editText = new EditText(this);
editText.setId(_intID);
editText.setHint("My lines");
editText.setWidth(180);
editTextList.add(editText);
return editText;
}
private TextView textView(int _intID)
{
TextView txtviewAll=new TextView(this);
txtviewAll.setId(_intID);
txtviewAll.setText("My lines:");
textviewList.add(txtviewAll);
return txtviewAll;
}
private Button button(int _intID)
{
Button btn = new Button(this);
btn.setId(_intID);
btn.setTag("but1");
btn.setOnClickListener(newContact);
return btn;
}
OnClickListener newContact = new OnClickListener() {
//onClick view
public void onClick(View v) {
LinearLayout layout = (LinearLayout)v.getParent();
layout.removeViewInLayout(v);
// setContentView(layout);
_intMyLineCount--;
}
};
private LinearLayout linearlayout(int _intID)
{
LinearLayout LLMain=new LinearLayout(this);
LLMain.setId(_intID);
LLMain.addView(textView(_intID));
LLMain.addView(editText(_intID));
LLMain.addView(button(_intID));
LLMain.setOrientation(LinearLayout.HORIZONTAL);
linearlayoutList.add(LLMain);
return LLMain;
}
In the newOnContact Listener, only the child button gets deleted, but not the entire linear layout that includes the textView and EditText.
How to do it?
You can use layout.setVisibility(View.INVISIBLE); or
layout.setVisibility(View.Gone); to remove the layout from UI.
View.INVISIBLE will hide the view, but it'll still take the space whereas View.GONE will remove the view as well as it won't take up any space in the UI.
Refer http://developer.android.com/reference/android/view/View.html#attr_android:visibility
Simply use GONE constant:
layout.setVisibility(LinearLayout.GONE)
then your linear layout will be invisible and it won't allocate space also.
Instead of setting the newContact onClickListener to the Button, try setting to LLMain LinearLayout. So the view that you receive in the onClick is the LinearLayout you want to remove, if I ve not misunderstood you.
Here is my code for how i make adding Dynamically TextView in to Table Layout using LinearLayout for it.
table_personalData.removeAllViews();
int i=0;
for(String header : headerDetail){
TextView label=new TextView(this);
TextView value=new TextView(this);
label.setTextAppearance(this, R.style.TextBase);
value.setTextAppearance(this, R.style.TextBase);
label.setText(String.format("%s:", header));
value.setText(String.format("%s", lead.data[i]==null?"":lead.data[i]));
i++;
LinearLayout rowview=new LinearLayout(this);
rowview.setOrientation(0);
rowview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
rowview.addView(label);
rowview.addView(value);
table_personalData.addView(rowview);
}
By Above code i get my table layout with dynamically added textview in that .
here i use Liner Layout for added two textview in One Row .
headerDetail is a ArrayList<String> headerDetail
lead.data[i] i used this to get my values for particular label.
All this work fine for me now what my problem is
Now i want to do one thing that is when we click one of the TextView of my LinearLayout i want that TextView in dynamically.
So finally my question is about to get retrieve textview value when we touch or click any textview which is in Linearlayout and LinearLayout is in TableLayout.
Anyone have idea what should i have to do with this .
i also try something like below but i don't get i can i get TextView Value from below code.
table_personalData.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for(int i=0;i<table_personalData.getChildCount();i++){
//CharSequence desc=table_personalData.getChildAt(i).getContentDescription();
//Log.v("log_tag", "the values are"+ desc);
}
}
});
EDIT:
if you want the Value of TextView when we Touch on that There is Two way that i write Below :
value.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView txt;
txt=(TextView)findViewById(v.getId());
String tempEmail;
tempEmail=txt.getText().toString().trim();
String aEmailList[]={tempEmail};
pattern=Pattern.compile(EMAIL_PATTERN);
if(validate(tempEmail)){
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,aEmailList );
startActivity(emailIntent);
}else{
pattern=Pattern.compile(PHONE_No_PATTERN);
if(validate(tempEmail)){
tempEmail=tempEmail.replaceAll("[^\\d]", "");
String uri="tel:" +tempEmail;
Intent mIntent=new Intent(Intent.ACTION_CALL);
mIntent.setData(Uri.parse(uri));
startActivity(mIntent);
}else{
//Toast.makeText(LeadDetailActivity.this, "Please Touch on Email or Mo:Number", Toast.LENGTH_SHORT).show();
}
}
}
});
1)Create how many text view u want,but set id for each one,and aslo set same click listener for all
TextView value1=new TextView(this);
value1.setid(1);
value1.setOnClickListener(clickListener);
TextView value2=new TextView(this);
value1.setid(2);
value2.setOnClickListener(clickListener);
TextView value3=new TextView(this);
value3.setid(3);
value3.setOnClickListener(clickListener);
2)second create one onclick listener,inside this listener u will get all cliks. so based on view id u can dynamically do anything
OnClickListener clickListener=new OnClickListener() {
public void onClick(View v) {
int id=v.getId();
}
};