Display an edit text box on click of a button - android

I am creating a user form in android. I want to display an edit text box on click of a button. below that button, while simultaneously the contents originally present below that button to move more down.
How can this be done?

If you just want to "display an edit text box on click of a button" why don't you just..
Keep the EditText in your XML layout file for that activity below the Button where you want it..
XML set it's
android:visibility = "gone"
and making instance of that
EditText et=(EditText)findViewById(R.id.thatEditText);
in activity...in your button click event set
et.setVisibility(View.VISIBLE);

Define the view in your layout, then in code, show and hide it with
myView.setVisibility(View.VISIBLE) and myView.setVisibility(View.GONE).

//xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button"
/>
<EditText
android:id="#+id/edtbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
//Activity
//oncreate
editText.setVisibility(View.GONE);
btn..setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
editText.setVisibility(View.VISIBLE);
}
});

If your layout is relative then addView(yourView, index) doesn't work. Suppose you want to add view after some other control and reference to that control.
e.g.
<RelativeLayout android:id="#+id/templayout">
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add"/>
and you want to add edit text control after text View then on button click :
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.templayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.title);
EditText yourEditText = new EditText(this);
relativeLayout.addView(yourEditText, params);

Define your EditText in your xml and hide it. On button click, change its visibility to View.Visible.
YourEditText.setVisibility(View.GONE);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
YourEditText.setVisibility(View.VISIBLE);
}
});

Related

Adding views programatically to a relative layout within a scrollview

Please bear with me as I am new to the use of Views and Layouts.
I am trying to create an application in which the user can enter a text field, press a button and have a new text field appear and they can continue adding text fields in this way.
My solution was to have the top level be a scrollview and then have a relative view as a child within that(this way I can then programatically insert more editviews in my code with the OnClick() listener.
I have seen and read a couple of other posts pertaining to relative views but it seems there is still something that I am missing. I have tried
Here is the xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_create_accounts"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nic.mybudget.CreateAccountsActivity">
<RelativeLayout
android:id="#+id/activity_create_accounts_relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/activity_name"
android:inputType="textAutoComplete"
android:layout_alignParentTop="true"
android:id="#+id/activity_createAccounts_relativeLayout_activityName"/>
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/activity_createAccounts_relativeLayout_activityName"
android:id="#+id/activity_create_accounts_relativeLayout_activityName_addButton" />
<Button
android:text="Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/activity_create_accounts_relativeLayout_activityName_addButton"
android:layout_centerHorizontal="true"
android:id="#+id/activity_create_accounts_relativeLayout_activityName_saveButton" />
</RelativeLayout>
And here is the Code where I try to add new editviews.
public class CreateAccountsActivity extends Activity {
static private final String TAG = "MAIN-Activity";
int numAccounts = 0;
int lastAccountID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_accounts);
final RelativeLayout Relative = (RelativeLayout) findViewById(R.id.activity_create_accounts_relativeLayout);
final TextView oldAccount = (TextView) findViewById(R.id.activity_createAccounts_relativeLayout_activityName);
final TextView newAccount = new TextView(this);
final Button addNewAccountButton = (Button) findViewById(R.id.activity_create_accounts_relativeLayout_activityName_addButton);
addNewAccountButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "addNewAccountOnClick");
numAccounts = numAccounts+1;
int newAccountID = oldAccount.getId() + numAccounts;
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
newAccount.setLayoutParams(rlp);
newAccount.setHint("Hint" );
newAccount.setId(newAccountID);
Relative.addView(newAccount);
RelativeLayout.LayoutParams blp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
blp.addRule(RelativeLayout.BELOW, newAccountID-1);
addNewAccountButton.setLayoutParams(blp);
}
});
}
}
As you can see what I am trying (and failing) to do is add the new edit view at the top of the page and simply push everything else down the page. What am I getting wrong here with the relative layout?
Any help is appreciated.
First thing View with id activity_createAccounts_relativeLayout_activityName is EditText and you are casting it with TextView so that is wrong cast it to EditText.
And to your actual problem:
You are using same EditText instance with variable newAccount and adding it again in relative layout if you want to add one more EditText in relative layout you have to initialise EditText inside onclicklistener.
Just add one line newAccount= new EditText(context)in your onclicklistener code before line numAccounts = numAccounts+1;
Happy Coding !

android Check mark on button click

On click of a button , a check mark icon should be displayed on the leftmost corner of the button, when reclicked on the same button , the check mark icon should disppear. Could some on help me out in this case?
While this is already answered, here's an alternative solution: add a unicode checkmark symbol. There are two of them: \u2713 and \u2714. Just add them to your strings:
<string name="button_label_on">\u2713 on</string>
<string name="button_label_off">off</string>
Of course, you can put this directly into your layout code, too:
<Button
...
android:text="\u2713 on"
/>
You can add an ImageView (lets say tick.png) with visibility Gone, at the left of the Button. And set its visibilty. Here is the code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/iv_tick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:src="#drawable/tick"/>
<Button
android:id="#+id/btn_tick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press"/>
</LinearLayout>
Now, on Button click event you set its visibilty:
Button btn_tick = (Button)findViewById(R.id.btn_tick);
btn_tick.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
ImageView iv_tick = (ImageView)findViewById(R.id.iv_tick);
int visibility = iv_tick.getVisibility();
if(visibility == View.VISIBLE)
{
iv_tick.setVisibility(View.GONE);
}
else
{
iv_tick.setVisibility(View.VISIBLE);
}
}
});
Checkout the CheckBox widget.

