Dynamically loading the layout - android

In my app, i am loading a layout dynamically with text views ,on a button's onclick event. when i click the button for the first time , i got my layout with text views. when i click it again ,it should display the layout again. but its showing error. my code is
private OnClickListener some_name = new OnClickListener(){
public void onClick(View v) {
LinearLayout my_list_layout = (LinearLayout)findViewById(R.id.my_list_layout);
my_list_layout.setOrientation(1);
my_list_layout.setId(50);
my_textview = new TextView[length];
for(int i=0; i<length ; i++)
{
my_textview[i]= new TextView(getApplicationContext());
my_textview[i].setText("sample text");
my_textview[i].setId(i);
if(i==0)
{
RelativeLayout.LayoutParams my_textviewparams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
my_textviewparams.addRule(RelativeLayout.ALIGN_PARENT_TOP,my_list_layout.getId());
my_list_layout.addView(my_textview[i],my_textviewparams);
}
else
{
RelativeLayout.LayoutParams my_textviewparams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
my_textviewparams.addRule(RelativeLayout.BELOW,my_textview[i-1].getId());
my_list_layout.addView(my_textview[i],my_textviewparams);
}
}
}
and my error log is
quick response is needed. anyone tell me, what is my wrong in coding, or what changes i have to do for my requirement?

You should remove this line:
my_list_layout.setId(50);
because once you set the id to 50, and again when you click the Button, the LinearLayout is null because you call findViewById(R.id. my_list_layout); its id has been changed to 50.
Cheers!

I got the answer.Here while i was doing first time the btn click, the child view will added to my parent view dynamically.
During the second click the i removed previously added child from my parent view and added the new child. So this will updates my content and displays clearly every time when I click the btn.
I got error in removing the views from my child, now I clear the error by assigning some variables for identifying btn click, whether user clicks it for first time or not. thank you all for your time here and for your answers.

Related

Wrong variable value in onClickListener

I am duplicating my linear layout dynamically and I have to set onClickListeners for buttons inside the linear layout.
for(int i = 0; i <10 ; i++){
// other code here
Button approve_btn = (Button) findViewById(R.id.rent_number_up_btn);
approve_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
approve_btn.setText(String.valueOf(i));
}
});
}
Everything works fine except that my button's text is always set to 9. I think that's because when the listener is called the value of i is 9 at that time. What I want the value of i at the time the button's listener is set and I am not sure how to do that.
How can I solve this problem? Any help is appreciated.
The issue is that you are setting click listener to the same button (by calling findViewById()) 10 times in a row. You get the value 9 because thats the last click listener which you added to the button.
for(int i = 0; i <10 ; i++){
// other code here
Button button = new Button(<Activity Instance>);
button.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
approve_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
approve_btn.setText(String.valueOf(i));
}
});
}
In above code you need to add those buttons in your linearlayout.
Hope this will help you,
Thanks
I am not sure what you want to do but:
Like #Shaishav said your are using same button (R.id.rent_number_up_btn) and your are replacing the click listeners on top of each other. The last value (of the your counter "i") before your loop finish is 9 , that's why it show 9 all the time. If you want to add 10 buttons inside your Linear layout just create new Button(context) every time when your loop starts and add this button to your layout via
yourLinearlayout.addView(yourNewButton);
Then if you set click listener to your new button maybe it will show different values :)

disable button click for certain rows of buttons

