Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
I want to show my date picker dialog like this where the background is blurred like a normal datePickerDialog but also has extra components like a textview and button. I cant seem to get this from making the dialog.
You have to custom very much to can create your DatePicker.
I only support a simple DatePicker and you must improve it:
1.Create custom layout: layoutdatetime.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="#ffffff"
android:text="SELECT TO DATE" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<CalendarView android:layout_marginTop="10dp"
android:id="#+id/calendarView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:id="#+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#f54242"
android:text="Cancel" />
<TextView
android:id="#+id/btnOK"
android:layout_width="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"
android:textColor="#f54242"
android:text="OK" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/button3"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Create funtion display DatePicker and custom process of controls on layout
public void ShowDate() {
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.layoutdatetime);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
TextView btnCancel = dialog.findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(v -> dialog.cancel());
dialog.show();
}
Requirements:
View binding
Create a custom layout named layoutdatetime.xml : (Taken from Dt's answer over here.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="#ffffff"
android:text="SELECT TO DATE" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<CalendarView android:layout_marginTop="10dp"
android:id="#+id/calendarView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:id="#+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#f54242"
android:text="Cancel" />
<TextView
android:id="#+id/btnOK"
android:layout_width="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"
android:textColor="#f54242"
android:text="OK" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/button3"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Make a class named DateListener and paste this code:
public interface DateListener {
void onDateNotPicked();
void onDatePicked(Date date);
void onGenerateStatement();
}
To make use of that, make this class and paste the code:
public class CustomDatePickerDialog extends Dialog {
private DateListener listener;
private LayoutdatetimeBinding binding;
public CustomDatePickerDialog(#NonNull Context context, DateListener dateListener) {
super(context);
listener = dateListener;
binding = LayoutdatetimeBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
init();
}
private void init() {
binding.btnOK.setOnClickListener(v -> {
listener.onDatePicked(new Date(binding.calendarView.getDate()));
dismiss();
});
binding.btnCancel.setOnClickListener(v -> {
dismiss();
listener.onDateNotPicked();
});
binding.generateStatement.setOnClickListener(v -> {
listener.onGenerateStatement();
dismiss();
});
}
}
Show it in this way:
CustomDatePickerDialog dialog = new CustomDatePickerDialog(this, new DateListener(){
#Override
public void onDateNotPicked() {
Toast.makeText(this, "date not picked", Toast.LENGTH_SHORT).show();
}
#Override
public void onDatePicked(Date date) {
Toast.makeText(this, "date picked", Toast.LENGTH_SHORT).show();
}
#Override
public void onGenerateStatement() {
Toast.makeText(this, "statemnt generated", Toast.LENGTH_SHORT).show();
}
});
dialog.show();
Related
Hi everyone I'm trying to create a menu that comes down when clicking on the profile picture:
but as you can see that ImageView is inside a cardView which is in turn inside a tool bar and as the initial parent I have an App Bar layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primaryPopHome"
android:minHeight="?attr/actionBarSize"
android:theme="#style/AppTheme.bar"
>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="centerInside"
android:src="#drawable/poplogo"
/>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="300dp"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="1.9"
app:cardCornerRadius="80dp">
<ImageView
android:id="#+id/img_profilomain"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:backgroundTint="#color/white"
android:scaleType="centerCrop"
/>
</androidx.cardview.widget.CardView>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.appcompat.widget.ActionMenuView
android:id="#+id/mnuItem"
android:layout_width="match_parent"
android:layout_height="250dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appBarLayout"
android:background="#color/def2"
android:visibility="invisible"
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:src="#drawable/poplike"
android:backgroundTint="#color/redButton2"
app:borderWidth="0dp"
app:fabSize="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<!--Floating action button for add alarm-->
<!--Make sure that you are constraining this
button to the parent button-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add_alarm_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
app:fabSize="normal"
app:layout_constraintBottom_toTopOf="#+id/add_fab"
app:layout_constraintEnd_toEndOf="#+id/add_fab"
app:layout_constraintStart_toStartOf="#+id/add_fab"
app:srcCompat="#drawable/ic_baseline_add_24"
app:borderWidth="0dp"
android:backgroundTint="#color/redButton2"
/>
<!--Action name text for the add alarm button-->
<!--Make sure that you are constraining this Text to
the add Alarm FAB button-->
<TextView
android:id="#+id/add_alarm_action_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Crea Post"
app:layout_constraintBottom_toBottomOf="#+id/add_alarm_fab"
app:layout_constraintEnd_toStartOf="#+id/add_alarm_fab"
app:layout_constraintTop_toTopOf="#+id/add_alarm_fab" />
<!--Floating action button for add person-->
<!--Make sure that you are constraining this
button to the add Alarm FAB button-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add_person_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
app:fabSize="normal"
app:layout_constraintBottom_toTopOf="#+id/add_alarm_fab"
app:layout_constraintEnd_toEndOf="#+id/add_alarm_fab"
app:layout_constraintStart_toStartOf="#+id/add_alarm_fab"
app:srcCompat="#drawable/ic_baseline_search_24"
app:borderWidth="0dp"
android:backgroundTint="#color/redButton2"
/>
<!--Action name text for the add person button-->
<!--Make sure that you are constraining this Text
to the add Person FAB button-->
<TextView
android:id="#+id/add_person_action_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Cerca utenti"
app:layout_constraintBottom_toBottomOf="#+id/add_person_fab"
app:layout_constraintEnd_toStartOf="#+id/add_person_fab"
app:layout_constraintTop_toTopOf="#+id/add_person_fab" />
<Button
android:id="#+id/btn_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="152dp"
android:text="Logout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/appBarLayout"
app:layout_constraintVertical_bias="0.111" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rcViewPostMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="56dp"
app:layout_constraintTop_toBottomOf="#+id/appBarLayout"
tools:layout_editor_absoluteX="-16dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
I tried to set an OnClick listener from the java code but when I go to run the app the onclick doesn't work when I click on the image:
ImageView img_profilo = findViewById(R.id.img_profilomain);
img_profilo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "d", Toast.LENGTH_SHORT).show();
mnuItem.setVisibility(View.VISIBLE);
}
});
I tried to do it with the toolbar too but it doesn't work anyway:
tlb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "sad", Toast.LENGTH_SHORT).show();
}
});
Edit:
It doesn't work with this either
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if(item.getItemId() == R.id.img_profilomain)
Toast.makeText(MainActivity.this, "wds", Toast.LENGTH_SHORT).show();
return true;
}
});
Would anyone know how to solve this problem?
The textinputlayout I created is for a login screen, but whenever the activity is launched, everything else shows except for that. Clicking on the space where it should be also does nothing (the keyboard does not come up) so it seems like its visibility has been set to "gone" even though it has not.
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="org.thelatinschool.canvasgrades.SplashScreenActivity">
<ImageView
android:id="#+id/logo"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_centerInParent="true"
android:contentDescription="#null"
android:src="#mipmap/ic_launcher_round"
android:visibility="visible" />
<ProgressBar
android:id="#+id/loadingProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="12dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="-4dp"
android:foregroundGravity="bottom"
android:indeterminate="true"
android:padding="0dp"
android:theme="#style/ProgressBarStyle"
android:visibility="visible" />
<RelativeLayout
android:id="#+id/afterAnimationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="20dp"
android:layout_marginTop="130dp"
android:layout_marginEnd="20dp"
android:orientation="vertical"
android:visibility="visible">
<TextView
android:id="#+id/WelcomeTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Highlands Latin School"
android:textColor="#color/colorPrimary"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="#+id/readItTogetherTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/WelcomeTextView"
android:layout_marginTop="10dp"
android:text="Canvas Grades"
android:textColor="#color/colorAccent"
android:textSize="15sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/loginButton"
android:layout_below="#+id/readItTogetherTextView"
android:gravity="center"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="Email"
android:textColor="#color/colorPrimary"
android:textColorHint="#color/colorAccent"
android:textSize="15sp"
app:boxBackgroundMode="outline" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="25dp"
android:hint="Password"
android:textColor="#color/colorPrimary"
android:textColorHint="#color/colorAccent"
android:textSize="15sp"
app:boxStrokeColor="#000000" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="5dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center_vertical"
android:background="#FFFFFF"
android:text="Forgot Password?"
android:textColor="#color/colorAccent"
android:textSize="14sp"
android:textStyle="bold" />
</FrameLayout>
</LinearLayout>
<Button
android:id="#+id/loginButton"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_above="#+id/skipTextView"
android:layout_marginBottom="5dp"
android:background="#drawable/button_drawable"
android:text="Login"
android:textAllCaps="false"
android:textColor="#FFFFFF"
android:textSize="16sp" />
<TextView
android:id="#+id/skipTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:padding="12dp"
android:text="Incorrect username or password!"
android:textColor="#B53737"
android:textSize="15sp"
android:visibility="invisible" />
</RelativeLayout>
</RelativeLayout>
And the corresponding java:
public class SplashScreenActivity extends AppCompatActivity {
public String theme1;
private ProgressDialog progressDialog;
private boolean animationStarted = false;
private ImageView bookIconImageView;
private TextView bookITextView;
private ProgressBar loadingProgressBar;
int progress;
private RelativeLayout rootView, afterAnimationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_activity);
bookIconImageView = findViewById(R.id.logo);
loadingProgressBar = (ProgressBar)findViewById(R.id.loadingProgressBar);
rootView = findViewById(R.id.rootView);
afterAnimationView = findViewById(R.id.afterAnimationView);
Thread thred = new Thread(new Runnable() {
#Override
public void run() {
doWork();
startApp();
}
public void doWork() {
for (progress=10;progress<100;progress=progress+10){
try {
Thread.sleep(350);
loadingProgressBar.setProgress(progress);
} catch (InterruptedException e) {
e.printStackTrace();
}} }
public void startApp(){
runOnUiThread(new Runnable() {
#Override
public void run() {
loadingProgressBar.setVisibility(GONE);
rootView.setBackgroundColor(ContextCompat.getColor(SplashScreenActivity.this, R.color.splashscreen));
bookIconImageView.setImageResource(R.mipmap.ic_launcher_round);
}
});
startAnimation();
};
});
thred.start();
}
private void startAnimation() {
ViewPropertyAnimator viewPropertyAnimator = bookIconImageView.animate();
viewPropertyAnimator.x(50f);
viewPropertyAnimator.y(100f);
viewPropertyAnimator.setDuration(1000);
viewPropertyAnimator.setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
afterAnimationView.setVisibility(VISIBLE);
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
}
// Intent myIntent = new Intent(SplashScreenActivity.this, MainActivity.class);
// SplashScreenActivity.this.startActivity(myIntent);
}
I have an animation for the splash screen that displays right before the login screen, I haven't touched anything to do with the text input box in java but maybe I made a mistake somewhere there.
Most probably you miss adding app level material design dependency
// material Design support library - androidx
implementation 'com.google.android.material:material:1.0.0'
// material Design support library - support library
implementation 'com.android.support:design:28.0.0'
UPDATE
You're missing adding TextInputEditText within TextInputLayout, so you won't expect to see something, also move android:hint, android:textSize, android:textColorHint & android:textColor attributes into TextInputEditText instead of TextInputLayout
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="50dp"
app:boxBackgroundMode="outline">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"
android:textColor="#color/colorPrimary"
android:textColorHint="#color/colorAccent"
android:textSize="15sp" />
</com.google.android.material.textfield.TextInputLayout>
I need to allow user to CLICK anywhere on the screen, so I've implemented onClick listener for mainLayout and it worked great.
But the problem is I've got to add a scrollview inside mainLayout to support small screen sizes and after adding scrollview, it blocks the parentLayout (in my case mainLayout) CLICK EVENT.
Just to exaplin this, a sample layout is as follows.
<!-- MAIN LAYOUT WHERE I NEED
THE CLICK EVENT-->
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bebebe"
android:orientation="vertical"
android:onClick="onClick_MainLayout">
<ScrollView
android:id="#+id/mainScrollView"
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/button1"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="TEST BUTTON 1"
android:onClick="onClick_Button1"/>
<Button
android:id="#+id/button2"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="TEST BUTTON 2"
android:onClick="onClick_Button2"/>
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Text View"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
I searched for this and found various ideas like disableing onClick manually in all other views inside scrollview etc but non of them works.
NOTE: I tried both implementing onClick listener manually in java and adding android:onClick="onClick_MainLayout" in to layout. Both the same.
EDITS ----------------------
All the changes I've done as you (#IntelliJ Amiya) mentioned is as below. Please let me know anything is missing.
<RelativeLayout
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bebebe">
<ScrollView
android:id="#+id/mainScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="TEST BUTTON 1"/>
<Button
android:id="#+id/button2"
android:layout_below="#+id/button1"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="TEST BUTTON 2"/>
<TextView
android:id="#+id/textview1"
android:layout_below="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Text View"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
And in on create
findViewById(R.id.mainLayout).setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Log.v("MyLogs", "Click Event Fires...");
}
});
Your Scroll view overlaps the linear layout i.e #mainLayout,so
set its height to wrap content
then you can perform onclick for both scroll view buttons and mainlayout.
Pls tell me if i wrong.
<ScrollView
android:id="#+id/mainScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bebebe"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:id="#+id/mainScrollView"
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/button1"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="TEST BUTTON 1"
android:onClick="onClick_Button1"/>
<Button
android:id="#+id/button2"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="TEST BUTTON 2"
android:onClick="onClick_Button2"/>
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Text View"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
JAVA
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick_Button1(View v)
{
Toast.makeText(this, "1ST Button Click", Toast.LENGTH_SHORT).show();
}
public void onClick_Button2(View v)
{
Toast.makeText(this, "2ND Button Click", Toast.LENGTH_SHORT).show();
}
}
My requirement :-
I have two activities..When I swipe it will go to another activity..
So,I have :--
MainActivity.java
public class MainActivity extends Activity {
private GestureDetectorCompat gestureDetectorCompat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_getservice);
gestureDetectorCompat = new GestureDetectorCompat(this, new MyGestureListener());
}
#Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetectorCompat.onTouchEvent(event);
return super.onTouchEvent(event);
}
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
//handle 'swipe left' action only
#Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
if(event2.getX() < event1.getX()){
//switch another activity
Intent intent = new Intent(
MainActivity.this, Join_form.class);
startActivity(intent);
finish();
}
return true;
}
}
}
and Join_form.java
public class Join_form extends Activity {
private GestureDetectorCompat gestureDetectorCompat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gestureDetectorCompat = new GestureDetectorCompat(this, new MyGestureListener());
}
#Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetectorCompat.onTouchEvent(event);
return super.onTouchEvent(event);
}
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
//handle 'swipe left' action only
#Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
if(event2.getX() > event1.getX()){
//switch another activity
Intent intent = new Intent(
Join_form.this, MainActivity.class);
startActivity(intent);
finish();
}
return true;
}
}
}
activity_getservice.xml is:--
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#0b2607"
>
<TextView android:id="#+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Form"
android:layout_marginTop="30dp"
android:padding="10dp"
android:textSize="20dp"
android:layout_gravity="center"/>
<View android:id="#+id/division1"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#999999" />
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView android:text="Name :"
android:id="#+id/id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:gravity="left"
android:layout_weight="1"
/>
<EditText android:id="#+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:hint="name"
android:layout_weight="1" />
</LinearLayout>
<View android:id="#+id/division2"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#999999" />
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView android:text="Address :"
android:id="#+id/address"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:gravity="left"
android:layout_weight="1"
/>
<EditText android:id="#+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:hint="address"
android:layout_weight="1" />
</LinearLayout>
<View
android:id="#+id/division9"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#999999" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:text="You Are :"
android:id="#+id/txt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:gravity="left"
android:layout_weight="1" />
<Spinner
android:id="#+id/spinner2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_weight="1" />
</LinearLayout>
<View
android:id="#+id/division3"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#999999" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:text="hii"
android:id="#+id/txt4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:gravity="left"
android:layout_weight="1" />
<Spinner
android:id="#+id/spinner3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_weight="1" />
</LinearLayout>
<View
android:id="#+id/division10"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#999999" />
<LinearLayout
android:id="#+id/check"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:text="Select your reqirements:-"
android:id="#+id/the"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
android:gravity="left"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lay1">
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="apple"
android:tag="apple"
android:onClick="onCheckboxClicked" />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="banana"
android:tag="banana"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:id="#+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="water_milon"
android:tag="water_milon"
android:onClick="onCheckboxClicked"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lay2">
<CheckBox
android:id="#+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="guava"
android:tag="guava"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:id="#+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="panir"
android:tag="panir"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:id="#+id/checkBox6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="chatni"
android:tag="chatni"
android:onClick="onCheckboxClicked"/>
</LinearLayout>
</LinearLayout>
<View
android:id="#+id/division6"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#999999"
android:layout_marginTop="5dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Write your message(optional) :"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine"
android:padding="10dip"
android:layout_marginTop="5dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#drawable/edit_text_stle"
android:hint="write something ">
<requestFocus />
</EditText>
<View
android:id="#+id/division7"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#999999"
android:layout_marginTop="5dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="10dp">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
activity_main.xml is:--
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
But I can not swipe the page. It is not going to the another page. Why it is happening?
Where is the problem?
Agree with #k3v1n4ud3. Let me just expand a bit since I can't really comment due to lack of karma.
With ViewPager (inside MainActivity.java) + MainFragment (Fragment)+ JoinForm (Fragment), you would be able to get away without having a listener for fling. It would just handle the swiping part automatically for you.
Just think about it like this: You have a brain (MainActivity w/ViewPager) and two arms (the first and second fragment you're implementing).
Create a MainActivity that would contain your ViewPager.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myViewPager = (ViewPager) findViewById(R.id.myViewPager);
//somehow fill the adapter
MyAdapter myAdapter = new MyAdapter(getSupportFragmentManager(), getApplicationContext()); //=== required as per the MyAdapter class
//set the adapter
myViewPager.setAdapter(myAdapter);
// display first fragment
myViewPager.setCurrentItem(0);
}
Create an Adapter that extends FragmentPagerAdapter (or FragmentStatePagerAdapter). It looks like this:
class MyAdapter extends FragmentPagerAdapter {
private final int[] titles = {R.string.main_activity_title, R.string.join_activity_title};
private final String[] fragments = {
MainFragment.class.getName(),
JoinFragment.class.getName()
};
private final Context ctx;
public MyAdapter(FragmentManager fm, Context ctx) {
super(fm);
this.ctx = ctx;
}
#Override
public CharSequence getPageTitle(int position) {
return ctx.getString(titles[position]);
}
#Override
public Fragment getItem(int position) {
return Fragment.instantiate(ctx, fragments[position]);
}
#Override
public int getCount() {
return titles.length;
}
}
(Optional) See those title thingys on top of each page, the ones you could click so you could fast forward to the other page? They're called tabhosts.
To use them, simply add a tabhost to your MainActivity layout and add this logic in MainActivity.java:
myViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// when user do a swipe the selected tab change
myTabHost.setSelectedNavigationItem(position);
}
});
//set titles
for (int i = 0; i < myAdapter.getCount(); i++) {
myTabHost.addTab(
myTabHost.newTab()
.setText(myAdapter.getPageTitle(i))
.setTabListener(this)
);
}
4. (Optional) Animations on 'fling'
So to animate when you switch fragments, you simple have to call this:
myViewPager.setPageTransformer(true, new DefaultTransformer());
And you can also customize it to your will.
I apologize if there are typos and other stuff. But this should cover majority of what you need.
You probably should change the architecture, transform your 2 activities into fragment and place them inside a viewPager. The viewPager will handle the sliding part for you.
I have a AlertDialog that is shown when there is an error and it prints 'Error, could not be added/whatever", I also have the result of the exception that I'd like to parse but not shown to all users, only to those that want to click on 'details' and read the exception.
AlertDialog.Builder dialogo1 = new AlertDialog.Builder(activity);
dialogo1.setTitle("Error");
dialogo1.setMessage("Could not update movie: " + result);
dialogo1.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogo1, int id) {
activity.finish();
}
});
dialogo1.show();
There is my code, pretty simple, right? I haven't been able to find this answer anyway and I'm starting to think it is just not possible
what you want is a custom alert dialog.
so for that you will have to us a layout and define your dialog.Then initialize your listeners etc like it was a normal view that you are defining
dialog_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/header_logo"
android:layout_width="match_parent"
android:layout_height="64dp"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="#string/app_name" />
<TextView
android:id="#+id/error"
andorid:text="Error Message"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/moreDetailsButton"
android:text="Click for more details"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/moreDetailsView"
android:text="Click for more details"
android:inputType="textPassword"
android:layout_width="match_parent"
android:visibility="gone";
android:layout_height="wrap_content"/>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/moreDetailsButton"
/>
</RelativeLayout>
and in your class
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_layout);
TextView error=(TextView)dialog.findViewById(R.id.error);
Button errorButton=(Button)dialog.findViewById(R.id.moreDetailsButton);
Button okButton=(Button)dialog.findViewById(R.id.ok);
//Do whatever you want with the rest of the dialog
// initialize all the onclick listeners a usual
errorButton.setOnClickListener(new View.onClickListener(){
#Override
public void onClick(View v){
TextView errorDetails=(TextView)dialog.findViewById(R.id.moreDetailsView);
errorDetails.setText(detailedErrorMessage) //add the details to this text view;
errorDetails.setVisibility(View.VISIBLE);
}
});
dialog.show();
Sorry if there are any syntactically errors if the ctrl+c,ctrl+v of this doesnt work.My IDE decided to misbehave today.
Follow this for details on a custom alert dialog.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/error"
android:text="Error Message"
android:layout_width="match_parent"
android:layout_height="37dp"
android:layout_toEndOf="#+id/moreDetailsView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/moreDetailsView"
android:text="No existen detalles para este tipo de error."
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="wrap_content" />
<Button
android:id="#+id/moreDetailsButton"
android:text="Click for more details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/moreDetailsView"
android:layout_below="#+id/error"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="139dp"
android:layout_height="wrap_content"
android:text="OK"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/moreDetailsView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</RelativeLayout>