use few button and other widget in Fragments - android

I want to use a few Button, Spinner and TextView on fragmets but i don't know how to do that since i was newbie in android programming.
I'd appreciate any help! Thanks a lot!
here's my java activity
package com.elvis.triagetags;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class formActivity extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.form, container, false);
return rootView;
}
}
and this my form.layout
<RelativeLayout 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"
tools:context=".MainActivity" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/relativeLayout1"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="428dp"
android:orientation="vertical"
android:padding="15dp" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/header"
android:textSize="25sp" />
<TextView
android:id="#+id/fullname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/fullname"
android:textColor="#0000CD"
android:textSize="21sp" />
<EditText
android:id="#+id/etIden"
android:layout_width="284dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:textSize="21sp" />
<TextView
android:id="#+id/Gen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/gen"
android:textColor="#0000CD"
android:textSize="21sp" />
<Spinner
android:id="#+id/spinGen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_dropdown" />
<TextView
android:id="#+id/tvdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/date"
android:textColor="#0000CD"
android:textSize="21sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/etdate"
android:layout_width="0.99dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:drawable/editbox_background"
android:ems="10"
android:inputType="date" >
</EditText>
<Button
android:id="#+id/btnDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/wp_button"
android:text="#string/date1" />
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="148dp"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_marginTop="25dp"
android:layout_weight="0.87"
android:contentDescription="#string/pop"
android:src="#drawable/no_image" />
<TextView
android:id="#+id/imageDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/urlImage"
android:textColor="#000000"
android:textSize="14sp"
android:textStyle="italic"
android:typeface="serif" />
<Button
android:id="#+id/capture_button"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/wp_button"
android:text="#string/capture" />
<Button
android:id="#+id/next_button"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="25dp"
android:background="#drawable/wp_button"
android:text="#string/bt" />
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#000000"
android:gravity="center" >
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/elvis"
android:textColor="#ffffff"
android:textSize="16sp" />
</RelativeLayout>

This is a small example to show you how to do:
package com.elvis.triagetags;
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.Spinner;
public class formActivity extends Fragment {
Button btnDate;
Button btnCapture;
Spinner spinGen;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.form, container, false);
btnCapture = (Button) rootView.findViewById(R.id.capture_button);
btnDate = (Button) rootView.findViewById(R.id.btnDate);
spinGen = (Spinner) rootView.findViewById(R.id.spinGen);
spinGen.setOnItemSelectedListener(new SpinnerGen());
btnDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//...
}
});
//...
return rootView;
}
}
public class SpinnerGen implements OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
//...
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
// TODO Auto-generated method stub
}
follow this link, you will find a good example

Related

Fragment is not Covering the entire screen even the layout width and height is match parent