I dynamically create Buttons by entering a word. If I write "met", it appears on the screen - one Button per letter. The same thing happens for the next word I enter, and it appears below the previous word --- as shown in the image above.
When I click on a Button it turns green. My question is, what is the best way to disable the clicking of a row of Buttons. Meaning, if the user clicks on the 'm' in "met" I want the user to only be able to click on the Buttons in "met" and to not be able to click on any of the Buttons in "had", "goes", or "ran"
Here is my code:
EDIT
int size = enter_txt.getText().toString().length();
for (int i=0; i<size; i++){
final Button dynamicButtons = new Button(view.getContext());
dynamicButtons.setLayoutParams(rlp);
dynamicButtons.getLayoutParams().width = 130;
dynamicButtons.getLayoutParams().height = 130;
dynamicButtons.setTag("0");
dynamicButtons.setId(1);
dynamicButtons.setText(edit_text_array[i]);
dynamicButtons.setBackgroundResource(R.drawable.button);
button_list.add(dynamicButtons);
linearLayout2.addView(dynamicButtons, rlp);
dynamicButtons.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
int i=0;
LinearLayout ll = (LinearLayout) dynamicButtons.getParent();
for(i=0; i<list_of_ll.size();i++){
if (ll == list_of_ll.get(i)){
list_of_ll.get(i).setId(i);
break;
}
}
if(list_of_ll.get(i).getId()==i)
ButtonOnClick(view);
}
});
}
linearLayout2.setId(0);
linearLayout2.setTag("0");
list_of_ll.add(linearLayout2);
EDIT
I created a List of the LinearLayouts for each row of Buttons. The Buttons turn green if the id of the LinearLayout is set to 1. When I click on a Button I want that LinearLayout to stay at 1 and have all other rows/LinearLayouts set to 0 so they become unclickable.
Currently, every Button I click turns green even if it's in a different row. Can someone please help me solve this issue?
Why you don't set Id in the for loop so that you are able to refer and set the onlicklistener to null like jpcrow already mentioned.
Set Id like:
YourCreatedBtn.setId(i+1);
//Id's setted programmatically don't.
have to be unique... But they should be
a positive number (referring to the
android documentation)
And in your on click method simply set onclicklistener for specified Id's to null. Just a hint, hope it helps
Update regarding Thread-openers Comment
I found two simple ways but i would prefer the one which is not commented out in the buttonIsClicked:
LinearLayout llrow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llrow = (LinearLayout) findViewById(R.id.test_layout);
//Adding 5 Buttons
for(int i = 0; i<5; i++) {
Button mybtn = new Button(this);
//set LayoutParams here
mybtn.setId(5);
mybtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonIsClicked(v);
}
});
llrow.addView(mybtn);
}
}
private void buttonIsClicked(View v) {
/*ArrayList<View> myButtons = llrow.getTouchables();
for(int i = 0; i < llrow.getChildCount(); i++){
myButtons.get(i).setOnClickListener(null);
}*/
for(int i = 0; i<llrow.getChildCount(); i++){
llrow.getChildAt(i).setOnClickListener(null);
}
}
It's just a simplified Version of your code, but i'm sure you will get the Content..
What if found out is, that you don't have to set the ID in both cases.. You can easily get all the child over
YourRowLinearLayout.getChildAt(starting from 0 to n-1-Views you added)...
I didn't found a way around the for-loop... But this small-little loop will not break your neck regarding to Performance..
The outcommented-code is the second Approach, finding all the Child over getTouchables which logically leads to an ArrayList and that's exactly the reason why i don't like it. You have to initialize an arraylist...... However, this also won't break your neck regarding to Performance but a penny saved is a penny got! ;) Hope it helps and everything is clear. Both of them work! Please mark as accepted answere if it fits your Needs...
You have to distinguish between the two rows, either add them to different ViewGroups or you can use View.setTag(int key, Object tag)

Android: Dynamically create image button with click event

I'm new to android and I require to have a list of image buttons in an activity which are created based on the data in a database. I haven't created anything like this in android before and so far I've been using HTML tables to show my data in a grid view. I'm not asking for any special code, I'm just clueless on how to implement this. I wanna know what the best approach is.
One problem I've faced is with the click events(in the way I've been doing them so far) which do not take in any EventArg, so I can't get the name of the button out of them.
If you're sure that the OnClickListener instance is applied to a Button, then you could just cast the received view to a Button and get the text:
public void onClick(View v) {
// 1) Possibly check for instance of first
Button b = (Button)v;
String buttonText = b.getText().toString();
}
// create the layout params that will be used to define how your button will be displayed
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// Create Button
final Button btn = new Button(this);
// Give button an ID
btn.setId(someinteger);
btn.setText("Add Text");
// set the layoutParams on the button
btn.setLayoutParams(params);

How can I remove / delete a dynamically generated button in Android?

