App force closes when I click on a page - java

I designed a profile page of an android app. But, it force closes when I click to this page. How can I prevent crash?
Reference of my profile page
res/layout/fragment_profile_vp.xml
<FrameLayout 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.ray.myoufd.Profile_vp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/header_cover_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="#drawable/profilebg" />
<ImageButton
android:id="#+id/user_profile_photo"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_below="#+id/header_cover_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="-60dp"
android:elevation="5dp"
android:scaleType="centerCrop"
android:src="#drawable/defaultpropic" />
<RelativeLayout
android:id="#+id/profile_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header_cover_image"
android:background="#color/grey"
android:elevation="4dp"
android:paddingBottom="24dp">
<TextView
android:id="#+id/user_profile_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:text="Name"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="#+id/user_profile_short_bio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/user_profile_name"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:text="profile short bio"
android:textColor="#fff"
android:textSize="14sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/profile_layout"
android:layout_marginTop="0dp"
android:orientation="horizontal"
android:id="#+id/linearLayout1"
android:background="#color/white">
<TextView
android:layout_height="wrap_content"
android:text="Name:"
android:layout_width="wrap_content"
android:textSize="18sp"
android:layout_margin="5dp"
android:textColor="#color/green"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textColor="#color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/profile_layout"
android:layout_marginTop="43dp"
android:orientation="horizontal"
android:id="#+id/linearLayout2"
android:background="#color/white">
<TextView
android:layout_height="wrap_content"
android:text="Gender:"
android:layout_width="wrap_content"
android:textSize="18sp"
android:layout_margin="5dp"
android:textColor="#color/green"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textColor="#color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/profile_layout"
android:layout_marginTop="86dp"
android:orientation="horizontal"
android:id="#+id/linearLayout3"
android:background="#color/white" >
<TextView
android:layout_height="wrap_content"
android:text="Email:"
android:layout_width="wrap_content"
android:textSize="18sp"
android:layout_margin="5dp"
android:textColor="#color/green"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textColor="#color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/profile_layout"
android:layout_marginTop="129dp"
android:orientation="horizontal"
android:id="#+id/linearLayout4"
android:background="#color/white">
<TextView
android:layout_height="wrap_content"
android:text="Date o birth:"
android:layout_width="wrap_content"
android:textSize="18sp"
android:layout_margin="5dp"
android:textColor="#color/green"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textColor="#color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/profile_layout"
android:layout_marginTop="172dp"
android:orientation="vertical"
android:id="#+id/linearLayout5"
android:background="#color/white">
<TextView
android:layout_height="wrap_content"
android:text="Message to friend"
android:layout_width="wrap_content"
android:textSize="18sp"
android:layout_margin="5dp"
android:textColor="#color/green"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textColor="#color/black"/>
</LinearLayout>
<Button
android:text="Logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_marginStart="115dp"
android:layout_marginLeft="115dp"
android:background="#color/green"
android:layout_marginTop="605dp" />
<Button
android:text="Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:layout_marginTop="605dp"
android:layout_marginLeft="210dp"
android:layout_marginStart="210dp"
android:background="#color/green"
/>
</RelativeLayout>
</ScrollView>
</FrameLayout>
java/com.example.ray.myoufd/Profile_vp.java
package com.example.ray.myoufd;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Profile_vp extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_profile_vp, container, false);
}
}
java/com.example.ray.myoufd/MainPage.java
package com.example.ray.myoufd;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import com.gigamole.navigationtabbar.ntb.NavigationTabBar;
import java.util.ArrayList;
import java.util.List;
public class MainPage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
initUI();
}
private void initUI() {
final ViewPager viewPager = (ViewPager) findViewById(R.id.vp_horizontal_ntb);
final String[] colors = getResources().getStringArray(R.array.default_preview);
final NavigationTabBar navigationTabBar = (NavigationTabBar) findViewById(R.id.ntb_horizontal);
final ArrayList<NavigationTabBar.Model> models = new ArrayList<>();
models.add(
new NavigationTabBar.Model.Builder(
getResources().getDrawable(R.drawable.home),
Color.parseColor(colors[0]))
.title("Home")
.build()
);
models.add(
new NavigationTabBar.Model.Builder(
getResources().getDrawable(R.drawable.search),
Color.parseColor(colors[1]))
.title("Search")
.build()
);
models.add(
new NavigationTabBar.Model.Builder(
getResources().getDrawable(R.drawable.friend),
Color.parseColor(colors[2])).
badgeTitle("123")
.title("Friend")
.build()
);
models.add(
new NavigationTabBar.Model.Builder(
getResources().getDrawable(R.drawable.personal),
Color.parseColor(colors[3]))
.title("Profile")
.build()
);
models.add(
new NavigationTabBar.Model.Builder(
getResources().getDrawable(R.drawable.chat),
Color.parseColor(colors[4]))
.title("Discuss")
.build()
);
navigationTabBar.setModels(models);
Search_vp search_Vp = new Search_vp();
Home_vp home_vp = new Home_vp();
Friend_vp friend_vp =new Friend_vp();
Discuss_vp discuss_vp = new Discuss_vp();
Profile_vp profile_vp = new Profile_vp();
List<Fragment> fragmentList = new ArrayList<>();
fragmentList.add(home_vp);
fragmentList.add(search_Vp);
fragmentList.add(friend_vp);
fragmentList.add(profile_vp);
fragmentList.add(discuss_vp);
FragmentPageAdapter fragmentPageAdapter =new FragmentPageAdapter(getSupportFragmentManager(),fragmentList);
viewPager.setAdapter(fragmentPageAdapter);
navigationTabBar.setViewPager(viewPager, 0);
navigationTabBar.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(final int position, final float positionOffset, final int positionOffsetPixels) {
}
#Override
public void onPageSelected(final int position) {
navigationTabBar.getModels().get(position).hideBadge();
}
#Override
public void onPageScrollStateChanged(final int state) {
}
});
navigationTabBar.postDelayed(new Runnable() {
#Override
public void run() {
for (int i = 0; i < navigationTabBar.getModels().size(); i++) {
final NavigationTabBar.Model model = navigationTabBar.getModels().get(i);
navigationTabBar.postDelayed(new Runnable() {
#Override
public void run() {
model.showBadge();
}
}, i * 100);
}
}
}, 500);
}
}

