Hy.... I'm making a simple project on eclipse. I'm using relative layout as shown below :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".ProfileActivity"
>
<Button
android:id="#+id/Prof_edit_btn"
android:layout_below="#+id/Prof_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit"
android:layout_marginTop="17dp"
android:layout_centerHorizontal="true"
/>
<LinearLayout
android:layout_below="#+id/Prof_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
>
<Button
android:id="#+id/Prof_save_btn"
android:layout_below="#+id/Prof_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:layout_marginTop="17dp"
android:layout_centerHorizontal="true"
android:visibility="invisible"
android:clickable="false"
/>
<Button
android:id="#+id/Prof_batal_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Batal"
android:layout_marginTop="17dp"
android:visibility="invisible"
android:clickable="false"
/>
</LinearLayout>
</RelativeLayout>
In the main activity, I've made a simple onClicklistener for Edit and Save button as shown below :
mEdit = (Button)findViewById(R.id.Prof_edit_btn);
mEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mBatal.setVisibility(View.VISIBLE);
mBatal.setClickable(true);
mSave.setVisibility(View.VISIBLE);
mSave.setClickable(true);
mEdit.setVisibility(View.INVISIBLE);
}
});
mSave = (Button)findViewById(R.id.Prof_save_btn);
mSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mEdit.setVisibility(View.VISIBLE);
mBatal.setVisibility(View.INVISIBLE);
mSave.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(), "Data Anda berhasil disimpan", Toast.LENGTH_SHORT).show();
}
});
Well... As we can see from my layout xml, only EDIT button will be visible on the start, then SAVE and BATAL button will be visible when EDIT is clicked by user according to the code program within setonclick listener of EDIT button. And as soon as EDIT button is clicked it will be invisible.... The problems here are WHen I clicked EDIT button, only SAVE button will be visible and BATAL button will be visible when SAVE button is clicked, another problem that I'm facing now is EDIT button is not invisible at all when I click it.... So.. anyone can help me with this problem please...??? :'(
Thanks in advance....
Naaah... I found my own solution... Just set the visibility of SAVE btn and BATAL btn to GONE... And it's all done.... :-)
<Button
android:id="#+id/Prof_save_btn"
android:layout_below="#+id/Prof_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:layout_marginTop="17dp"
android:layout_centerHorizontal="true"
android:visibility="gone"<-----------=
android:clickable="false"
/>
<Button
android:id="#+id/Prof_batal_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Batal"
android:layout_marginTop="17dp"
android:visibility="gone"<-----------=
android:clickable="false"
/>
Related
I have Following layout Scenario
Want to implement the Click even of TextView ..but the TextView Click not working
Here is code Sample
<FrameLayout
android:id="#+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:clickable="false">
<customview
android:id="#+id/custom"
android:layout_width="200dp"
android:layout_height="200dp"
android:clickable="false"/>
<LinearLayout
android:id="#+id/abc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/zero_done"
android:padding="5dp"
android:clickable="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</LinearLayout>
My TextView ClickEvent not working ...please help me
Class Code
TextView view= (TextView) findViewById(R.id.btn);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Press",
Toast.LENGTH_SHORT).show();
}
});
You didn't call show method of Toast,so you have no idea whether TextView click event did happen or not.
Try this:
Toast.makeText(getApplicationContext(), "Press",
Toast.LENGTH_SHORT).show(); // don't miss show method.
In my android code, I have implemented onclick event on 3 textviews using following code. But nothing happens when they are clicked. What is wrong ?
<TextView
android:id="#+id/tv1"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/menucircle"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:onClick="selectit"
android:textColor="#ffffff"
/>
public void selectit(View v)
{
Log.d("tv0","ok");
if(v.getId()==tv1.getId())
{Log.d("tv1","ok");
selectoption(1);
Log.d("tv1","ok");
}
if(v.getId()==tv2.getId())
{Log.d("tv2","ok");
selectoption(2);
}
if(v.getId()==tv3.getId())
{Log.d("tv3","ok");
selectoption(3);
}
}
Add this into your xml
android:clickable="true"
You probably need to have this code in your xml
android:clickable="true"
Or Set the Clicklistenner to TextView via code by
TextView btn=(TextView) findViewById(R.id.tv1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
selectit(v);
}
});
It will automatically make it clickable so no need of android:clickable="true"
You can set the click handler in xml with these attribute:
android:clickable="true"
Don't forget the clickable attribute, without it, the click handler isn't called.
main.xml
<TextView
android:id="#+id/tv1"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/menucircle"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:onClick="selectit"
android:clickable="true"
android:textColor="#ffffff"
/>
i hope it will help u all the best
Try like below:
android:onClick="onClick"
android:clickable="true"
My textview:
<TextView
android:id="#+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:textSize="55sp"
android:onClick="onClick"
android:clickable="true"/>
Main Activity:
public class MyActivity extends Activity {
public void onClick(View v) {
...
}
}
I am trying to change visibility of Relative Layout in my code on Button click, But i am facing a weird behavior.
Whenever i click on my Button say A first time, it shows the Relative layout say RA, and after clicking it(Second time) again it hides RA. The problem i am facing here is when i click on A second time it also hides its parent layout, And then on clicking it again it shows RA and its parent layout, After that it hides and shows both layouts on click event.
How this is happening here,
Here is my Code
this is my XML
<RelativeLayout
android:id="#+id/mainscreenrelativeLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="3dp"
android:background="#ffffff" >
<Button
android:id="#+id/imagebuttonSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:background="#drawable/setting_icon_hover"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/RL_mainpage_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/mainscreenrelativeLayout2"
android:layout_alignParentRight="true"
android:visibility="invisible"
>
<Button
android:id="#+id/button_settings_monthlyissue"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/monthly_issue_select"/>
<Button
android:id="#+id/button_settings_changepassword"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button_settings_monthlyissue"
android:background="#drawable/password_select" />
<Button
android:id="#+id/button_settings_logout"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button_settings_changepassword"
android:background="#drawable/logout_select" />
</RelativeLayout>
And this is my CODE in Activity Class:
Button _settings;
RelativeLayout __settingslayout;
boolean isSettingsClicked = false;
........
........
_settings = (Button) findViewById(R.id.imagebuttonSettings);
_settingslayout = (RelativeLayout) findViewById(R.id.RL_mainpage_settings);
_settings.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (isSettingsClicked == false) {
_settings.setBackgroundResource(R.drawable.setting_icon);
_settingslayout.setVisibility(View.VISIBLE);
isSettingsClicked = true;
} else if (isSettingsClicked == true) {
_settingslayout.setVisibility(View.INVISIBLE);
_settings
.setBackgroundResource(R.drawable.setting_icon_hover);
isSettingsClicked = false;
}
}
});
The problem is , RelativeLayout with id android:id="#+id/mainscreenrelativeLayout2" is also hiding on click event, as you can see i haven't used this id in my Activity.
I want to change the background image and TextView Color on click of a layout .The background color is chnaging properly but my textview color is not changing.Here is my XML code :
<RelativeLayout
android:id="#+id/flight_relative"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_below="#+id/imgLogo"
android:layout_marginTop="5dp"
android:background="#drawable/button_effect"
android:gravity="center_vertical" >
<TextView
android:id="#+id/flight_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/flight_list_image"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/flight_list_image"
android:text="#string/flight_tittle"
android:textColor="#152b72"
android:textSize="15dp"
android:textStyle="bold" />
<TextView
android:id="#+id/content_flight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/flight_content"
android:layout_toRightOf="#+id/flight_list_image"
android:text="#string/flight_content"
android:textColor="#2f2f2f"
android:textSize="10sp" />
<ImageView
android:id="#+id/flight_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/flight_content"
android:layout_alignParentRight="true"
android:src="#drawable/arrow" />
<ImageView
android:id="#+id/flight_list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="3dip"
android:src="#drawable/flight_icon" />
</RelativeLayout>
Code to chnage the textview color is :
flightRelative = (RelativeLayout )findViewById(R.id.flight_relative);
flightRelative.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
flight = (TextView)findViewById(R.id.flight_content);
flight.setTextColor(Color.WHITE);
}
});
What wrong i am doing please suggest me .For the first time it is not working on second time it is working
You should add this definitely working.I am also check it out...
flightRelative.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
TextView flight = (TextView)findViewById(R.id.flight_content);
flight.setTextColor(Color.parseColor("#FFFFFF"));
}
});
you should use
#Override
public void onClick(View arg0) {
flight = (TextView)flightRelative.findViewById(R.id.flight_content);
flight.setTextColor(Color.WHITE);
}
I believe it should work now.
Hi all i have updated my post to show the full xml file, which will show if i am doing something wrong.
i implemented a custom dialog with an edit text field and it keeps crashing. I will
also like to access the values filled in the text field. Any idea where i am going wrong? Thanks.
Below is the offending/troublesome??? code
First i show the xml file which contains my layout for the alert dialog.
<?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">
<ImageView android:id="#+id/ImageView01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<ScrollView android:id="#+id/ScrollView01"
android:layout_width="wrap_content" android:layout_below="#+id/ImageView01"
android:layout_height="200px">
<TextView android:text="#+id/TextView01" android:id="#+id/TextView01"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<EditText
android:id="#+id/lastname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="blah"
/>
</ScrollView>
<Button android:id="#+id/Button01"
android:layout_below="#id/ScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Cancel" />
</RelativeLayout>
Then main activity file which implements
my button click events...etc.
.......
setContentView(R.layout.main);
//this button will show the dialog
Button button1main = (Button) findViewById(R.id.Button01main);
button1main.setOnClickListener(new View.OnClickListener() {
public void onClick(View OnClickListener) {
//set up dialog
final Dialog dialog = new Dialog(MoredialogActivity.this);
dialog.setContentView(R.layout.maindialog);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText(R.string.lots_of_text);
//set up image view
ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
img.setImageResource(R.drawable.icon);
//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
//finish();
}
});
dialog.show();
}
});
}
}
You have two items in your ScrollView. According to the doc :
A ScrollView is a FrameLayout, meaning you should place one child in
it containing the entire contents to scroll; this child may itself be
a layout manager with a complex hierarchy of objects.
So you should had another layout, like a linear layout :
<ScrollView
android:id="#+id/ScrollView01"
android:layout_width="wrap_content"
android:layout_below="#+id/ImageView01"
android:layout_height="200px">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:text="#+id/TextView01"
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/lastname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="blah" />
</LinearLayout>
</ScrollView>