Characters in TextView not displayed properly while typing - android

I made a TextView and a button. Clicking on the button will save the value entered by the user in a variable. The problem is, when the keyboard pops up after touching the textview, only the first character will be visible properly. The rest of the characters aren't displayed. An _ appears instead of the other characters. After i'm done typing and press the back button and when the keyboard disappears, I can see the characters properly. What is the problem?
Button okbutton = (Button) findViewById(R.id.okbutton);
okbutton.setClickable(true);
final TextView radius = (TextView) findViewById(R.id.radius);
okbutton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
i=radius.getText().toString();
}
});
Here is the complete layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView android:id="#+id/icon" android:src="#drawable/settings"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:paddingLeft="10px"></ImageView>
<TextView android:textSize="20px" android:typeface="sans"
android:layout_height="wrap_content" android:id="#+id/alarmset"
android:layout_width="fill_parent" android:text="#string/alarmset"
android:textStyle="bold" android:gravity="center"
android:drawablePadding="10px" android:paddingTop="10px"></TextView>
<TextView android:text="#string/distance" android:id="#+id/distance"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="#+id/alarmset"></TextView>
<TextView android:id="#+id/kmvalue" android:layout_below="#+id/distance"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:text="#string/km" android:id="#+id/km"
android:layout_below="#+id/distance" android:layout_toRightOf="#+id/kmvalue"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:paddingLeft="10px"></TextView>
<TextView android:text="#string/musichooser" android:id="#+id/musictext"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="#+id/km" android:paddingTop="10px"></TextView>
<Button android:id="#+id/chooser" android:layout_below="#+id/musictext"
android:drawableRight="#drawable/lov_ena"
android:layout_alignParentRight="true" android:layout_width="wrap_content"
android:layout_height="30px" android:layout_alignBaseline="#+id/musicfile">
</Button>
<EditText android:id="#+id/musicfile" android:layout_width="fill_parent"
android:layout_below="#+id/musictext" android:layout_toLeftOf="#id/chooser"
android:layout_height="40px">
</EditText>
<TextView android:text="#string/imgchooser" android:id="#+id/imgText"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="#+id/chooser" android:paddingTop="20px"></TextView>
<Button android:id="#+id/imgPicker" android:layout_below="#+id/imgText"
android:drawableRight="#drawable/lov_ena"
android:layout_alignParentRight="true" android:layout_width="wrap_content"
android:layout_height="30px" android:layout_alignBaseline="#+id/imgfile">
</Button>
<EditText android:id="#+id/imgfile" android:layout_width="fill_parent"
android:layout_below="#+id/imgText" android:layout_toLeftOf="#id/chooser"
android:layout_height="40px">
</EditText>
<RelativeLayout android:id="#+id/InnerRelativeLayout"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentBottom="true" android:layout_centerInParent="true">
<Button android:layout_width="wrap_content"
android:layout_height="50px" android:id="#+id/setalarm"
android:layout_centerInParent="true" android:text="#string/setalarm" />
</RelativeLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imgfile"
android:text="Enter Distance" />
<Button
android:id="#+id/okbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView1"
android:text="OK" />
<EditText
android:id="#+id/radius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/imgfile"
android:layout_alignTop="#+id/okbutton" />
</RelativeLayout>
This image should make the problem easy to understand
http://i39.tinypic.com/r8t64l.jpg

Try to change the TextView in your Code to EditText.

Button okbutton = (Button) findViewById(R.id.okbutton);
okbutton.setClickable(true);
EditText radius = (EditText) findViewById(R.id.radius);
okbutton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
i=radius.getText().toString();
}
});
try this....

by mistake you have puted any other view over your edit-text; jus go to your xml file and open Graphical layout, then find that view using mouse-pointer by clicking on that part in which your can not see the text in your shown picture, by double-clicking on that part you can goto xml, and jus remove that view from your xml

Related

Instagram options menu from bottom

