Android: OnClick method for Button doesn't respond - android

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

Related

How start New Activity when Click on Title Of HEADER_VIEW in RecyclerView To display items in IT

enter image description here
In Activty one i have RecyclerView with to View :
HEADER_VIEW
ITEM_VIEW
Header_view.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="?attr/actionBarSize"
android:background="#FFF7F7F7">
<TextView
android:id="#+id/tv_group_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:layout_weight="1"
android:text="TextView"
android:textColor="#F50057"
android:textSize="20sp" />
<TextView
android:id="#+id/more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:text="More..." />
</RelativeLayout>
Item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/ll_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:elevation="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:src="#mipmap/ic_launcher" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_below="#id/imageView"
android:background="#f5fff2">
<TextView
android:id="#+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="name"
android:textAlignment="center"
android:textColor="#FF20C200"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/new_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/old_price"
android:layout_marginRight="5dp"
android:layout_marginStart="5dp"
android:layout_toEndOf="#+id/old_price"
android:text="480.00 DA"
android:textColor="#FF65C2FD"
android:textStyle="bold" />
<TextView
android:id="#+id/old_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/tv_name"
android:layout_marginBottom="10dp"
android:layout_marginRight="5dp"
android:layout_marginStart="5dp"
android:text="250.00 DA"
android:textAlignment="center"
android:textColor="#F50057" />
<ImageButton
android:id="#+id/overflow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/new_price"
android:layout_marginEnd="5dp"
android:layout_marginLeft="5dp"
android:background="?attr/selectableItemBackgroundBorderless"
app:srcCompat="#drawable/ic_more_vert_black_24dp" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
I created the Adapter and it Work Fine .
My problem is :
I need when I click on more TextView In HEADER_VIEW an New Activity Start
with Specific Itemes Of Stection.
My try :
In Adapter Exactly in onBindViewHolder
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
//Context context = mContextWeakReference.get();
if (mContext == null) {
return;
}
if (SECTION_VIEW == getItemViewType(position)) {
final SectionViewHolder sectionViewHolder =(SectionViewHolder)holder;
GroupTitleModel sectionItem = ((GroupTitleModel) mUsersAndSectionList.get(position));
sectionViewHolder.title.setText(sectionItem.title);
sectionViewHolder.more.setText(sectionItem.moreSection);
sectionViewHolder.more.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
// I TRY TO DO IT HERE BUT NO THINGS
}
} );
return;
}
My first thought is that you are bringing yourself into trouble by doing it this way.
You separate the Header view and its Content view, so the problem appear when the Header has no idea which content to continue to next step. If you don't want to refactor than it's ok, there will be a workaround. But my suggestion is that you combine the header and the content into 1 ViewHolder, keep the title, more text, and items inside 1 Model class too. They need to go with each other so they know how to handle with interaction

android studio zoom image in + horizontal scrolling

I am working on an android project, and for that I have a human model image:
I would like to zoom right in, so that you can only see one human model e.g. the middle one to start with, then have 2 arrows on either side of the image, and click left arrow to see the left human model, and right to see the right human model.
This is my attempt, but doesn't work
layout activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_home_page"
tools:context="com.example.lahirufernando.skinsensor.HomePage"
android:background="#e0e0e0">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textViewBottom"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#drawable/peace" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:background="#drawable/loginbox" />
<Button
android:id="#+id/ibLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#drawable/left_arrow" />
<Button
android:id="#+id/ibRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView"
android:layout_alignLeft="#+id/questionBtn"
android:layout_alignStart="#+id/questionBtn"
android:background="#drawable/right_arrow" />
<HorizontalScrollView
android:id="#+id/hsv"
android:layout_width="wrap_content"
android:fadingEdgeLength="100dp"
android:layout_toRightOf="#id/ibLeft"
android:layout_toLeftOf="#id/ibRight"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/humanbody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:scaleType="centerCrop"
android:background="#null" />
</LinearLayout>
</HorizontalScrollView>
<TextView
android:id="#+id/textViewBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:textColor="#FFFF"
android:text="Tip: You can zoom in and turn your device!"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/questionBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/bodyPart"
android:layout_marginEnd="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="35dp"
android:clickable="true"
app:backgroundTint="#FFFF"
app:fabSize="mini"
app:srcCompat="#drawable/question" />
<TextView
android:id="#+id/bodyPart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Click a Body Region"
android:textColor="#000000"
android:textSize="20dp" />
activity:
private final String TAG = getClass().getSimpleName();
HorizontalScrollView hSV;
Button ibLeft,ibRight;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
FloatingActionButton homeBtn = (FloatingActionButton) findViewById(R.id.homeButton);
FloatingActionButton questionButton = (FloatingActionButton) findViewById(R.id.questionBtn);
hSV = (HorizontalScrollView) findViewById(R.id.hsv);
ibLeft=(Button) findViewById(R.id.ibLeft);
ibLeft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
hSV.scrollTo((int)hSV.getScrollX() - 10, (int)hSV.getScrollY());
}
});
ibRight=(Button) findViewById(R.id.ibRight);
ibLeft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
hSV.scrollTo((int)hSV.getScrollX() + 10, (int)hSV.getScrollY());
}
});
// Add image
ImageView image = (ImageView) findViewById(R.id.imageView);
image.setImageResource(R.drawable.human);

