About Spinners and button interface - java

how do I combine the
slected item of 3 spinners i have created and link it to a button??
here is my code...
package com.example.nikhiljoshi.time_table;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
char branch1;
Spinner spbranch,spsem,spdiv;
ArrayAdapter<CharSequence>adapter;
ArrayAdapter<CharSequence>adapter1;
ArrayAdapter<CharSequence>adapter2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Submit = (Button) findViewById(R.id.button);
spbranch = (Spinner)findViewById(R.id.spinner);
spsem = (Spinner)findViewById(R.id.spinner2);
spdiv = (Spinner)findViewById(R.id.spinner3);
adapter = ArrayAdapter.createFromResource(this,R.array.branch,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spbranch.setAdapter(adapter);
spbranch.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int i;
// branch1=spbranch.getItemAtPosition(i).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
adapter1 = ArrayAdapter.createFromResource(this,R.array.sem,android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spsem.setAdapter(adapter1);
adapter2 = ArrayAdapter.createFromResource(this,R.array.division,android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spdiv.setAdapter(adapter2);
Submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(view, "this is my Toast message!!! =)",
// Toast.LENGTH_LONG).show();
// Intent intent = new Intent(v.getContext(), nik.class);
// startActivityForResult(intent, 0);
}
});
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
layout...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Branch"
android:id="#+id/textView"
android:layout_marginTop="105dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="27dp"
android:layout_marginStart="27dp"
android:textSize="20dp" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_marginLeft="34dp"
android:layout_marginStart="34dp"
android:layout_alignTop="#+id/textView"
android:layout_alignLeft="#+id/button"
android:layout_alignStart="#+id/button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sem"
android:id="#+id/textView2"
android:layout_below="#+id/spinner"
android:layout_alignLeft="#+id/textView"
android:layout_alignStart="#+id/textView"
android:layout_marginTop="30dp"
android:textSize="20dp" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner2"
android:layout_alignTop="#+id/textView2"
android:layout_alignLeft="#+id/spinner"
android:layout_alignStart="#+id/spinner" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Division"
android:id="#+id/textView3"
android:layout_below="#+id/spinner2"
android:layout_alignLeft="#+id/textView2"
android:layout_alignStart="#+id/textView2"
android:layout_marginTop="30dp"
android:textSize="20dp" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner3"
android:layout_alignTop="#+id/textView3"
android:layout_alignLeft="#+id/spinner2"
android:layout_alignStart="#+id/spinner2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="#+id/button"
android:layout_below="#+id/spinner3"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter the details"
android:id="#+id/textView4"
android:textSize="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
when i press the submit button, the following things should happen
for eg
if(branch=='cs'&& sem=='5' && div=='A')
then new activity
how do i do that?

You can use Spinner.getSelectedItem() to get the currently selected values from your three spinners, then use those values to determine if you should launch your Activity.

Related

error in navigating from one activity to another activity

/* i have tried many times but i am not able to navigate to another activity while clicking on the item in the navigation drawer and when i click on that Item the app crashes(error:open app again).*/
drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/admin"
android:title="Admin Profile"
android:icon="#drawable/ic_dashboard"/>
<item
android:id="#+id/createaccount"
android:title="User Registration"
android:icon="#drawable/ic_event_black_24dp"/>
<item
android:id="#+id/manageuser"
android:title="Manage User"
android:icon="#drawable/ic_settings_black_24dp"/>
<item
android:id="#+id/us"
android:title="About us"
android:icon="#drawable/ic_call_to_action_black_24dp"/>
<item
android:id="#+id/logout"
android:title="Logout"
android:icon="#drawable/ic_cancel_black_24dp"/>
</menu>
/activity_navigation_bar.xml/
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
android:paddingTop="16dp"
tools:context="com.ereports.navigationBar">
<android.support.v7.widget.LinearLayoutCompat
android:background="#drawable/bg"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.LinearLayoutCompat>
<android.support.design.widget.NavigationView
app:headerLayout="#layout/header"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/white"
app:itemTextColor="#color/darkgray"
app:itemIconTint="#color/darkgray"
app:menu="#menu/drawermenu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
/navigationBar.java/
package com.ereports;
import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import static com.ereports.R.id.us;
public class navigationBar extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_bar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.Open, R.string.Close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.us) {
Intent intent = new Intent(this,Registration.class);
this.startActivity(intent);
return true;
}
if (id == R.id.action_search) {
Toast.makeText(this, "Android Menu is Clicked", Toast.LENGTH_LONG).show();
return true;
}
if (id == R.id.action_settings) {
Toast.makeText(this, "Android Menu is Clicked", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
}
/*another activity to navigate for::
'Registration.java*/
package com.ereports;
import android.annotation.SuppressLint;
import android.content.res.ColorStateList;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Registration extends Fragment implements OnClickListener {
private static View view;
private static EditText fullName, emailId, mobileNumber, location,
password, confirmPassword;
private static TextView login;
private static Button signUpButton1;
private static CheckBox terms_conditions;
public Registration() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_registration, container, false);
initViews();
setListeners();
return view;
}
// Initialize all views
private void initViews() {
fullName = (EditText) view.findViewById(R.id.fullName);
emailId = (EditText) view.findViewById(R.id.userEmailId);
mobileNumber = (EditText) view.findViewById(R.id.mobileNumber);
location = (EditText) view.findViewById(R.id.location);
password = (EditText) view.findViewById(R.id.password);
confirmPassword = (EditText) view.findViewById(R.id.confirmPassword);
signUpButton1 = (Button) view.findViewById(R.id.signUpBtn);
login = (TextView) view.findViewById(R.id.already_user);
terms_conditions = (CheckBox) view.findViewById(R.id.terms_conditions);
// Setting text selector over textviews
#SuppressLint("ResourceType") XmlResourceParser xrp = getResources().getXml(R.drawable.text_selector);
try {
ColorStateList csl = ColorStateList.createFromXml(getResources(),
xrp);
login.setTextColor(csl);
terms_conditions.setTextColor(csl);
} catch (Exception e) {
}
}
// Set Listeners
private void setListeners() {
signUpButton1.setOnClickListener(this);
login.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.signUpBtn1:
// Call checkValidation method
checkValidation();
break;
}
}
// Check Validation Method
private void checkValidation() {
// Get all edittext texts
String getFullName = fullName.getText().toString();
String getEmailId = emailId.getText().toString();
String getMobileNumber = mobileNumber.getText().toString();
String getLocation = location.getText().toString();
String getPassword = password.getText().toString();
String getConfirmPassword = confirmPassword.getText().toString();
// Pattern match for email id
Pattern p = Pattern.compile(Utils.regEx);
Matcher m = p.matcher(getEmailId);
// Check if all strings are null or not
if (getFullName.equals("") || getFullName.length() == 0
|| getEmailId.equals("") || getEmailId.length() == 0
|| getMobileNumber.equals("") || getMobileNumber.length() == 0
|| getLocation.equals("") || getLocation.length() == 0
|| getPassword.equals("") || getPassword.length() == 0
|| getConfirmPassword.equals("")
|| getConfirmPassword.length() == 0)
new CustomToast().Show_Toast(getActivity(), view,
"All fields are required.");
// Check if email id valid or not
else if (!m.find())
new CustomToast().Show_Toast(getActivity(), view,
"Your Email Id is Invalid.");
// Check if both password should be equal
else if (!getConfirmPassword.equals(getPassword))
new CustomToast().Show_Toast(getActivity(), view,
"Both password doesn't match.");
// Make sure user should check Terms and Conditions checkbox
else if (!terms_conditions.isChecked())
new CustomToast().Show_Toast(getActivity(), view,
"Please select Terms and Conditions.");
// Else do signup or do your stuff
else
Toast.makeText(getActivity(), "User Registered", Toast.LENGTH_SHORT)
.show();
}
}
activity_registration.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="20dp" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="#string/signUp"
android:textColor="#color/white_greyish"
android:textSize="25sp"
android:textStyle="bold" />
<EditText
android:id="#+id/fullName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/user"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:hint="#string/fullName"
android:inputType="textCapWords"
android:padding="10dp"
android:singleLine="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="16sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#color/white_greyish" />
<EditText
android:id="#+id/userEmailId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/email"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:hint="#string/email"
android:inputType="textEmailAddress"
android:padding="10dp"
android:singleLine="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="16sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#color/white_greyish" />
<EditText
android:id="#+id/mobileNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/phone"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:hint="#string/mobileNumber"
android:inputType="phone"
android:padding="10dp"
android:singleLine="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="16sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#color/white_greyish" />
<EditText
android:id="#+id/location"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/location"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:hint="#string/location"
android:inputType="textCapWords"
android:padding="10dp"
android:singleLine="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="16sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#color/white_greyish" />
<EditText
android:id="#+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/password"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:hint="#string/passowrd"
android:inputType="textPassword"
android:padding="10dp"
android:singleLine="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="16sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#color/white_greyish" />
<EditText
android:id="#+id/confirmPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/confirm_password"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:hint="#string/confirmPassword"
android:inputType="textPassword"
android:padding="10dp"
android:singleLine="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="16sp" />
<CheckBox
android:id="#+id/terms_conditions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:text="#string/terms_conditions"
android:textColor="#color/white"
android:textSize="14sp" />
<Button
android:id="#+id/signUpBtn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="#drawable/loginbutton_selector"
android:padding="3dp"
android:text="Register"
android:textColor="#color/background_color"
android:textSize="17sp"
android:textStyle="bold" />
</LinearLayout>
/*app is crashing
expected results: it should navigate to Registration.java
actual results: app is crashing*/
Use fragments to switch between activities
[https://www.youtube.com/watch?v=J8GB_b8qyK8]
this covers with a good explanation. I tried it myself

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);.

