How to avoid the softkeyboard overlapping the EditText in fragment? - java

I try both methods addling line android:windowSoftInputMode="adjustPan" in manifest or adding getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);on Fragment onCreateView but still facing the same issue. That when i type in the my custom EditText the screen does not auto resize.
here is my fragment:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:fillViewport="true"
tools:context=".Fragments.HelpersFrag.UseAvailablePackage.AddListBasicFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/toolbar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:orientation="vertical">
<include layout="#layout/firstpage_indicator_layout" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="24dp"
android:orientation="horizontal">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:contentDescription="#string/pencil_icon"
android:src="#drawable/pencil_icon" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="#style/TextViewTitles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/basic" />
<include
android:id="#+id/edt_jobTitle"
layout="#layout/custom_edittext" />
<include
android:id="#+id/cbAceptCreditCard"
layout="#layout/checkbox_layout" />
<include
android:id="#+id/cbCoupons"
layout="#layout/checkbox_layout" />
<include
android:id="#+id/cbSos"
layout="#layout/checkbox_layout" />
<include
android:id="#+id/cbNonSmoker"
layout="#layout/checkbox_layout" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
My Custom EditText:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:are="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="15dp"
android:background="#drawable/boundaries">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottombar"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:padding="5dp">
<com.chinalwb.are.AREditText
android:id="#+id/arEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|left"
android:hint="#string/job_description" />
</ScrollView>
<LinearLayout
android:id="#+id/bottombar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:gravity="center"
android:padding="5dp"
android:orientation="horizontal"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:weightSum="1000"
tools:ignore="RtlSymmetry">
<com.chinalwb.are.styles.toolbar.ARE_ToolbarDefault
android:id="#+id/areToolbar"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="1000"
android:gravity="center_vertical"/>
<ImageButton
android:id="#+id/btnUnlink"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="11dp"
android:layout_marginLeft="11dp"
android:background="#color/transparent_color"
android:contentDescription="#string/text_unlink"
android:scaleType="fitCenter"
android:src="#drawable/unlink_icon" />
<ImageButton
android:id="#+id/btnUndo"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="11dp"
android:layout_marginLeft="11dp"
android:background="#color/transparent_color"
android:contentDescription="#string/text_undo"
android:scaleType="fitCenter"
android:src="#drawable/undo_icon" />
<ImageButton
android:id="#+id/btnRedo"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="11dp"
android:layout_marginLeft="11dp"
android:background="#color/transparent_color"
android:contentDescription="#string/text_redo"
android:scaleType="fitCenter"
android:src="#drawable/redo_icon" />
</LinearLayout>
</RelativeLayout>
checkbox
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_selector"
android:fontFamily="#font/raleway_bold"
android:text="#string/financial_planning"
android:textColor="#color/texthint_color"
android:textSize="13sp"
android:padding="10dp"
android:theme="#style/CheckBox" />
AddListFragment.java
package com.blendev.niffty.Fragments.HelpersFrag.UseAvailablePackage;
import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import com.blendev.niffty.Constants.Constants;
import com.blendev.niffty.Fragments.BaseFragment;
import com.blendev.niffty.R;
import com.chinalwb.are.AREditText;
import com.chinalwb.are.styles.toolbar.IARE_Toolbar;
import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Bold;
import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Italic;
import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Link;
import com.chinalwb.are.styles.toolitems.ARE_ToolItem_ListBullet;
import com.chinalwb.are.styles.toolitems.ARE_ToolItem_ListNumber;
import com.chinalwb.are.styles.toolitems.IARE_ToolItem;
import com.google.android.material.snackbar.Snackbar;
import java.util.ArrayList;
import java.util.List;
import ren.qinc.edit.PerformEdit;
public class AddListBasicFragment extends BaseFragment {
Button btnNext;
CheckBox cbCreditCard, cbCoupons, cbSos, cbNonSmoker;
View view;
private IARE_Toolbar mToolbar;
private AREditText mEditText;
PerformEdit mPerformEdit;
EditText edtTitle;
ImageButton btnUndo, btnRedo, btnUnlink;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
String selectedText;
private String mParam1;
private String mParam2;
List arrayList;
public AddListBasicFragment() {
}
public static AddListBasicFragment newInstance(String param1, String param2) {
AddListBasicFragment fragment = new AddListBasicFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_add_list_basic, container, false);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
initView();
initToolbar();
return view;
}
void initView() {
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
toolbar.setTitle("");
arrayList = new ArrayList<String>();
ImageButton btnClose = (ImageButton) view.findViewById(R.id.btnClose);
edtTitle = view.findViewById(R.id.edt_jobTitle);
btnNext = view.findViewById(R.id.btnNext_id);
cbCreditCard = view.findViewById(R.id.cbAceptCreditCard);
cbCoupons = view.findViewById(R.id.cbCoupons);
cbSos = view.findViewById(R.id.cbSos);
cbNonSmoker = view.findViewById(R.id.cbNonSmoker);
btnUndo = view.findViewById(R.id.btnUndo);
btnRedo = view.findViewById(R.id.btnRedo);
btnUnlink = view.findViewById(R.id.btnUnlink);
cbCreditCard.setText(R.string.cbAcceptCreditCard);
cbCoupons.setText(R.string.cbCoupons);
cbSos.setText(R.string.cbSosHelper);
cbNonSmoker.setText(R.string.cbNonSmoker);
edtTitle.setHint(R.string.job_title);
btnClose.setOnClickListener(this);
btnNext.setOnClickListener(this);
btnUndo.setOnClickListener(this);
btnRedo.setOnClickListener(this);
btnUnlink.setOnClickListener(this);
}
private void initToolbar() {
mToolbar = view.findViewById(R.id.areToolbar);
IARE_ToolItem bold = new ARE_ToolItem_Bold();
IARE_ToolItem italic = new ARE_ToolItem_Italic();
IARE_ToolItem listNumber = new ARE_ToolItem_ListNumber();
IARE_ToolItem listBullet = new ARE_ToolItem_ListBullet();
IARE_ToolItem link = new ARE_ToolItem_Link();
mToolbar.addToolbarItem(bold);
mToolbar.addToolbarItem(italic);
mToolbar.addToolbarItem(listNumber);
mToolbar.addToolbarItem(listBullet);
mToolbar.addToolbarItem(link);
mEditText = view.findViewById(R.id.arEditText);
mEditText.setTextSize(13);
mEditText.setToolbar(mToolbar);
mPerformEdit = new PerformEdit(mEditText);
bold.getStyle().getImageView().setImageResource(R.drawable.bold_icon);
bold.getStyle().getImageView().setScaleType(ImageView.ScaleType.CENTER_INSIDE);
italic.getStyle().getImageView().setImageResource(R.drawable.italic_icon);
italic.getStyle().getImageView().setScaleType(ImageView.ScaleType.CENTER_INSIDE);
try {
listNumber.getStyle().getImageView().setImageResource(R.drawable.numbering_icon);
listBullet.getStyle().getImageView().setImageResource(R.drawable.bullet_icon);
link.getStyle().getImageView().setImageResource(R.drawable.link_icon);
} catch (Exception e) {
Log.e("ERROR: ", e.toString());
}
}
#SuppressLint("NonConstantResourceId")
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnNext_id:
String jobTitle = edtTitle.getText().toString();
String jobDescription = mEditText.getText().toString();
validateInputs(jobTitle, jobDescription);
break;
case R.id.btnClose:
getActivity().onBackPressed();
break;
case R.id.btnUndo:
mPerformEdit.undo();
break;
case R.id.btnRedo:
mPerformEdit.redo();
break;
case R.id.btnUnlink:
break;
}
}
void validateInputs(String jobTitle, String jobDescription) {
if (jobTitle.isEmpty() || jobDescription.isEmpty()) {
Log.d("TAG", "String field empty");
Snackbar snackBar = Snackbar.make(getActivity().findViewById(android.R.id.content),
"Please fill the requirements....", Snackbar.LENGTH_LONG);
snackBar.show();
} else {
Bundle bundle = new Bundle();
bundle.putString(Constants.JOB_TITLE, jobTitle);
bundle.putString(Constants.JOB_DESCRIPTION, jobDescription);
arrayList.clear();
if (cbCreditCard.isChecked()) {
arrayList.add(cbCreditCard.getText());
}
if (cbCoupons.isChecked()) {
arrayList.add(cbCoupons.getText());
}
if (cbSos.isChecked()) {
arrayList.add(cbSos.getText());
}
if (cbNonSmoker.isChecked()) {
arrayList.add(cbNonSmoker.getText());
}
Log.d("TAG", "List size: " + arrayList.size());
Fragment imgFileFragment = new ImagesFilesFragment();
imgFileFragment.setArguments(bundle);
this.changeFragment(imgFileFragment, Constants.ADD_LIST_BASIC_FRAGMENT);
}
}
}
Manifest
<activity
android:name=".Activities.MainScreenActivity"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustPan"/>

