Error in data Binding in Android - java

activity_main.xml
<data class="LoginDataBinding">
<variable
name="login"
type="com.example.itstym.logina.LoginViewModel" />
<variable
name="ModelClickListener"
type="com.example.itstym.logina.interface.ClickListener" />
</data>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textStyle="bold"
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome, Back"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="24dp"/>
<EditText
app:error="#{login.errorEmail}"
android:hint="#{login.emailHint}"
android:text="#{login.userEmailAddress}"
android:id="#+id/user_email_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:ems="10"
android:inputType="textEmailAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="#+id/textView"
android:layout_marginStart="8dp"/>
<EditText
app:error="#{login.errorPassword}"
android:text="#{login.userPassword}"
android:hint="#{login.passwordHint}"
android:id="#+id/user_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/user_email_address"/>
<Button
android:onClick="#{(v)->ModelClickListener.onSubmitButtonClick()}"
android:text="#{login.loginButtonText}"
android:id="#+id/login_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="#+id/user_password"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"/>
</android.support.constraint.ConstraintLayout>
MainActivity.java
public class SampleActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LoginDataBinding loginDataBinding= DataBindingUtil.setContentView(this,R.layout.activity_main);
LoginViewModel viewModel = new LoginViewModel("Enter Email Address","Enter password","Login");
loginDataBinding.setLogin(viewModel);
loginDataBinding.setModelClickListener(new ClickListener() {
#Override
public void onClick() {
Toast.makeText(getApplicationContext(), loginDataBinding.getLogin().getUserEmailAddress(), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), loginDataBinding.getLogin().getUserPassword(), Toast.LENGTH_SHORT).show();
}
});
}
}
But I am getting the error unresolved reference for setModelClickListener() but the setLogin() works perfertly normal.
One more thing why the DataBindingUtil.setContentView(this,R.layout.activity_main); return the LoginDataBinding instead of MainActivityBinding?

Related

Attempt to invoke ListView.setAdapter on a null object reference

I am trying to show am Integer Arraylist on a ListView. But I fail due to a null object error. My object is not null. Basically I am trying to make a scoreboard for 2 teams.
Here is my class:
public class PointsList extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.points_list);
Bundle b;
b = getIntent().getExtras();
ArrayList<Integer> scoreA = new ArrayList<Integer>();
ArrayList<Integer> scoreB = new ArrayList<Integer>();
ListView ListA, ListB;
scoreA = b.getIntegerArrayList("scoreA");
scoreB = b.getIntegerArrayList("scoreB");
ArrayAdapter arrayAdapterA = new ArrayAdapter<Integer>(
this, R.layout.points_list, R.id.scoreAtext, scoreA );
ArrayAdapter arrayAdapterB = new ArrayAdapter<Integer>(
this, R.layout.points_list, R.id.scoreBtext, scoreB );
ListA = (ListView) findViewById(R.id.scoreAList);
ListB = (ListView) findViewById(R.id.scoreBList);
ListA.setAdapter(arrayAdapterA);
ListB.setAdapter(arrayAdapterB);
}
}
And here is my xml file:
<?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:id="#+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background"
android:fillViewport="true">
<ListView
android:id="#+id/scoreBList"
android:layout_width="189dp"
android:layout_height="571dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.927"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.593" >
<TextView
android:id="#+id/scoreBtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</ListView>
<ImageView
android:id="#+id/imageView2"
style="#style/custom_image"
android:layout_width="376dp"
android:layout_height="619dp"
android:background="#drawable/dragon"
android:contentDescription="#string/logoDescription"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/teamNameA"
android:layout_width="150dp"
android:layout_height="62dp"
android:autoSizeTextType="none"
android:background="#drawable/red_button"
android:gravity="center_horizontal|center_vertical"
android:inputType="textShortMessage"
android:maxLines="1"
android:singleLine="true"
android:text="#string/team_a"
android:textColor="#000000"
android:textColorHint="#000000"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.103"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.049" />
<TextView
android:id="#+id/teamNameB"
android:layout_width="150dp"
android:layout_height="62dp"
android:autoSizeTextType="none"
android:background="#drawable/red_button"
android:gravity="center_horizontal|center_vertical"
android:inputType="textShortMessage"
android:maxLines="1"
android:singleLine="true"
android:text="#string/team_b"
android:textColor="#000000"
android:textColorHint="#000000"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.934"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.049" />
<View
android:id="#+id/divider"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="#+id/scoreAList"
android:layout_width="189dp"
android:layout_height="571dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.072"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.593" >
<TextView
android:id="#+id/scoreAtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</ListView>
</androidx.constraintlayout.widget.ConstraintLayout>
I have tried entering just a blank xml with a textview as I saw on other threads that this is what ArrayAdapter expects. But failed. Any help is greatly appreciated!