I have an activity and when I click on the "Register" TextView a fragment will open and in that fragment all the Registration detail is there.But when I click on the textview the Fragment is loaded successfully but it did'nt cover up the entire screen.
Here is my code:
MainActivity.java
Register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
android.support.v4.app.FragmentManager fragmentManager=getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
Fragments fragments=new Fragments();
fragmentTransaction.replace(R.id.Fragment_Register,fragments);
fragmentTransaction.commit();
fragmentTransaction.addToBackStack(null);
}
});
}
MainActivity.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"
tools:context="com.example.user.attendance.MainActivity"
android:background="#drawable/blurbackgound"
android:padding="10dp"
android:focusableInTouchMode="true"
>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="WELCOME"
android:textColor="#color/white"
android:textStyle="bold"
android:fontFamily="monospace"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"/>
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:id="#+id/login_mobile_number"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:background="#drawable/edit_text_border"
android:padding="15dp"
android:textColor="#color/white"
android:inputType="number"
android:hint="Enter mobile number"
android:gravity="center"
android:maxLength="10"
android:textColorHint="#color/hint_color"
/>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/ic_local_phone_black_24dp"
android:layout_below="#id/title"
android:layout_marginLeft="30dp"
android:layout_marginTop="73dp"
android:id="#+id/phonelogo"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mb_counter"
android:layout_below="#id/title"
android:layout_marginLeft="300dp"
android:layout_alignBaseline="#id/login_mobile_number"
android:text="abssh"
android:textColor="#color/white"
/>
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/login_mobile_number"
android:layout_marginTop="25dp"
android:background="#drawable/edit_text_border"
android:paddingRight="50dp"
android:hint="Password"
android:gravity="center"
android:textColorHint="#color/hint_color"
android:inputType="textPassword"
android:textColor="#color/white"
android:id="#+id/login_password"
/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="1dp"
android:layout_below="#id/login_password"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#color/white"
android:text="Student"
android:textColor="#color/white"
android:id="#+id/Radio_button_student"
android:layout_marginLeft="19dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:buttonTint="#color/white"
android:text="Teacher"
android:textColor="#color/white"
android:id="#+id/Radio_button_teacher"/>
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SHOW"
android:textColor="#color/white"
android:textStyle="bold"
android:layout_alignRight="#id/login_password"
android:layout_alignBaseline="#id/login_password"
android:id="#+id/password_visibility"
android:layout_marginRight="10dp"
/>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/ic_lock_outline_black_50dp"
android:layout_marginLeft="30dp"
android:layout_below="#id/phonelogo"
android:layout_marginTop="39dp"
/>
<Button
android:stateListAnimator="#null"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:hint="Login"
android:id="#+id/login"
android:background="#drawable/login_buttom"
android:textColorHint="#color/hint_color"
android:layout_below="#id/login_password"
android:layout_marginTop="50dp"
android:layout_marginLeft="35dp"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FORGOT PASSWORD"
android:textColor="#color/white"
android:layout_below="#id/login"
android:layout_marginTop="18dp"
android:layout_marginLeft="48dp"
android:id="#+id/forgot_password"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|"
android:textColor="#color/white"
android:layout_alignBaseline="#id/forgot_password"
android:layout_marginLeft="170dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="REGISTER"
android:textColor="#color/white"
android:layout_below="#id/login"
android:layout_marginLeft="185dp"
android:layout_marginTop="20dp"
android:id="#+id/Register"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Fragment_Register"></RelativeLayout>
</RelativeLayout>
Fragments.java
package com.example.user.attendance;
import android.app.FragmentManager;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import de.hdodenhof.circleimageview.CircleImageView;
public class Fragments extends Fragment {
Button Register_Cancel_fullname, Register_Cancel_email, Register_Cancel_phone,
Register_Cancel_password, Sign_up,Register_Cancel_Confirm_Password;
String Passcode,Confirm_Passcode;
EditText Full_Name, Email_Address, Phone_number, Password_register,Confirm_Password_register;
CircleImageView Register_add_photo;
LinearLayout select_photo_from_camera,select_photo_from_gallery;
TextView textView,hint_textview_circular_image_view;
// RadioButton Category_teacher,Category_student;
RadioGroup Teacher_student;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myfragmentVieew = inflater.inflate(R.layout.fragment_signup, container, false);
//Edit Text
Full_Name = (EditText) myfragmentVieew.findViewById(R.id.Register_name_and_surname);
Email_Address = (EditText) myfragmentVieew.findViewById(R.id.Register_Email_address);
Phone_number = (EditText) myfragmentVieew.findViewById(R.id.Register_Phone);
Password_register = (EditText) myfragmentVieew.findViewById(R.id.Register_Password);
Confirm_Password_register=(EditText)myfragmentVieew.findViewById(R.id
.Confirm_Password_Register);
//Button
Sign_up = (Button) myfragmentVieew.findViewById(R.id.Register_Signup);
Register_Cancel_fullname = (Button) myfragmentVieew.findViewById(R.id.Register_First_Cancel_Button);
Register_Cancel_email = (Button) myfragmentVieew.findViewById(R.id.Register_Second_Cancel_Button);
Register_Cancel_phone = (Button) myfragmentVieew.findViewById(R.id.Register_Third_Cancel_Button);
Register_Cancel_password = (Button) myfragmentVieew.findViewById(R.id.Register_fourth_Cancel_Button);
Register_Cancel_Confirm_Password=(Button)myfragmentVieew.findViewById(R.id
.Register_Cancel_Confirm_Password_Button);
Register_add_photo=(CircleImageView)myfragmentVieew.findViewById(R.id.Register_add_photo);
//TextView
textView = (TextView) myfragmentVieew.findViewById(R.id.Register_textview);
hint_textview_circular_image_view=(TextView)myfragmentVieew.findViewById(R.id
.hint_of_image_view);
return myfragmentVieew;
}
}
Here is what I am getting:
Remove this line from root layout.
android:padding="10dp"
This will solve the problem.