Related

Why do I get a null object reference on my RealtiveLayout.setBackgroundColor although I have initialized it

Here is my java code:
package com.example.project_final;
import static android.content.ContentValues.TAG;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import java.util.ArrayList;
public class history extends AppCompatActivity {
FirebaseFirestore Fstore;
String userID;
FirebaseAuth fAuth ;
int times;
int heart;
int avg_heart;
String date;
int count = 0;
TextView Data;
RelativeLayout [] L = new RelativeLayout[7];
TextView [] hr = new TextView[7];
TextView [] dates = new TextView[7];
ImageView [] img = new ImageView[7];
TextView last;
ArrayList<Integer> intheart = new ArrayList<>();
ArrayList<String> intdates = new ArrayList<>();
TextView average;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
Fstore = FirebaseFirestore.getInstance();
fAuth = FirebaseAuth.getInstance();
userID= fAuth.getUid();
DocumentReference documentReference = Fstore.collection("users").document(userID);
Data = findViewById(R.id.txtNodata);
Intent intent1 = getIntent();
String signup = intent1.getStringExtra("signup");
last = findViewById(R.id.txtlast);
average = findViewById(R.id.txtaverage);
for(int i = 0; i<=6 ;i++) {
int resourceId = this.getResources().getIdentifier("layout"+ i+1, "id", this.getPackageName());
L[i] = findViewById(resourceId);
}
for(int i = 0; i<=6 ;i++) {
int resourceId = this.getResources().getIdentifier("heartrate"+ i+1, "id", this.getPackageName());
hr[i] = findViewById(resourceId);
}
for(int i = 0; i<=6 ;i++) {
int resourceId = this.getResources().getIdentifier("txtdate"+ i+1, "id", this.getPackageName());
dates[i] = findViewById(resourceId);
}
for(int i = 0; i<=6 ;i++) {
int resourceId = this.getResources().getIdentifier("img"+ i+1, "id", this.getPackageName());
img[i] = findViewById(resourceId);
}
documentReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
times = Math.toIntExact(task.getResult().getLong("times"));
avg_heart = Math.toIntExact(task.getResult().getLong("average_heart_rate"));
for (int i = 0 ; i <times ;i++){
intdates.add(task.getResult().getString("date" + i));
intheart.add(Math.toIntExact(task.getResult().getLong("heart"+i)));
}
}
});
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(!(intheart.isEmpty()||intdates.isEmpty())){
int count =0;
if(times>7){
times=7;
}
last.setText("Last "+ times +" Days history");
for(int i =0 ; i<intdates.size();i++){
if(1.2*avg_heart<intheart.get(i)||0.8*avg_heart<intheart.get(i)){
L[i].setBackgroundColor(R.drawable.color_gradient1);
img[i].setImageResource(R.drawable.sad);
}
else{
L[i].setBackgroundColor(R.drawable.gradient_color);
img[i].setImageResource(R.drawable.happy);
}
dates[i].setText(intdates.get(i));
hr[i].setText(intheart.get(i)+" Bpm");
L[i].setVisibility(View.VISIBLE);
}
}
else{
handler.postDelayed(this, 100);
}
}
}, 100);
}
}
Here is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout 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:padding="20dp"
android:orientation="vertical"
tools:context=".history">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/txtlast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text=""
android:textSize="30sp"
android:layout_gravity="center"
android:textColor="#000"
android:textStyle="bold"
/>
<TextView
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text=""
android:id="#+id/txtaverage"
android:textColor="#000"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_color"
android:padding="10dp"
android:visibility="invisible"
>
<TextView
android:id="#+id/txtday1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 1"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtdate1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday1"
android:layout_marginTop="10dp"
android:textColor="#ffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate1"
android:id="#+id/heartrate1"
android:layout_marginTop="10dp"
android:textColor="#ffff"
/>
<ImageView
android:id="#+id/img1"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:id="#+id/layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:background="#drawable/color_gradient1"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 2"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday2"
/>
<TextView
android:id="#+id/txtdate2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday2"
android:layout_marginTop="10dp"
android:textColor="#ffff" />
<TextView
android:id="#+id/heartrate2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate2"
android:layout_marginTop="10dp"
android:textColor="#ffff" />
<ImageView
android:id="#+id/img2"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_color"
android:id="#+id/layout3"
android:visibility="invisible"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 3"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday3"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday3"
android:textColor="#ffff"
android:layout_marginTop="10dp"
android:id="#+id/txtdate3"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate3"
android:layout_marginTop="10dp"
android:textColor="#ffff"
android:id="#+id/heartrate3"
/>
<ImageView
android:id="#+id/img3"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:id="#+id/layout4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/color_gradient1"
android:visibility="invisible"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 4"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday4"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday4"
android:textColor="#ffff"
android:layout_marginTop="10dp"
android:id="#+id/txtdate4"
/>
<TextView
android:id="#+id/heartrate4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate4"
android:layout_marginTop="10dp"
android:textColor="#ffff" />
<ImageView
android:id="#+id/img4"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:id="#+id/layout5"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_color"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 1"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday5"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday5"
android:textColor="#ffff"
android:layout_marginTop="10dp"
android:id="#+id/txtdate5"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate5"
android:layout_marginTop="10dp"
android:textColor="#ffff"
android:id="#+id/heartrate5"
/>
<ImageView
android:id="#+id/img5"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_color"
android:id="#+id/layout6"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 6"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday6"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday6"
android:textColor="#ffff"
android:layout_marginTop="10dp"
android:id="#+id/txtdate6"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate6"
android:id="#+id/heartrate6"
android:layout_marginTop="10dp"
android:textColor="#ffff"
/>
<ImageView
android:id="#+id/img6"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:visibility="invisible"
android:id="#+id/layout7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_color"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 7"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday7"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday7"
android:textColor="#ffff"
android:layout_marginTop="10dp"
android:id="#+id/txtdate7"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate7"
android:layout_marginTop="10dp"
android:textColor="#ffff"
android:id="#+id/heartrate7"
/>
<ImageView
android:id="#+id/img7"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<TextView
android:id="#+id/txtNodata"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:fontFamily="#font/lovelo"
android:gravity="center"
android:text="No data Available"
android:textColor="#color/colorRed"
android:textSize="20sp"
android:visibility="invisible" />
</LinearLayout>
</ScrollView>
When I run the code I get this error on a null object reference when I try to setbackground color inside the handler:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setBackgroundColor(int)'
The code should run fine since I initialized all the views. I've been stuck for a while I don't know where is the problem help.
I think you are not getting the name correct, so it's not initializing it, name must be a string as a whole :
for(int i = 0; i<=6 ;i++) {
int resourceId = this.getResources().getIdentifier("layout" + (i+1).toString(), "id", this.getPackageName());
L[i] = findViewById(resourceId);
}