Related

Android Studio RecyclerView: Why always ( No adapter attached; skipping layout )

I am a student who currently taking mobile application development. Although my app can run but it doesnt show any activity instead of just give me W/RecyclerView: No adapter attached; skipping layout. I dont know how to solve the issue, so I was hoping can get some guidlines from experts.
Here is my code
MainActivity.java
package my.edu.utar.practicalassignment;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private Toolbar tool;
private TextView textView;
private RecyclerView recyclerView;
private Button AddBtn;
RecyclerView.LayoutManager layoutManager;
private FirebaseAuth auth;
private DatabaseReference ref;
private String userID = "";
private ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tool = findViewById(R.id.toolbar);
setSupportActionBar(tool);
//getSupportActionBar().setTitle("Today's Spending");
textView=findViewById(R.id.totalCashBack);
recyclerView =findViewById(R.id.recycle_view);
// recyclerView =(RecyclerView)findViewById(R.id.recycleView);
// layoutManager = new LinearLayoutManager(this);
// recyclerView.setLayoutManager(layoutManager);
AddBtn = findViewById(R.id.add_new);
auth = FirebaseAuth.getInstance();
userID = auth.getCurrentUser().getUid();
ref = FirebaseDatabase.getInstance().getReference().child("expenses").child(userID);
// if(userID !=null){
// try{
// ref = FirebaseDatabase.getInstance().getReference().child("expenses").child(userID);
//
// }catch (NullPointerException ex){
// ex.printStackTrace();
// }
// }
progressDialog = new ProgressDialog(this);
AddBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addItem();
}
});
}
private void addItem() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater layout = LayoutInflater.from(this);
View view = layout.inflate(R.layout.input_layout, null);
alert.setView(view);
final AlertDialog dialog = alert.create();
dialog.setCancelable(false);
final Spinner spinnerItem = view.findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.category));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerItem.setAdapter(adapter);
final EditText amount = view.findViewById(R.id.insert_amount);
final EditText notes = view.findViewById(R.id.insert_note);
final Button saveButton = view.findViewById(R.id.add_button);
final Button cancelButton = view.findViewById(R.id.cancel_button);
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String amountGet = amount.getText().toString();
String notesGet = notes.getText().toString();
String categoryGet = spinnerItem.getSelectedItem().toString();
Drawable icon = getResources().getDrawable(R.drawable.error);
if(amountGet.isEmpty()){
amount.setError("Please enter an amount!!!", icon );
return;
}
if(notesGet.isEmpty()){
notes.setError("Please enter a notes!!!", icon );
return;
}
if(categoryGet.equals("Select a Category")){
Toast.makeText(MainActivity.this, "Please select a valid category", Toast.LENGTH_SHORT).show();
}
else{
progressDialog.setMessage("Had added the information into database");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
String id = ref.push().getKey();
DateFormat format = new SimpleDateFormat("dd-MM-yyyy");
Calendar calendar = Calendar.getInstance();
String date = format.format(calendar.getTime());
Data inform = new Data(categoryGet, date, id, notesGet, Integer.parseInt(amountGet));
ref.child(id).setValue(inform).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if(task.isSuccessful()){
Toast.makeText(MainActivity.this, "Information added successfully", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "Fail to add the information", Toast.LENGTH_SHORT).show();
}
progressDialog.dismiss();
}
});
}
dialog.dismiss();
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
}
}
MainActivity.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"
android:background="#000"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffff00"
android:elevation="4dp" />
<TextView
android:id="#+id/totalCashBack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:text=" Total Cash Back = RM 0"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="556dp" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>
<Button
android:id="#+id/add_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:backgroundTint="#ffff00"
android:text="Add"
app:fabSize="normal" />
</LinearLayout>
InputLayout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:elevation="10dp"
app:cardElevation="10dp"
android:layout_margin="10dp"
android:orientation="vertical"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="What did you had spend on ?"
android:gravity="center"
android:textColor="#fff"
android:textStyle="bold"
android:layout_margin="5dp"
android:textSize="20sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#ffff00"
android:entries="#array/category"/>
<EditText
android:id="#+id/insert_amount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Please enter a amount:"
android:textColor="#fff"
android:inputType="number"
android:textColorHint="#fff"/>
<EditText
android:id="#+id/insert_note"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Please enter a note:"
android:textColor="#fff"
android:textColorHint="#fff"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="#+id/cancel_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cancel"
android:textAllCaps="false"
android:textColor="#000"
android:textStyle="bold"
android:background="#fff"
android:layout_margin="2dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="#+id/add_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Transaction"
android:textAllCaps="false"
android:textColor="#000"
android:textStyle="bold"
android:background="#ffff00"
android:layout_margin="2dp"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
strings.xml
<resources>
<string name="app_name">PracticalAssignment</string>
<string-array name="category">
<item>Select a category</item>
<item>Petrol Spend</item>
<item>Groceries Spend</item>
<item>eWallet Transaction</item>
<item>Other Eligible Spend</item>
</string-array>
</resources>
You have to create an adapter class and add with your recycler view.
The basic snippet to set adapter to recycler view is like bellow
YourAdater adapter = YourAdater();
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
for more detail that how to create a RecyclerView AdapterClass you can see this or this