Click TextView inside linear layout inside ScrollLayout

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

how to hide nested layout in android?

I have an linear layout in my application,In that i am using one root layout(invitation_single) and one nested layout(hidden).when i am onclick root layout at run time the nested layout visible(it contains"yes,no,maybe" buttons)successfully.Now my need is if i have similar layout values(consider event 2) immediately below the previous layout values(consider event 1),when i am on click "event 2" i need to hide nested layout of "event 1"(atpresent when i am click button the nested layout gone)automatically.how can i achieve this..here is my layout code,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/invitation_single"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="?android:dividerVertical"
android:dividerPadding="5dp"
android:showDividers="middle"
tools:context=".MainActivity">
<ImageButton
android:id="#+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_action_event" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:clickable="false"
android:focusable="true"
android:orientation="vertical">
<TextView
android:id="#+id/invitation_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="0dp"
android:paddingTop="3dp"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:id="#+id/invitation_place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="0dp"
android:textColor="#color/black"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/hidden"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginLeft="-270dp"
android:layout_marginTop="60dp"
android:layout_weight="1"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:paddingTop="1dp"
android:visibility="gone"
android:weightSum="3">
<Button
android:id="#+id/yesbutton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="7dp"
android:layout_weight="1"
android:background="#color/blue"
android:text="Yes"
android:textColor="#color/black"></Button>
<Button
android:id="#+id/nobutton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="25dp"
android:layout_weight="1"
android:background="#color/blue"
android:text="No"
android:textColor="#color/black"></Button>
<Button
android:id="#+id/buttonmaybe"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="#color/blue"
android:text="Maybe"
android:textColor="#color/black"></Button>
</LinearLayout>
</LinearLayout>
my programming code is below,
final LinearLayout first = (LinearLayout) convertView.findViewById(R.id.invitation_single);
final LinearLayout second = (LinearLayout) convertView.findViewById(R.id.hidden);
first.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.invitation_single:
second.setVisibility(View.VISIBLE);
break;
}
}
});
is this what you're looking for?
first.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.invitation_single:
second.setVisibility(second.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
break;
}
}
});

Issue when using Scrollview inside Dialog

I am inflating a layout inside a dialog, which consist of a scrollview but the layout is not scrolling. I have referred so many questions from stackoverflow but then also its not working. My layout.xml is given below:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#color/White"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/White"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:text="Update available"
android:textColor="#000000" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center"
android:text="#string/upgrade_msg"
android:textColor="#000000" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_margin="3dp"
android:layout_weight="0.02"
android:src="#drawable/updateimg2" />
<Button
android:id="#+id/btn_upgrade"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_forget_pwd"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#color/White"
android:gravity="center"
android:text="Upgrade Now"
android:textColor="#color/Black" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="#drawable/line_y_h" />
<Button
android:id="#+id/btn_remind"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_forget_pwd"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#color/White"
android:gravity="center"
android:text="Remind Me Later"
android:textColor="#color/Blue" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="#drawable/line_y_h" />
</LinearLayout>
</ScrollView>
Dialog code
public void show_Alert_version_custom(String str, final String url,
final String exitStatus)// use for
{
final Dialog dialog = new Dialog(Splash.this);
dialog.setContentView(R.layout.version_update);
dialog.setTitle(getString(R.string.alert_title));
Button dialogButton2 = (Button) dialog.findViewById(R.id.btn_remind);
dialogButton = (Button) dialog.findViewById(R.id.btn_upgrade);
ImageView viewLine = (ImageView) dialog.findViewById(R.id.imageView2);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {}
});
// if button is clicked, close the custom dialog
dialogButton2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {}
});
dialog.show();
}
android:fillViewport="true" in ScrollView solved my problem. Now its scrolling even inside dialog.
You can use simply below code on ScollView. it's working fine.
<ScrollView>
<LinearLayout android:orientation="vertical"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
</LinearLayout>
</ScrollView>

Categories

Resources