unable to transfer some data to a new page

I want to show the data I got from json in my android project in the search layout section, but the data is not visible. Can you help me?
My Code:
public void searchView (View view){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
SearchFragment searchFragment = new SearchFragment();
fragmentTransaction.replace(R.id.frameLayout, searchFragment).commit();
enterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
city = cityAdd.getEditText().getText().toString().trim();
String url = "https://api.openweathermap.org/data/2.5/weather?q="+city+"&appid="+apikey;
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new com.android.volley.Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
Log.d("Temperature",(url));
JSONObject temps = response.getJSONObject("main");
String temperatures = temps.getString("temp");
country.setText(city);
temp.setText(temperatures);
Intent intent = new Intent(MainActivity.this, SearchFragment.class);
intent.putExtra("city", country.getText().toString());
intent.putExtra("temp",temp.getText().toString());
startActivity(intent);
} catch (JSONException e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}, new com.android.volley.Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Please check the city name", Toast.LENGTH_SHORT).show();
}
}
);
queue.add(request);
}
});
}
Search Fragment.java
package com.nisaefendioglu.weatherapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SearchView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class SearchFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.search_layout, container,false);
TextView country = viewGroup.findViewById(R.id.country);
TextView temp = viewGroup.findViewById(R.id.temp);
TextView back = viewGroup.findViewById(R.id.back);
return viewGroup;
}
}
XML
Country İtem
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="3dp"
android:layout_marginHorizontal="8dp"
android:layout_marginTop="8dp">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="7dp"
android:layout_marginHorizontal="15dp">
<TextView
android:id="#+id/countryName"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentStart="true"
android:layout_marginEnd="30dp"
android:textColor="#color/black"
android:text="Türkiye"
android:layout_centerVertical="true"
android:gravity="center"
android:textSize="15dp" ></TextView>
<TextView
android:id="#+id/temperature"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_marginEnd="30dp"
android:textColor="#color/black"
android:text="Temperature"
android:layout_centerVertical="true"
android:gravity="center"
android:textSize="15dp" ></TextView>
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
Main xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="12dp"
>
<EditText
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:hint="Enter City" />
<Button
android:onClick="searchView"
android:id="#+id/enterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="300dp"
android:text="Enter"
android:textAllCaps="false"
/>
</RelativeLayout>
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="#drawable/weather"/>
<RelativeLayout
android:id="#+id/activityRelative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="12dp"
>
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="30dp"
android:textColor="#color/black"
android:text="Country Name"
android:textSize="15dp" ></TextView>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_marginEnd="30dp"
android:text="Temperature"
android:textColor="#color/black"
android:textSize="15dp" ></TextView>
</RelativeLayout>
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/countries"
tools:listitem="#layout/country_item_layout"
app:layoutManager="LinearLayoutManager"
/>
</FrameLayout>
</LinearLayout>
Search Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:id="#+id/country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="30dp"
android:textColor="#color/black"
android:text=""
android:textSize="15dp" ></TextView>
<TextView
android:id="#+id/temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginEnd="30dp"
android:text=""
android:textColor="#color/black"
android:textSize="15dp" ></TextView>
<TextView
android:onClick="tempBack"
android:id="#+id/back"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="left"
android:layout_marginLeft="20dp"
android:layout_marginTop="200dp"
android:gravity="center"
android:text="←"
android:textColor="#ff793f"
android:textSize="40dp"></TextView>
</LinearLayout>
Hello, I want to show the data I got from json in my android project in the search layout section, but the data is not visible. Can you help me?
You are retrieving your data in your main activity and sending the data through your intent
intent.putExtra("city", country.getText().toString()); intent.putExtra("temp",temp.getText().toString());
However, you are not fetching the data that you sent through intent in your fragment.
You have to fetch them according to the key values by which you sent.
In your onCreateView() in your fragment, you can fetch each data you sent through your intent in the form:
String city = getArguments().getString("city");