Issues With Android App Recycler View Displaying User Thumb and User Name

I am programming a simple search for users but my app is not displaying the results of the query and crashes about 40% of the time after I click search. Here is my code. Any advice would be appreciated.
My Activity:
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.MyApp.Objects.ParticipantsObject;
import com.MyApp.R;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
public class ParticipantsActivity extends AppCompatActivity {
private EditText mSearchField;
private ImageButton mSearchBtn;
private RecyclerView mResultList;
private DatabaseReference mUserDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_neighbors);
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child("Participants");
mSearchField = (EditText) findViewById(R.id.search_field);
mSearchBtn = (ImageButton) findViewById(R.id.search_btn);
mResultList = (RecyclerView) findViewById(R.id.result_list);
mResultList.setHasFixedSize(true);
mResultList.setLayoutManager(new LinearLayoutManager(this));
mSearchBtn.setOnClickListener(view -> {
String searchText = mSearchField.getText().toString();
firebaseUserSearch(searchText);
});
}
private void firebaseUserSearch(String searchText) {
Toast.makeText(ParticipantsActivity.this, "Started Search", Toast.LENGTH_LONG).show();
Query firebaseSearchQuery = mUserDatabase.orderByChild("name").startAt(searchText);
FirebaseRecyclerOptions<ParticipantsObject> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<ParticipantsObject>()
.setQuery(firebaseSearchQuery, ParticipantsObject.class)
.build();
class UserHolder extends RecyclerView.ViewHolder {
private TextView imageThumbTextView, nameTextView
UserHolder(View itemView) {
super(itemView);
imageThumbTextView = itemView.findViewById(R.id.profile_image);
nameTextView = itemView.findViewById(R.id.name_text);
}
void setUsers(ParticipantsObject participantsObject) {
String imageThumb = driverObject.getThumb_image();
imageThumbTextView.setText(imageThumb);
String name = participantsObject.getName();
nameTextView.setText(name);
}
}
FirebaseRecyclerAdapter<ParticipantsObject, UserHolder> firebaseRecyclerAdapter;
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<ParticipantsObject, UserHolder>(firebaseRecyclerOptions) {
#Override
protected void onBindViewHolder(#NonNull UserHolder userHolder, int position, #NonNull ParticipantsObject participantsObject) {
userHolder.setUsers(participantsObject);
}
#Override
public UserHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_layout, parent, false);
return new UserHolder(view);
}
};
mResultList.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();
}
}
My activity_participants.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
tools:context="com.MyApp.ParticipantsActivity">
<TextView
android:id="#+id/heading_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:text="Firebase Search"
android:textColor="#555555"
android:textSize="24sp" />
<EditText
android:id="#+id/search_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/heading_label"
android:layout_below="#+id/heading_label"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_toStartOf="#+id/search_btn"
android:background="#drawable/search_layout"
android:ems="10"
android:hint="Search here"
android:inputType="textPersonName"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:textColor="#999999"
android:textSize="16sp" />
<ImageButton
android:id="#+id/search_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/search_field"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/search_field"
android:layout_marginRight="30dp"
android:background="#android:color/background_light"
app:srcCompat="#mipmap/search_button" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/result_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/search_field"
android:layout_marginTop="50dp">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
My list layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/profile_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
app:srcCompat="#mipmap/ic_default_user" />
<TextView
android:id="#+id/name_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="14dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="213dp"
android:layout_marginRight="20dp"
android:layout_toEndOf="#+id/profile_image"
android:text="Username"
android:textColor="#555555"
android:textSize="16sp" />
</RelativeLayout>
Anyone have any ideas as to why it's not displaying results?
My relevant Firebase DB basic paths are like so:
Users -> Participants -> UserIDs -> name, image, etc
Logcat isn't giving me much at the moment.