In the current version of instagram app (Todays date 24-06-2021) ,inside the profile activity/fragment when hamburgur button is pressed (or simply the 3 line icon) now a menu pops up from bottom and can be scroll down by swipe down ,previously when cliked it opens a new fragment/activity
im in love with this animation or a view and im despirat to implement it in my app
but the problem is i dont know what it is called
i want to know what it is exactly and also similar kind of animation or a view is also there in pintrest app
Screen Shot
Update 1
how can i navigate to a fragment from a bottom sheet for eg if in my bootomsheet there is 3 textview i want that if any textView is cliked it should open the respected new fragment i.e if a editprofle textView is clicked it open the editprofile fragment this is how my bottomsheet opens up
Profile_Fragment.java
accountSettings the imageView when clicked the bottomSheetopens up
accountSettings.setOnClickListener(
v -> {
BottomSheet bottomSheet = new BottomSheet();
bottomSheet.show(requireActivity().getSupportFragmentManager(), bottomSheet.get Tag());
}
);
bottom_sheet_profile.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/slidedownview"
android:layout_width="40dp"
android:layout_height="3dp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:background="#drawable/rounded_corner_bottomsheetline"
android:layout_marginTop="11dp"/>
<TextView
android:id="#+id/settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/slidedownview"
android:layout_alignParentStart="true"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"
android:elevation="10dp"
android:text="#string/settings"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="normal" />
<TextView
android:id="#+id/edit_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/settings"
android:layout_alignParentStart="true"
android:layout_marginStart="30dp"
android:layout_marginTop="17dp"
android:elevation="10dp"
android:text="#string/edit_profile"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="normal" />
<TextView
android:id="#+id/favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/edit_profile"
android:layout_alignParentStart="true"
android:layout_marginStart="30dp"
android:layout_marginTop="17dp"
android:elevation="10dp"
android:text="#string/favorite"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="normal" />
<TextView
android:id="#+id/log_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/favorite"
android:layout_alignParentStart="true"
android:layout_marginStart="30dp"
android:layout_marginTop="17dp"
android:layout_marginBottom="15dp"
android:elevation="10dp"
android:text="#string/log_out"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="normal" />
</RelativeLayout>
Include the following library in your app.gradle file.
implementation 'com.google.android.material:material:<version>'
create custom layout bottom_sheet_dialog_layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:drawableStart="#drawable/ic_settings"
android:layout_gravity="center_vertical"
android:padding="8dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Archive"
android:drawableStart="#drawable/ic_archive"
android:layout_gravity="center_vertical"
android:padding="8dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Activity"
android:drawableStart="#drawable/ic_activity"
android:layout_gravity="center_vertical"
android:padding="8dp"/>
....
....
....
in Class file
final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
bottomSheetDialog.setContentView(R.layout.activity_sof);
TextView settings = bottomSheetDialog.findViewById(R.id.settings);
TextView archive = bottomSheetDialog.findViewById(R.id.archive);
TextView your_activity = bottomSheetDialog.findViewById(R.id.activity);
...
...
...
bottomSheetDialog.show();
If you want to click event
settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Copy is Clicked ", Toast.LENGTH_LONG).show();
bottomSheetDialog.dismiss();
}
});

Why is my relative layout filling the screen rather than wrapping content when I add an image?

I have a RelativeLayout, we'll call this the 'slider', that I want to overlay on another RelativeLayout (by switching visibility="gone" and "visible") when "Add People" is clicked, but the overlay should only take up as much width of the screen as needed. This layout will then be removed when "Cancel" is clicked. Everything is working fine so far.
RelativeLayout slider;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_people);
RelativeLayout add = (RelativeLayout) findViewById(R.id.add_wrapper);
RelativeLayout cancel = (RelativeLayout) findViewById(R.id.cancel_wrapper);
slider = (RelativeLayout) findViewById(R.id.add_people_slider);
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
slider.setVisibility(View.VISIBLE);
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
slider.setVisibility(View.GONE);
}
});
}
The issue arises when I try to add another image to the slider. I am adding this just above the #id/cancel_wrapper RelativeLayout in the XML (full XML at bottom).
<ImageView
android:id="#+id/transparent_add"
android:src="#drawable/ic_add_active_256"
android:layout_height="30dp"
android:layout_width="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true" />
For some reason, this is making the width of the slider the full width of the screen.
What's even more bizarre is if I add android:layout_marginRight="50dp" to this ImageView to move it left a little it starts making the slider smaller from the left. I would like this "transparent_add" image to be lined up with the old "add" image.
My 2 issues, then, are that when I add the "transparent_add" image it changes the width of the slider for an unknown reason, and also when I add marginRight on the image it makes the width of the slider smaller from the left.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white" >
<TextView
android:id="#+id/btn_people"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingLeft="10dp"
android:text="#string/label_people"
android:textColor="#color/blue"
android:textSize="16dp" />
<TextView
android:id="#+id/people_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/btn_people"
android:paddingLeft="10dp"
android:text="#string/label_people_info"
android:textSize="11dp" />
<RelativeLayout
android:id="#+id/add_wrapper"
android:layout_width="65dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#color/blue" >
<ImageView
android:id="#+id/plus_sign"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="19dp"
android:layout_marginTop="6dp"
android:src="#drawable/ic_add_256" />
<TextView
android:id="#+id/add_people"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingBottom="2dp"
android:text="#string/label_add_people"
android:textColor="#color/white"
android:textSize="11dp" />
</RelativeLayout>
<ListView
android:id="#+id/contacts_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="#id/people_info" />
<!-- switch between visible/gone -->
<RelativeLayout
android:id="#+id/add_people_slider"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="#color/blue"
android:visibility="gone" >
<TextView
android:id="#+id/label_add_new_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="65dp"
android:layout_alignParentTop="true"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:text="#string/label_add_new_contact"
android:textColor="#color/white"
android:textSize="16dp" />
<TextView
android:id="#+id/label_add_from_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/label_add_new_contact"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:text="#string/label_add_from_phone"
android:textColor="#color/white"
android:textSize="16dp" />
<TextView
android:id="#+id/label_add_from_facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/label_add_from_phone"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:text="#string/label_add_from_facebook"
android:textColor="#color/white"
android:textSize="16dp" />
<TextView
android:id="#+id/label_add_from_linkedin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/label_add_from_facebook"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:text="#string/label_add_from_linkedin"
android:textColor="#color/white"
android:textSize="16dp" />
<!-- insert image here -->
<RelativeLayout
android:id="#+id/cancel_wrapper"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingBottom="10dp" >
<ImageView
android:id="#+id/image_cancel"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="4dp"
android:src="#drawable/ic_close_256"/>
<TextView
android:id="#+id/label_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/label_cancel"
android:textColor="#color/white"
android:textSize="8dp" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
EDIT: This is happening both in Eclipse and on my Android device.
EDIT: I have tried placing the image above the "Add a new contact" text and then placing the text below that, but the same thing happens

