I have a Layout that has a button that shows another view, but only for 1 time, I mean, you click the button, it dissapear and the other view is shown, the second time you should click the parent view of that button and the other view (the one that was shown) should dissapear and the button should appear again. I am trying with clickable:false and focusable:false but it is not working. How can I achieve that?
Relevant Code
XML
<LinearLayout
android:id="#+id/item_tournament_header"
android:background="#drawable/bg_card_tournament"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="15">
<RelativeLayout
android:clickable="false"
android:focusable="false"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent">
<com.github.siyamed.shapeimageview.CircularImageView
android:id="#+id/item_friend_img_profile_pic"
android:layout_height="48dp"
android:layout_width="48dp"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
android:src="#drawable/ic_profile"
app:siBorderColor="#color/white"/>
</RelativeLayout>
<LinearLayout
android:clickable="false"
android:focusable="false"
android:layout_weight="10"
android:layout_width="0dp"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:id="#+id/tournament_name"
android:textSize="#dimen/text_h3"
android:textStyle="bold"
android:textColor="#color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tournament_client"
android:textSize="#dimen/text_p"
android:textColor="#color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<RelativeLayout
android:clickable="false"
android:focusable="false"
android:layout_weight="2"
android:layout_width="0dp"
android:orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:id="#+id/btn_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/ic_plus_tournaments"/>
</RelativeLayout>
</LinearLayout>
Java
btn_plus = (ImageView) findViewById(R.id.btn_plus);
TournamentContent =(LinearLayout)findViewById(R.id.item_tournament_content);
TournamentHeadaer =(LinearLayout)findViewById(R.id.item_tournament_header);
btn_plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TournamentContent.setVisibility(View.VISIBLE);
btn_plus.setVisibility(View.GONE);
}
});
TournamentHeadaer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(btn_plus.getVisibility()==View.VISIBLE)
{
// It's not entering here!!!
TournamentContent.setVisibility(View.GONE);
btn_plus.setVisibility(View.VISIBLE);
}
}
});
btn_plus = (ImageView) findViewById(R.id.btn_plus);
TournamentContent =(LinearLayout)findViewById(R.id.item_tournament_content);
TournamentHeadaer =(LinearLayout)findViewById(R.id.item_tournament_header);
btn_plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TournamentContent.setVisibility(View.VISIBLE);
btn_plus.setVisibility(View.GONE);
}
});
TournamentHeadaer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(btn_plus.getVisibility()==View.GONE)
{
// It's not entering here!!!
TournamentContent.setVisibility(View.GONE);
btn_plus.setVisibility(View.VISIBLE);
}
}
});
Maybe i miss understood the problem, but i think that your problem is the logic of if statement : if(btn_plus.getVisibility()==View.VISIBLE)
that should be :
if(TournamentContent.getVisibility()==View.VISIBLE) or if(btn_plus.getVisibility()==View.GONE)
Try to modify your code like this:
btn_plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TournamentContent.setVisibility(View.VISIBLE);
btn_plus.setVisibility(View.INVISIBLE);
}
});
TournamentHeadaer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(btn_plus.getVisibility()==View.INVISIBLE)
{
TournamentContent.setVisibility(View.GONE);
btn_plus.setVisibility(View.VISIBLE);
}
}
});
Related
I have LinearLayout which has 2 children which are used only as a preview. LinearLayout functionality here is same as Button. I want to disable any interaction with its children but make LinearLayoutclickable by using onClickListener. Currently if I click on Spinner it will not trigger onClickListener of LinearLayout. I also called spinner.isEnabled = false in code.
Layout:
<LinearLayout
android:id="#+id/clickableLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center_vertical">
<Spinner
android:id="#+id/picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:enabled="false"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false">
</Spinner>
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:textColor="#color/colorBlack"
android:textSize="14dp"
android:enabled="false"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:singleLine="true"
android:duplicateParentState="true"
android:maxLength="9"/>
</LinearLayout>
Make on Click Listener for All of the views and then call your function, so it does not matter on which view you click. It will always trigger that function.
LinearLayout linearLayout;
TextView textView;
Spinner spinner;
linearLayout = findViewById(R.id.clickableLayout);
spinner = findViewById(R.id.picker);
textView = findViewById(R.id.text1);
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myFunction();
}
});
spinner.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myFunction();
}
});
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myFunction();
}
});
}
private void myFunction() {
Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_LONG).show();
}
The problem is next, when I typed in ImageView mIV_WishItem "heart", caused onClick background ImageView mIV_WishItem
The code this buttons:
holder.mIV_WishItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setFavorites(holder, sectionsItem, true);
}
});
holder.mIV_pictureGoodsItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SectionsItem sectionsItem = sectionsItems.get(position);
sectionItemId = sectionsItem.getId();
getDataForItemDetails(mContext);
}
});
And xml for this buttons
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="#dimen/_200sdp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="end"
>
<ImageView
android:id="#+id/iV_Wish"
android:layout_width="#dimen/bottom_navigation_icon"
android:layout_height="#dimen/bottom_navigation_icon"
/>
</LinearLayout>
<ImageView
android:id="#+id/iV_pictureGoods_Item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
/>
</RelativeLayout>
Please help me find solution
You need to add attribute in your ImageView(mIV_WishItem) :
android:focusable="true"
android:focusableInTouchMode="true"
Viewholder item itself focusable, so we need to set focus on imageview. After that we can enable click on imageview.
I have a class wich extends ListActivity. Inside there are 3 buttons and a listview (clickable items), list items and 2 of the buttons work fine but one doesn't respond to clicks (it is created exactly the same way as the others). I think it is something related with the focusability of the button, i have already tried android:focusable="true" and "false" but nothing changes.
this is my xml:
<?xml version="1.0" encoding="utf-8"?>
<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">
<ListView
android:id="#android:id/list"
android:layout_below="#+id/checkall"
android:layout_width="match_parent"
android:layout_height="400dp" >
</ListView>
<Button
android:id="#+id/checkall"
android:layout_height="38dp"
android:layout_width="wrap_content"
android:layout_marginLeft="19dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="Seleccionar todos"
android:background="#D1D1D1" />
<Button
android:id="#+id/uncheckall"
android:layout_height="38dp"
android:layout_width="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/checkall"
android:layout_alignParentTop="true"
android:text="Borrar selección"
android:background="#D1D1D1"
android:layout_alignParentRight="true" />
//this is the one not accepting clicks -->
<Button
android:id="#+id/mostrar"
android:layout_height="49dp"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:text="Mostrar en mapa"
android:background="#D1D1D1" />
</RelativeLayout>
There is no problem in your layout and it works fine with this code-behind:
public class MyActivity extends ListActivity {
Button checkall, uncheckall, mostrar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
checkall = (Button) findViewById(R.id.checkall);
uncheckall = (Button) findViewById(R.id.uncheckall);
mostrar = (Button) findViewById(R.id.mostrar);
checkall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MyActivity.this, "checkall is clicked", Toast.LENGTH_SHORT).show();
}
});
uncheckall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MyActivity.this, "uncheckall is clicked", Toast.LENGTH_SHORT).show();
}
});
mostrar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MyActivity.this, "mostrar is clicked", Toast.LENGTH_SHORT).show();
}
});
}
Try to 'clean' and 'Rebuild' your project or post your code here
I am trying to show text with a button click.
Here is my onClick code:
public class Uteliv extends Activity {
public void onCrate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uteliv);
TextView tidiTekst = (TextView) findViewById(R.id.tidiTekst);
tidiTekst.setVisibility(View.GONE);
Button tidiKnapp= (Button) findViewById(R.id.tidiKnapp);
tidiKnapp.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tidiTekst.setVisibility(View.VISIBLE);
}
});
}
}
What is wrong? When I test it on my phone I only get a blank page.
Unless this was a typo in your post, your problem is you haven't declared the proper Activity method
public void onCrate(Bundle savedInstanceState) {
should be
public void onCreate(Bundle savedInstanceState) {
Also, you should probably make your Views member variables (declare them before onCreate() and initialize them inside of onCreate())
Edit
To show/hide your TextView you can use getVisibility to determine what to do. So it would be something like
public void onClick(View v) {
tidiTekst.setVisibility((tidiTekst.getVisibility() == View.Visible)
? View.GONE : View.VISIBLE);
}
activity_Main.xml
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:padding="10dip" >
<Button
android:id="#+id/mybtn"
style="?buttonBarButtonStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_weight="1"
android:background="#c4c5c7"
android:shadowColor="#959597"
android:shadowDx="0"
android:shadowDy="1"
android:shadowRadius="1"
android:text="Button"
android:textColor="#ffffff"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="#+id/myTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="hidden"
android:visibility="gone"/>
</LinearLayout>
code for MainActivity.java
private TextView mytxtvw;
private Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mytxtvw=(TextView)findViewById(R.id.myTextView);
myButton=(Button)findViewById(R.id.mybtn);
onBtnClick();
}
public void onBtnClick()
{
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mytxtvw.setVisibility((mytxtvw.getVisibility() == View.VISIBLE)
? View.GONE : View.VISIBLE);
}
});
}
}
Define your button and textView variables outside of the on-create method. Until you post the error you are getting, I'm going to assume that you are getting a null pointer because they are going out of scope once onCreate is finished.
TextView tidiTekst;
Button tidiKnapp;
public void onCrate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uteliv);
tidiTekst = (TextView) findViewById(R.id.tidiTekst);
tidiTekst.setVisibility(View.GONE);
tidiKnapp= (Button) findViewById(R.id.tidiKnapp);
tidiKnapp.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tidiTekst.setVisibility(View.VISIBLE);
}
});
}
Is it working if you decalre your textView as final ?
final TextView tidiTekst = (TextView) findViewById(R.id.tidiTekst);
Please check your R.layout.activity_uteliv in a WYSIWYG IDE such as Eclipse or IntelliJ IDEA, edit it in graphic mode, it must be something wrong with the layout(xml).
In xml :
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_marginTop="#dimen/_6sdp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MORE"
android:textStyle="bold"
android:textColor="#color/darkgray"
android:textSize="#dimen/_10sdp"
android:gravity="left"
android:id="#+id/moreone"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/darkgray"
android:layout_marginTop="#dimen/_6sdp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_marginTop="#dimen/_9sdp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="16) I will not indulge in any way in any act that is against the spirit of law and society. "
android:textStyle="bold"
android:textColor="#color/darkgray"
android:textSize="#dimen/_10sdp"
android:gravity="left"
android:id="#+id/tandcone"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_marginTop="#dimen/_6sdp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="17) I will treat all with dignity irrespective and provide them with high level of service and remedies for their dissatisfaction any time during the course of my business as an Independent Associate."
android:textStyle="bold"
android:textColor="#color/darkgray"
android:textSize="#dimen/_10sdp"
android:gravity="left"
android:id="#+id/tandctwo"/>
</LinearLayout>
In MainActivity :
moreone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tandcone.setVisibility((tandcone.getVisibility() == View.VISIBLE)
? View.GONE : View.VISIBLE);
tandctwo.setVisibility((tandctwo.getVisibility() == View.VISIBLE)
? View.GONE : View.VISIBLE);
}
});
I have an image view, i want to to create two buttons which will enable users to go to next or previous image. When a user taps the image , the buttons should appear and on the second tap , the button should disappear. How to go about doing this ?
Fortunately currently I am working on the same thing and here is my code modify it to suit your needs
xml code
<?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/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<LinearLayout
android:id="#+id/button_holder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:visibility="gone">
<Button
android:id="#+id/buy"
android:text="Buy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="#+id/cancel"
android:text="Cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
Main code
Button mBuy = (Button) findViewById(R.id.buy);
mBuy.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
Button mCancel = (Button) findViewById(R.id.cancel);
mCancel.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
finish();
}
});
ImageView mView = (ImageView) findViewById(R.id.image);
mView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
LinearLayout mLayout = (LinearLayout) findViewById(R.id.button_holder);
if(!isVisible)
{
isVisible = true;
mLayout.setVisibility(View.VISIBLE);
}
else
{
isVisible = false;
mLayout.setVisibility(View.GONE);
}
}
});