So I generated some button. The numbers it depends on the user (when clicked a button, create a new one).
This is how I made it:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layoutcprompt);
RelativeLayout.LayoutParams OBJ = new RelativeLayout.LayoutParams (140,80);
if ((commandsi%6)==0) {adjust=adjust+86; commandsi=1;}
OBJ.leftMargin =(140*(commandsi-1))+10;
OBJ.topMargin =250+adjust;
Button command = new Button(this);
command.setLayoutParams(OBJ);
command.setId(ID);
command.setText(edittxt.getText().toString());
edittxt.setText("");
command.setBackgroundResource(R.drawable.costum_button);
command.setTextColor(Color.WHITE);
command.setTextSize(14);
layout.addView(command);
command.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Button b = (Button)view;
scommand=b.getText().toString();
}
});
command.setVisibility(View.VISIBLE);
I want to remove/delete them, but I don't know how.... I gave them a unique id, but I still dunno how can I remove them :/
I cannot comment another post, but using
command = new Button(this)
might involve an implicit memory Leak on this! (which can be the Activity). Rather use Context object. Or remove the button at least.
Then because you have the parent of your Button. Just remove it:
ViewGroup layout = (ViewGroup) findViewById(R.id.layoutcprompt);
View command = layout.findViewById(ID);
layout.removeView(command);
Make command a global variable. Then you can access it later, and call command.setVisibility(View.GONE);
So at the top, of your class file, you would declare the global variable:
Button command;
Then remove the redefinition later on and instead assign to the global variable:
command = new Button(this);
Then when you want to hide it, call:
command.setVisibility(View.GONE);
Try using the documentation, 5 seconds of research can lead you to the RemoveView method.
layout.removeView(command);
Update
If you have a null pointer exception on this line, means your layout is null, not your command. Make your layout variable global for that class.
Also be sure to keep different variables for each of your created buttons. If you have a global variable, and create 10 buttons using the same variable you will only have a reference to the last one created. If you explain exactly when you want to remove the button we might be able to help you further.
As an example, if you want to remove the button when the user clicks on it you can change your clickListener:
command.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Button b = (Button)view;
scommand=b.getText().toString();
layout.removeView(view);
}
});
One other implementation that may help others who view this thread is that you can consider removing all the child elements in the parent layout. Once you get the view's parent (which I assume is a layout container),you can remove all the child elements.
command.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ViewGroup vg = (ViewGroup)view.getParent();
vg.removeAllViews();
}
});
You can also do it like this for security purpose:
ViewGroup layout = (ViewGroup) command.getParent();
if(null!=layout) //for safety only as you are doing onClick
layout.removeView(command);
You can also use this snipper
For Adding the Button
LinearLayout dynamicview = (LinearLayout)findViewById(R.id.buttonlayout);
LinearLayout.LayoutParams lprams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setId(count);
final int id_ = btn.getId();
btn.setText("Capture Image" + id_);
btn.setTextColor(Color.WHITE);
btn.setBackgroundColor(Color.rgb(70, 80, 90));
dynamicview.addView(btn, lprams);
btn = ((Button) findViewById(id_));
btn.setOnClickListener(this);
For removing the button
ViewGroup layout = (ViewGroup) findViewById(R.id.buttonlayout);
View command = layout.findViewById(count);
layout.removeView(command);

Clear previous View and show new View on button click in the same Activity

I have created an activity with two buttons at the top. One button to show "SMS Logs" and second to show "Call Logs".
On clicking "SMS Logs" button, i am dynamically creating textviews and linear layout to show sms logs.
On Clicking "Call Logs", i am dynamically creating another textviews and linear layout to show call logs.
But the problem is that, once if we click "sms log" button and then we click "call log" button, the previously created linear layouts are not removed and the both(previous layouts and the current layouts) are shown simultaneously.
But i want that the previous layouts should be removed on clicking the second button.
Which function, should i use to remove the previous viewgroups or the layouts. Tell me if you need to read my class file.
Edit:
This is my Activity's code,
public class General extends Activity
{
String phone, message;
TextView Logs;
View layout, callLayout;
TextView data, callData, line, callLine;
Button smsLog, callLog;
LinearLayout ll, callll;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.general_main);
Logs = (TextView)findViewById(R.id.Logs);
layout = findViewById(R.id.layout);
callLayout = findViewById(R.id.layout);
smsLog = (Button)findViewById(R.id.smsLogs);
callLog = (Button)findViewById(R.id.callLogs);
smsLog.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
callLayout.setVisibility(View.GONE);
ll = new LinearLayout(getApplicationContext());
ll.setOrientation(LinearLayout.VERTICAL);
data = new TextView(getApplicationContext());
data.setText("First Line");
data.setTextColor(Color.YELLOW);
line = new TextView(getApplicationContext());
line.setText("Second Line");
((ViewGroup) ll).addView(data);
((ViewGroup) layout).addView(line);
((ViewGroup) layout).addView(ll);
}
});
callLog.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
layout.setVisibility(View.GONE);
callll = new LinearLayout(getApplicationContext());
callll.setOrientation(LinearLayout.VERTICAL);
callData = new TextView(getApplicationContext());
callLine = new TextView(getApplicationContext());
callData.setText("Third Line");
callLine.setText("Fourth Line");
((ViewGroup) callll).addView(callData);
((ViewGroup) callLayout).addView(callLine);
((ViewGroup) callLayout).addView(callll);
}
});
}
}
I have removed the extra code and made it simple to understand.
You can use FrameLayout to solve your problem. But I recommend you to use tabview.Here is the link that demonstrates how to develop tabbed applications.Good Luck
You could implement a TabView.
But having your current setup just change the visibility of one view group to GONE and the other to VISIBLE.
GONE will make the view invisible and it won't take up any space anymore.
EDIT based on the code added to the question
Both your layout and callLayout are using the same XML view. Implement 2 identical views in your xml and keep one visible and one gone. This way when you set layout or callLayout visibility to GONE they are 2 different ones not the same. So your onClick() will have something like this:
for smsLog:
layout.setVisibility(View.GONE);
callLayout.setVisibility(View.VISIBLE);
for callLog:
callLayout.setVisibility(View.GONE);
callLayout.setVisibility(View.VISIBLE);

Categories

Resources