After trying a long not able to provide space between 2 card view elements inside a recycler view for both horizontal and vertical [duplicate]

This question already has answers here:
How to add dividers and spaces between items in RecyclerView
(45 answers)
Closed 2 years ago.
I want to add some space between 2 cards in both the horizontal recycler view and the vert recycler view but not able to add tried everything but won't getting a sol as u can see in the below image For horizontal both the cards are over each other.
My XML code in which I had defined recycler view Both hor. and ver. :-
<?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"
android:background="#color/AppBackground">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#color/AppBackground">
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:id="#+id/nestedsview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appBar">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:fontFamily="#font/playfair_display_bold_italic"
android:text="Looking for a delecious cake?"
android:textColor="#android:color/black"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="#id/recyclerViewHorizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:fontFamily="#font/playfair_display_bold_italic"
android:text="More"
android:textColor="#android:color/black"
android:textSize="20sp"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="200dp"
android:layout_marginEnd="20dp"
android:text="Button"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="266dp" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
app:layout_constraintBottom_toTopOf="#+id/textView6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.526"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linear"
tools:context=".FirstActivity">
</androidx.recyclerview.widget.RecyclerView>
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginTop="10dp"
android:fontFamily="#font/playfair_display_bold_italic"
android:text="Best Selling"
android:textColor="#android:color/black"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="#+id/recyclerViewVertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/recyclerViewHorizontal" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewVertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView6"
tools:context=".FirstActivity"
>
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
My Xml Code for card :
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
>
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="170dp"
android:layout_height="220dp"
android:padding="10dp"
android:elevation="10dp"
app:cardBackgroundColor="#color/CardColor"
app:cardCornerRadius="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraint"
android:layout_width="130dp"
android:layout_height="180dp"
android:layout_gravity="center"
android:background="#color/CardColor"
android:padding="5dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="90dp"
android:layout_height="100dp"
android:foregroundGravity="center_horizontal"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:fontFamily="#font/playfair_display_bold_italic"
android:gravity="center"
android:text="Great"
android:textColor="#android:color/black"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView" />
<TextView
android:id="#+id/textViewForPieceInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:fontFamily="#font/playfair_display_bold_italic"
android:text="Ram"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
<TextView
android:id="#+id/textViewforPrice"
android:text="Shyam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:fontFamily="#font/playfair_display_bold_italic"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textViewForPieceInfo" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginBottom="2dp"
app:fabSize="mini"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#drawable/ic_baseline_add_24" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
My recycler view class code:
package com.example.cako;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;
import android.os.Bundle;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class FirstActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
String []x={"Aaran", "Aaren", "Aarez", "Aarman", "Aaron", "Aaron-James", "Aarron", "Aaryan", "Abhinav","Ankit"};
String []image= {"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Aloo_Tikki_served_with_chutneys.jpg/180px-Aloo_Tikki_served_with_chutneys.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Aloo_Mattar.jpg/180px-Aloo_Mattar.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Aloo_Methi_%28Aaloo_Methi%29.JPG/180px-Aloo_Methi_%28Aaloo_Methi%29.JPG",
"https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Spicy_alloo_with_tadka_mirchi.jpg/180px-Spicy_alloo_with_tadka_mirchi.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Aloo_Tikki_served_with_chutneys.jpg/180px-Aloo_Tikki_served_with_chutneys.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Aloo_Mattar.jpg/180px-Aloo_Mattar.jpg"
};
Toolbar toolbar=findViewById(R.id.toolbar) ;
setSupportActionBar(toolbar);
RecyclerView horizontalrecyclerView =(RecyclerView)findViewById(R.id.recyclerViewHorizontal);
horizontalrecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false));
horizontalrecyclerView.setAdapter(new RecyclerVview(this,x,image));
RecyclerView verticalrecyclerView =(RecyclerView)findViewById(R.id.recyclerViewVertical);
verticalrecyclerView.setLayoutManager(new GridLayoutManager(this,2));
verticalrecyclerView.setAdapter(new RecyclerVview(this,x,image));
}
}
class RecyclerVview extends RecyclerView.Adapter<RecyclerVview.Holder>{
Activity co;
String[] name;
String[] image;
public RecyclerVview(Context applicationContext, String[] x, String[] image) {
co=(Activity)applicationContext;
name=x;
this.image=image;
}
#NonNull
#Override
public Holder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v=LayoutInflater.from(co).inflate(R.layout.layout_for_horizontal_recyclerview,parent,false);
return new Holder(v);
}
#Override
public void onBindViewHolder(#NonNull final Holder holder, int position) {
holder.textViewForItemName.setText(name[position]);
Glide.with(co).load(image[position]).into(holder.image);
holder.floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(co, "yesWorking", Toast.LENGTH_SHORT).show();
Log.i("yes","log");
}
});
holder.textViewForPrice.setText("30$");
holder.textViewForPieceInfo.setText("Single Piece Only.");
}
#Override
public int getItemCount() {
return name.length;
}
public class Holder extends RecyclerView.ViewHolder{
ImageView image;
TextView textViewForItemName;
FloatingActionButton floatingActionButton;
TextView textViewForPrice;
TextView textViewForPieceInfo;
public Holder(#NonNull View itemView) {
super(itemView);
image=itemView.findViewById(R.id.imageView);
textViewForItemName=itemView.findViewById(R.id.textView4);
floatingActionButton=itemView.findViewById(R.id.floatingActionButton);
textViewForPieceInfo=itemView.findViewById(R.id.textViewForPieceInfo);
textViewForPrice=itemView.findViewById(R.id.textViewforPrice);
}
}
}
Add margin for the CarView layout and inside the ContraintLayout
and Use this for your card.
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="wrap_content"
android:layout_height="wrap_content"
android:margin="8dp"
>
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="170dp"
android:layout_height="220dp"
android:margin="8dp"
android:elevation="10dp"
app:cardBackgroundColor="#color/CardColor"
app:cardCornerRadius="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraint"
android:layout_width="130dp"
android:layout_height="180dp"
android:layout_gravity="center"
android:background="#color/CardColor"
android:padding="5dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="90dp"
android:layout_height="100dp"
android:foregroundGravity="center_horizontal"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:fontFamily="#font/playfair_display_bold_italic"
android:gravity="center"
android:text="Great"
android:textColor="#android:color/black"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView" />
<TextView
android:id="#+id/textViewForPieceInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:fontFamily="#font/playfair_display_bold_italic"
android:text="Ram"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
<TextView
android:id="#+id/textViewforPrice"
android:text="Shyam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:fontFamily="#font/playfair_display_bold_italic"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textViewForPieceInfo" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginBottom="2dp"
app:fabSize="mini"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#drawable/ic_baseline_add_24" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