Android application: calc function does not work after changing to DrawerLayout

I changed the layout in my app from RelativLayout to RelativLayout as a child of DrawerLayout. Everything works fine, except the everything inside onClick() in MainActivity.java. After pressing the button button1 the result of the calculation is NaN which was working before. It has to do something with the change of the layout. I don't have a clue what causes this issue.
Maybe someone can help me out?
GitHub: https://github.com/ephlox/GradesCalculator2
MainActivity.java
package com.lob.gradescalculator;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
DrawerLayout drawerLayoutgesamt;
ActionBarDrawerToggle drawerToggle;
int sum = 0;
int ects = 0;
float mark = 0;
NumberFormat numberFormat = new DecimalFormat("0.00");
Button button1;
TextView textView1;
EditText editText1;
EditText editText2;
EditText editText3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
drawerLayoutgesamt = (DrawerLayout) findViewById(R.id.drawerlayoutgesamt);
drawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayoutgesamt, R.string.auf, R.string.zu);
drawerLayoutgesamt.setDrawerListener(drawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawerToggle.syncState();
button1 = (Button)findViewById(R.id.button1);
textView1 = (TextView)findViewById(R.id.textView1);
editText1 = (EditText)findViewById(R.id.editTextGDI);
editText2 = (EditText)findViewById(R.id.editTextGDW);
editText3 = (EditText)findViewById(R.id.editTextProgrammieren1);
loadSavedPreferences();
button1.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v){
ViewGroup group = (ViewGroup)findViewById(R.id.activity_main);
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
if(view.equals(editText1) && !(editText1.getText().toString().matches(""))){ //GDI
System.out.println("edittext1");
ects += 5;
try{ sum += Integer.parseInt(editText1.getText().toString()) *5; } catch(NumberFormatException n){}
}
else if(view.equals(editText2)&& !(editText2.getText().toString().matches(""))){ //GDW
System.out.println("edittext2");
ects += 5;
try{ sum += Integer.parseInt(editText2.getText().toString()) *5; } catch(NumberFormatException n){}
}
else if(view.equals(editText3)&& !(editText3.getText().toString().matches(""))){
System.out.println("edittext3");
ects += 7;
try{ sum += Integer.parseInt(editText3.getText().toString()) *7; } catch(NumberFormatException n){}
}
}
}
mark = (float)sum / (float)ects;
textView1.setText(String.valueOf(numberFormat.format(mark)));
savePreferences("editText1", editText1.getText().toString());
savePreferences("editText2", editText2.getText().toString());
savePreferences("editText3", editText3.getText().toString());
sum = 0;
ects = 0;
mark = 0;
}
}
);
}
private void loadSavedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
editText1.setText(sharedPreferences.getString("editText1", ""));
editText2.setText(sharedPreferences.getString("editText2", ""));
editText3.setText(sharedPreferences.getString("editText3", ""));
}
private void savePreferences(String key, String value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == R.id.action_settings){
return true;
}
if(drawerToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(#Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(new Configuration());
}
}
activity_main.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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lob.gradescalculator.MainActivity">
<android.support.v4.widget.DrawerLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/drawerlayoutgesamt"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<!-- Activity Layout für Hauptbereich -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/activitylayout"
>
<android.support.v7.widget.Toolbar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar1"
android:background="#c6d9ff"
android:fitsSystemWindows="true"
>
</android.support.v7.widget.Toolbar>
<!-- TextViews -->
<TextView
android:id="#+id/WIF_Title"
android:text="Wirtschaftsinformatik B.Sc."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:layout_x="23dp"
android:layout_y="200dp"
android:layout_below="#+id/toolbar1"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/textViewGDW"
android:text="Grundlagen der Wirtschaftsinformatik"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textStyle="normal|bold"
android:textAlignment="textStart"
android:layout_below="#+id/textViewGDI"
android:layout_marginTop="30dp"
android:layout_x="45dp"
android:layout_y="344dp"
android:layout_alignStart="#+id/textViewGDI" />
<TextView
android:id="#+id/TextViewProgrammieren1"
android:text="Programmieren 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textStyle="normal|bold"
android:textAlignment="textStart"
android:layout_below="#+id/textViewGDW"
android:layout_marginTop="32dp"
android:layout_x="113dp"
android:layout_y="238dp"
android:layout_alignStart="#+id/textViewGDW" />
<!-- EditTexts -->
<!-- Buttons -->
<EditText
android:id="#+id/editTextGDI"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_below="#+id/WIF_Title"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:inputType="number"
android:textColorHint="#drawable/selector"
android:background="#drawable/roundedborders"
android:digits="12345"
android:maxLength="1"
/>
<TextView
android:id="#+id/textViewGDI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Grundlagen der Informatik"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textStyle="normal|bold"
android:textAlignment="center"
android:layout_x="125dp"
android:layout_y="184dp"
android:layout_below="#+id/WIF_Title"
android:layout_alignParentStart="true"
android:layout_marginStart="3dp"
android:layout_marginTop="15dp"/>
<EditText
android:id="#+id/editTextGDW"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:textColorHint="#drawable/selector"
android:background="#drawable/roundedborders"
android:ems="10"
android:layout_x="69dp"
android:layout_y="91dp"
android:layout_alignBaseline="#+id/textViewGDW"
android:layout_alignBottom="#+id/textViewGDW"
android:layout_alignStart="#+id/editTextGDI"
android:layout_alignEnd="#+id/editTextGDI" />
<EditText
android:id="#+id/editTextProgrammieren1"
android:layout_height="wrap_content"
android:inputType="number"
android:textColorHint="#drawable/selector"
android:background="#drawable/roundedborders"
android:ems="10"
android:layout_x="78dp"
android:layout_y="264dp"
android:layout_width="50dp"
android:layout_alignBaseline="#+id/TextViewProgrammieren1"
android:layout_alignBottom="#+id/TextViewProgrammieren1"
android:layout_alignStart="#+id/editTextGDW"
android:layout_alignEnd="#+id/editTextGDW" />
<TextView
android:id="#+id/textView1"
android:text="..."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="71dp"
android:layout_x="23dp"
android:layout_y="200dp"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/button1"
android:text="Berechne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="75dp"
android:layout_x="128dp"
android:layout_y="442dp"
android:layout_alignParentBottom="true"
android:layout_alignEnd="#+id/textViewGDW" />
</RelativeLayout>
<!-- Drawerlayout für links -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/drayerlayoutsingle"
android:layout_gravity="start"
android:background="#fff">
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
Solved it. I had to change ViewGroup group = (ViewGroup)findViewById(R.id.activity_main); to ViewGroup group = (ViewGroup)findViewById(R.id.activitylayout);.