android add TextView on click of (+) button with setText as 1and in next 2 and so on

I have one textView and two EditText when i am clicking on the plus button the textView and the two EditText should be added ,and i am able to do that .My problem is the when i am adding the TextView the text should be set as 1 then when the TextView is added again then the text should be set as 2 and so on .i am not able set the text by using t1.setText(i).Here is my code-
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends Activity {
Button btnDisplay;
ImageButton btnAdd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = (ImageButton) findViewById(R.id.btnAdd);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
MyLayoutOperation.add(this, btnAdd);
MyLayoutOperation.display(this, btnDisplay);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
MyLayoutOperation.java
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MyLayoutOperation {
public static void display(final Activity activity, Button btn)
{
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout scrollViewlinerLayout = (LinearLayout) activity.findViewById(R.id.linearLayoutForm);
java.util.ArrayList<String> msg = new ArrayList<String>();
for (int i = 0; i < scrollViewlinerLayout.getChildCount(); i++)
{
LinearLayout innerLayout = (LinearLayout) scrollViewlinerLayout.getChildAt(i);
TextView t2=(TextView)innerLayout.findViewById(R.id.t1);
t2.setText(i);
EditText e1=(EditText)innerLayout.findViewById(R.id.e1);
EditText edit = (EditText) innerLayout.findViewById(R.id.editDes);
msg.add(t2.getText().toString());
msg.add(e1.getText().toString());
msg.add(edit.getText().toString());
}
Toast t = Toast.makeText(activity.getApplicationContext(), msg.toString(), Toast.LENGTH_SHORT);
t.show();
}
});
}
public static void add(final Activity activity, ImageButton btn)
{
final LinearLayout linearLayoutForm = (LinearLayout) activity.findViewById(R.id.linearLayoutForm);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final LinearLayout newView = (LinearLayout)activity.getLayoutInflater().inflate(R.layout.rowdetail, null);
newView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ImageButton btnRemove = (ImageButton) newView.findViewById(R.id.btnRemove);
btnRemove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
linearLayoutForm.removeView(newView);
}
});
linearLayoutForm.addView(newView);
}
});
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutTeste"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/textView1"
android:layout_width="95dp"
android:layout_height="fill_parent"
android:layout_alignBottom="#+id/btnAdd"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/btnAdd"
android:gravity="center_vertical|center_horizontal"
android:text="#string/titleTecnologies"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageButton android:id="#+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/btnAdd"
android:src="#android:drawable/ic_input_add" />
</RelativeLayout>
<ScrollView android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="275dp">
<LinearLayout android:id="#+id/linearLayoutForm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<Button android:id="#+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnDisplay" />
</LinearLayout>
rowdetails.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowdetail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:layout_marginLeft="10dp"
android:id="#+id/t1"/> <requestFocus />
<EditText android:id="#+id/e1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.62"
android:ems="10"
android:inputType="text"> <requestFocus />
</EditText>
<EditText android:id="#+id/editDes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.62"
android:ems="10"
android:inputType="text"> <requestFocus />
</EditText>
<ImageButton android:id="#+id/btnRemove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/btnRemove"
android:src="#android:drawable/ic_delete" />
</LinearLayout>
Got the answer just do
t2.setText(i+".");