How to set a textView to visible after an onClick event and Activity change?

I have three passages in my scrollview that need to each become visible after an onclick event on one of three buttons.
I have currently set them to all invisible. And since I cannot get it to work, I am only trying it out with one of the passages.
Because of this I created a private textview constant for only the first passage. But after I pass the intent to switch the activity, I also try to turn the view on that package to visible.
I have included my MainActivity.java and the xml file I used to set invisible.
package com.example.threebuttons;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView passage1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
passage1 = findViewById(R.id.passage_1);
}
public void launchPassageOne(View view) {
passage1.setVisibility(view.VISIBLE);
Intent intent = new Intent(this, PassageActivity.class);
startActivity(intent) ;
}
public void launchPassageTwo(View view) {
Intent intent = new Intent(this, PassageActivity.class);
startActivity(intent) ;
}
public void launchPassageThree(View view) {
Intent intent = new Intent(this, PassageActivity.class);
startActivity(intent) ;
}
}
<?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=".PassageActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/passage_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:text="#string/passage1"
android:visibility="invisible"/>
<EditText
android:id="#+id/passage_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:text="#string/passage2"
android:visibility="invisible"/>
<EditText
android:id="#+id/passage_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:text="#string/passage3"
android:visibility="invisible"/>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
My program just crashes. And I cannot find any error messages.
How can I make the packages visible whenever I want the activity to change?
There are three passages that I want to each become visible for the respective button, then turn invisible if the back button is pressed.
It seams the three views are in the started activity. And so you can't change their visibility because they haven't been created.
Add this before you start the activity
intent.putExtra("passageNum", 1)
Then call startActivity(intent)
In PassageAactivity onCreate do the following :
If (getIntent().hasExtra("passageNum") && getIntent().getExtras().getInt("passageNum") == 1)
passage1.setVisibility(View.VISIBLE)
And so on for the other views
passage1.setVisibility(View.VISIBLE)
read more about views and how to modify their behavior here :
https://developer.android.com/reference/android/view/View
Use View.VISIBLE, capital V, it's a integer constant from the View class. Remove the View argument from the method launchPassageOne:
public void launchPassageOne() {
passage1.setVisibility(View.VISIBLE);
Intent intent = new Intent(this, PassageActivity.class);
startActivity(intent) ;
}
Image click hereWhatever I understood with your code I got that you are not initializing your methods in On create, whatever defined outside the On create will not be used until or unless called from inside On create method.
Designed some code may help you understanding in a better way.
In below code, I made text views scrollable, but you can only scroll if text is too long to fill the entire textview.
MainActivity.java
package com.example.threebuttons;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText edit1;
EditText edit2;
EditText edit3;
Button btn1;
Button btn2;
Button btn3;
Button btnV;
Button btnI;
TextView t1;
TextView t2;
TextView t3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// defining id for edit texts
edit1=findViewById(R.id.passage_1);
edit2=findViewById(R.id.passage_2);
edit3=findViewById(R.id.passage_3);
// defining id for buttons
btn1=findViewById(R.id.button_1);
btn2=findViewById(R.id.button_2);
btn3=findViewById(R.id.button_3);
btnV=findViewById(R.id.btnvisi);
btnI=findViewById(R.id.btninvisi);
// defining id for text views
t1=findViewById(R.id.textview1);
t2=findViewById(R.id.textview2);
t3=findViewById(R.id.textview3);
// making text views scrollable
t1.setMovementMethod(new ScrollingMovementMethod());
t2.setMovementMethod(new ScrollingMovementMethod());
t3.setMovementMethod(new ScrollingMovementMethod());
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t1.setText(edit1.getText().toString());
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t2.setText(edit2.getText().toString());
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t3.setText(edit3.getText().toString());
}
});
btnV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Making passages Visible
t1.setVisibility(View.VISIBLE);
t2.setVisibility(View.VISIBLE);
t3.setVisibility(View.VISIBLE);
}
});
btnI.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t1.setVisibility(View.INVISIBLE);
t2.setVisibility(View.INVISIBLE);
t3.setVisibility(View.INVISIBLE);
}
});
}
}
Set activitymain.xml as below
<?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"
android:background="#2196F3"
tools:context=".MainActivity">
<EditText
android:id="#+id/passage_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:hint="passage 1"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.016" />
<EditText
android:id="#+id/passage_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:hint="Passage 2"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.102" />
<EditText
android:id="#+id/passage_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:hint="Passage 3"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.194" />
<Button
android:id="#+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="#+id/passage_2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/passage_1"
app:layout_constraintTop_toTopOf="#+id/passage_1" />
<Button
android:id="#+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="#+id/passage_3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/passage_2"
app:layout_constraintTop_toTopOf="#+id/passage_2" />
<Button
android:id="#+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="#+id/passage_3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/passage_3"
app:layout_constraintTop_toTopOf="#+id/passage_3" />
<TextView
android:id="#+id/textview1"
android:layout_width="319dp"
android:layout_height="74dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="#3F51B5"
android:hint="Passage 1"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.486"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.446"
tools:visibility="invisible" />
<TextView
android:id="#+id/textview2"
android:layout_width="319dp"
android:layout_height="74dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="#3F51B5"
android:hint="Passage 2"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.672"
tools:visibility="invisible" />
<TextView
android:id="#+id/textview3"
android:layout_width="319dp"
android:layout_height="74dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="#3F51B5"
android:hint="Passage 3"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.486"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.895"
tools:visibility="invisible" />
<Button
android:id="#+id/btnvisi"
android:layout_width="175dp"
android:layout_height="44dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="Passage Visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.036"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.299" />
<Button
android:id="#+id/btninvisi"
android:layout_width="174dp"
android:layout_height="47dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="passage invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.886"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />
</androidx.constraintlayout.widget.ConstraintLayout>
I hope it makes you understand in a better way,
Thanks