Fragments not showing content on viewPager scroll - Android

I'm new to Android..
I am trying to create a simple application with a sliding tab to scroll between 2 fragments. The fragments are populated with dummy buttons just to see if the scrolling works. However when I run all I see are the viewPager indexes on top without the fragments. I am also using these two files SlidingTabLayout.java and SlidingTabStrip.java that are given by Android. Here are my classes:
MyFragmentPagerAdapter.java
package billdozer.com.testxml;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
// Holds tab titles
private String tabTitles[] = new String[] { "Frag #1", "Frag #2"};
private Context context;
public MyFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public int getCount() {
return 2;
}
// Return the correct Fragment based on index
#Override
public Fragment getItem(int position) {
if(position == 0){
return new TabFragment1();
} else if(position == 1) {
return new TabFragment2();
}
return null;
}
#Override
public CharSequence getPageTitle(int position) {
// Return the tab title to SlidingTabLayout
return tabTitles[position];
}
}
TabFragment1.java
package billdozer.com.testxml;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TabFragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.tab_fragment_1, container, false);
return view;
}
}
TabFragment2.java
package billdozer.com.testxml;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TabFragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.tab_fragment_2, container, false);
return view;
}
}
tab_fragment_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:id="#+id/tl"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center" >
<Button
android:id="#+id/button1"
android:layout_weight="1"
android:text="#string/_1" />
<Button
android:id="#+id/button2"
android:layout_weight="1"
android:text="#string/_2" />
<Button
android:id="#+id/button3"
android:layout_weight="1"
android:text="#string/_3" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="#+id/button5"
android:layout_weight="1"
android:text="#string/_4" />
<Button
android:id="#+id/button6"
android:layout_weight="1"
android:text="#string/_5" />
<Button
android:id="#+id/button7"
android:layout_weight="1"
android:text="#string/_6" />
</TableRow>
</TableLayout>
</LinearLayout>
tab_fragment_2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:id="#+id/tl"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center" >
<Button
android:id="#+id/button1"
android:layout_weight="1"
android:text="#string/_7" />
<Button
android:id="#+id/button2"
android:layout_weight="1"
android:text="#string/_8" />
<Button
android:id="#+id/button3"
android:layout_weight="1"
android:text="#string/_9" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="#+id/button5"
android:layout_weight="1"
android:text="#string/_10" />
<Button
android:id="#+id/button6"
android:layout_weight="1"
android:text="#string/_11" />
<Button
android:id="#+id/button7"
android:layout_weight="1"
android:text="#string/_12" />
</TableRow>
</TableLayout>
</LinearLayout>
MainActivity.java
package com.example.user.billdozer_ui;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import com.example.user.billdozer_ui.stab.SlidingTabLayout;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(),
MainActivity.this));
SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
slidingTabLayout.setViewPager(viewPager);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.user.billdozer_ui.MainActivity">
<com.example.user.billdozer_ui.stab.SlidingTabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px" />
</LinearLayout>