How to add a text under the image button in Android?

I want to add text under a image button, which are also clickable, and will be redirected to the same activity as the image button. basically it's like the app list in any android phone, e.g.
If you have your ImageButton declared in XML, then just put it into a LinearLayout which also contains a TextView and set the onClickListener on the LinearLayout. The structure would be like
<LinearLayout
... > <!-- This is the LinearLayout for the xml document -->
<!-- Some other layout code here if you please -->
<LinearLayout
android:id="#+id/linLayout"
... >
<ImageButton
... />
<TextView
... />
</LinearLayout>
</LinearLayout>
And then in your java:
LinearLayout layout = (LinearLayout)findViewById(R.id.linLayout);
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
If you're adding each ImageButton dynamically via java code, then it will still maintain the same structure. Let me know if I need to add anything.
setAlpha and do the below
<Button
android:id="#+id/comparePack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:onClick="compareButtonClick"
android:text="#string/compare"
android:drawableTop="#drawable/compare1edit"
android:gravity="left|center_vertical" />
if you want to create this king of list use gridview
and in getview method inflate customlayout with imageview and textview
Also you can set onClickListener to TextView
<!-- Some other layout code here if you please -->
<LinearLayout
android:id="#+id/linLayout"
... >
<ImageButton android:id="#+id/btn1"
... />
<TextView android:id="#+id/tv1"
... />
</LinearLayout>
java code:
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myClick();
}
});
TextView tv1 = (TextView) findViewById(R.id.tv1);
tv1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myClick();
}
});

Trying to Read Edit box inside Custom Dialog... Getting FC

I am trying to read a EditText box and send its contents to another method from within a Custom dialog box. My current code causes a Force Close. The logcat is very vague... however I know the uncaught exception takes place in this method:
public void getName(){
Dialog dialog = new Dialog(main.this);
dialog.setContentView(R.layout.customdialog);
dialog.setTitle("New Game");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
final EditText inputBox = new EditText(this);
//set up text
final TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText("Enter Your Name...");
//set up button
final Button button = (Button) dialog.findViewById(R.id.namebutton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str = inputBox.getText().toString();
setName(str);
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
Here is the custom XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/linearLayout1">
<TextView android:text="#+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/nameprompt"></TextView>
<EditText android:layout_width="fill_parent" android:id="#+id/editText1" android:layout_height="wrap_content" android:inputType="textPersonName" android:text="Player">
<requestFocus></requestFocus>
</EditText>
<Button android:layout_height="wrap_content" android:id="#+id/namebutton" android:text="Ok" android:layout_width="fill_parent"></Button>
</LinearLayout>
</RelativeLayout>
Any ideas???
You have the EditText in your XML for the Dialog's layout... and you're properly using findViewById() to instantiate your TextView...
You need to do the same for the EditText, also use findViewById() to instantiate it.
final EditText inputBox = (EditText) dialog.findViewById(R.id.editText1);
Something is up with the inputBox object. You create it in this method, but I don't see you actually adding to the layout anywhere. When this method completes, you'll be displaying the dialog box, but the input box won't be displayed anywhere. In fact, I think that inputBox might be garbage collected since there are not references to if around after the getName() method completes. Therefore when you call get input on it, it might be null.
I think what you meant to do was this:
final EditText inputBox = (EditText)dialog.findViewById(R.id.editText1)

first click on linearlayout doesn flash background click color, it just flash for second and next click

I try to get the effect click background color for linear layout. I've set clickable to linear layout. and from the code also I've put the click listener the setBackgroundResource.
Here it is the xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="#+id/llinsertmem"
android:clickable="true"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50px">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="PUSH it"
/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
</LinearLayout>
and the java code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout linearInsertMem = (LinearLayout)findViewById(R.id.llinsertmem);
linearInsertMem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(android.R.drawable.list_selector_background);
Toast.makeText(testdoank.this, "succeded", Toast.LENGTH_SHORT)
.show();
}
});
}
When first time click the clickable linearlayout, the toast text is displayed but the background color click effect doesn't. The flash background click color is only work from the second click.
any idea what the problem is?
Is not necessary do anything in JAVA code.
You can only add this as attribute:
android:background="#android:drawable/list_selector_background"
And it works for me (on Android 2.2 device)
After try and error, somehow it's work.
just put the setBackgroundResource also on the onCreate.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout linearInsertMem = (LinearLayout)findViewById(R.id.llinsertmem);
linearInsertMem.setBackgroundResource(android.R.drawable.list_selector_background);
linearInsertMem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(android.R.drawable.list_selector_background);
Toast.makeText(testdoank.this, "succeded", Toast.LENGTH_SHORT)
.show();
}
});
}
Don't know the logic explanation. if you have a thought, please.
In the onClick method you are using v as the View which is passed, but that view may not be the LinearLayout which you want to change the background.
Thus you need to create a class level variable or a final varibale and pass the handle for the LineraLayout to the onClick method.
Remove/cut from onCreate:
linearInsertMem.setBackgroundResource(android.R.drawable.list_selector_background);
and paste it into onClick or change v. into linearInsertMem.
I think that Exlipse will then demand that linearInsertMem must be final like:
final LinearLayout linearInsertMem = (LinearLayout)findViewById(R.id.llinsertmem);
Or you can define this object above onCreate like this:
LinearLayout linearInsertMem;
then in onCreate you state:
linearInsertMem = (LinearLayout)findViewById(R.id.llinsertmem);
then onClick method will know exactly which view you want to change if you use linearInsertMem.setBackgroundResource...

Categories

Resources