daimajia AndroidSwipeLayout Button click issue in Activity - android

I am using SwipeLayout in my activity.
Activity is having 2 buttons. On button click, I want to set respective fragment on bottom layout and swipe view programmatically. And on close, it should set replace bottom layout with default fragment so that on swipe it displays default fragment.
When I set default fragment in onClose method of swipeLayout's addSwipeListener, it does not listen to button click.
This works fine with RecyclerView but now I want same in activity
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:id="#+id/card_view"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardBackgroundColor="#android:color/white"
card_view:cardCornerRadius="6dp"
card_view:cardElevation="6dp"
xmlns:swipe="http://schemas.android.com/tools">
<com.daimajia.swipe.SwipeLayout
android:id="#+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Bottom View Start-->
<!--What you want to show-->
<LinearLayout
android:id="#+id/sub_item_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#android:color/holo_orange_light">
</LinearLayout>
<!-- Bottom View End-->
<!-- Surface View Start -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/frame_list_card_item"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9"
android:background="#eefeef"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is the Title this is the Title this is the Title this is the Title this is the Title "
android:textColor="#000"
android:textSize="15sp"/>
<LinearLayout
android:id="#+id/container_list_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="this is description text this is description text this is description text this is description text this is description text this is description text this is description text this is description text this is description text this is description text this is description text "
android:textSize="13sp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="#+id/btn_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="0dp"
android:text="4"/>
<Button
android:id="#+id/btn_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="0dp"
android:text="5"/>
</LinearLayout>
</LinearLayout>
<!--What you want to show in SurfaceView-->
</LinearLayout>
</LinearLayout>
<!-- Surface View End -->
</com.daimajia.swipe.SwipeLayout>
</android.support.v7.widget.CardView>
JAVA
package com.sp.spsamples3.activities;
public class SwipeLayoutActivity extends AppCompatActivity {
SwipeLayout mSwipeLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe_layout);
mSwipeLayout = (SwipeLayout) findViewById(R.id.swipe_layout);
getSupportFragmentManager().beginTransaction().replace(R.id.sub_item_container, new DefaultFragment()).commit();
mSwipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
mSwipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
#Override
public void onStartOpen(SwipeLayout layout) {
}
#Override
public void onOpen(SwipeLayout layout) {
}
#Override
public void onStartClose(SwipeLayout layout) {
}
#Override
public void onClose(SwipeLayout layout) {
getSupportFragmentManager().beginTransaction().replace(R.id.sub_item_container, new DefaultFragment()).commit();
}
#Override
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
}
#Override
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
}
});
initClicks();
}
private void initClicks() {
((Button) findViewById(R.id.btn_one)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("sp testing", "1 clicked");
setFragment(new ScreenOneFragment());
mSwipeLayout.open(true);
}
});
((Button) findViewById(R.id.btn_two)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("sp testing", "2 clicked");
setFragment(new ScreenTwoFragment());
mSwipeLayout.open(true);
}
});
}
private void setFragment(Fragment fragment) {
getSupportFragmentManager().beginTransaction().replace(R.id.sub_item_container, fragment).commit();
}
}

Related

How can I adjust the size deppending on the visible content in an android view?