App bugged: ImageView doesn't appear

I don't understand why the app doesn't work when I launch it.
First of all, this is what I want:
Spaceship appears perfectly
And this is what appears when I launch the game:
Spaceship disappears!
I dont know what is wrong. I haven't an atribute which makes invisible the ImageView... Here is my code:
game_activity.xml (game design)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.spaceinvaders.GameActivity">
<ImageView
android:id="#+id/fondo_juego"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/fondo3" />
<ImageView
android:id="#+id/enemigo"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:src="#drawable/enemigodiseno11"/>
<Button
android:id="#+id/disparo"
android:layout_width="wrap_content"
android:layout_height="90dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
android:onClick="dispara"
android:visibility="visible" />
<ImageView
android:id="#+id/municion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/disparo"
android:layout_centerHorizontal="true"
android:src="#drawable/municion"
android:visibility="invisible" />
<ImageView
android:id="#+id/nave"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:src="#drawable/diseno11" />
<Button
android:id="#+id/control_izquierda"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#android:color/transparent"
android:onClick="actualizaPosicion"
android:visibility="visible" />
<Button
android:id="#+id/control_derecha"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:onClick="actualizaPosicion"
android:visibility="visible" />
</RelativeLayout>
activity_main.xml (This is the principal screen)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/principal_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.spaceinvaders.MainActivity">
<ImageView
android:id="#+id/fondo_pantalla_principal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/fondo2" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:adjustViewBounds="true"
android:src="#drawable/iconotitulo"
android:layout_centerHorizontal="true"/>
<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/iconotitulo1"/>
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical">
<ImageButton
android:id="#+id/play_boton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:scaleType="centerCrop"
android:onClick="iniciaJuego"
android:src="#drawable/boton1" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_margin="10dp">
<ImageButton
android:id="#+id/opcion_boton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
android:scaleType="centerCrop"
android:src="#drawable/boton2" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
popup_activity.xml (a simple popup which allow the user to change some parameters)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#839ceaac"
android:orientation="vertical">
<ImageButton
android:id="#+id/volver_boton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_marginRight="2dp"
android:layout_marginTop="3dp"
android:background="#android:color/transparent"
android:scaleType="centerCrop"
android:src="#drawable/cerrar" />
<TextView
android:id="#+id/titulo_opciones"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="6dp"
android:layout_marginTop="5dp"
android:text="Opciones gráficas"
android:textSize="20dp"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="#id/titulo_opciones"
android:background="#9dc8a6" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/titulo_opciones"
android:layout_marginBottom="8dp"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/fondo_titulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:text="Fondo"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/fondo_1"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/fondo_titulo"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="actualizaFondo"
android:scaleType="centerCrop"
android:src="#drawable/fondo" />
<ImageView
android:id="#+id/fondo_2"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/fondo_1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="actualizaFondo"
android:scaleType="centerCrop"
android:src="#drawable/fondo1" />
<ImageView
android:id="#+id/fondo_3"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/fondo_2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="actualizaFondo"
android:scaleType="centerCrop"
android:src="#drawable/fondo3" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/nave_titulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:text="Nave"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/nave_1"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/nave_titulo"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="actualizaNave"
android:src="#drawable/diseno11" />
<ImageView
android:id="#+id/nave_2"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/nave_1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="actualizaNave"
android:src="#drawable/diseno21" />
<ImageView
android:id="#+id/nave_3"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/nave_2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="actualizaNave"
android:src="#drawable/diseno31" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/enemigos_titulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:text="Enemigos"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/enemigo_1"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/enemigos_titulo"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="actualizaEnemigo"
android:rotation="180"
android:src="#drawable/enemigodiseno11" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
GameActivity.java (game_activity.xml functionality)
package com.example.android.spaceinvaders;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class GameActivity extends AppCompatActivity {
ImageView municion;
ImageView nave;
ImageView fondoJuego;
ImageView enemigo;
Button botonDisparo;
Handler manejaDisparo = new Handler();
Handler manejaEnemigo = new Handler();
final int movimiento = 30;
final int movimientoEnemigo = 20;
boolean inicioAFin = false;
int ladeadoIzq, ladeadoDer, frontal, disparo;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_activity);
municion = (ImageView) findViewById(R.id.municion);
nave = (ImageView) findViewById(R.id.nave);
enemigo = (ImageView) findViewById(R.id.enemigo);
fondoJuego = (ImageView) findViewById(R.id.fondo_juego);
botonDisparo = (Button) findViewById(R.id.disparo);
Intent i = getIntent();
if (i != null) {
String data = i.getStringExtra("arg");
introduceCambios(data);
}
manejaEnemigo.postDelayed(accionMovimiento, 0);
}
private void introduceCambios(String data) {
String[] info = data.split(" ");
int idFondo = getResources().getIdentifier(info[0], "drawable", getPackageName());
fondoJuego.setImageResource(idFondo);
int idNave = getResources().getIdentifier(info[1], "drawable", getPackageName());
cambiosMovilidad(idNave);
nave.setImageResource(frontal);
int idEnemigo = getResources().getIdentifier(info[2], "drawable", getPackageName());
enemigo.setImageResource(idEnemigo);
}
public void actualizaPosicion(View v) {
switch (v.getId()) {
case (R.id.control_derecha):
if (!seSale("der", "CU")) {
nave.setImageResource(ladeadoIzq);
nave.setX(nave.getX() - movimiento);
}
break;
case R.id.control_izquierda:
if (!seSale("izq", "CU")) {
nave.setImageResource(ladeadoDer);
nave.setX(nave.getX() + movimiento);
}
break;
}
}
private void cambiosMovilidad(int idNave) {
switch (idNave) {
case 2130837589:
frontal = R.drawable.diseno11;
ladeadoDer = R.drawable.diseno13;
ladeadoIzq = R.drawable.diseno12;
disparo = R.drawable.municion;
break;
case 2130837592:
frontal = R.drawable.diseno21;
ladeadoDer = R.drawable.diseno23;
ladeadoIzq = R.drawable.diseno22;
disparo = R.drawable.municion1;
break;
case 2130837595:
frontal = R.drawable.diseno31;
ladeadoDer = R.drawable.diseno33;
ladeadoIzq = R.drawable.diseno32;
break;
}
}
public void dispara(View v) {
nave.setImageResource(frontal);
municion.setImageResource(disparo);
municion.setX(nave.getX() + (((nave.getWidth()) / 2) - 5));
municion.setY(nave.getY());
municion.setVisibility(View.VISIBLE);
botonDisparo.setEnabled(false);
manejaDisparo.postDelayed(accionDisparo, 0);
}
Runnable accionDisparo = new Runnable() {
#Override
public void run() {
municion.setY(municion.getY() - 50);
if (llegaAlFinal()) {
municion.setVisibility(View.INVISIBLE);
manejaDisparo.removeCallbacks(accionDisparo);
botonDisparo.setEnabled(true);
}
manejaDisparo.postDelayed(this, 80);
}
};
Runnable accionMovimiento = new Runnable() {
#Override
public void run() {
if (inicioAFin) {
enemigo.setImageResource(R.drawable.enemigodiseno12);
enemigo.setX(enemigo.getX() + movimientoEnemigo);
} else {
enemigo.setImageResource(R.drawable.enemigodiseno13);
enemigo.setX(enemigo.getX() - movimientoEnemigo);
}
if (seSale("izq", "IA") || seSale("der", "IA"))
inicioAFin = !inicioAFin;
manejaEnemigo.postDelayed(this, 80);
}
};
private boolean llegaAlFinal() {
return municion.getY() <= 20;
}
private boolean seSale(String direccion, String jugador) {
switch (direccion) {
case "izq":
switch (jugador) {
case "CU":
return (nave.getX() + movimiento + nave.getWidth()) > findViewById(R.id.activity_main).getWidth();
case "IA":
return (enemigo.getX() + movimiento + enemigo.getWidth()) > findViewById(R.id.activity_main).getWidth();
}
case "der":
switch (jugador) {
case "CU":
return (nave.getX() - movimiento) < 0;
case "IA":
return (enemigo.getX() - movimiento) < 0;
}
}
return true;
}
}
MainActivity.java (activity_main.xml and popup_activity.xml functionality)
package com.example.android.spaceinvaders;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
ImageButton opcionBoton;
private PopupWindow popup;
private RelativeLayout layoutPrincipal;
String[] resultados;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultados = new String[3];
resultados[0] = "fondo3";
resultados[1] = "diseno11";
resultados[2] = "enemigodiseno11";
opcionBoton = (ImageButton) findViewById(R.id.opcion_boton);
layoutPrincipal = (RelativeLayout) findViewById(R.id.principal_screen);
opcionBoton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View vistaPopup = inflater.inflate(R.layout.popup_activity, null);
popup = new PopupWindow(
vistaPopup,
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
ImageButton cerrarPop = (ImageButton) vistaPopup.findViewById(R.id.volver_boton);
cerrarPop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
popup.dismiss();
opcionBoton.setVisibility(View.VISIBLE);
}
});
popup.showAtLocation(layoutPrincipal, Gravity.BOTTOM, 0, 0);
opcionBoton.setVisibility(View.INVISIBLE);
}
});
}
public void iniciaJuego(View view) {
Intent juego = new Intent(view.getContext(), GameActivity.class);
juego.putExtra("arg", resultados[0] + " " + resultados[1] + " " + resultados[2]);
startActivity(juego);
}
public void actualizaFondo(View vista) {
switch (vista.getId()) {
case R.id.fondo_1:
resultados[0] = "fondo";
break;
case R.id.fondo_2:
resultados[0] = "fondo1";
break;
case R.id.fondo_3:
resultados[0] = "fondo3";
break;
}
}
public void actualizaNave(View vista) {
switch (vista.getId()) {
case R.id.nave_1:
resultados[1] = "diseno11";
break;
case R.id.nave_2:
resultados[1] = "diseno21";
break;
case R.id.nave_3:
resultados[1] = "diseno31";
break;
}
}
public void actualizaEnemigo(View vista) {
switch (vista.getId()) {
case R.id.enemigo_1:
resultados[2] = "enemigodiseno11";
break;
}
}
}
I hope you could help me, please... I don't know where is the error.
The project repository is: github.com/cvazquezlos/Space-Invaders-Android
Thank you so much!

