Show/hide text with button - android

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);
}
});

Related

How to make your parent clickable but children not?

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();
}

Android: How to catch click on any button in Linear Layout?

I have the following Layout:
<LinearLayout
android:id="#+id/group_button_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:layout_centerHorizontal="true">
<Button
android:id="#+id/group_button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="Week"/>
<Button
android:id="#+id/group_button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:padding="0dp"
android:text="Month"/>
<Button
android:id="#+id/group_button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="Quarter"/>
<Button
android:id="#+id/group_button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="Half Year"/>
</LinearLayout>
I created the onClickListener on layout like that:
private void initViews() {
mGroupedButtonLayout = (LinearLayout) findViewById(R.id.group_button_layout);
}
private void initListeners() {
mGroupedButtonLayout.setOnClickListener(mButtonGroupListener);
}
And I thought that after click on a button onClick event will be triggered:
private View.OnClickListener mButtonGroupListener = new View.OnClickListener() {
public void onClick(View v) {
Logger.d("Clicked");
Logger.d(String.valueOf(v.getId()));
// do something when the button is clicked
}
};
Is it possible to catch onClick event on every button without the need to create a listener for each button?
You could set the android:onClick attribute for the buttons, and handle your logic in the method you point it to.
XML
<Button
android:id="#+id/group_button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="Week"
android:onClick="doButtonClick"/>
Java
public void doButtonClick(View v) {
switch(v.getId()) {
case R.id.group_button_1:
//do whatever for this button
break;
default:
break;
}

How to make the parent view to handle all the OnClick events?

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);
}
}
});

OnClick crashes my application

I have a textview in dialog and I want when I click this textview to do something.
I want it when clicking on the textview to do something.
I have added clickable and onClick in my layout
Here are my codes
public class Activitytwo extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//WORKING CODES
//WORKING CODES
//WORKING CODES
//WORKING CODES
}
// This one to show the dialog
//This one works fine!
public void share(View view){
sharedialog = new Dialog(Activitytwo.this);
sharedialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
sharedialog.setContentView(R.layout.postdialog);
sharedialog.setCanceledOnTouchOutside(true);
sharedialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
sharedialog.show();
}
public void facebookp(View view){
//This one for the TextView (onClick)
//This one doesn't work..
}
}
This is my layout (postdialog.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/postdialogl"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_height="150dp"
android:background="#drawable/postdialog" >
<TextView
android:id="#+id/postdtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:textSize="18sp"
android:textStyle="bold"
android:text="#string/post"
android:textColor="#a7b8d6"/>
<ImageView
android:id="#+id/poststrokea"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#202e4c"
android:layout_below="#+id/postdtitle"
android:layout_marginTop="5dp"/>
<ImageView
android:id="#+id/poststrokeb"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#803e5482"
android:layout_below="#+id/poststrokea"/>
<TextView
android:id="#+id/facebookpost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="60dp"
android:layout_marginLeft="30dp"
android:drawablePadding="5dp"
android:text="#string/facebook"
android:textColor="#a7b8d6"
android:textSize="12sp"
android:drawableTop="#drawable/facebook"
android:clickable="true"
android:onClick="facebookp"/>
</RelativeLayout>
One solution is to declare your textview inside your dialog.
public void share(View view){
//your code.
sharedialog = new Dialog(Activitytwo.this);
sharedialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
sharedialog.setContentView(R.layout.postdialog);
sharedialog.setCanceledOnTouchOutside(true);
sharedialog.getWindow().setBackgroundDrawable(newColorDrawable(android.graphics.Color.TRANSPARENT));
final TextView tviPost = (TextView) sharedialog.findViewById(R.id.sharedialog);
tviPost.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
//Do something
}
});
sharedialog.show();
}
and delete the next lines from your layout.
android:clickable="true"
android:onClick="facebookp"

onclick event in dialog preference

I have a custom dialog preference that have bunch of button.each button represent a number.something like clock application when you want to set alarm. how can I get onClick of each button in my dialog preference?
this is part of my dialog layout (time_picker) :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<LinearLayout
android:id="#+id/showTimer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="_ _ : _ _"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<RelativeLayout
android:id="#+id/Buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_below="#+id/showTimer"
android:layout_marginTop="10dp">
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button5"
android:layout_alignBottom="#+id/button5"
android:layout_alignLeft="#+id/button3"
android:layout_centerInParent="true"
android:text="6"
android:textStyle="bold"
android:textSize="20dp"
android:tag="1"
android:background="#android:color/transparent" />
<Button
android:id="#+id/button2"
android:layout_width="100dp"
...
this is Setting layout that launch dialog preference:
<com.example.test.TimerForNoti
android:key = "pref_sync_noti_timer"
android:title = "#string/pref_sync_noti_timer"
android:summary = "#string/pref_sync_noti_timer_summ"
android:dialogMessage = "#string/pref_sync_noti_timer">
</com.example.test.TimerForNoti>
and this is class of dialog preference (TimerForNoti) :
public class TimerForNoti extends DialogPreference
{
public TimerForNoti(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.time_picker);
setPositiveButtonText(R.string.ok);
setNegativeButtonText(R.string.cancel);
Dialog di = new Dialog(context);
di.setContentView(R.layout.time_picker);
OnClickListener test = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String tag = v.getTag().toString();
if(tag == "1"){
Log.v("onOne", "ok");
}
// .....
}
};
Button btn1 = (Button) di.findViewById(R.id.button1);
btn1.setOnClickListener(test);
}
}
If I understand your question right, you can set tag for each button and set one OnClickListener for all of them:
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button5"
android:layout_alignBottom="#+id/button5"
android:layout_alignLeft="#+id/button3"
android:layout_centerInParent="true"
android:text="6"
android:textStyle="bold"
android:textSize="20dp"
android:tag="6"
android:background="#android:color/transparent" />
Code:
OnClickListener buttonsListener = new OnClickListener() {
#Override
public void onClick(View v) {
String buttonText = v.getTag().toString();
// your logic here
}
};
Button btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(buttonsListener);
Button btn2 = (Button)findViewById(R.id.button2);
btn2.setOnClickListener(buttonsListener);
Button btn3 = (Button)findViewById(R.id.button3);
btn3.setOnClickListener(buttonsListener);
// your buttons here
Also you can use v.getId() in OnClickListener for determining which button was clicked
As much I understood your question. You want one method to be called on every button clicked.
For that Add this to your every button element in xml:
<Button
android:onClick="OnClick"
.. />
And this method to your .java file where you want to perform task:
public void OnClick(View v){
// do something....
}
Or you can directly call setOnClickListener at button object
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// do something....
}
});

Categories

Resources