Android Bootstrap Widgets

I am working with com.beardedhen.androidbootstrap.BootstrapDropDown and I don't understand how to get the value of the selected item from the dropdown
dropdownresource.xml
`<resources>
<string name="app_name">GTFirstPage</string>
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>
<string name="action_settings">Settings</string>
<array name="bootstrap_dropdown_example_data">
<item>Family Trip</item>
<item>Camping</item>
<item>Business Trip</item>
<item>{dropdown_separator}</item>
<item>Custom Trip</item>
</array>
</resources>`
plan.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hp.gtfirstpage.plantrip">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<AutoCompleteTextView
android:id="#+id/autotv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|center_horizontal|top"
android:layout_marginTop="25dp"
android:background="#f3f3f3"
android:hint="Where do you want to go?"
android:textSize="30sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="107dp"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/textView2"
android:layout_width="176dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Destination"
android:textSize="30sp" />
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="#f3f3f3"
android:ems="10"
android:textSize="20sp"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="107dp"
android:orientation="horizontal">
<TextView
android:id="#+id/textView11"
android:layout_width="176dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="0.00"
android:text="Category"
android:textSize="30sp"/>
<com.beardedhen.androidbootstrap.BootstrapDropDown
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
app:bootstrapBrand="regular"
app:bootstrapExpandDirection="down"
app:bootstrapSize="md"
app:bootstrapText="Select Category"
app:dropdownResource="#array/bootstrap_dropdown_example_data"
app:roundedCorners="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="107dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Date"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="85dp"
android:orientation="horizontal">
<TextView
android:id="#+id/tv4"
android:layout_width="165dp"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:background="#f3f3f3"
android:inputType="date" />
<TextView
android:id="#+id/textView14"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:text="to"
android:textSize="30sp" />
<TextView
android:id="#+id/tv5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:background="#f3f3f3"
android:inputType="date" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="107dp"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<Button
android:id="#+id/bt1"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="BACK" />
<Button
android:id="#+id/bt2"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="DONE" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
plantrip.java
package com.example.hp.gtfirstpage;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.TextView;
import android.widget.Toast;
import com.beardedhen.androidbootstrap.BootstrapDropDown;
public class plantrip extends Fragment implements
AdapterView.OnItemSelectedListener, DatePickerDialog.OnDateSetListener
{
BootstrapDropDown spin;
String city,category,dest;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View root=null;
root= inflater.inflate(R.layout.fragment_plantrip, container, false);
spin = (BootstrapDropDown) root.findViewById(R.id.spinner);
spin.setOnDropDownItemClickListener(new
BootstrapDropDown.OnDropDownItemClickListener() {
#Override
public void onItemClick(ViewGroup parent, View v, int id) {
// category=spin.getResources().getResourceName(spin.getId());
}
});
return root;
}
}
I am new to android so if there is any mistake please explain properly.
Please tell me how to retrieve items from dropdown list.
Thank You!!!
BootstrapDropDown has a method called getDropdownData();
This method returns a String[] array containing the items in your BootstapDropDown. Use int id as an index for your array. The 'int id' gets passed to you as a parameter of the method:
public void onItemClick(ViewGroup parent, View v, int id) {...}
I believe you can grab the selected item by adding this line to your code:
String[] dropDownItems = spin.getDropdownData();
String item = dropDownItems[id];
spin.setOnDropDownItemClickListener(new
BootstrapDropDown.OnDropDownItemClickListener() {
#Override
public void onItemClick(ViewGroup parent, View v, int id) {
// category=spin.getResources().getResourceName(spin.getId());
String[] dropDownItems = spin.getDropdownData();
String item = dropDownItems[id];
}
});
// made a couple of edits to fix a mistake
// I hope this helped!

Android - Change RelativeLayout to Fragment