Update and remove item in ListView

when make edit to array list by popup windows or remove item by long click
it just update and remove last value
arraylist and adapter and listview not updated together
this is my code:
CustomListViewAapter.java
package com.zoom.plumbingcalculation;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
public class CustomListViewAdapter extends BaseAdapter{
public static final String colFrom="From";
public static final String colTo="To";
public static final String colCFM="CFM";
public static final String colDia="Dia";
public static final String colLength="Length";
public static final String colTotLoss="Loss";
public static final String colDP="DP";
public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFrom;
TextView txtTo;
TextView txtCFM;
TextView txtDia;
TextView txtLength;
TextView txtTotLoss;
TextView txtDP;
public CustomListViewAdapter(Activity activity, ArrayList<HashMap<String, String>> list){
super();
this.activity=activity;
this.list=list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=activity.getLayoutInflater();
if(convertView == null){
convertView=inflater.inflate(R.layout.custom_list_view, null);
txtFrom=(TextView) convertView.findViewById(R.id.From);
txtTo=(TextView) convertView.findViewById(R.id.To);
txtCFM=(TextView) convertView.findViewById(R.id.CFM);
txtDia=(TextView) convertView.findViewById(R.id.Dia);
txtLength=(TextView) convertView.findViewById(R.id.Length);
txtTotLoss=(TextView) convertView.findViewById(R.id.TotLoss);
txtDP=(TextView) convertView.findViewById(R.id.DP);
}
HashMap<String, String> map=list.get(position);
txtFrom.setText(map.get(colFrom));
txtTo.setText(map.get(colTo));
txtCFM.setText(map.get(colCFM));
txtDia.setText(map.get(colDia));
txtLength.setText(map.get(colLength));
txtTotLoss.setText(map.get(colTotLoss));
txtDP.setText(map.get(colDP));
return convertView;
}
}
Activity.java
package com.zoom.plumbingcalculation;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colCFM;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colDP;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colDia;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colFrom;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colLength;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colTo;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colTotLoss;
public class ps_ss_compressed_air extends Activity {
//Compressed Air Variable
private ImageButton CAButAdd;
private ListView CALvDataTable;
private ArrayList<HashMap<String, String>> CALvDataTableListItems;
private CustomListViewAdapter CALvDataTableAdapter;
//Compressed Air Popup Add Variable
private LayoutInflater layoutAddInflater;
private View popupAddView;
private PopupWindow popupAddWindow;
private Button CAPAButAdd;
private Button CAPAButCancel;
private Spinner CAPASpDia;
private EditText CAPATxtFrom;
private EditText CAPATxtTo;
private EditText CAPATxtCFM;
private EditText CAPATxtLength;
//Compressed Air Popup Edit Variable
private LayoutInflater layoutEditInflater;
private View popupEditView;
private PopupWindow popupEditWindow;
private Button CAPEButEdit;
private Button CAPEButCancel;
private Spinner CAPESpDia;
private EditText CAPETxtFrom;
private EditText CAPETxtTo;
private EditText CAPETxtCFM;
private EditText CAPETxtLength;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ps_ss_compressed_air);
//Compressed Air Variable
CAButAdd = (ImageButton) findViewById(R.id.CAButAdd);
CALvDataTable =(ListView) findViewById(R.id.CALvDataTable);
CALvDataTableListItems= new ArrayList<HashMap<String,String>>();
CALvDataTableAdapter=new CustomListViewAdapter(this, CALvDataTableListItems);
CALvDataTable.setAdapter(CALvDataTableAdapter);
CAButAdd.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
layoutAddInflater = (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
popupAddView = layoutAddInflater.inflate(R.layout.ps_ss_compressed_air_popup_add, null);
//popupView.setAlpha(0.5F);
popupAddView.setAlpha(1);
popupAddWindow = new PopupWindow(
popupAddView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
CAPAButAdd = (Button)popupAddView.findViewById(R.id.CAPAButAdd);
CAPAButCancel = (Button)popupAddView.findViewById(R.id.CAPAButCancel);
CAPASpDia = (Spinner) popupAddView.findViewById(R.id.CAPASpDia);
CAPATxtFrom = (EditText) popupAddView.findViewById(R.id.CAPATxtFrom);
CAPATxtTo = (EditText) popupAddView.findViewById(R.id.CAPATxtTo);
CAPATxtCFM = (EditText) popupAddView.findViewById(R.id.CAPATxtCFM);
CAPATxtLength = (EditText) popupAddView.findViewById(R.id.CAPATxtLength);
CAPAButAdd.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
//Add--------------------------------------------------------------------------------------------
HashMap<String,String> temp = new HashMap<>();
temp.put(colFrom, CAPATxtFrom.getText().toString());
temp.put(colTo, CAPATxtTo.getText().toString());
temp.put(colCFM, CAPATxtCFM.getText().toString());
temp.put(colDia, CAPASpDia.getSelectedItem().toString());
temp.put(colLength, CAPATxtLength.getText().toString());
temp.put(colTotLoss, "");
temp.put(colDP, "");
CALvDataTableListItems.add(temp);
CALvDataTableAdapter.notifyDataSetChanged();
//-----------------------------------------------------------------------------------------------
popupAddWindow.dismiss();
}});
CAPAButCancel.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
popupAddWindow.dismiss();
}});
//popupWindow.showAsDropDown(CAButAdd, 50, 50);
popupAddWindow.showAtLocation(popupAddView, Gravity.CENTER,0,0);
popupAddWindow.setFocusable(true);
popupAddWindow.update();
}});
CALvDataTable.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id)
{
layoutEditInflater = (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
popupEditView = layoutEditInflater.inflate(R.layout.ps_ss_compressed_air_popup_edit, null);
popupEditView.setAlpha(1);
popupEditWindow = new PopupWindow(
popupEditView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
CAPEButEdit = (Button)popupEditView.findViewById(R.id.CAPEButEdit);
CAPEButCancel = (Button)popupEditView.findViewById(R.id.CAPEButCancel);
CAPESpDia = (Spinner) popupEditView.findViewById(R.id.CAPESpDia);
CAPETxtFrom = (EditText) popupEditView.findViewById(R.id.CAPETxtFrom);
CAPETxtTo = (EditText) popupEditView.findViewById(R.id.CAPETxtTo);
CAPETxtCFM = (EditText) popupEditView.findViewById(R.id.CAPETxtCFM);
CAPETxtLength = (EditText) popupEditView.findViewById(R.id.CAPETxtLength);
HashMap<String, String> temp=CALvDataTableListItems.get(position);
CAPETxtFrom.setText(temp.get(colFrom));
CAPETxtTo.setText(temp.get(colTo));
CAPETxtCFM.setText(temp.get(colCFM));
CAPESpDia.setSelection(((ArrayAdapter<String>)CAPESpDia.getAdapter()).getPosition(temp.get(colDia)));
CAPETxtLength.setText(temp.get(colLength));
CAPEButEdit.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
//Edit-------------------------------------------------------------------------------------------
HashMap<String,String> temp= CALvDataTableListItems.get(position);
temp.put(colFrom, CAPETxtFrom.getText().toString());
temp.put(colTo, CAPETxtTo.getText().toString());
temp.put(colCFM, CAPETxtCFM.getText().toString());
temp.put(colDia, CAPESpDia.getSelectedItem().toString());
temp.put(colLength, CAPETxtLength.getText().toString());
temp.put(colTotLoss, "");
temp.put(colDP, "");
CALvDataTableListItems.set(position,temp);
CALvDataTableAdapter.notifyDataSetChanged();
//-----------------------------------------------------------------------------------------------
popupEditWindow.dismiss();
}});
CAPEButCancel.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
popupEditWindow.dismiss();
}});
//popupWindow.showAsDropDown(CAButEdit, 50, 50);
popupEditWindow.showAtLocation(popupEditView, Gravity.CENTER,0,0);
popupEditWindow.setFocusable(true);
popupEditWindow.update();
//Toast.makeText(ps_ss_compressed_air.this, Integer.toString(position+1) + " Clicked", Toast.LENGTH_SHORT).show();
}
});
CALvDataTable.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
CALvDataTableListItems.remove(position);
CALvDataTableAdapter.notifyDataSetChanged();
Toast.makeText(ps_ss_compressed_air.this,"Removed", Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
custom_list_view.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="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal"
android:divider="#000000"
android:weightSum="7">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/From"
android:layout_weight="1"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/To"
android:layout_weight="1"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/CFM"
android:layout_weight="1" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/Dia"
android:layout_weight="1"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/Length"
android:layout_weight="1"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/TotLoss"
android:layout_weight="1"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/DP"
android:layout_weight="1"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />
</LinearLayout>
Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.AppBarLayout
android:id="#+id/CAappbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/CAtoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:logo="#mipmap/ic_launcher"
app:title="#string/app_name">
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:paddingStart="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="14sp"
android:text="#string/Compressed_Air_Name"
android:id="#+id/CALabCAName"
android:layout_weight="7"/>
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/CAButAdd"
android:layout_weight="1"
android:background="#drawable/ps_ss_compressed_air_but_add"/>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#+id/CAappbar"
android:id="#+id/linearLayoutCAHeader">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:divider="#000000">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/Nodes_Lab"
android:id="#+id/CALabNodes"
android:textStyle="bold"
android:layout_weight="1"/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_gravity="center"
android:weightSum="2"
android:layout_weight="1">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/Nodes_From_Lab"
android:id="#+id/CALabFrom"
android:textStyle="bold"
android:layout_weight="1"/>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/Nodes_To_Lab"
android:id="#+id/CALabTo"
android:textStyle="bold"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/CFM_Lab"
android:id="#+id/CALabCFM"
android:textStyle="bold"
android:layout_weight="1"/>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/Diameter_Lab"
android:id="#+id/CALabDia"
android:textStyle="bold"
android:layout_weight="1"/>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/Length_Lab"
android:id="#+id/CALabLength"
android:textStyle="bold"
android:layout_weight="1"/>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/Total_Loss_Lab"
android:gravity="center"
android:id="#+id/CALabTotLoss"
android:textStyle="bold"
android:layout_weight="1"/>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/Pressure_Drop_Lab"
android:id="#+id/CALabDP"
android:textStyle="bold"
android:layout_weight="1"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000000" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/linearLayoutCAHeader"
android:id="#+id/CALvDataTable"
android:longClickable="true"/>
</RelativeLayout>

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