I have 2 button invite and share, if i click on invite linearlayout bar1 will appear which contains 4 imageviews, and for share button also same linearlayout bar2 within that 4 imageview options, if i click on invite and share button both the layout bar appear, but for me when i click on invite or share only one corresponding bar should appear at a time...
if I understand you correctly something like this will do the trick:
invite.setOnClickListener(new OnClickListener(){
public void onClick(View v){
linearlayoutbar1.setVisibility(View.VISIBLE);
linearlayoutbar2.setVisibility(View.GONE);
}
});
share.setOnClickListener(new OnClickListener(){
public void onClick(View v){
linearlayoutbar2.setVisibility(View.VISIBLE);
linearlayoutbar1.setVisibility(View.GONE);
}
});
Insert LinearyLayout upon your requirements
<merge>
<LinearLayout
android:id="#+id/main"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:visibility="gone"
/>
<LinearLayout
android:id="#+id/sub"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:visibility="gone"
/>
</merge>
depending upon your invite and share button you can put these code invite.setOnClickListener() or share.setOnClickListener()
Insert Visibility of LinearLayout according to your Logic
LinearLayout mainLayout=(LinearLayout)this.findViewById(R.id.main);
LinearLayout subLayout=(LinearLayout)this.findViewById(R.id.sub);
invite.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
mainLayout.setVisibility(View.VISIBLE);
}
});
share.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
subLayout.setVisibility(View.VISIBLE);
}
});
Related
XML Layout Here This is my layout. I want everything after the button to be invisible until the button is tapped.
/**
* This activity toggles open the order_form layout AFTER "Order" Button is clicked.
*/
Button properties = (Button) findViewById(R.id.order_button);
// and LinearLayout to toggle
final LinearLayout propLayout = (LinearLayout) findViewById(R.id.order_form);
properties.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
propLayout.setVisibility((propLayout.getVisibility() == View.VISIBLE)
? View.INVISIBLE
: View.VISIBLE);
}
});
place and visibility attribute in xml
like so
android:visibility="invisible"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="invisible">
<-- edittext views here-->
<EditText
......./>
</LinerLayout>
I spent an hour trying to add "Load More" Button and indeterminate ProgressBar to the footer of my ListView.
The supposed scinario works like this:
When the button is clicked, ProgressBar is shown while AsyncTask is downloading 20 items. when the items are inserted to the ListView, the ProgressBar dismisses and the Button appears again in the footer.
I come to the following solution and would be nice if you have better solution:
layout/progress_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/load_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
/>
And assuming you have the following fields in the Activity:
private ListView listview;
private Button loadMoreButton;
private ProgressBar progressBar;
after preparing your list view add the footer like this, (at the end of the activity.onCreate()):
loadMoreButton = new Button(this);
loadMoreButton.setText("Load More");
progressBar = (ProgressBar) LayoutInflater.from(this).inflate(R.layout.progress_bar, null);
loadMoreButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//downloadItems();
listview.removeFooterView(loadMoreButton);
listview.addFooterView(progressBar);
}
});
When the data is ready (either first page or subsequent pages), call the following after adapter update.
//adapter.notifyDataSetChanged();
listview.removeFooterView(progressBar);
listview.addFooterView(loadMoreButton);
If you have better solutions, please share it in the answers.
Instead of removing and adding a view as footer.. you can have both button and progressbar in the same footer view and make visibility VISIBLE and Visibility GONE according to the requirement.
I came to a solution without adding/removing the footer. Just put the two views (Load More Button and ProgressBar) in LinearLayout. Add the linearlayout as listview footer for first time only. Then change between the button and progressbar by changing visibility property.
Here is the updated code:
layout/list_footer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/load_more_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Load More"/>
<ProgressBar android:id="#+id/load_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
android:indeterminate="true" />
</LinearLayout>
call this method at the end of Activity.onCreate() to add footer and handle button click (load data, hide button and show progress).
private void prepareListFooter() {
View view = (View) LayoutInflater.from(this).inflate(R.layout.list_footer, null);
loadMoreButton = (Button) view.findViewById(R.id.load_more_button);
progressBar = (ProgressBar) view.findViewById(R.id.load_progress);
listview.addFooterView(view);
loadMoreButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//downloadMore();
loadMoreButton.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
}
});
}
also this code after adapter change to show the button and hide the progress.
adapter.notifyDataSetChanged();
loadMoreButton.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
I'm a novice android developper and was wondering:
Could I have 2 buttons to be linked into the same, 1 onClick method (which i'll presumably override to accept 2 extra parameter, int btnId and View targetTextView for instance) in order to decide which button is calling the method and then which TextView text to update?
For Example:
btn1 will update the text on text_view_1
and btn2 will update text_view_2.
Except they we will be linked to the same method:
public void generalOnClick(View view, String btnId, String textViewId){...}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One"
android:onClick="btnClick"/>
<TextView
android:id="#+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Two"
android:onClick="btnClick"/>
</LinearLayout>
Button Click Function in your Activity
public void btnClick(View view) {
TextView tv = (TextView)view;
int id = tv.getId();
if(id==R.id.one) {
tv.setText("One Clicked");
} else if(id==R.id.two){
tv.setText("Two Clicked");
}
}
set an tag to button and verify it with onClick method that which buttons click event has been triggered , if it doesn't work then follows following trick,
define an common method for the functionality which you are going to execute on button click, make it as common function.
define independent onclick event for both button and then while calling the common function which created above pass some unique param and verify it.
use following code:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View.OnClickListener clickListener = new View.OnClickListener() {
public void onClick(View v) {
if(v.getId().Equals(btn1.getId())){
//do your work here
}
else{
//work for 2nd button
}
};
btn1 = (Button)findViewById(R.id.btn_1);
btn2 = (Button)findViewById(R.id.btn_2);
btn1.setOnClickListener(clickListener);
btn2.setOnClickListener(clickListener);
}
I create a button and TextView when I press the button to visible the TextView its working good but my question was when same button pressed invisible the TextView how can i do this? This is my code:
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
/>
<TextView
android:id="#+id/pas_rules"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:text="welcome"
android:visibility="gone"/>
main activity:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
textview.setVisibility(View.VISIBLE);
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
textview.setVisibility(textview.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
// which is just like:
/*
if(textview.getVisibility() == View.VISIBLE)
textview.setVisibility(View.GONE);
else
textview.setVisibility(View.VISIBLE);
*/
}
});
you can achieve this by checking the visibility of the view:-
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(textview.getVisibility()==View.GONE)
{
textview.setVisibility(View.VISIBLE);
}
else
{
textview.setVisibility(View.GONE);
}
}
});
Another Way
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="Button_Click"
android:clickable="true"
/>
<TextView
android:id="#+id/pas_rules"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:text="welcome"
android:visibility="gone"/>
//Now Declare Button_Click Function in your Java Class
public void Button_Click(View i)
{
textview.setVisibility(textview.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
// Do your Code in here
}
You can check whether your Textview is visible or not if you click on the button. If it's visible, you can remove the visibility, if not, you can make it visible. You can find a solution here: How to check TextView Visibility using IF
You have to use the textview.getVisibility() method and check it to View.VISIBLE. If it is visible you have to set your textview invisible: textview.setVisibility(View.GONE);
Your code could look like:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (textview.getVisibility()==View.VISIBLE){
textview.setVisibility(View.GONE);
}
else{
textview.setVisibility(View.VISIBLE);
}
});
This Question Is Pretty Silly to ask but out, but i Would like to know how it Works?
I had a Relative Layout with 2 ImageViews as Child having separate clickListner instances. One above Another, of same Size and Attributes.
Overlaps Each other. Both having Different images.
Question is When i click on one image both ImageView Click listners are Called.
Or if i disable the Click on ImageView Top, The ImageView Below Still Works, I was Clicking on Image View Above though. How it is I'ts Getting callback from both.
I Just Want to know How it works? not The code, i do not have any issue writing code for clickListners Whether only one Working or Both.
<RelativeLayout
----
---
>
<ImageView
---
---<!--Child 1-->
<ImageView
---
---<!--Child 2-->
<RelativeLayout/>
Taken from here:
if you are working with just one clicklistener, you can do:
View.OnClickListener myOnlyhandler = new View.OnClickListener() {
public void onClick(View v) {
switch(v.getId()) {
case R.id.b1:
// it was the first button
break;
case R.id.b2:
// it was the second button
break;
}
}
}
Use ImageButton
Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Activity class
public class MainActivity extends Activity {
ImageButton imgButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addButtonListener();
}
public void addButtonListener() {
imgButton = (ImageButton) findViewById(R.id.imageButton);
imgButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"ImageButton is working!", Toast.LENGTH_SHORT).show();
}
});
}
}
Give this to your parent layout
android:context="Yourclasshere"
then give this to your image view
android:onclick="onclick"
and then implement the on click listener or make the method like Vitly A make above