I'm creating a list view in an android app and each item expands and collapses.
Here is each item's code:
<?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="wrap_content"
android:background="#drawable/alarme_layout"
android:backgroundTint="#color/colorPrimaryDark"
android:padding="20dp">
<TextView
android:id="#+id/textView_aHora"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:text="00:00"
android:textSize="26sp"
android:textColor="#color/colorOnPrimary"
android:layout_marginBottom="10dp"/>
<Switch
android:id="#+id/switch_aEstado"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<LinearLayout
android:id="#+id/layout_ultimaLinhaCollapsed"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView_aHora"
android:layout_alignParentStart="true">
<LinearLayout
android:id="#+id/layout_diasRepetir"
android:orientation="horizontal"
android:layout_width="0px"
android:layout_weight=".9"
android:layout_height="25dp">
</LinearLayout>
<ImageView
android:id="#+id/imageView_arrowDown"
android:layout_width="0px"
android:layout_weight=".1"
android:layout_height="wrap_content"
android:tint="#color/colorOnPrimary"
android:src="#drawable/ic_keyboard_arrow_down_24dp"
android:focusable="true"
android:clickable="true"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/layout_detalhesExpanded"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView_aHora">
<CheckBox
android:id="#+id/checkbox_repetir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="#string/repetir"
android:textColor="#color/colorOnPrimary"
android:foregroundTint="#color/colorSecondary"/>
<LinearLayout
android:id="#+id/layout_diasRepetirButtons"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkbox_repetir"
android:layout_alignStart="#+id/checkbox_repetir"
android:layout_marginTop="10dp"
android:visibility="invisible">
</LinearLayout>
<LinearLayout
android:id="#+id/ultimaLinhaExpanded"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/layout_diasRepetirButtons"
android:layout_marginTop="10dp">
<EditText
android:id="#+id/editText_mensagem"
android:layout_width="0px"
android:layout_weight=".9"
android:layout_height="wrap_content"
android:hint="#string/mensagemHint"
android:inputType="text"
android:backgroundTint="#color/colorOnPrimary"
android:foregroundTint="#color/colorSecondary"
android:textColor="#color/colorSecondary"
android:shadowColor="#color/colorSecondary"
android:textColorHighlight="#color/colorSecondaryLight" />
<ImageView
android:id="#+id/imageView_arrowUp"
android:layout_width="0px"
android:layout_weight=".1"
android:layout_height="wrap_content"
android:tint="#color/colorOnPrimary"
android:src="#drawable/ic_keyboard_arrow_up_24dp"
android:focusable="true"
android:clickable="true"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
And here is the "getView()" method of list view:
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.alarme, null);
final Alarme alarme = alarmes.get(position);
//main view
TextView hora = convertView.findViewById(R.id.textView_aHora);
Switch estado = convertView.findViewById(R.id.switch_aEstado);
LinearLayout layoutDiasRepetir = convertView.findViewById(R.id.layout_diasRepetir);
final ImageView arrowDown = convertView.findViewById(R.id.imageView_arrowDown);
//detalhes
final RelativeLayout layoutDetalhesExpanded = convertView.findViewById(R.id.layout_detalhesExpanded);
CheckBox cbRepetir = convertView.findViewById(R.id.checkbox_repetir);
LinearLayout layoutDiasRepetirButtons = convertView.findViewById(R.id.layout_diasRepetirButtons);
EditText editText = convertView.findViewById(R.id.editText_mensagem);
final ImageView arrowUp = convertView.findViewById(R.id.imageView_arrowUp);
layoutDetalhesExpanded.setVisibility(View.INVISIBLE);
hora.setText(alarme.getHora());
estado.setChecked(alarme.isLigado());
cbRepetir.setChecked(alarme.isRepete());
arrowDown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
arrowDown.setVisibility(View.INVISIBLE);
arrowUp.setVisibility(View.VISIBLE);
layoutDetalhesExpanded.setVisibility(View.VISIBLE);
alarme.setCollapsed(false);
}
});
arrowUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
arrowUp.setVisibility(View.INVISIBLE);
arrowDown.setVisibility(View.VISIBLE);
layoutDetalhesExpanded.setVisibility(View.INVISIBLE);
alarme.setCollapsed(true);
}
});
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
alarme.setMensagem(s.toString());
}
});
arrowDown.setVisibility(alarme.isCollapsed() ? View.VISIBLE : View.INVISIBLE);
arrowUp.setVisibility(alarme.isCollapsed() ? View.INVISIBLE : View.VISIBLE);
layoutDetalhesExpanded.setVisibility(alarme.isCollapsed() ? View.INVISIBLE : View.VISIBLE);
editText.setText(alarme.getMensagem());
return convertView;
}
I thought each item would have its size wrapped depending on the visible content, but I have this result instead:
This is the item when expanded:
https://imgur.com/gallery/oIv50dT
This is the item when collapsed:
https://imgur.com/gallery/dmzw5GU
I wanted the collapsed item to have its size wrapped to the content.
I am going to guess here that in your XML layout you have set a static height.
You need to set it to be wrap_content which allows for dynamic row heights.
I solved it by changing the visibility of the views from View.INVISIBLE to View.GONE.

SlidingDrawer underlap in RelativeLayout