I want to "remake" my app.
I already write an app and it works OK. But, it use Activity and Spinner to switch between screens - that is too uncomfortable and slow.
I here find an example of Fragment usage for user-comfortable usage and fast switching beetween screens with swiping:
http://www.theappguruz.com/blog/android-tab-layout-with-swipeable-views
But I cant set RelativeLayout for it from my app, because it use Fragment.
Is there a way to "copy-paste" the markup file (*.xml) without making fundamental changes in the application?
I tried to do it (the commented line of code), but it does not work.
FirstTab.java
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class FirstTab extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/*View view=inflater.inflate(R.layout.tab_first, container, false);
Button imageView = (Button) getView().findViewById(R.id.btn_quit);
return view;*/
TextView tv = new TextView(getActivity());
tv.setText("First Tab");
tv.setGravity(Gravity.CENTER);
tv.setTextColor(Color.WHITE);
tv.setWidth(LayoutParams.MATCH_PARENT);
tv.setHeight(LayoutParams.MATCH_PARENT);
tv.setBackgroundColor(Color.RED);
tv.setTextAppearance(getActivity(),android.R.style.TextAppearance_Large);
return tv;
}
}
first_tab.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:paddingBottom="5sp"
android:paddingTop="5sp"
android:paddingRight="5sp"
android:paddingLeft="5sp"
android:background="#color/colorPrimary">
<Spinner
android:entries="#array/activity_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_alignRight="#+id/view1"
android:layout_alignEnd="#+id/view1"
android:layout_toRightOf="#+id/btn_quit"
android:layout_toEndOf="#+id/btn_quit"
android:layout_alignBottom="#+id/btn_quit"
android:layout_alignParentTop="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close\napp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/btn_quit"
android:onClick="quit"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="#android:color/holo_orange_light"
android:id="#+id/view1"
android:layout_below="#+id/btn_quit"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/eng_text"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="#android:color/white"
android:hint="Enter text"
android:minLines="1"
android:layout_below="#+id/view1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:inputType="textCapSentences|textAutoCorrect|textMultiLine"
android:maxLines="7"/>
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="#android:color/holo_orange_light"
android:id="#+id/view"
android:layout_below="#+id/eng_text"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/morse_text"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="#android:color/white"
android:hint="Get a code"
android:minLines="1"
android:layout_below="#+id/view"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:inputType="textCapSentences|textAutoCorrect|textMultiLine"
android:maxLines="8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Translate"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/trans_btn"
android:layout_above="#+id/btn_sound"
android:layout_centerHorizontal="true"
android:layout_alignParentRight="true"
android:onClick="translate"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|bottom"
android:text="Play\ncode:"
android:id="#+id/play_label"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/trans_btn"
android:layout_toLeftOf="#+id/btn_sound"
android:layout_toStartOf="#+id/btn_sound" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btn_sound"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/btn_flash"
android:layout_toStartOf="#+id/btn_flash"
android:padding="2dp"
android:onClick="play_sound"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btn_flash"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/view"
android:layout_alignEnd="#+id/view"
android:padding="2dp"
android:onClick="play_flash"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Current input\nlanguage:"
android:id="#+id/cur_lang"
android:layout_alignTop="#+id/trans_btn"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btn_flag"
android:layout_alignTop="#+id/cur_lang"
android:layout_toRightOf="#+id/cur_lang"
android:layout_toEndOf="#+id/cur_lang"
android:enabled="false"/>
</RelativeLayout>
You want to convert your whole three activities to fit into a single page using view pager
Make a new Activity with root tag a android.supprot.v4.ViewPager
Set up the adapter and all the other things
In your first tab use the following code
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.tab_first, container, false);
Button button = (Button)view.findViewById(R.id.btn_quit);
button.setText("Working quit");
return view;
}
use view to access any of the elements of first_tab

android <merge /> can be used only with a valid viewgroup root and attachtoroot=true