Button onClick() causing force stop

Hello StackOverFlowGeeks!
Currently, I'm learning how to write a basic Android app, that has few buttons.
I have four activities(Java Classes) and four xmls.
I have the RegisterScreen.java that has two buttons(Register and Back button). Currently, I am trying to get back from this RegisterScreen.java to SignInScreen.java. The problem is that when I click on the back button it causes a failure and the app is forced to shut down...
So I've uploaded this way:
1) RegisterScreen.java
2) SignInScreen.java
3) RegisterScreen XML file
4) SignInScreen XML file
Thanks in advance.
RegisterScreen:
public class RegisterScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_screen);
}
public void onClickRegisterButton(View view){
}
public void onClickBackButton(View view) {
Button backBtn = (Button) findViewById(R.id.backBtn);
backBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(RegisterScreen.this, SignInScreen.class);
RegisterScreen.this.startActivity(intent);
}
});
}
}
SignInScreen:
public class SignInScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_screen);
}
#SuppressLint("SetTextI18n")
public void onClickSignInButton(View view){
TextView username = (TextView)findViewById(R.id.username_text);
TextView password = (TextView)findViewById(R.id.password_text);
Button signIn = (Button)findViewById(R.id.sign_in_button);
if (username.getText().toString().equals("Ivan Simeonov") && password.getText().toString().equals("Ivan9603116245")){
signIn.setText("Welcome " + username.getText().toString());
signIn.setBackgroundColor(Color.YELLOW);
setContentView(R.layout.activity_logged_screen);
}else{
signIn.setText("The input data is incorrect! Try again!");
signIn.setBackgroundColor(Color.GREEN);
}
}
public void onClickRegisterButton(View view){
Button register = (Button)findViewById(R.id.register_button);
setContentView(R.layout.activity_register_screen);
}
public void onClickResetPasswordButton(View view){
Button resetPassword = (Button)findViewById(R.id.reset_password_button);
setContentView(R.layout.activity_reset_password_screen);
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.megat0n.startproject.RegisterScreen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView_Register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/register_your_account_by_filling_up_the_following_data"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="24sp"
android:textStyle="bold" />
<EditText
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_username"
android:inputType="textPersonName" />
<EditText
android:id="#+id/first_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_firstname"
android:inputType="textPersonName" />
<EditText
android:id="#+id/last_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_lastname"
android:inputType="textPersonName" />
<EditText
android:id="#+id/birth_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_birthdate"
android:inputType="date" />
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_email"
android:inputType="textPersonName" />
<EditText
android:id="#+id/email_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/confirm_email"
android:inputType="textPersonName" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_password"
android:inputType="textPassword" />
<EditText
android:id="#+id/password_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/confirm_password"
android:inputType="textPassword" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/reg_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/register"
android:onClick="onClickRegisterButton"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/backBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/back"
android:onClick="onClickBackButton"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.megat0n.startproject.SignInScreen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="#string/welcome_to_the_home_screen"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="24sp"
android:textStyle="bold" />
<EditText
android:id="#+id/username_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/username"
android:inputType="textPersonName"
android:textAlignment="center"
android:textStyle="bold" />
<EditText
android:id="#+id/password_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/password"
android:inputType="textPassword"
android:textAlignment="center"
android:textStyle="bold" />
<Button
android:id="#+id/sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickSignInButton"
android:text="#string/sign_in"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/register_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickRegisterButton"
android:text="#string/register"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/reset_password_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickResetPasswordButton"
android:text="#string/reset_password"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Use this onClickBackButton() instead of yours. If you use onClick attribute in your layout, you don't need creating button and click listener.
public void onClickBackButton(View view) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
Try in RegisterScreen use onBackPressed() method:
backBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});

switch to another activity with swipe is not working

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.

Categories

Resources