I used a SlidingDrawer like in this tutorial
It works in a LinearLayout, but there is always a whitespace depending of the hight of the slider and all other stuff show up below the whitespace.
The soloution should be to use a Relative Layout. This deletes the whitespace but the handlerbutton from the slider seems to be under the content in the scrollview and don't do anything by clicking on it. See the screenshot
MainActivity.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="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/tbar"
layout="#layout/primerdivisor" />
<SlidingDrawer
android:id="#+id/SlidingDrawer"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:content="#+id/contentLayout"
android:handle="#+id/slideButton"
android:orientation="vertical"
android:padding="10dip"
android:rotation="180">
<!-- Handle button -->
<Button
android:id="#+id/slideButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_selector"
android:ems="10"
android:rotation="180"
android:text="Show" android:clickable="true"/>
<!-- Content Layout -->
<LinearLayout
android:id="#+id/contentLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFCC"
android:gravity="center"
android:orientation="vertical"
android:padding="10dip">
<Button
android:id="#+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="#drawable/button_selector"
android:text="Button 1"
android:textColor="#color/textBlack" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="#drawable/button_selector"
android:gravity="center"
android:padding="5dp"
android:text="Text View Item"
android:textColor="#color/textBlack" />
</LinearLayout>
</SlidingDrawer>
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:orientation="vertical">
....
MainActivity.java
...
private void createSlider(){
slideButton = (Button) findViewById(R.id.slideButton);
slidingDrawer = (SlidingDrawer) findViewById(R.id.SlidingDrawer);
b1 = (Button) findViewById(R.id.Button01);
textView = (TextView) findViewById(R.id.text);
// Setting Listeners to all buttons and textview
setListeners();
// Listeners for sliding drawer
slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
#Override
public void onDrawerOpened() {
// Change button text when slider is open
Log.i("----","blaaaaaa open");
slideButton.setText("Close");
}
});
slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {
#Override
public void onDrawerClosed() {
// Change button text when slider is close
slideButton.setText("Open");
}
});
}
// Listeners method
void setListeners() {
b1.setOnClickListener(this);
textView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// Toast shown on sliding drawer items click
if (v.getId() == R.id.text) {
Toast.makeText(MainActivity.this, textView.getText() + " Clicked",
Toast.LENGTH_SHORT).show();
} else {
Button b = (Button) v;
Toast.makeText(MainActivity.this, b.getText() + " Clicked",
Toast.LENGTH_SHORT).show();
}
}
...
The slider should overlap the scrollview when its whiped out
In view hierarchy terms, views defined at the beginning of your layout XML are "deeper" than those defined later.
In your case your ScrollView is added to the hierarchy after your SlidingDrawer so what your seeing is to be expected.
Move your SlidingDrawer to below your ScrollView and it should be above it.

How to delete space when I hide a CardView

How can I delete the space when I hide a CardView? I doing an app with android and Firebase and I show the information from firebase in cardviews, but when the info isn't correct the cardview must disappear. The point is that the cardview disappear, but still uses a space in the layout, I've tried the answers in
setVisibility(GONE) view becomes invisible but still occupies space But it doesn't work for me.
#Override
protected void onStart() {
super.onStart();
firebaseAuth.addAuthStateListener(firebaseAuthListener);
final FirebaseRecyclerAdapter<CursosDB, MainActivity.CursosPViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<CursosDB, MainActivity.CursosPViewHolder>(
CursosDB.class,
R.layout.design_row_cursos,
MainActivity.CursosPViewHolder.class,
myRef)
{
#Override
protected void populateViewHolder(MainActivity.CursosPViewHolder viewHolder, final CursosDB Cmodel, int position)
{
String estado = Cmodel.getSTATUS().toString();
if (estado.equals("OK")){
viewHolder.setPhotoURL(getApplicationContext(), Cmodel.getURI());
viewHolder.setTitle(Cmodel.getTITLE());
viewHolder.setCiudad(Cmodel.getPLACE());
viewHolder.setLevel(Cmodel.getLEVEL());
viewHolder.setDur(Cmodel.getDURACION());
viewHolder.setPrice(Cmodel.getCOSTO());
}else{
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.mView.getLayoutParams();
layoutParams.setMargins(0,0,0,0);
viewHolder.mView.setLayoutParams(layoutParams);
viewHolder.mView.setVisibility(View.INVISIBLE);
viewHolder.mView.setVisibility(View.GONE);
//viewHolder.mView.setVisibility(View.GONE);
//Toast.makeText(getApplicationContext(), "Ningún curso aprobado aún", Toast.LENGTH_SHORT).show();
}
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(getApplicationContext(), Cmodel.getUSERID(), Toast.LENGTH_LONG).show();
//Toast.makeText(getApplicationContext(), Cmodel.getTITLE(), Toast.LENGTH_SHORT).show();
//Toast.makeText(UsersList.this, user_key, Toast.LENGTH_LONG).show();
}
});
}
};
mCourses.setAdapter(firebaseRecyclerAdapter);
}
This is my layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardview_cursos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:onClick="positionAction"
android:padding="4dp"
android:paddingBottom="4dp"
android:paddingEnd="4dp"
android:paddingStart="4dp"
android:paddingTop="4dp"
app:cardElevation="4dp">
<LinearLayout android:id="#+id/layout_to_hide"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/Cimg"
android:layout_width="match_parent"
android:layout_height="165dp"
android:scaleType="center"
app:srcCompat="#drawable/knowit_logo" />
<TextView
android:id="#+id/Ctitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/Cimg"
android:layout_marginLeft="14dp"
android:layout_marginStart="14dp"
android:paddingTop="8dp"
android:text="Titulo"
android:textStyle="bold" />
<TextView
android:id="#+id/Clugar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Ctitle"
android:layout_alignStart="#+id/Ctitle"
android:layout_below="#+id/Ctitle"
android:layout_marginTop="10dp"
android:text="Lugar" />
<TextView
android:id="#+id/textView29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Clugar"
android:layout_alignStart="#+id/Clugar"
android:layout_below="#+id/Clugar"
android:layout_marginTop="10dp"
android:text="$" />
<TextView
android:id="#+id/Cprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView29"
android:layout_marginBottom="4dp"
android:layout_toEndOf="#+id/textView29"
android:layout_toRightOf="#+id/textView29"
android:paddingLeft="5dp"
android:text="0.0" />
<TextView
android:id="#+id/Cdur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/Clugar"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="126dp"
android:layout_marginRight="126dp"
android:text="Duración" />
<TextView
android:id="#+id/Cnivel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/Cprice"
android:layout_alignLeft="#+id/Cdur"
android:layout_alignStart="#+id/Cdur"
android:layout_marginBottom="0dp"
android:text="Nivel" />
</RelativeLayout>
</LinearLayout> </android.support.v7.widget.CardView>
This is the mView:
public static class CursosPViewHolder extends RecyclerView.ViewHolder {
View mView;
public CursosPViewHolder(final View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title){
TextView post_title = (TextView)mView.findViewById(R.id.Ctitle);
post_title.setText(title);
}
public void setCiudad (String ciudad){
TextView post_city = (TextView)mView.findViewById(R.id.Clugar);
post_city.setText(ciudad);
}
public void setPhotoURL(Context ctx, String pgotouserurl) {
ImageView post_image =(ImageView)mView.findViewById(R.id.Cimg);
Picasso.with(ctx).load(pgotouserurl).into(post_image);
} }
I hope you can help me.
First the XML code you've posted ils wrong closed,(must finish with a CardView tag)
On addition be sure you're pointing the CardView layout with mView field ans Only use GONE or INVISIBLE,
INVISIBLE change your Panel ( Root view ) structure but GONE don't

