Help

Quickly share your Android Code Snippets here...

Help

Postby ramesh_bh » Mon Oct 11, 2010 6:38 am

HI,

My project requirement is like i have to display multiple rows, and each row should contain three columns name, email, address.
If i click on any values i have to implement onclickListener also,
Can anyone give example to do this type of requirements in LinearLayout or TableLayout.
Please Help :(
ramesh_bh
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Oct 07, 2010 4:58 am

Top

Re: Help

Postby ramesh_bh » Mon Oct 11, 2010 7:51 am

Okay,

Here i am giving my input :
Here is my code :
LinearLayout lLayout1= null;
LinearLayout main_lLayout= null;
LinearLayout lLayout2= null;

LinearLayout myLayout[] = new LinearLayout[2];
@Override
public void onCreate(Bundle icici) {
super.onCreate(icici);

main_lLayout = new LinearLayout(this);

main_lLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
main_lLayout.setOrientation(LinearLayout.VERTICAL);

for(int i=0;i<2;i++) {
TextView t1 = new TextView(this);
t1.setText("Ramesh");
t1.setPadding(0, 0, 20, 10);
TextView t2 = new TextView(this);
t2.setText("Kumar");
t2.setPadding(0, 0, 20, 10);
myLayout[i] = new LinearLayout(this);
myLayout[i].setOrientation(LinearLayout.HORIZONTAL);
myLayout[i].addView(t1);
myLayout[i].addView(t2);
myLayout[i].setClickable(true);
myLayout[i].setOnClickListener(this);
main_lLayout.addView(myLayout[i]);
}

setContentView(main_lLayout);
}

@Override
public void onClick(View v) {
for(int i=0;i<2;i++) {
if(myLayout[i].isPressed()) {
System.out.println("Count "+myLayout[i].getChildCount());
System.out.println("Clicked."+((TextView)myLayout[i].getChildAt(0)).getText());
}
}
}
I got what row i am clicking , but
For every row i have two columns, how to know what column i have clicked, Is this approach is correct ?
Please help
ramesh_bh
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Oct 07, 2010 4:58 am

Re: Help

Postby aspic » Tue Oct 12, 2010 9:54 am

For this I would first defined a gridlayout in the .xml-file for your activity. Then I would've requested that layouts ID in the code and added rows/columns dynamically. See http://evan.tiggerpalace.com/articles/2 ... n-android/ for some ideas.

I have some code snippets which kind of does what you want. Tell me if you want to have it :)
aspic
Junior Developer
Junior Developer
 
Posts: 20
Joined: Sun Oct 10, 2010 1:36 am

Re: Help

Postby ramesh_bh » Wed Oct 13, 2010 6:23 am

HI aspic,
Thanks for the reply, yes gimme some code of event listeners so that i can forward with my code.
Thanks again :)
ramesh_bh
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Oct 07, 2010 4:58 am

Re: Help

Postby aspic » Thu Oct 14, 2010 9:55 am

ramesh_bh wrote:HI aspic,
Thanks for the reply, yes gimme some code of event listeners so that i can forward with my code.
Thanks again :)

Well, first of all the main reason why you can't detect clicks on your added views is because they don't have IDs stored in the generated R.xml-file.

My way around this is pretty straight forward, and should perhaps have been done very different, but the main idea is to manually add an id to the views (myView.setId(someInt)), and store this id in an array. Then, when the OnClickListeners gets an event (an ID has been clicked) one just loops through the array, and breaks at the correct id.

I believe this method is very mean and should not be used, therefore I got second thoughts when I reviewed my code (it is pretty old). If you still want to use this method I can go about and paste some code :)
aspic
Junior Developer
Junior Developer
 
Posts: 20
Joined: Sun Oct 10, 2010 1:36 am

Re: Help

Postby ramesh_bh » Tue Oct 19, 2010 5:19 am

Thanks for the Reply ... :)
Yes Can you please post some relate code so that i can go further...
ramesh_bh
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Oct 07, 2010 4:58 am

Top

Re: Help

Postby ramesh_bh » Tue Oct 19, 2010 9:59 am

HI, i have implemented like this,
But i got same results for all the text view clicks (i didn't got the value what i click), can you tell me where is the mistake ?


LinearLayout lLayout1= null;
LinearLayout main_lLayout= null;
LinearLayout lLayout2= null;
TextView myText[] = new TextView[2];

LinearLayout myLayout[] = new LinearLayout[2];
@Override
public void onCreate(Bundle icici) {
super.onCreate(icici);

main_lLayout = new LinearLayout(this);

main_lLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
main_lLayout.setOrientation(LinearLayout.VERTICAL);

for(int i=0;i<2;i++) {
myLayout[i] = new LinearLayout(this);

for(int j=0;j<2;j++) {
myText[j] = new TextView(this);
myText[j].setText("asdf"+i+j);
myText[j].setPadding(0, 0, 20, 10);
myText[j].setClickable(true);
myText[j].setId(j);
myText[j].setOnClickListener(this);
myLayout[i].addView(myText[j]);

}
myLayout[i].setClickable(true);
myLayout[i].setId(i);
myLayout[i].setOnClickListener(this);
main_lLayout.addView(myLayout[i]);
}
setContentView(main_lLayout);
}

@Override
public void onClick(View v) {

System.out.println("Values ==="+((TextView)myLayout[v.getId()].getChildAt(v.getId())).getText());
System.out.println("::::::"+myText[v.getId()].getText());
}
ramesh_bh
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Oct 07, 2010 4:58 am

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 1 guest