I used the following code but the editview layout appearing is in the bottom and i want it to get displayed on the top.
If anybody can please tell me what to do so that it get displayed on the top
private OnClickListener OnClick() {
// TODO Auto-generated method stub
return new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText t = new EditText(getApplicationContext());
t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
mLayout.addView(t);
}
};
}
mLayout.addView(t) will add the new view at the end of the layout. Try to use mLayout.addView(t, 0) to add it as the first view in the layout.
You should add the EditText in the layout by default, but with it's visibility set to android:visibility="gone". This will cause the EditText to be present, but not visible. The rest of your layout will adjust for the View not being there.
Then in your onClick change the visibility on the EditText to visible. This will cause your UI to update with the now visible EditText.
mLayout.addView(t, 0);
where 0 indicates the position within the layout.
See this.
Related
I want to ask how to make a list of text that we can tap in each of the text and then get the selected text to editText.
I just added the screenshot
http://i.stack.imgur.com/ddZSg.png
I have been searching it since yesterday, but I can not find exact solution. I also tried with listview, but I don't know how it's possible with horizontal, and flow list item.
I am also new in android. But I can think of the logic what you want. You can try this out if you want.
First of all, you can make your list of texts EditText by list of buttons Buttons
You can dynamically add as many Buttons with their respective text as you want to show.
set their corresponding onClickListener
In their onClickListener , create an object of the EditText you are using to add texts.
Store the value of EditText into a String Variable first.
Add the text of the clicked Button to the variable.
and now again set the text in EditText with the variable you created to store the values.
Your task will be done.
Try referring this code.
and change the code accordingly as your needs.
// Adding EditText and a button in a new linear layout and then adding
// the new linearLayout to the main layout
String[] valuesToBeAdded={"A","B","C","D"};
String selectedValues=null;
LinearLayout mainLayout=(LinearLayout) findViewById(R.id.mainLayout);
LinearLayout localLayout = new LinearLayout(context);
localLayout.setOrientation(LinearLayout.VERTICAL);
localLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
EditText editText=new EditText(context);
editText.setText(selectedValues);
editText.setId(5000);
localLayout.addView(editText);
for(int i=0;i<valuesToBeAdded.length();i++){
Button button = new Button(context);
button.setText(R.string.scanDocument);
button.setId(i);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText ed=(EditText) findViewById(5000);
selectedValues=ed.getText();
selectedValues=selectedValues +" " + this.getText();
ed.setText(selectedValues);
}
});
localLayout.addView(button);
}
mainLayout.addView(localLayout);
Thank You
Could you not make as many buttons as you need and then in the button_Click method for all buttons add:
Buttonwithtext_Click(object sender, EventArgs e)
{
editTextBox.Text = editTextBox.Text + Buttonwithtext.text + ", ":
}
Currently i am having some problem with implementing onclicklistener to a dynamically creating textview. I will explain the problem more detailed. What i need to do is, i need to create textviews when i click a button in an activity and when i click on that textview it should get removed. but i am not able to set onclicklistener to each textview. Since, set onclicklistener of textviews are written inside the onclick function of the above said button(button used for creating the textview), its scope get over when it exits from onclick function of the button(i think this is the problem). So i tried using visible and invisible feature, which will create the textviews before hand and make them invisible and they are made visible only when the button(button used for creating the textview)is clicked. But here even though it is invisible the space will be allocated(ie, blank space will be availabe).
Here is my code
This button addphone will dynamically create textview by inserting the value present in the edittext phoneno
addphone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(phoneno.getText().toString().length() > 0 && counter < MAX)
{
addphoneno[counter] = phoneno.getText().toString();
phoneno.setText("");
final TextView mybox = new TextView(getApplicationContext());
mybox.setText(addphoneno[counter]);
mybox.setPadding(5, 5, 5, 5);
mybox.setBackgroundColor(Color.rgb(99, 99, 99));
contactbox[counter] = mybox;
contactbox[counter].setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
contactbox[counter].setId(100+counter);
contactbox[counter].setText(addphoneno[counter]+" "+"X");
contactbox[counter].setClickable(true);
contactbox[counter].setOnClickListener(this); //This doesn't work!!!!!
counter = counter+1;
}
}
});
But the setOnClickListener in the above line is not working
So can anyone pls help me with this problem. I hope you are clear with my question.
Thank You!
You can try this:
private OnClickListener phoneViewClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
// your code
}
};
and use that listener in your TextViews:
contactbox[counter].setOnClickListener(phoneViewClickListener);
You will have to actually define a onClickListener instead of simply setting it as a boolean value.
contactbox[counter].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//this is where you would handle your click event
}
});
Good luck!
If your button was defined on the xml layout you can do that.
In your xml layout you can define which method will be called when a user click on your button:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/add_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="addTextView" /> // This is most imporant line
Your activity must have a method with the same name with a View parameter, like that:
/** Called when the user touches the button */
public void addTextView(View view) {
// Do something in response to button click
if(phoneno.getText().toString().length() > 0 && counter < MAX)
{
addphoneno[counter] = phoneno.getText().toString();
phoneno.setText("");
final TextView mybox = new TextView(getApplicationContext());
mybox.setText(addphoneno[counter]);
mybox.setPadding(5, 5, 5, 5);
mybox.setBackgroundColor(Color.rgb(99, 99, 99));
contactbox[counter] = mybox;
contactbox[counter].setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
contactbox[counter].setId(100+counter);
contactbox[counter].setText(addphoneno[counter]+" "+"X");
contactbox[counter].setClickable(true);
contactbox[counter].setOnClickListener(this); //This will work \o/
counter = counter+1;
}
}
}
On this method you should put your code to addViews.
As the behavior of all added textview must to be the same( i understood in that way), be removed when a user clicked on it, you can make your activity implements onClickListener and with that you just need to implement correctly the onClick method of your activity.
I have created the textview dynamically. My whole project is in white background so I made this textview background white. If I click one of the textview items it should take me to another activity. I got that output.
But my problem is I cant see Textview is clicked or not. I have done this through textview.setClickable(true). I can see if it is black background. can anyone help me please
Sorry I forgot to add, My textcolor is black
you have to implement onClickListener for that text field, for example:
yourTextField.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
i set my activity theme to translucent so as to see through to the underneath activity window.
I want to know if it's possible to enable click event when user tap on empty area on this translucent activity?
Thanks,
dara kok
It is possible to add click event to your activity. You need to do as below:
You could have done setContentView(R.layout.main); in onCreate() of your activity.
In main.xml, give some id to the root layout. For eg.,
Lets consider you have root as LinearLayout with id set as below,
Then in onCreate() of your activity, you will have to do the following:
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Overriding this would work:
http://developer.android.com/reference/android/app/Activity.html#onTouchEvent(android.view.MotionEvent)
However i think it's your translucent activity that will get the taps, not the one visible under it.
You can add OnClickListener on parent view of your layout.
For example, add android:id="#+id/some_id" to your parent LinearLayout in main.xml.
Then add this code after setContentView in onCreate method:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.some_id);
FrameLayout frameLayout = (FrameLayout) linearLayout.getParent(); // Get parent FrameLayout
frameLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed(); // Close activity, for example
}
});
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// empty block for prevent frameLayout click event, if you need
}
});
I am new to Android.Can anyone give some ideas for my problem.
/* Parent Linear Layout */
final LinearLayout par_layout=new LinearLayout(this);
par_layout.setOrientation(LinearLayout.VERTICAL);
/* Child Linear Layout */
final LinearLayout chl_layout=new LinearLayout(this);
chl_layout.setOrientation(LinearLayout.VERTICAL);
TextView tv_name=new TextView(this);
tv_name.setText("Name ");
TextView tv_item=new TextView(this);
tv_item.setText("Items ");
Button btn_submit=new Button(this);
btn_submit.setText("Submit");
btn_submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
par_layout.removeAllViewsInLayout();
}
});
chl_layout.addView(tv_name);
chl_layout.addView(tv_item);
chl_layout.addView(btn_submit);
par_layout.addView(chl_layout);
setContentView(par_layout);
In the above code at the time of button click i wish to clear the chl_layout from the par_layout.But i can't . Can anyone give some ideas ??
Note :
The following code also not working
par_layout.removeView(chl_layout);
Use below code to remove child view from parent view.
par_layout.removeView(chl_layout);
to make whole layout empty
there is a void function ...
LinearLayout li=new LinearLayout(this);
li.removeAllViews();
Try using this:
fatherLayout.removeViewInLayout(childLayout);
I can't remove a child view (TableRow) from its parent (TableLayout) with removeView(View view).
But addView is working.
Strange...