Slide up/down layout on click

I am looking to slide/up down a nested layout on its parent layout click.
Ie the parent layout will have a hidden child. On click I would like the parents height to animate down (slide down) to fit the child layout. On click again I would like the child to animate up (slide up). Basically just animating the parents height to show/hide the child.
I have found this which looks to work but seems like a lot of code:
http://gmariotti.blogspot.com/2013/09/expand-and-collapse-animation.html
I have seen a lot of things using 'animateLayoutChanges' to animate things however I cannot get that to work.
I have tried this:
<LinearLayout android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout android:id="#+id/child"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:animateLayoutChanges="true">
<TextView android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text to show/hide"/>
</LinearLayout>
</LinearLayout>
Then in code:
LinearLayout parent = (LinearLayout)findViewById(R.id.parent);
parent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout child = (LinearLayout)findViewById(R.id.child);
child.setVisibility(child.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
});
That sets the visibility of the child view correctly but there is absolutely no animation.
Well, first of all android:animateLayoutChanges effects the child elements. So, obviously, if you are changing the properties of the element itself, it will not be animated.
I think you can accomplish what you are trying to in API 16 by enabling the LayoutTransition.CHANGING animation.
<LinearLayout android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:animateLayoutChanges="true">
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"/>
<TextView android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="Some text to show/hide"/>
</LinearLayout>
LinearLayout parent = (LinearLayout)findViewById(R.id.parent);
parent.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
View text = findViewById(R.id.text);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View message = findViewById(R.id.message);
ViewGroup.LayoutParams params = message.getLayoutParams();
params.height = params.height == 0 ? ViewGroup.LayoutParams.WRAP_CONTENT : 0;
message.setLayoutParams(params);
}
});
Try to use SlidingDrawer
use this code
xml layout sideupdown.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/bopen"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:text="open" />
<Button
android:id="#+id/btogg"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:text="change" />
<Button
android:id="#+id/bclose"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:text="close" />
</LinearLayout>
<SlidingDrawer
android:id="#+id/slidingDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:content="#+id/content"
android:handle="#+id/handle" >
<Button
android:id="#+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Handle" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<CheckBox
android:id="#+id/chkbok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="On/Off" />
</LinearLayout>
</SlidingDrawer>
</FrameLayout>
java class SlideUpDown.java
public class SlideUpDown extends Activity implements OnClickListener,
OnCheckedChangeListener, OnDrawerOpenListener, OnDrawerCloseListener {
Button opn, cloz, chng, hndl;
CheckBox chkbox;
SlidingDrawer sd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sideupdown);
refference();
opn.setOnClickListener(this);
cloz.setOnClickListener(this);
chng.setOnClickListener(this);
chkbox.setOnCheckedChangeListener(this);
sd.setOnDrawerOpenListener(this);
sd.setOnDrawerCloseListener(this);
}
private void refference() {
opn = (Button) findViewById(R.id.bopen);
cloz = (Button) findViewById(R.id.bclose);
chng = (Button) findViewById(R.id.btogg);
chkbox = (CheckBox) findViewById(R.id.chkbok);
sd = (SlidingDrawer) findViewById(R.id.slidingDrawer);
hndl = (Button) findViewById(R.id.handle);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bopen:
sd.open();
break;
case R.id.bclose:
sd.close();
break;
case R.id.btogg:
sd.toggle();
break;
}
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (chkbox.isChecked()) {
sd.lock();
hndl.setEnabled(false);
} else {
sd.unlock();
hndl.setEnabled(true);
}
}
#Override
public void onDrawerOpened() {
}
#Override
public void onDrawerClosed() {
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
In one of our applications we hide and show a header and footer. We do this by changing the height and visibility of the view.
It looks something like this
ValueAnimator va;
if (show)
va = ValueAnimator.ofInt(0, px);
else
va = ValueAnimator.ofInt(px, 0);
va.setDuration(400);
va.setInterpolator(new DecelerateInterpolator(2));
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
topicNavBar.getLayoutParams().height = value;
topicNavBar.requestLayout();
if (value == 0) {
if (show)
topicNavBar.setVisibility(View.VISIBLE);
else
topicNavBar.setVisibility(View.GONE);
}
if (show && value == px) {
animationInProgress = false;
}
if (!show && value == 0) {
animationInProgress = false;
}
}
});
va.start();
Hope this helps

