this below code for my login_activity xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBackgroundLogin"
android:clipToPadding="false"
android:fillViewport="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linear_layout"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:paddingTop="20dp"
android:paddingBottom="20dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:src="#drawable/login"
tools:ignore="ContentDescription"/>
</RelativeLayout>
<EditText
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:id="#+id/email_edit_text"
android:hint="#string/email_address_string" />
<EditText
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword"
android:id="#+id/password_edit_text"
android:hint="#string/password_string" />
<Button
android:layout_width="match_parent"
android:textAllCaps="true"
android:text="#string/login_string"
android:background="#drawable/button_style"
android:layout_marginTop="20dp"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:textColor="#fff"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:id="#+id/login_button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:textStyle="italic"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
</LinearLayout>
</ScrollView>
I want to click on TextView id=need_an_account that like below:
public class LoginActivity extends Activity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.need_an_account:
// some code
break;
}
}
}
Note that i don't want to use findViewById() method
But onClick not Working at all how can I solve this problem?
If you don't want to use OnClickListener, Here is an alternative -
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:textStyle="italic"
android:onClick="myTextViewMethod"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
Now write myTextViewMethodin you Activity
public void myTextViewMethod(View v) {
// Do your stuff here...
}
Plus point, No need to implement OnClickListener, Android will do it in background.
And if you wanna use an Interface View.OnClickListener
Simple, set need_an_account.setOnClickListener(this);
In oncreate add this,
TextView need_an_account = (TextView) findViewById(R.id.need_an_account);
need_an_account.setOnClickListener(this);
You can add OnClick to your xml to avoid using findViewById:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:clickable="true"
android:textStyle="italic"
android:onClick="textViewClick"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
and then write your function in the activity as:
public void textViewClick(View v) {
switch (v.getId())
{
case R.id.need_an_account:
// some code
break;
}
}
no need to implement an OnClickListener if you dont want to define the TextView programatically. Remember to set TextView to clickable and for authenticity i also set background to ?selectableItemBackground so the user knows that the TextView can be clicked
Related
I am new to android development. I want to include the same horizontal Scroll view in all android activities.I have defined the layout and Onclick events in one separate activity and extended that class with other activites.But the onclick events are not working
here is my Base Activity
public class Footer extends AppCompatActivity implements View.OnClickListener {
private ImageView img_school, img_group, img_news, img_schemes, img_jobs, img_gallery, img_goddess, img_services, img_census, img_address, img_abt_us;
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.imgs_school:
Intent i = new Intent(Footer.this, SchoolActivity.class);
startActivity(i);
break;
case R.id.imgs_galary:
Intent gi = new Intent(Footer.this, GalleryActivity.class);
startActivity(gi);
break;
case R.id.imgs_events:
Intent ni = new Intent(Footer.this, EventsActivity.class);
startActivity(ni);
break;
case R.id.imgs_abtus:
break;
case R.id.imgs_jobs:
Intent ji = new Intent(Footer.this, JobsActivity.class);
startActivity(ji);
break;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_footer);
initAll();
img_gallery.setOnClickListener(this);
img_school.setOnClickListener(this);
img_news.setOnClickListener(this);
img_jobs.setOnClickListener(this);
img_abt_us.setOnClickListener(this);
}
public void initAll() {
img_school = findViewById(R.id.imgs_school);
img_news = findViewById(R.id.imgs_events);
img_jobs = findViewById(R.id.imgs_jobs);
img_gallery = findViewById(R.id.imgs_galary);
img_abt_us = findViewById(R.id.imgs_abtus);
}
}
and the XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:paddingLeft="#dimen/_20sdp"
android:paddingRight="#dimen/_50sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgs_school"
android:layout_gravity="center"
android:clickable="true"
android:src="#drawable/school"
android:layout_width="#dimen/_35sdp"
android:layout_height="#dimen/_35sdp" />
<TextView
android:text="School"
android:layout_gravity="center"
android:textSize="#dimen/_13sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:paddingRight="#dimen/_50sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgs_events"
android:clickable="true"
android:src="#drawable/news"
android:layout_gravity="center"
android:layout_width="#dimen/_35sdp"
android:layout_height="#dimen/_35sdp" />
<TextView
android:text="Events"
android:layout_gravity="center"
android:textSize="#dimen/_13sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:paddingRight="#dimen/_50sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgs_jobs"
android:layout_gravity="center"
android:clickable="true"
android:src="#drawable/jobs"
android:layout_width="#dimen/_35sdp"
android:layout_height="#dimen/_35sdp" />
<TextView
android:text="Jobs"
android:layout_gravity="center"
android:textSize="#dimen/_13sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:paddingRight="#dimen/_50sdp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgs_galary"
android:clickable="true"
android:src="#drawable/gallery"
android:layout_gravity="center"
android:layout_width="#dimen/_35sdp"
android:layout_height="#dimen/_35sdp" />
<TextView
android:text="Gallery"
android:layout_gravity="center"
android:textSize="#dimen/_13sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:paddingRight="#dimen/_50sdp"
android:layout_height="wrap_content">
<ImageView
android:layout_gravity="center"
android:id="#+id/imgs_abtus"
android:clickable="true"
android:src="#drawable/abt_us"
android:layout_width="#dimen/_35sdp"
android:layout_height="#dimen/_35sdp" />
<TextView
android:text="About Us"
android:layout_gravity="center"
android:textSize="#dimen/_13sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
And i am extending this activity with other classes
such as
public class SchoolActivity extends Footer {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_school);
}
}
And its activity_school xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.evoqis.manovaru.SchoolActivity">
<include layout="#layout/activity_footer"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
But the onclick events are not happening.
Thanks in adavance
Here is the problem:
In subclass you call:
super.onCreate(savedInstanceState);
Which calls:-
setContentView(R.layout.activity_footer); //which creates a new views
initAll(); //which finds views by id
img_gallery.setOnClickListener(this); //setting listeners.
and then in subclass you call:
setContentView(R.layout.activity_school); which creates new view, and overrides all the listeners you have set in superclass.
How to fix it
Move
img_gallery.setOnClickListener(this);
img_school.setOnClickListener(this);
img_news.setOnClickListener(this);
img_jobs.setOnClickListener(this);
img_abt_us.setOnClickListener(this);
Inside public void initAll(), and in subclass do this:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_school);
initAll();
Also you can remove onCreate() method from superclass.
I have been reading about how to dynamically add an EditText field to my Linear Layout, every-time a user clicks a TextView (which has an onClick Listener attached).
I have had some mild success - I know that the EditText field is being created because when the button is clicked, all other elements move up as if something is being added to the screen.
My problem is that the EditText aren't visible and I haven't a clue why that is, so any help would be appreciated.
The the app isn't crashing so nothing to add in terms of the StackTrace and Log, as far as the app is concerned, everything is being created.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/facebookBlue"
android:orientation="vertical"
android:weightSum="1"
tools:context="com.test.practise.AddTeamMembers">
<android.support.design.widget.TextInputEditText
android:id="#+id/tv_teamNames"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/teamName"
android:textColor="#android:color/background_light"
android:textColorLink="#android:color/background_light"
android:textSize="30sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.26"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.47"
android:gravity="center"
android:text="Enter Player Names Below!"
android:textColor="#android:color/background_light"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/et_team_name1"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="#android:color/background_light"
android:ems="10"
android:hint="Team Name"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="70dp"
android:singleLine="true"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/et_team_name2"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:background="#android:color/background_light"
android:ems="10"
android:hint="Team Name"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="70dp"
android:singleLine="true"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/et_team_name3"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_gravity="center"
android:background="#android:color/background_light"
android:ems="10"
android:hint="Team Name"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="70dp"
android:singleLine="true"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/editTextGroupLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.07"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/tv_add_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="+ Add Name"
android:textColor="#android:color/background_light"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#color/facebookBlue"
android:gravity="center"
android:text="Ready to join!"
android:textColor="#android:color/background_light" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Below is the AddTeamMembers Class that calls the above XML
public class AddTeamMembers extends Fragment implements
View.OnClickListener
{
private SharedPreferences pref;
private TextView tv_teamNames, tv_add_name;
private LinearLayout mLayout;
//The below method must be overridden in order to implement a fragment -
this changes the lifecycle method
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_add_team_members,
container, false);
initViews(view);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// 0, Sets Shared pref mode to private
pref = getActivity().getPreferences(0);
tv_teamNames.setText(pref.getString(Constants.Team_Name, ""));
}
private void initViews(View view) {
tv_teamNames = (TextView) view.findViewById(R.id.tv_teamNames);
tv_add_name = (TextView) view.findViewById(R.id.tv_add_name);
mLayout = (LinearLayout) view.findViewById(R.id.editTextGroupLayout);
tv_add_name.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv_add_name:
createEditTextView();
break;
}
}
#TargetApi(Build.VERSION_CODES.M)
public void createEditTextView() {
try{
//dynamically create new EditText when user clicks to add another
name
//target user using API 22 and above (lollipop and above)
EditText editTextView = null;
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.M) {
editTextView = new EditText(getContext());
}else{
Toast.makeText(getActivity(), "App is not supported on this
device",
Toast.LENGTH_LONG).show();
}
editTextView.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1);
editTextView.setLayoutParams(params);
mLayout.addView(editTextView);
}catch(Exception e){
Log.d(TAG, "Failed to create new edit text");
}
}
}
[![Before Button is clicked][1]][1]
[![I have clicked 2 add 3 EditText here to make it obvious whats happening]
[2]][2]
[1]: https://i.stack.imgur.com/DGoPd.png
[2]: https://i.stack.imgur.com/3LWcY.png
I have a layout in which I have an image,textview, editbox and a button so whenever the keyboard opens I wanted my screen to scroll down to the button so that the user can always type and press submit button. I did do this by using scrollview but than I removed the scroll view did some changes and now when I again use a scrollview it does not work.
here is the code please check.
layout_file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="myapp.anis.hotel.MainActivity"
android:background="#drawable/backgroundhotel"
android:fitsSystemWindows="true">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scroll">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Please share your feedback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView6"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textColor="#android:color/background_dark"
android:fontFamily="serif"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
<TextView
android:text="#string/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/thanks"
android:shadowColor="#android:color/background_light"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textColorHighlight="#android:color/background_dark"
android:textColor="#android:color/background_dark"
android:fontFamily="serif"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
<ImageView
android:layout_width="wrap_content"
app:srcCompat="#drawable/hotellogo"
android:id="#+id/imageView"
android:layout_height="150dp"
android:layout_marginTop="15dp"
android:layout_below="#+id/thanks"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:ems="10"
android:maxLines="50"
android:background="#android:drawable/editbox_background"
android:layout_marginTop="15dp"
android:id="#+id/feedback"
android:hint="Enter FeedBack"
android:inputType="textMultiLine"
android:gravity="left"
android:textColorHint="#android:color/background_dark"
android:textColor="#android:color/background_dark"
android:textColorHighlight="#android:color/background_dark"
android:layout_below="#+id/textView6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_height="180dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/submitting1"
android:background="#android:color/transparent"
android:onClick="onSubmit"
android:id="#+id/submit"
android:layout_below="#id/feedback"
android:layout_alignParentBottom="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
and in my main activity I am using the scrollto method to go to my image button.
public class MainActivity extends AppCompatActivity {
ImageButton submit;
EditText feedBack;
static public String saveFeedBack="";
ScrollView view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit = (ImageButton) findViewById(R.id.submit);
feedBack = (EditText) findViewById(R.id.feedback);
view = (ScrollView) findViewById(R.id.scroll);
view.scrollTo(submit.getScrollX(),submit.getScrollY());
}
public void onSubmit(View view){
saveFeedBack = feedBack.getText().toString();
if(saveFeedBack.length() >=4) {
Intent intent = new Intent(this, Main3Activity.class);
startActivity(intent);
}
else{
Toast.makeText(getApplicationContext(),"Enter valid Feedback please.",Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onRestart() {
super.onRestart();
feedBack.setText("");
feedBack.setHint("Enter Feedback");
}
}
but when I run my application the scrollview scrolls to my edit text but not my button. I have tried adjustResize,adjustPan and all those things but none of them worked.
ok after much of research I have solved your problem. Actually the thing is when you use scroll to any position inside onCreate it doesn't work because a small delay is required to inflate the view.
So use the below code inside your onCreate:
view.postDelayed(new Runnable() {
#Override
public void run() {
view.fullScroll(ScrollView.FOCUS_DOWN);
}
},200);
Hope it Helps!!!
I have a Relative layout as:
<RelativeLayout
android:id="#+id/iPay_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="· Use iPay"
android:textColor="#686A86"
android:textSize="18sp" />
<Button
android:layout_width="50dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:background="#drawable/i_pay"
android:textAllCaps="false"
android:textColor="#ffffff" />
</RelativeLayout>
I want my relative layout to perform onCLickListener but does not work.
I am able to do the setonclicklistener but it works only with the textview part and not for the image.
btniPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//for ipay
}
});
This works for the text part only but not for the image. Any idea what I might be missing.
Alternatively, you can add android:clickable="false" in your button xml attribute.
<RelativeLayout
android:id="#+id/iPay_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="· Use iPay"
android:textColor="#686A86"
android:textSize="18sp" />
<Button
android:layout_width="50dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:background="#drawable/i_pay"
android:textAllCaps="false"
android:clickable="false"
android:textColor="#ffffff" />
</RelativeLayout>
may be you can find some good example thats use with imageview and buttons but the easy way i did it by using adding TextView instead of images and buttons.
<RelativeLayout
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:clickable="true"
android:gravity="right">
<TextView
android:id="#+id/englishheading1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/urduheading1"
android:text="English text"
android:textColor="#ffffff"
android:textSize="15sp" />
<TextView
android:id="#+id/urduheading1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:text="urdu text1"
android:textColor="#ffffff"
android:textSize="16sp" />
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_alignParentBottom="true"
android:layout_marginTop="8dp"
android:background="#ffffff" />
</RelativeLayout>
Here is java code to perform onClick.
RelativeLayout layout1 = (RelativeLayout) view.findViewById(R.id.layout1);
layout1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something
}
});
see my code easily run change your code:
.xml file
<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"
tools:context="${relativePackage}.${activityClass}" >
<RelativeLayout
android:id="#+id/iPay_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal" >
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="· Use iPay"
android:textColor="#686A86"
android:textSize="18sp" />
<Button
android:id="#+id/btn"
android:layout_width="50dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:background="#drawable/ic_launcher"
android:textAllCaps="false"
android:textColor="#ffffff" />
</RelativeLayout>
</RelativeLayout>
.java file
public class MainActivity extends Activity {
TextView tv;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.tv);
btn = (Button) findViewById(R.id.btn);
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_SHORT).show();
}
});
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "button", Toast.LENGTH_SHORT).show();
}
});
}
}
Just mark android:clickable="true" in your RelativeLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_id"
android:clickable="true">
Using ButterKnife lib make bind here:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xml_name);
ButterKnife.bind(this);
}
#OnClick(R.id.activity_id)
void onActivityClick() {
// do smth ...
}
I didn't have this problem until now. I don't know what is the problem here because in the same layout i have several buttons and only buttons which are in LinearLayout won't respond.
This is the layout where my buttons won't respond onClick (EDITED):
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:background="#drawable/texture"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="600dp"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<EditText
android:id="#+id/input_first_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:ems="10"
android:inputType="text"
android:textSize="#dimen/text_size">
<requestFocus />
</EditText>
<EditText
android:id="#+id/input_last_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/input_first_name"
android:ems="10"
android:inputType="text"
android:textSize="#dimen/text_size" />
<EditText
android:id="#+id/input_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/input_last_name"
android:ems="10"
android:inputType="number"
android:textSize="#dimen/text_size" />
<ImageView
android:id="#+id/profile_image"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_below="#+id/input_age"
android:layout_centerHorizontal="true"
android:src="#drawable/add" />
<Button
android:id="#+id/save_button"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/slider"
android:layout_toLeftOf="#+id/edit_button"
android:onClick="run"
android:text="#string/save_button" />
<Button
android:id="#+id/edit_button"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/slider"
android:text="#string/edit_button" />
<Button
android:id="#+id/delete_button"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/slider"
android:layout_toLeftOf="#+id/save_button"
android:layout_toStartOf="#+id/save_button"
android:text="#string/delete_button" />
<Button
android:id="#+id/edit_birthday_date"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/profile_image"
android:layout_centerHorizontal="true" />
<LinearLayout
android:id="#+id/button_container1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/cake_image"
android:layout_marginTop="10dp"
android:clickable="true"
android:weightSum="3"
android:orientation="horizontal">
<Button
android:id="#+id/button_movie"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
<Button
android:id="#+id/button_books"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
<Button
android:id="#+id/button_tech"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/button_container2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button_container1"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="3"
android:clickable="true">
<Button
android:id="#+id/button_body_care"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
<Button
android:id="#+id/button_clothes"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
<Button
android:id="#+id/button_accessories"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/button_container3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button_container2"
android:layout_marginTop="10dp"
android:clickable="true"
android:orientation="horizontal">
<Button
android:id="#+id/button_games"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/edit_button"
android:layout_alignTop="#+id/slider"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<ImageView
android:id="#+id/cake_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignStart="#+id/edit_birthday_date"
android:layout_below="#+id/edit_birthday_date"
android:layout_marginTop="20dp"
android:src="#drawable/birthday_cake" />
<TextView
android:id="#+id/turning_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cake_image"
android:layout_toEndOf="#+id/cake_image"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<com.daimajia.slider.library.SliderLayout
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#+id/cake_image" />
</RelativeLayout>
</ScrollView>
I have set for every linear layout visibility when one button which is working is clicked to visible, so i think that is not the problem.
This is the code in java:
if (getMovieCategory.equals("movies")) {
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_cancelar, 0, 0, 0);
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String movies = "movies";
dbh.updateCategoryMovies(birthdayId, movies);
}
});
} else {
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_add, 0, 0, 0);
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dbh.deleteMovies(getMovieCategory);
}
});
}
Here everything is working except onClick. I'm getting no errors. My button doesn't respond on click.
try by handling setOnClickListener out side the if condition.
.You're coding with a lot of redundancy and risking introducing errors, some of which may not be immediately obvious. Here's a leaner and clearer version of your posted code sample, using one onClick method to handle your if(){else} conditions.
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(
getMovieCategory.equals("movies") ? R.drawable.ic_cancelar : R.drawable.ic_add, 0,0,0
);
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getMovieCategory.equals("movies")) {
String movies = "movies";
dbh.updateCategoryMovies(birthdayId, movies);
} else {
dbh.deleteMovies(getMovieCategory);
}
}
});
EDIT 1: I mistakenly left out your String movies = "movies" and have added it back in. If you're only going to use this String once, to pass it as a parameter for dbh.updateCategoryMovies(birthdayId, movies), then you could simply call dbh.updateCategoryMovies(birthdayId, "movies") and delete String movies = "movies"
EDIT 2: Without seeing your entire source code, I don't know if you're at all changing the value of getMovieCategory somewhere. If not, then the below sample will change it on every click of the button (once you launch the app to test the sample, your buttonCategoryMovie will be a generic Button; once you start clicking the button, it should toggle between showing your R.drawable.ic_cancelar and R.drawable.ic_add resources. If the button toggles between these two drawables, then you at least know that the button is in fact receiving the onClick.
Remember that, if the value of getMovieCategory doesn't change on each click, your button will always perform only one of the sets of onClick actions and it'll look like nothing's happening.
If it works for you as I've described, then you're on your way ;)
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getMovieCategory.equals("movies")) {
getMoviesCategory = "not movies"; // this is here just to test
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_cancelar, 0, 0, 0);
String movies = "movies";
dbh.updateCategoryMovies(birthdayId, movies);
} else {
getMoviesCategory = "movies"; // this is here just to test
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_add, 0, 0, 0);
dbh.deleteMovies(getMovieCategory);
}
}
});
Try changing android:visibility="gone" to android:visibility="visible" in your LinearLayout.
And again add android:clickable="true" to your LinearLayout