Visibility of TextView in Dialog

I have a dialog, which is created with this code:
final Dialog dialog1 = new Dialog(Test.this);
dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog1.setContentView(R.layout.test_layout);
dialog1.setCancelable(true);
...
The layout file 'test_layout.xml' contains a typical TextView:
<TextView
android:id="#+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Test" />
I want in the code to set this TextView invisible (gone) in some cases.
I tried following:
TextView tv = (TextView) dialog1.findViewById(R.id.tv_username);
tv.setVisibility(TextView.GONE);
But the TextView still appears in the dialog. If I set in the layout xml file android:visibility="gone" it is not appearing in the dialog. But I need to do it via code.
Here the whole code:
if (whichButton == 1) {
final Dialog dialog1 = new Dialog(Test.this);
dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog1.setContentView(R.layout.test_layout);
dialog1.setCancelable(true);
TextView tv = (TextView) dialog1.findViewById(R.id.tv_username);
tv.setVisibility(View.GONE);
...
dialog1.show();
}
Here the whole XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="250dp"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/tv_name"
android:paddingTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:textSize="14dp"
android:text="Name:" />
<EditText
android:id="#+id/username"
android:hint="#string/enterName"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:maxLength="15"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:scrollHorizontally="true"
android:capitalize="words"
android:singleLine="true"
android:gravity="fill_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Test" />
<TextView
android:id="#+id/tv_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:textSize="14dp"
android:text="#string/message" />
<EditText
android:id="#+id/tip"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:maxLength="290"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:scrollbars="vertical"
android:inputType="textMultiLine"
android:capitalize="words"
android:gravity="fill_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/button"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/send_message" />
</LinearLayout>
Try tv.setVisibility(View.INVISIBLE);
I found a solution or workaround.
The dialog was created in the the onClick method of an AlertDialog.
Now I use the showDialog() method and handle it in the corresponding callback method.
There it is working fine.
I do not understand why this is working, but I only want to tell other users how they can solve this issue.
I would be happy if someone can come up with a clarification.

ScrollView hides top elements of the layout