android widget within a widget not working as intended

I use a FrameLayout as a custom list item. I have an onItemClickListener set up to the listView but my items each have a button within themselves, and while the button in them is clickable, the item itself is not and they're supposed to do different things. How do I make the item as well as the button within it both clickable?
Here's the code:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class ProjectCreateScreen extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondary_layout1);
final TextView noProject = (TextView) findViewById(R.id.NOPROJECT);
Button btn = (Button) findViewById(R.id.addBtn);
final ArrayList<String> listItems=new ArrayList<String>();
final ListAdapter addAdapter = new ArrayAdapter<String>(this,
R.layout.list_item, R.id.listFrame, listItems);
final ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(addAdapter);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
noProject.setVisibility(View.GONE);
lv.setVisibility(View.VISIBLE);
listItems.add("New Project");
((ArrayAdapter) addAdapter).notifyDataSetChanged();
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent switchToEdit = new Intent(ProjectCreateScreen.this,
teamCreateScreen.class);
startActivity(switchToEdit);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The custom listView item xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/listFrame"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="20sp"
/>
<Button android:id="#+id/frameBtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="top|right"
/>
</FrameLayout>
The xml of the activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rl">
<Button
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="#string/AddProject"
android:id="#+id/addBtn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/noProjectsNotice"
android:id="#+id/NOPROJECT"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="16sp"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/addBtn"
android:id="#+id/lv"
android:visibility="invisible">
</ListView>
</RelativeLayout>
Add the following two attributes for any view you want to be clickable inside the listview:
android:focusable="false"
android:focusableInTouchMode="false"
That should solve most cases. If that is not enough, also add the following attribute to the root layout of your list view layout
android:descendantFocusability="blocksDescendants"

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