I want to swipe between two linear layouts which are wrapped in frame layout. At one time, only one of the linear layout is visible. I am using button to show/hide them. I want to implement swipe functionality in them. I tried ViewFliper but didn't work out on finger swipe. Is it possible to do it using ViewPager?
Layout XML is
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000" >
<LinearLayout
android:id="#+id/layTopUp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/yellowspace"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:padding="25dp"
android:visibility="visible" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="XXX"
android:textColor="#000000" />
<Spinner
android:id="#+id/spnTopupOperator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="XXX"
android:textColor="#000000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_vertical|center_horizontal" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XXX"
android:textColor="#000000" />
<EditText
android:id="#+id/etTopUpCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:hint="Code"
android:inputType="number"
android:maxLength="3" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/etTopUpNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="7"
android:hint="Number"
android:inputType="number"
android:maxLength="7"
android:shadowColor="#000000" />
</LinearLayout>
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="XXX"
android:textColor="#000000" />
<Spinner
android:id="#+id/spnTopupAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp" />
<LinearLayout
android:id="#+id/layConv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvConvertedAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="XXX "
android:textColor="#000000" />
<ProgressBar
android:id="#+id/pbConversion"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
<ImageButton
android:id="#+id/bTopUpSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:background="#drawable/sendrechbotton" />
</LinearLayout>
<LinearLayout
android:id="#+id/layScratchCard"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/yellowspace"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:padding="25dp"
android:visibility="gone" >
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="XXXX"
android:textColor="#000000" />
<Spinner
android:id="#+id/spnRechargeOperator"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="XXX"
android:textColor="#000000" />
<EditText
android:id="#+id/etRechargeEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress" />
<TextView
android:id="#+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="XXXX"
android:textColor="#000000" />
<Spinner
android:id="#+id/spnRechargeAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/bSendCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/sendcardbotton" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
Solution:
You need to use GestureDetector to get notified whenever swipe occurs on your LinearLayout
Please have a look at GestureDetector for reference.
As of now,I have applied swipe in your code,but I did not have your drawables so I have just used mine.rest of the thing is ready for you.so just use your xml file instead of mine.
Example:
MainActivity.java
package com.mehuljoisar.swipe;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
private GestureDetector gesturedetector = null;
private Intent i;
private FrameLayout flContainer;
private LinearLayout ivLayer1,ivLayer2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acti);
flContainer = (FrameLayout) findViewById(R.id.flContainer);
ivLayer1 = (LinearLayout)findViewById(R.id.layTopUp);
ivLayer2 = (LinearLayout)findViewById(R.id.layScratchCard);
gesturedetector = new GestureDetector(new MyGestureListener());
flContainer.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
gesturedetector.onTouchEvent(event);
return true;
}
});
}
public boolean dispatchTouchEvent(MotionEvent ev) {
super.dispatchTouchEvent(ev);
return gesturedetector.onTouchEvent(ev);
}
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 20;
private static final int SWIPE_MAX_OFF_PATH = 100;
private static final int SWIPE_THRESHOLD_VELOCITY = 100;
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
float dX = e2.getX() - e1.getX();
float dY = e1.getY() - e2.getY();
if (Math.abs(dY) < SWIPE_MAX_OFF_PATH &&
Math.abs(velocityX) >= SWIPE_THRESHOLD_VELOCITY &&
Math.abs(dX) >= SWIPE_MIN_DISTANCE) {
if (dX > 0) {
Toast.makeText(getApplicationContext(), "Right Swipe",
Toast.LENGTH_SHORT).show();
//Now Set your animation
if(ivLayer2.getVisibility()==View.GONE)
{
Animation fadeInAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_right_in);
ivLayer2.startAnimation(fadeInAnimation);
ivLayer2.setVisibility(View.VISIBLE);
}
} else {
Toast.makeText(getApplicationContext(), "Left Swipe",
Toast.LENGTH_SHORT).show();
if(ivLayer2.getVisibility()==View.VISIBLE)
{
Animation fadeInAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_left_out);
ivLayer2.startAnimation(fadeInAnimation);
ivLayer2.setVisibility(View.GONE);
}
}
return true;
} else if (Math.abs(dX) < SWIPE_MAX_OFF_PATH &&
Math.abs(velocityY) >= SWIPE_THRESHOLD_VELOCITY &&
Math.abs(dY) >= SWIPE_MIN_DISTANCE) {
if (dY > 0) {
Toast.makeText(getApplicationContext(), "Up Swipe",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Down Swipe",
Toast.LENGTH_SHORT).show();
}
return true;
}
return false;
}
}
}
acti.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/flContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#android:color/black" >
<LinearLayout
android:id="#+id/layTopUp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/darker_gray"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:padding="25dp"
android:visibility="visible" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="XXX"
android:textColor="#000000" />
<Spinner
android:id="#+id/spnTopupOperator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="XXX"
android:textColor="#000000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_vertical|center_horizontal" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XXX"
android:textColor="#000000" />
<EditText
android:id="#+id/etTopUpCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:hint="Code"
android:inputType="number"
android:maxLength="3" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/etTopUpNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="7"
android:hint="Number"
android:inputType="number"
android:maxLength="7"
android:shadowColor="#000000" />
</LinearLayout>
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="XXX"
android:textColor="#000000" />
<Spinner
android:id="#+id/spnTopupAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp" />
<LinearLayout
android:id="#+id/layConv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvConvertedAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="XXX "
android:textColor="#000000" />
<ProgressBar
android:id="#+id/pbConversion"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
<ImageButton
android:id="#+id/bTopUpSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:background="#drawable/icon" />
</LinearLayout>
<LinearLayout
android:id="#+id/layScratchCard"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/icon"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:padding="25dp"
android:visibility="gone" >
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="XXXX"
android:textColor="#000000" />
<Spinner
android:id="#+id/spnRechargeOperator"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="XXX"
android:textColor="#000000" />
<EditText
android:id="#+id/etRechargeEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress" />
<TextView
android:id="#+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="XXXX"
android:textColor="#000000" />
<Spinner
android:id="#+id/spnRechargeAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/bSendCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/icon" />
</LinearLayout>
</FrameLayout>
<!-- <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd" />
-->
</LinearLayout>
slide_left_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="800"/>
</set>
slide_right_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="800"/>
</set>
I hope it will be helpful !!
Create an Activity extending FragmentActivity.
Create two fragments - each for the linear layouts
Create your View pager adapter extending FragmentStatePagerAdapter.
Supply a list of fragments to yout View pager adapter.
Related
This is a Noob question. I am sorry if it has been answered. I have been searching but either could not find or understand the answer. Anyway, I am trying to write a simple weight and balance program for a fleet of aircraft but I'm not sure how to proceed.
I'm trying to figure out how to have the program write to a specific field while a certain radio button is checked.
Could anyone be of any assistance? Thank you!
XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_weight_and_balance"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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="com.tropicaircharters.android.employee.weightAndBalance">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Aircraft Currently Selected:"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/aircraft_selected"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="50dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="A" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="B" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="D" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="E" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="F" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/display_a"
android:text="#string/a_weight" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/display_b"
android:text="#string/b_weight" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/display_c"
android:text="#string/c_weight" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/display_d"
android:text="#string/d_weight" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/display_e"
android:text="#string/e_weight" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/display_f"
android:text="#string/f_weight" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Weight" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/weight_display"
android:text="#string/weight_display" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="CG" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/cg_display" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="25dp"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Last Weights Entered" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/last_weight"
android:id="#+id/display_last_entry_1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/last_weight2"
android:id="#+id/display_last_entry_2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/last_weight3"
android:id="#+id/display_last_entry_3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/last_weight4"
android:id="#+id/display_last_entry_4" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/last_weight5"
android:id="#+id/display_last_entry_5"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fuel Tanks"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:text="Main Tanks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView5"
android:layout_weight="1"/>
<TextView
android:text="Auxiliary Tanks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView4"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButton2"
android:layout_weight="1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButton"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:text="0 gallons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView3"
android:layout_weight="1"/>
<TextView
android:text="0 gallons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView2"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:text="0 pounds"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView3"
android:layout_weight="1"/>
<TextView
android:text="0 pounds"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView2"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="30dp"
android:text="Weight in Section" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/radio_section">
<RadioButton
android:id="#+id/radio_section_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonClicked"
android:text="A" />
<RadioButton
android:id="#+id/radio_section_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonClicked"
android:text="B" />
<RadioButton
android:id="#+id/radio_section_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonClicked"
android:text="C" />
<RadioButton
android:id="#+id/radio_section_d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonClicked"
android:text="D" />
<RadioButton
android:id="#+id/radio_section_e"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonClicked"
android:text="E" />
<RadioButton
android:id="#+id/radio_section_f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonClicked"
android:text="F" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/enter_weight_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:gravity="center"
android:inputType="number"
android:text="#string/enter_weight" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/add_weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add Weight"
android:onClick="add_weight"/>
<Button
android:text="Subtract Weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/subtract_weight"
android:layout_weight="1"
android:onClick="subtract_weight" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/change_aircraft"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Change Aircraft"
android:onClick="change_aircraft"/>
<Button
android:text="Calculator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/calculator"
android:layout_weight="1"
android:onClick="startActivity(calculator)" />
</LinearLayout>
Java
package com.tropicaircharters.android.employee;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.TextView;
import android.view.View;
import static android.R.attr.onClick;
import static android.R.id.input;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
import static com.tropicaircharters.android.employee.R.id.display_last_entry_1;
import static com.tropicaircharters.android.employee.R.id.subtract_weight;
import static com.tropicaircharters.android.employee.R.string.a_weight;
import static com.tropicaircharters.android.employee.R.string.weight_display;
public class weightAndBalance extends AppCompatActivity {
//These are weights for each section.
int main_tank_gallons = 0;
int main_tank_weight = main_tank_gallons*6;
int aux_tank_gallons = 0;
int aux_tank_weight = aux_tank_gallons*6;
EditText enter_weight_box;
TextView display_a, weight_display, display_last_weight, display_last_weight_2, display_last_weight_3, display_last_weight_4, display_last_weight_5;
Button add_weight, subtract_weight;
RadioButton radio_section_a, radio_section_b, radio_section_c, radio_section_d, radio_section_e, radio_section_f;
double enter_weight, total_weight, a_weight;
String current_section, last_shizzle, last_shizzle2, last_shizzle3, last_shizzle4, last_shizzle5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weight_and_balance);
enter_weight_box = (EditText) findViewById(R.id.enter_weight_box);
display_a = (TextView) findViewById(R.id.display_a);
weight_display = (TextView) findViewById(R.id.weight_display);
add_weight = (Button) findViewById(R.id.add_weight);
subtract_weight = (Button) findViewById(R.id.subtract_weight);
display_last_weight = (TextView) findViewById(R.id.display_last_entry_1);
display_last_weight_2 = (TextView) findViewById(R.id.display_last_entry_2);
display_last_weight_3 = (TextView) findViewById(R.id.display_last_entry_3);
display_last_weight_4 = (TextView) findViewById(R.id.display_last_entry_4);
display_last_weight_5 = (TextView) findViewById(R.id.display_last_entry_5);
//This displays adds the entered number to A, the Total,
// and creates the last weights entered log.
add_weight.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
enter_weight = Double.parseDouble(enter_weight_box.getText().toString());
a_weight = a_weight + enter_weight;
total_weight = total_weight + enter_weight;
last_shizzle5 = last_shizzle4;
last_shizzle4 = last_shizzle3;
last_shizzle3 = last_shizzle2;
last_shizzle2 = last_shizzle;
last_shizzle = "Added " + Double.toString(enter_weight) + " in A";
//These create displays for totals and the last entered log.
display_a.setText(Double.toString(a_weight));
weight_display.setText(Double.toString(total_weight));
display_last_weight.setText(last_shizzle);
display_last_weight_2.setText(last_shizzle2);
display_last_weight_3.setText(last_shizzle3);
display_last_weight_4.setText(last_shizzle4);
display_last_weight_5.setText(last_shizzle5);
enter_weight_box.setText("");
}
});
//This displays and subtracts the entered number to A, the Total,
// and creates the last weights entered log.
subtract_weight.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
enter_weight = Double.parseDouble(enter_weight_box.getText().toString());
a_weight = a_weight - enter_weight;
total_weight = total_weight - enter_weight;
last_shizzle5 = last_shizzle4;
last_shizzle4 = last_shizzle3;
last_shizzle3 = last_shizzle2;
last_shizzle2 = last_shizzle;
last_shizzle = "Subtracted " + Double.toString(enter_weight) + " in A";
//These create displays for totals and the last entered log.
display_a.setText(Double.toString(a_weight));
weight_display.setText(Double.toString(total_weight));
display_last_weight.setText("Subtracted "+Double.toString(enter_weight)+" in "+"A");
display_last_weight.setText(last_shizzle);
display_last_weight_2.setText(last_shizzle2);
display_last_weight_3.setText(last_shizzle3);
display_last_weight_4.setText(last_shizzle4);
display_last_weight_5.setText(last_shizzle5);
enter_weight_box.setText("");
}
});
}
}
I have two views in my fragment. In the first view, I have a linear layout, which holds some controls and in the second view, I have a listview.
I want the linear layout in the first view to be collapsed / expand when I scroll up / down listview.
I tried to handle scroll up / down events of listview in the OnScrollListener and collapse / expand the listview.
But there is a problem with the onScroll method,i.e, when I scroll the listview, it fires many times when I scroll the listview one time. So expanding and collapsing process of linear layout is going crazy.
Here is - what is happening on the screen.
https://www.youtube.com/watch?v=U7KNwS6JlUk
What am I doing wrong?
What is the best way to handle scrol up / down events of listview and collapse / expand linear layout?
My Layout
<LinearLayout 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"
tools:context="com.training.mehmetyilmaz.mywallet.TransactionsActivity.TransactionsFragment"
android:orientation="vertical">
<LinearLayout
android:id="#+id/transactions_filter_linear_layout"
android:layout_width="match_parent"
android:layout_height="160dp"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#color/blue">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/account_type_label"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
/>
<RadioGroup
android:id="#+id/transactions_account_type_radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="#+id/transactions_rd_cash"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/cash"
android:checked="true"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
android:buttonTint="#color/white" />
<RadioButton
android:id="#+id/transactions_rd_bank"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/bank"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
android:buttonTint="#color/white"
/>
</RadioGroup>
</LinearLayout><!-- Money Add Type-->
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/currency_label"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
/>
<Spinner
android:id="#+id/transactions_spinner_currency"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/entry_currency"
android:layout_gravity="right"
android:gravity="right|end"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
style="#style/DropDownColor"
android:background="#drawable/abc_spinner_mtrl_am_alpha"
>
</Spinner>
</LinearLayout><!-- Currency -->
</LinearLayout><!-- First Line-->
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/cyan_ligth"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
>
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_marginTop="5dp"
>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/money_type_label"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
/>
<RadioGroup
android:id="#+id/transactions_money_type_radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="#+id/transactions_rd_add"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/add"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
android:buttonTint="#color/white" />
<RadioButton
android:id="#+id/transactions_rd_sub"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/subtract"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
android:buttonTint="#color/white" />
</RadioGroup>
</LinearLayout><!-- Money Type-->
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/entry_date_label_all_dates"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
/>
<Spinner
android:id="#+id/transactions_spinner_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/entry_date"
android:layout_gravity="right"
android:gravity="right|end"
android:textSize="#dimen/transactions_filter_text"
style="#style/DropDownColor"
android:background="#drawable/abc_spinner_mtrl_am_alpha">
</Spinner>
</LinearLayout><!-- Date -->
</LinearLayout><!-- Second Line -->
</LinearLayout><!-- Filter -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.training.mehmetyilmaz.mywallet.ScrollDetectingListView
android:layout_width="match_parent"
android:layout_height="320dp"
android:id="#+id/transactions_list_view"
></com.training.mehmetyilmaz.mywallet.ScrollDetectingListView>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/transactions_total_textView"
android:text="#string/total"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textSize="#dimen/abc_text_size_medium_material"
android:background="#color/green"
android:textColor="#color/white"
android:gravity="center"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
</LinearLayout>
OnScrollListener
mTransactionListView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//Log.d("Scroll State", "State : " + scrollState);
}
private int mInitialScroll = 0;
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
Log.d("Scroll", "Called");
int scrolledOffset = mTransactionListView.getVerticalScrollOffset();
if (scrolledOffset != mInitialScroll) {
boolean scrollUp = (scrolledOffset - mInitialScroll) < 0;
if (scrollUp) {
Log.d("Scroll", "Up");
expand(mTransactionsFilterLinearLayout);
} else {
Log.d("Scroll", "Down");
collapse(mTransactionsFilterLinearLayout);
}
mInitialScroll = scrolledOffset;
}
}
});
I think the solution is not possible with the classic ListView of Android SDK.
I found a custom ListView called Android-ObservableScrollView and implemented it in my project and the result is success.
You can find it from here
https://github.com/ksoichiro/Android-ObservableScrollView/
try setting a booolean value like this as global.
boolean flag = true;
then
if (scrollUp) {
if(flag == false){
Log.d("Scroll", "Up");
expand(mTransactionsFilterLinearLayout);
}
flag = true;
} else {
if(flag == true){
Log.d("Scroll", "Down");
collapse(mTransactionsFilterLinearLayout);
}
flag = false;
}
First let me give a short introduction and then show you what end goal looks:
So far I've added a scrolllistener to my listactivity, so that I can detect scrolling, though it does not give me any animation on android:animateLayoutChanges="true". So when i change the margin's of the view programmatically, it just does these changes instantly, and not that well, any help is most welcomed.
As you can see in my xml, i place the item I'm going to hide slowly first so other will be above it on the Z-axis.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:lector="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:animateLayoutChanges="true"
android:background="#color/background_greyish">
<LinearLayout
android:id="#+id/disappearingHeaderLabel"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="60dp"
android:animateLayoutChanges="true"
android:layout_height="50dp"
android:background="#color/solid_white"
android:orientation="horizontal"
android:weightSum="7">
<TextView
android:id="#+id/ProductDetailLabel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginLeft="15dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dip"
android:layout_weight="4"
android:singleLine="true"
android:text="PRODUKTER"
android:textColor="#color/solid_red"
android:textSize="20dp" />
<Button
android:id="#+id/priceChooserButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:layout_weight="3"
android:background="#drawable/buttonbackground"
android:clickable="true"
android:singleLine="true"
android:text="#string/retailprice"
android:textColor="#android:color/white"
android:textSize="15dp"
android:textStyle="bold" />
</LinearLayout>
<dk.myapp.mobile.views.PageHeaderView
android:id="#+id/header"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></dk.myapp.mobile.views.PageHeaderView>
<LinearLayout
android:id="#+id/expandingListContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:animateLayoutChanges="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="110dp"
android:layout_above="#+id/paginationBarContainer"
android:orientation="vertical">
<LinearLayout
android:id="#+id/headerLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#color/transparent"
android:orientation="horizontal"
android:weightSum="11">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/sortAmountLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:singleLine="false"
android:text="1354"
android:textColor="#color/solid_red"
android:textSize="15dp" />
<TextView
android:id="#+id/sortLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:text="Produkter"
android:singleLine="true"
android:textColor="#color/solid_red"
android:textSize="15dp" />
</LinearLayout>
<Button
android:id="#+id/filterBtn"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:layout_weight="4"
android:background="#drawable/button_background_red"
android:clickable="true"
android:singleLine="true"
android:text="#string/retailprice"
android:textColor="#color/solid_white"
android:textSize="15dp"
android:textStyle="bold" />
<Spinner
android:id="#+id/sortSpinner"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="35dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/spinner_background"
android:drawSelectorOnTop="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:prompt="#string/sort" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#color/divider_color"></LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#color/divider_color"
android:dividerHeight="1dip"
android:paddingLeft="15dip"
android:scrollbarStyle="outsideInset"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
</LinearLayout>
</LinearLayout>
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int marginRemoved = firstVisibleItem * 30;
if (marginRemoved > 110) {
marginRemoved = 110;
}
if (previousMarginRemoved != marginRemoved) {
previousMarginRemoved = marginRemoved;
LinearLayout disappearingHeaderContainer = (LinearLayout) findViewById(R.id.disappearingHeaderLabel);
RelativeLayout.LayoutParams disappearingHeaderContainerLayoutParams = (RelativeLayout.LayoutParams) disappearingHeaderContainer.getLayoutParams();
LinearLayout expandingListViewContainer = (LinearLayout) findViewById(R.id.expandingListContainer);
RelativeLayout.LayoutParams expandingListViewContainerParams = (RelativeLayout.LayoutParams) expandingListViewContainer.getLayoutParams();
if (disappearingHeaderStartMargin == -1 && expandingHeaderStartMargin == -1) {
disappearingHeaderStartMargin = disappearingHeaderContainerLayoutParams.topMargin;
expandingHeaderStartMargin = expandingListViewContainerParams.topMargin;
}
float disappearingHeaderTopMarginFloat = disappearingHeaderStartMargin - (marginRemoved * pixelDensity);
int disappearingHeaderTopMargin = Math.round(disappearingHeaderTopMarginFloat);
if (marginRemoved > 50) {
marginRemoved = 50;
}
float expandingContainerTopMarginFloat = expandingHeaderStartMargin - (marginRemoved * pixelDensity);
int expandingContainerTopMargin = Math.round(expandingContainerTopMarginFloat);
disappearingHeaderContainerLayoutParams.setMargins(disappearingHeaderContainerLayoutParams.leftMargin,
disappearingHeaderTopMargin,
disappearingHeaderContainerLayoutParams.rightMargin,
disappearingHeaderContainerLayoutParams.bottomMargin);
expandingListViewContainerParams.setMargins(expandingListViewContainerParams.leftMargin,
expandingContainerTopMargin,
expandingListViewContainerParams.rightMargin,
expandingListViewContainerParams.bottomMargin);
if ((lastChangeDate < System.currentTimeMillis()-100)) {
lastChangeDate = System.currentTimeMillis();
disappearingHeaderContainer.setLayoutParams(disappearingHeaderContainerLayoutParams);
expandingListViewContainer.setLayoutParams(expandingListViewContainerParams);
}
}
}
};
While designing the UI for an android app I faced the following problem. I wanted to align the item in the ListView of the drawer to the bottom. I am new to android development. Any help will be appreciated.
This is my drawer code
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E0E0E0" >
<!-- Your main screen -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Location: "
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvLocInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Rajeev Chowk, Block A, Connaught Place"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Time: "
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvTimeInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="9:30 PM"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.79"
android:orientation="vertical" >
<ImageView
android:id="#+id/ivMap"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_weight="0.85"
android:scaleType="fitXY"
android:src="#drawable/map" />
<Button
android:id="#+id/btnPanic"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:background="#drawable/red_button"
android:text="Panic Button"
android:textColor="#FFFFFF" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_weight="0.010"
android:background="#drawable/top_border" >
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="#+id/tvDeviceStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Device Status: Working"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
android:id="#+id/tvDeviceStatus1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDeviceStatus"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Battery: 91%(~11 months)"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
<!-- left drawer -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="380dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#E0E0E0"
android:choiceMode="singleChoice"
android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>
These are the three xml file that I inflate in the ListView
1) profile_view.xml
<ImageView
android:id="#+id/ivProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:src="#drawable/sneha_lata" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sneha Lata"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit Profile"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="" />
<ImageView
android:id="#+id/ivForward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/forward" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
2) emergency_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="match_parent"
android:paddingBottom="20dp"
android:paddingTop="20dp" >
<TextView
android:id="#+id/tvManage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:text="Manage Emergency Contacts"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/ivForward"
android:layout_toRightOf="#id/tvManage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/forward" />
<TextView
android:id="#+id/tvManageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvManage"
android:layout_alignParentLeft="true"
android:layout_alignRight="#id/tvManage"
android:layout_marginLeft="40dp"
android:fontFamily="sans-serif-light"
android:text="#string/tvManageText"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
3) logout view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/logout_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/btnLogout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/red_button"
android:text="Log Out"
android:textColor="#FFFFFF" />
</RelativeLayout>
Even if I used android:layout_alignParentBottom="true" in my logout_view.xml still it does not align at the bottom in the drawer but it does in logout_view.xml
This is the code for the DrawerAdapter
package com.majhe.android.adapter;
import com.majhe.android.LoginActivity;
import com.majhe.android.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
public class DrawerAdapter extends ArrayAdapter<String> implements OnClickListener {
Context context;
Button btnLogout;
public DrawerAdapter(Context context) {
super(context, android.R.layout.simple_list_item_1, new String[] {
"Profile View", "Emergency View", "Logout View" });
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
switch (position) {
case 0:
convertView = inflater.inflate(R.layout.profile_view, null, true);
return convertView;
case 1:
convertView = inflater.inflate(R.layout.emergency_view, null,true);
return convertView;
case 2:
convertView = inflater.inflate(R.layout.logout_view, null, true);
btnLogout=(Button)convertView.findViewById(R.id.btnLogout);
btnLogout.setOnClickListener(this);
return convertView;
}
return super.getView(position, convertView, parent);
}
#Override
public void onClick(View v) {
context.startActivity(new Intent(context,LoginActivity.class));
((Activity)context).finish();
}
}
I think you dont need to use ListView at all if I am right? replace your listview with something like this might help
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:orientation="vertical"
android:paddingEnd="10dp" android:gravity="bottom">
<include layout="#layout/profile_view"/>
<include layout="#layout/emergency_view"/>
<include layout="#layout/logout_view"/>
</LinearLayout>
set layout_height 0 dp and layout_weight to 1 of the view to fill the remaining space ( most probably emergency_view ) so that your logot_view will alwyas stick to bottom
I've look in the android documentation on how to implement alertdialog with a xml layout
here is the documentation
when i run the program and click the todo button the program crashes can anyone help?
here's my source code:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class StartMoving extends Activity implements OnClickListener {
Button todo;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.start_moving);
todo = (Button) findViewById(R.id.bTodo);
todo.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bTodo:
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.to_do_list, null);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
break;
default:
break;
}
}
}
here's the xml file named to_do_list.xml
<?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"
android:weightSum="100" >
<ScrollView
android:id="#+id/svTips"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="87" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/tvTipsTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:text="#string/tips"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/tvtmb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/two_months_before"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvtmbSAP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tmb_sort_and_purge"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvtmb_sap_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/tmb_sap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvtmbR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tmb_research"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvtmb_r_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/tmb_r_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvtmbCAMB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tmb_create_a_moving_builder"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvtmb_camb_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/tmb_camb_content" />
<TextView
android:id="#+id/tvswb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/six_weeks_before"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvswb_ordersupplies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/swb_order_supplies"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvswb_os_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/swb_os_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvswb_use_it_or_lose_it"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/swb_use_it_or_lose_it"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvswb_uioli_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/swb_uioli_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvswb_take_measurement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/swb_take_measurement"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvswb_tm_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/swb_tm_content" />
<TextView
android:id="#+id/tvomb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/one_month_before"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_choose_your_mover_and_confirm_the_arragements"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_choose_your_mover_and_confirm_the_arragements"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_cymacta_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_cymacta_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_begin_packing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_begin_paking"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_bp_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_bp_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_label"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_l_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_l_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_separate_values"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_separate_values"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_sv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_sv_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_do_a_change_of_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_do_a_change_of_address"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_dacoa_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_dacoa_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_notify_important_parties"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_notify_all_important_parties"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_naip_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_naip_content" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="13"
android:orientation="horizontal" >
<Button
android:id="#+id/bReturntoTop"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:text="Go back to top" />
<Button
android:id="#+id/bgoto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:text="Go to" />
</LinearLayout>
</LinearLayout>
this is what the logcat shows
use
Context mContext = v.getApplicationContext();
instead of
Context mContext = getApplicationContext();
or
builder = new AlertDialog.Builder(StartMoving.this);
EDIT :
You are reading button
todo = (Button) findViewById(R.id.bgoto); <-----------
^^^^^
todo.setOnClickListener(this);
AlertDialog.Builder diag = new AlertDialog.Builder(this);
diag.setTitle("DIALOG TITLE")
.setMessage("YOUR MESSAGE")
.setCancelable(false)
.setNegativeButton("WHAT EVER THE BUTTON TEXT",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//enter code here
//do what ever the button in the dialog is clicked
dialog.cancel();
}
});
diag.show();
Easiest way to have a alert dialog
and dont forget to change the Button id in java to R.id.goto
if your are using alertDialog in TabHost then you have to write..
final AlertDialog alertDialog = new AlertDialog.Builder(**getParent()**).create();
//alertDialog.setTitle("Alert....");
alertDialog.setMessage("Your Text");
alertDialog.setButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
finish();
}
});
alertDialog.show();
It seems that I've been reading the button from the Context and not from the alertDialog. The solution was to create a View that will be set as View for the AlertDialog and declare a button and set it as a child for the View.
final View view;
LayoutInflater inf = LayoutInflater.from(StartMoving.this);
view = inf.inflate(R.layout.rename, null);
final EditText newname = (EditText) view.findViewById(R.id.etNewRoomName);
final Button todo = (BUtton) view.findViewById(R.id.bTodo);
new AlertDialog.Builder(StartMoving.this)
.setView(view)
.setTitle("Rename")
.setMessage("Enter new name for room " + currentRoom)
.show();