I have pretty simple dialog layout and added scrollview because dialog can be quite long and not fit into the screen
When I initially run the dialog (it fits the screen) everything is fine, then I change device orientation to landscape, dialog does not fit to screen anymore, scrolling appears, but several top elements (up to #+id/addr_label) become invisible though some empty space appears between Title and first visible element exist. The most confusing thing is- when I change device back to portrait view- these elements are still invisible.
UPDATE: Just checked the case when phone is initially in landscape mode- in this case all elements are shown correctly. when I change it back to portrait- the elements disappear. seems to be a "change orientation" bug
What can be a problem?
Layout and screenshots are below (Android is 2.3.5)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root22" android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:padding="0dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="10dp">
<TextView android:id="#+id/details_ssid"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text=""
android:layout_marginLeft="5dip"/>
<TextView android:id="#+id/details_bssid"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text=""
android:layout_weight="1"
android:layout_marginLeft="5dip"/>
<TextView android:id="#+id/latitude"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text=""
android:layout_weight="1"
android:layout_marginLeft="5dip"/>
<TextView android:id="#+id/longitude"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text=""
android:layout_weight="1"
android:layout_marginLeft="5dip"/>
<TextView android:id="#+id/addr_label"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="#string/address_label"
android:layout_weight="1"
android:layout_marginLeft="5dip"/>
<EditText android:id="#+id/address"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text=""
android:layout_weight="1"
android:layout_marginLeft="5dip"/>
<TextView android:id="#+id/descr_label"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="#string/description_label"
android:layout_weight="1"
android:layout_marginLeft="5dip"/>
<EditText android:id="#+id/description"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text=""
android:layout_weight="1"
android:layout_marginLeft="5dip"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="10dp">
<Button
android:id="#+id/yes"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="10dip"
android:layout_marginRight="10dip"
android:text= "#string/Yes"
android:visibility="visible"
/>
<Button
android:id="#+id/no"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="10dip"
android:layout_marginRight="10dip"
android:text= "#string/No"
android:visibility="visible"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
Code is pretty boring:
onCreateDialog in Activity which sets layout and
onPrepareDialog whic sets values to all these fields
onCreateDialog
case CONFIRM_SHARE_AP:
{
final Dialog dialog = new Dialog(this){
public void onBackPressed()
{
dismissDialog(CONFIRM_SHARE_AP);
}
};
dialog.setContentView(R.layout.ap_add_confirmation);
dialog.setTitle(R.string.confirm_share);
Button cancel = (Button) dialog.findViewById(R.id.no);
cancel.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
dismissDialog(CONFIRM_SHARE_AP);
}
});
return dialog;
}
onPrepareDialog
has nothing specific at all
http://img525.imageshack.us/img525/4642/devicev1.png
http://img13.imageshack.us/img13/985/devicev2.png
Found a root of the prob.
This is because during orientation change activity is restarted (by default) and therefore dialogs are recreated.

View.OnClickListener() in Android responding for the wrong button

I am having an issue registering a call when a button is clicked.
I have 2 EditTexts, one Button and one CheckBox. I would like to get the onClick() function called when the Button is pressed. Unfortunately, it only happens when the CheckBox is pressed, but it's not the correct ID so I am confused.
Here is the java code:
Button connect = (Button) findViewById(R.id.connectToServer);
connect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
....
}
As you can see, I look for the id "connectToServer".
This is my .xml file:
<?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">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#ffb6b6b6"
android:textColor="#ffffffff" android:padding="2px" android:text="Settings" />
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="13px">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="25px"
android:textColor="#ffffffff" android:text="Set server settings" />
<TableLayout android:layout_marginTop="10px"
android:stretchColumns="1" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView android:text="IP address:" />
<EditText android:id="#+id/serverIP" android:maxLength="15" />
</TableRow>
<TableRow>
<TextView android:layout_marginTop="4px" android:text="Port:" />
<EditText android:id="#+id/serverPort" android:maxLength="15" />
</TableRow>
</TableLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_marginTop="10px" android:orientation="horizontal"
android:layout_height="fill_parent" android:gravity="center">
<Button android:text="Connect" android:id="#+id/connectToServer"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#ffb6b6b6"
android:textColor="#ffffffff" android:padding="2px"
android:layout_marginTop="10px" android:text="Availability" />
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="13px">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="25px"
android:textColor="#ffffffff" android:text="Set device available" />
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
<TableRow>
<TextView android:layout_marginTop="10px"
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Uncheck to disallow anyone from\nremotely connecting to the device." />
<CheckBox android:id="#+id/checkbox" android:gravity="right"
android:checked="true" android:layout_weight="1"
android:layout_width="wrap_content" android:layout_height="fill_parent" />
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
The important line I believe would be this:
<Button android:text="Connect" android:id="#+id/connectToServer"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
but the code only gets called when the CheckBox button is checked or unchecked. I am not sure why that is.
Thank you very much!
Try this,
Button connect = (Button) findViewById(R.id.connectToServer);
connect.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
there is much simpler way to handle click events
1) add android:onClick to assign the function like that
<Button android:id="#+id/connectToServer"
android:text="Connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick_connectToServer"
/>
2) implement onClick inside your activity
public void onClick_connectToServer(View v) {
String tag = "" + v.getTag();
// here you can handle the action
}
in this case, you don't need to add onClickListener explicitly.
Social Coding # AspiroTV
I ran into the same problem. I changed the button order within XML and the onClickListener started calling the wrong event. The view looked correct from the device but the View IDs in R.java were incorrect.
Using Eclipse: I simply did Project->Clean...
The event listeners started working correctly.

Categories

Resources