Placing Two Image Buttons on Top of Each Other

I need to place two ImageButtons on top of each other something like the following image.
While placing them has been no issue, I am unable to click the blue button. The red button when clicked works perfectly well.
The XML layout code is as follows:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageButton
android:id="#+id/bluebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:onClick="onButtonClicked"
android:scaleType="fitCenter"
android:src="#drawable/bluebutton" />
<ImageButton
android:id="#+id/redbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:onClick="onButtonClicked"
android:scaleType="fitCenter"
android:src="#drawable/redbutton" />
</FrameLayout>
How do i ensure that both buttons can be clicked ?
You can simply use a RelativeLayout to do that. See this I made just now:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton btn = (ImageButton)findViewById(R.id.bluebutton);
ImageButton btn2 = (ImageButton)findViewById(R.id.redbutton);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView t = (TextView)findViewById(R.id.txt);
t.setText("Hello!");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView t = (TextView)findViewById(R.id.txt);
t.setText("Hi, how are you?");
}
});
}
it's just an example
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageButton
android:background="#ff040ab2"
android:id="#+id/bluebutton"
android:layout_width="150dp"
android:layout_height="250dp"
android:onClick="onButtonClicked"
android:scaleType="fitCenter"/>
<ImageButton
android:rotation="90"
android:background="#be2222"
android:id="#+id/redbutton"
android:layout_width="150dp"
android:layout_height="250dp"
android:onClick="onButtonClicked"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt"
android:textColor="#ffffff"/>
</RelativeLayout>
Changed the code.
public class MainActivity extends Activity {
ImageButton red,blue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
red = (ImageButton)findViewById(R.id.redbutton);
blue = (ImageButton)findViewById(R.id.bluebutton);
}
public void onRedButtonClicked(final View view) {
Toast.makeText(getApplicationContext(), "Button red is clicked!", Toast.LENGTH_SHORT).show();
}
public void onBlueButtonClicked(final View view) {
Toast.makeText(getApplicationContext(), "Button blue is clicked!", Toast.LENGTH_SHORT).show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And the layout:
<FrameLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageButton
android:id="#+id/redbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:onClick="onRedButtonClicked"
android:scaleType="fitCenter"
android:src="#drawable/redbutton"
android:visibility="visible"/>
<ImageButton
android:id="#+id/bluebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:onClick="onBlueButtonClicked"
android:scaleType="fitCenter"
android:src="#drawable/bluebutton"
android:visibility="visible"/>
</FrameLayout>
Hope this helps..:)

Categories

Resources