Activities togglebutton when clicking a part of an image

I'm currently trying to make a small application in java, and I have encountered a small problem.
I need to activate a togglebutton when I touch a particular area of the image that I have provided.
I enclose the code of my pages.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dip" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/btnPag1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lights"
tools:ignore="HardcodedText" >
</Button>
<Button
android:id="#+id/btnPag2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Automation"
tools:ignore="HardcodedText" >
</Button>
<Button
android:id="#+id/btnPag3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Garden"
tools:ignore="HardcodedText" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
</LinearLayout>
page1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Light 1"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Light 2"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<ToggleButton
android:id="#+id/toggleButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Light 3"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<ToggleButton
android:id="#+id/toggleButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Light 4"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<ToggleButton
android:id="#+id/toggleButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:ignore="HardcodedText" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/casa"
tools:ignore="ContentDescription" />
</LinearLayout>
page2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gate"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activates"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
page3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Irrigation"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
MercuryActivity.java
package it.anddev.bradipao.mercury;
// derived from http://thepseudocoder.wordpress.com/2011/10/05/android-page-swiping-using-viewpager/
import java.util.List;
import java.util.Vector;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MercuryActivity extends FragmentActivity implements Page1Fragment.OnPageListener {
// list contains fragments to instantiate in the viewpager
List<Fragment> fragments = new Vector<Fragment>();
// page adapter between fragment list and view pager
private PagerAdapter mPagerAdapter;
// view pager
private ViewPager mPager;
// activity data
public String p2text,p3text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// creating fragments and adding to list
fragments.add(Fragment.instantiate(this,Page1Fragment.class.getName()));
fragments.add(Fragment.instantiate(this,Page2Fragment.class.getName()));
fragments.add(Fragment.instantiate(this,Page3Fragment.class.getName()));
// creating adapter and linking to view pager
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(),fragments);
mPager = (ViewPager) super.findViewById(R.id.pager);
mPager.setAdapter(this.mPagerAdapter);
// upper bar button listener, allows direct page access
Button button = (Button)findViewById(R.id.btnPag1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(0); // go to first page
}
});
button = (Button)findViewById(R.id.btnPag2);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(1); // go to second page
}
});
button = (Button)findViewById(R.id.btnPag3);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(2); // go to third page
}
});
}
// page 1 fragment listener implementation
#Override
public void onPage1(String s) {
// set activity data with received string
p2text = new String(s+" 2");
p3text = new String(s+" 3");
// page 2 fragment update
Page2Fragment f2 = (Page2Fragment) fragments.get(1);
f2.ptext = p2text;
// if page 2 view is already created, update
View v2 = f2.getView();
if (v2!=null) {
}
// page 3 fragment update
Page3Fragment f3 = (Page3Fragment) fragments.get(2);
f3.ptext = p3text;
// if page 3 view is already created, update
View v3 = f3.getView();
if (v3!=null) {
}
}
}
Page1Fragment.java
package it.anddev.bradipao.mercury;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class Page1Fragment extends Fragment {
public String ptext="..PAGE 1..";
// activity listener interface
private OnPageListener pageListener;
public interface OnPageListener {
public void onPage1(String s);
}
// onAttach : set activity listener
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// if implemented by activity, set listener
if(activity instanceof OnPageListener) {
pageListener = (OnPageListener) activity;
}
// else create local listener (code never executed in this example)
else pageListener = new OnPageListener() {
#Override
public void onPage1(String s) {
Log.d("PAG1","Button event from page 1 : "+s);
}
};
}
// onCreateView :
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// fragment not when container null
if (container == null) {
return null;
}
// inflate view from layout
View view = (LinearLayout)inflater.inflate(R.layout.page1,container,false);
// update text
return view;
}
// set text helper function
}
Page2Fragment.java
package it.anddev.bradipao.mercury;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class Page2Fragment extends Fragment {
public String ptext="..PAGE 2..";
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// fragment not when container null
if (container == null) {
return null;
}
// inflate view from layout
View view = (LinearLayout)inflater.inflate(R.layout.page2,container,false);
// update text
return view;
}
// set text helper function
}
Page3Fragment.java
package it.anddev.bradipao.mercury;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
public class Page3Fragment extends Fragment {
Button btnWrite;
public String ptext="..PAGE 3..";
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// fragment not when container null
if (container == null) {
return null;
}
// inflate view from layout
View view = (LinearLayout)inflater.inflate(R.layout.page3,container,false);
// update text
return view;
}
}
PageAdapter.java
package it.anddev.bradipao.mercury;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class PagerAdapter extends FragmentPagerAdapter {
// fragments to instantiate in the viewpager
private List<Fragment> fragments;
// constructor
public PagerAdapter(FragmentManager fm,List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
// return access to fragment from position, required override
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
// number of fragments in list, required override
#Override
public int getCount() {
return this.fragments.size();
}
}

Categories

Resources