I am trying to inflate a fragment xml that does not have merge anywhere in it. Yet I am being given the error stated in the question.
My fragment class :
package com.xxxxx.www.xxxxxx;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ExcercisePlanFragment extends Fragment
{
public static ExcercisePlanFragment newInstance()
{
ExcercisePlanFragment fragment = new ExcercisePlanFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_excercise_plan, container, false);
}
}
Here is fragment_excercise_plan.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:background="#color/dark_bg"
android:orientation="vertical">
<RelativeLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<Button
android:layout_width="wrap_content"
android:layout_height="44dp"
android:minWidth="88dp"
android:background="#color/mediumGray"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:layout_centerInParent="true"
android:textColor="#FFF"
android:textSize="14sp"
android:text="CREATE A PLAN"/>
</RelativeLayout>
<RelativeLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#FFF"
android:textSize="20sp"
android:text="OR" />
</RelativeLayout>
<RelativeLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<Button
android:layout_width="wrap_content"
android:layout_height="44dp"
android:minWidth="88dp"
android:background="#color/mediumGray"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:layout_centerInParent="true"
android:textColor="#FFF"
android:textSize="14sp"
android:text="CHOOSE A PLAN"/>
</RelativeLayout>
</LinearLayout>
I have no idea where build.gradle is picking up merge. I have another fragment activity which is working perfectly.

Modifying Image View Dynamically on a button click inside a fragment

package com.example.layout.layout;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.HashMap;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.View.OnClickListener;
import org.w3c.dom.Text;
/**
* A placeholder fragment containing a simple view.
*/
public class MovieInfoFile extends Fragment {
public static final String ARG_OPTION="argument_option";
HashMap[]items=new HashMap[30];
int i;
public ImageView image;
TextView name;
TextView des;
TextView stars;
TextView year;
TextView rating;
public static MovieInfoFile newInstance(int option)
{
MovieInfoFile fragment=new MovieInfoFile();
Bundle args=new Bundle();
args.putInt(ARG_OPTION,option);
fragment.setArguments(args);
return fragment;
}
public MovieInfoFile() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.movieinfolayout, container, false);
Button loadnext=(Button) view.findViewById(R.id.next);
image=(ImageView) view.findViewById((R.id.image1));
name=(TextView) view.findViewById(R.id.nameans);
des=(TextView) view.findViewById(R.id.desans);
stars=(TextView) view.findViewById(R.id.starans);
year=(TextView) view.findViewById(R.id.yearans);
rating=(TextView) view.findViewById(R.id.ratingans);
enter code hereView.OnClickListener aButtonChangeImageListener=new OnClickListener() {
#Override
public void onClick(View v) {
if(v.getId()==R.id.next)
{
int id=(Integer)items[1].get("image");
image.setImageResource(id);
}
}
};
return view;
}
}
Here is the layout (XML file)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#drawable/baground"
android:weightSum="1">
<ImageView
android:layout_width="315dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:src="#drawable/avatar"
android:id="#+id/image1"
android:layout_weight="0.53" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="center_horizontal"
android:id="#+id/table1">
<TableRow>
<TextView
android:text="Movie Name: "
android:textStyle="bold"
android:id="#+id/name"
/>
<TextView
android:text="Movie Name: "
android:textStyle="bold"
android:id="#+id/nameans"
/>
</TableRow>
<TableRow>
<TextView
android:text="Description: "
android:textStyle="bold"
android:id="#+id/des"
/>
<TextView
android:text="Description: "
android:textStyle="bold"
android:id="#+id/desans"
/>
</TableRow>
<TableRow>
<TextView
android:text="Stars "
android:textStyle="bold"
android:id="#+id/star"
/>
<TextView
android:text="Stars "
android:textStyle="bold"
android:id="#+id/starans"
/>
</TableRow>
<TableRow>
<TextView
android:text="Year"
android:textStyle="bold"
android:id="#+id/year"
/>
<TextView
android:text="Year"
android:textStyle="bold"
android:id="#+id/yearans"
/>
</TableRow>
<TableRow>
<TextView
android:text="Rating"
android:textStyle="bold"
android:id="#+id/rating"
/>
<TextView
android:text="Rating"
android:textStyle="bold"
android:id="#+id/ratingans"
/>
</TableRow>
</TableLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LoadPrev"
android:id="#+id/prev"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loadnext"
android:layout_gravity="center_horizontal"
android:id="#+id/next"/>
</LinearLayout>
I am trying to change the image view dynamically on a button click inside a fragment. But when i do this, it is giving me error, "Unfortunately your app has stopped".Can any one help me in this and tell me the right way of modifying views dynamically on a button click with in a fragment.
for the button load next(i guess after clicking this image should change), So pass aButtonChangeImageListener to the Button loadNext like this:
loadNext.setOnClickListener(aButtonChangeImageListener);
before return view line

Categories

Resources