When I click on login button, it says that app is stopped working. But It should be changed to fragment register. Can anyone tell what I should change in following java file ? because I am new for Android, Please kindly help.
**AdminFragment.java**
public class AdminFragment extends Fragment implementsView.OnClickListener {
EditText etUsername,etPassword;
Button bLogin;
public View mRootView;
public AdminFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mRootView = inflater.inflate(R.layout.fragment_admin, container, false);
etUsername=(EditText)mRootView.findViewById(R.id.etUsername);
etPassword=(EditText)mRootView.findViewById(R.id.etPassword);
bLogin=(Button)mRootView.findViewById(R.id.bLogin);
bLogin.setOnClickListener(this);
return mRootView;
}
#Override
public void onClick(View v) {
Fragment fragment=null;
switch (v.getId()){
case R.id.bLogin:
fragment = new RegisterFragment();
replaceFragment(fragment);
break;
}
}
private void replaceFragment(Fragment AdminFragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.tab,AdminFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
enter code here
my Admin Xml file
My xml file in admin fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="match_parent"
tools:context="com.swach.wecareapp.fragments.AdminFragment">
<TextView
android:id="#+id/tvUsername"
android:text="Username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/etUsername"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvPassword"
android:text="Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/etPassword"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/bLogin"
android:text="Login"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
register xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res /android"
xmlns:tools="http://schppemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:layout_height="match_parent"
tools:context="com.swach.wecareapp.fragments.RegisterFragment">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="#string/Hospital_Name"
/>
<EditText
android:id="#+id/etHospital"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="City"
/>
<EditText
android:id="#+id/etCity"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Level"
/>
<EditText
android:id="#+id/etLevel"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Complaint_Address"
/>
<EditText
android:id="#+id/etAddress"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Phone"
/>
<EditText
android:id="#+id/etPhone"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Street"
/>
<EditText
android:id="#+id/etStreet"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/bRegister"
android:text="Register"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</LinearLayout>
My Register java file
public class RegisterFragment extends Fragment implementsView.OnClickListener {
Button bRegister;
EditText etHospital;
public View mRootView;
public RegisterFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mRootView= inflater.inflate(R.layout.fragment_register, container, false);
bRegister=(Button)mRootView.findViewById(R.id.bRegister);
etHospital=(EditText)mRootView.findViewById(R.id.etHospital);
bRegister.setOnClickListener(this);
return mRootView;
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bRegister:
break;
}
}
}
My register xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schppemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:layout_height="match_parent"
tools:context="com.swach.wecareapp.fragments.RegisterFragment">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="#string/Hospital_Name"
/>
<EditText
android:id="#+id/etHospital"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="City"
/>
<EditText
android:id="#+id/etCity"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Level"
/>
<EditText
android:id="#+id/etLevel"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Complaint_Address"
/>
<EditText
android:id="#+id/etAddress"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Phone"
/>
<EditText
android:id="#+id/etPhone"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Street"
/>
<EditText
android:id="#+id/etStreet"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/bRegister"
android:text="Register"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
Related
in the below code At a time both conditions are execution on click card view listener is executing every time .Edit and share option are not working properly. when I am click on cardview want to execute that one click.If I am click buttonViewOptions want to execute this onClick.
but every time cardview onClick only executing
private boolean isedit=false;
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// showPopupMenu(holder.buttonViewOption);
PopupMenu popup = new PopupMenu(mContext, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_card, popup.getMenu());
// popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
isedit=true;
switch (item.getItemId()) {
case R.id.edit:
editItemClickListner.myEditItemClick(position);
return true;
case R.id.share:
//item.setVisible(false);
return true;
default:
}
return false;
}
});
popup.show();
}
});
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isedit=false;
clickListener.myItemClick(position);
}
});
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="wrap_content"
android:background="#color/back_blue"
android:id="#+id/contact_card"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_anchorGravity="center"
app:layout_behavior="#string/appbar_scrolling_view_behavior" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/card_details">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/cards"
android:background="#color/back_blue">
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/slivergray"
app:cardCornerRadius="5dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/White">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:id="#+id/names">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/salutations"
android:layout_below="#+id/names">
<TextView
android:id="#+id/text_states"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/salutations"
android:paddingLeft="16dp"
android:text="salutationtype"
android:textColor="#color/gray"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/firstname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/appImage"
android:padding="2sp"
android:layout_below="#+id/text_states"
android:textStyle="bold"
android:text="#string/first_name"
android:textColor="#color/gray"
android:textSize="18sp"
tools:ignore="RtlHardcoded"/>
<TextView
android:id="#+id/lastname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/firstname"
android:layout_toRightOf="#+id/appImage"
android:padding="2sp"
android:textStyle="bold"
android:text="#string/last_name"
android:textColor="#color/gray"
android:textSize="18sp"
tools:ignore="RtlHardcoded"/>
</LinearLayout>
<TextView
android:id="#+id/textViewOptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:textColor="#color/linecolor"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:text="⋮"
android:textAppearance="?android:textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/account_names"
android:layout_below="#+id/names"
android:weightSum="4">
<TextView
android:id="#+id/text_acc"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_below="#+id/bill_city"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="Account Name"
android:textColor="#color/gray"
android:textSize="15sp"
/>
<TextView
android:id="#+id/account_name"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/text_spec"
android:layout_weight="2"
android:textStyle="bold"
android:textColor="#color/linecolor"
android:textSize="15sp"/>
</LinearLayout>
<!-- <TextView-->
<!-- android:id="#+id/facility_type"-->
<!-- style="#style/Base.TextAppearance.AppCompat.Subhead"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_below="#+id/account_name"-->
<!-- android:paddingLeft="16dp"-->
<!-- android:text="Facility Type"-->
<!-- android:paddingRight="16dp"-->
<!-- android:textColor="#color/gray"-->
<!-- android:textSize="15sp"/>-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/streets"
android:layout_below="#+id/account_names"
android:weightSum="4">
<TextView
android:id="#+id/text_spec"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_below="#+id/bill_city"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="Specialization"
android:textColor="#color/gray"
android:textSize="15sp"
/>
<TextView
android:id="#+id/specilization"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/text_spec"
android:layout_weight="2"
android:textColor="#color/gray"
android:textSize="15sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/citys"
android:weightSum="4"
android:layout_below="#+id/streets">
<TextView
android:id="#+id/text_city"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/citys"
android:paddingLeft="16dp"
android:text="Job Title"
android:paddingRight="16dp"
android:layout_weight="2"
android:textColor="#color/gray"
android:textSize="15sp" />
<TextView
android:id="#+id/job_title"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/text_city"
android:layout_weight="2"
android:textColor="#color/gray"
android:textSize="15sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/districts"
android:weightSum="4"
android:paddingBottom="3dp"
android:layout_below="#+id/citys">
<TextView
android:id="#+id/text_district"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/bill_district"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="Contact Type"
android:layout_weight="2"
android:textColor="#color/gray"
android:textSize="15sp"
/>
<TextView
android:id="#+id/contact_type"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/text_district"
android:layout_weight="2"
android:paddingRight="16dp"
android:textColor="#color/gray"
android:textSize="15sp"/>
</LinearLayout>
<!-- <TextView-->
<!-- android:id="#+id/textViewOptions"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_alignParentRight="true"-->
<!-- android:layout_alignParentTop="true"-->
<!-- android:paddingLeft="#dimen/activity_horizontal_margin"-->
<!-- android:text="⋮"-->
<!-- android:textColor="#color/gray"-->
<!-- android:layout_marginRight="10dp"-->
<!-- android:textAppearance="?android:textAppearanceLarge" />-->
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/slivergray"
android:layout_below="#id/districts"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4"
android:layout_below="#id/districts">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal">
<Button
android:id="#+id/email"
style="#style/Base.Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:text="ramesh#vshospital.com"
android:drawableLeft="#drawable/ic_email_black_24dp"
android:textColor="#color/gray"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="2"
android:layout_gravity="right">
<Button
android:id="#+id/mobile"
style="#style/Base.Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile Number"
android:drawableLeft="#drawable/ic_phone_black_24dp"
android:inputType="number"
android:textColor="#color/password"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<!-- </com.daimajia.swipe.SwipeLayout>-->
</LinearLayout>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
you issue is that you are setting two different listeners so the final one is excecuting.
it seems the you are using a RecyclerView so set your listeners in the ViewHolder class
public class MViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public View x;
public View y;
public MViewHolder(#NonNull View itemView) {
super(itemView);
x = itemView.findViewById(R.id.....);
y = itemView.findViewById(R.id.....);
x.setOnClickListener(this);
y.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.....:
// code
break;
case R.id....;
//code
break;
}
}`
I have button on my activity that show up on the fragment and only the button that show up , i did search about it and find out its a rendering issue but the answers didn't worked for me, here how it looks.
Fragment
Activity
is there any chance that the issue code be from the margin of the button
Fragment.java
public class Smsar extends Fragment {
public Smsar() {
// Required empty public constructor
}
View root;
Button mSmsar,mFinder;
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
root= inflater.inflate(R.layout.fragment_smsar, container, false);
mSmsar=(Button)root.findViewById(R.id.smsar);
mFinder=(Button)root.findViewById(R.id.finder);
mSmsar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().onBackPressed();
}
});
mFinder.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Open Discover.
}
});
return root;
}
}
Fragment 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="#drawable/main_background_difference"
tools:context=".Smsar">
<!-- TODO: Update blank fragment layout -->
<Button
android:id="#+id/smsar"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="59dp"
android:background="#drawable/btn_rounded"
android:fontFamily="#font/cabin"
android:text="Smsar ?"
android:textAlignment="center"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textSize="18sp"
android:typeface="normal" />
<Button
android:id="#+id/finder"
android:layout_width="204dp"
android:layout_height="63dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="138dp"
android:background="#drawable/btn_rounded"
android:fontFamily="#font/cabin"
android:text="Want Apartment ?"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
</RelativeLayout>
Activity 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="#drawable/background"
android:orientation="vertical"
tools:context=".MainActivity"
android:id="#+id/mainView"
>
<ImageView
android:id="#+id/logo"
android:layout_width="162dp"
android:layout_height="196dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:adjustViewBounds="true"
android:maxHeight="64dp"
android:maxWidth="64dp"
app:srcCompat="#drawable/logo" />
<Button
android:id="#+id/logIn"
android:layout_width="218dp"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/userName"
android:layout_alignParentBottom="true"
android:layout_marginEnd="31dp"
android:layout_marginBottom="113dp"
android:background="#color/buttons"
android:clickable="true"
android:fontFamily="#font/catamaran"
android:onClick="logIN"
android:text="Login"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textSize="22sp" />
<EditText
android:id="#+id/userName"
android:layout_width="277dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ems="10"
android:fontFamily="#font/catamaran"
android:hint="UserName"
android:inputType="textPersonName"
android:text="" />
<EditText
android:id="#+id/password"
android:layout_width="277dp"
android:layout_height="wrap_content"
android:layout_below="#+id/userName"
android:layout_centerHorizontal="true"
android:fontFamily="#font/catamaran"
android:hint="Password"
android:inputType="textPassword" />
<TextView
android:id="#+id/_new"
android:layout_width="159dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-35dp"
android:layout_marginBottom="0dp"
android:layout_toStartOf="#+id/sginUp"
android:fontFamily="#font/catamaran"
android:text="New ? Start Now By"
android:textAlignment="center"
android:textSize="18sp" />
<TextView
android:id="#+id/sginUp"
android:layout_width="81dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginStart="17dp"
android:layout_marginBottom="0dp"
android:layout_centerHorizontal="true"
android:layout_toEndOf="#id/mError"
android:clickable="true"
android:fontFamily="#font/catamaran"
android:text="SginUp"
android:textAlignment="center"
android:textColor="#color/links"
android:textSize="18sp" />
<TextView
android:id="#+id/mError"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="82dp"
android:textColor="#android:color/holo_red_dark"
android:visibility="invisible"
android:text="mError" />
</RelativeLayout>
Activity.Java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_login =(Button)findViewById(R.id.logIn);
pref = getSharedPreferences("user_details", MODE_PRIVATE);
if(pref.contains("username")&&pref.contains("password"))
success();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.mainView, mFrag);
ft.commit();
imageView=(ImageView)findViewById(R.id.logo);
mDBHelper =new DBHelper(this);
mError=(TextView)findViewById(R.id.mError);
//Define the variables
userNameEdit=(EditText)findViewById(R.id.userName);
passwordEdit=(EditText)findViewById(R.id.password);
_signUp=(TextView)findViewById(R.id.sginUp);
//End
_signUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
intent=new Intent(MainActivity.this,Signup.class);
startActivity(intent);
}
});
}
I solve it by surround the button with LinearLayout
<LinearLayout
android:id="#+id/buttonContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/password"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
>
<Button
android:id="#+id/logIn"
android:layout_width="186dp"
android:layout_height="wrap_content"
android:layout_marginVertical="12dp"
android:background="#drawable/btn_rounded"
android:minWidth="200dp"
android:onClick="logIN"
android:text="#string/title_login"
android:textAllCaps="false"
android:textColor="#color/ms_white"
android:textSize="18sp" />
</LinearLayout>
What have I missed out here? I wanted to open edit_information fragment from edit activity. It can display the Toast but not displaying the edit_information layout.
Edit
listViewEdit.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding listview_item_row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this listview_item_row in the database.
String ID =
cursor.getString(cursor.getColumnIndexOrThrow("_id"));
EditInformation fragment2 = new EditInformation();
Bundle bundle = new Bundle();
bundle.putString("ID", ID);
fragment2.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment1, fragment2);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
EditInformation
public class EditInformation extends Fragment {
String ID;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View edit_info = inflater.inflate(R.layout.edit_information, container, false);
Bundle bundle = this.getArguments();
if(getArguments()!=null)
{
ID=bundle.getString("ID");
}
Toast.makeText(getActivity(),"Edit_Information",Toast.LENGTH_LONG).show();
return edit_info;
}
}
edit
<?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:id="#+id/fragment1">
<RelativeLayout
android:layout_width="209dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/tomato"
android:paddingLeft="10sp"
android:textSize="20sp"
android:paddingTop="10sp"
android:hint="Long Press to delete"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/tomato"
android:paddingTop="40sp"
android:textSize="20sp"
android:paddingLeft="10sp"
android:hint="Hint to edit"/>
</RelativeLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listViewEdit"
android:paddingTop="10sp"/>
</LinearLayout>
edit_information
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10sp"
android:orientation="vertical"
android:background="#color/beige">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_corner_up"
android:padding="10dp"
android:text="Name"
android:textColor="#color/tomato"
android:textSize="20dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Name"
android:layout_marginTop="-15dp"
android:background="#drawable/round_corner_up">
<Spinner
android:layout_width="339dp"
android:layout_height="52dp"
android:id="#+id/SpinnerName" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<TextView
android:id="#+id/Weather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_corner_up"
android:padding="10dp"
android:text="Weather"
android:textColor="#color/tomato"
android:textSize="20dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Weather"
android:layout_marginTop="-15dp"
android:background="#drawable/round_corner_up">
<Spinner
android:layout_width="342dp"
android:layout_height="52dp"
android:id="#+id/SpinnerWeather">
</Spinner>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Date"
android:background="#drawable/round_corner_up"
android:padding="10dp"
android:text="Date"
android:textColor="#color/tomato"
android:textSize="20dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Date"
android:layout_marginTop="-15dp"
android:background="#drawable/round_corner_up">
<EditText
android:layout_width="311dp"
android:layout_height="52dp"
android:layout_margin="10sp"
android:background="#drawable/round_corner_square"
android:id="#+id/date"
android:paddingLeft="20dp" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Status"
android:background="#drawable/round_corner_up"
android:padding="10dp"
android:text="Status"
android:textColor="#color/tomato"
android:textSize="20dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Status"
android:layout_marginTop="-15dp"
android:background="#drawable/round_corner_up">
<Spinner
android:layout_width="339dp"
android:layout_height="52dp"
android:id="#+id/SpinnerStatus">
</Spinner>
</LinearLayout>
</RelativeLayout>
<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20sp"
android:orientation="horizontal">
<Button
android:layout_width="187dp"
android:layout_weight="0.83"
android:text="Add Work Force"
android:textSize="20sp"
android:drawableRight="#mipmap/add_workforce"
android:layout_height="75dp"
android:id="#+id/AddForce"
android:layout_x="12dp"
android:layout_y="11dp" />
<Button
android:layout_width="123dp"
android:layout_weight="0.83"
android:text="Next"
android:textSize="20sp"
android:drawableRight="#mipmap/next"
android:layout_height="71dp"
android:id="#+id/Next"
android:layout_x="211dp"
android:layout_y="14dp" />
</AbsoluteLayout>
</LinearLayout>
Try using FrameLayout as a container for fragment instead of LinearLayout. I believe because of LinearLayout your fragment is getting created out of screen. where as FrameLayout will overlap your fragment on other Views.
How to remove main activities buttons from the fragment with the codes in android?
There are a few buttons in activity_main.xml. I want to access and remove a button from the fragment class. Is this posssible and how?
main_activ.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/paper"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:orientation="horizontal" >
<Button
android:id="#+id/butBack"
android:layout_width="wrap_content"
android:layout_height="60.0dip"
android:layout_gravity="fill"
android:layout_weight="0.5"
android:text=".."
android:textColor="#ffffffff"
android:textSize="16.0sp"
android:background="#drawable/button_style"
android:textStyle="bold" />
<Button
android:id="#+id/butMain"
android:layout_width="wrap_content"
android:layout_height="60.0dip"
android:layout_gravity="fill"
android:layout_marginLeft="2dp"
android:layout_weight="0.5"
android:text="..."
android:textColor="#ffffffff"
android:textSize="16.0sp"
android:background="#drawable/button_style"
android:textStyle="bold" />
<Button
android:id="#+id/butNext"
android:layout_width="wrap_content"
android:layout_height="60.0dip"
android:layout_gravity="fill"
android:layout_marginLeft="2dp"
android:layout_weight="0.5"
android:text=".."
android:textColor="#ffffffff"
android:textSize="16.0sp"
android:background="#drawable/button_style"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
My fragment
public class Frag_Menu extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_menu, container, false);
return view;
}
public void onActivityCreated(Bundle savedInstanceState) {
ViewGroup layout = (ViewGroup) getActivity().findViewById(R.id.butBack).getParent();
if(null!=layout)
layout.removeView(layout);
};
public static android.support.v4.app.Fragment newInstance(String string) {
return null;
}
}
You can use getActivity().findViewById() to get your Button from the fragment class in onActivityCreated().
View button = getActivity().findViewById(R.id.butBack);
if (button != null && button.getParent() != null) {
((ViewGroup)button.getParent()).removeView(button);
}
i am using fragment in my application.there are 5 tabs.in 1 tab i need to inflate a list view on a button click.given below is my code for that tab.
public class TabStoreLocatorFragment extends Fragment implements OnClickListener {
private Button mapSwitcher;
private LinearLayout lastSelectedStoreButton;
private LinearLayout noSelectedStoreSection;
private TextView storeInfoName;
private TextView storeInfoAddress;
private TextView storeInfoCity;
private RelativeLayout phoneLocationButton;
private EditText searchStoreLocation;
private ListView storeList;
private ViewFlipper storeFlipper;
LinearLayout theLayout;
Store store;
#SuppressWarnings("static-access")
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
}
theLayout = (LinearLayout) inflater.inflate(
R.layout.tab_store_locator_layout, container, false);
phoneLocationButton = (RelativeLayout) theLayout.findViewById(R.id.phone_location_button);
mapSwitcher=(Button)theLayout.findViewById(R.id.switch_state_button);
lastSelectedStoreButton=(LinearLayout)theLayout.findViewById(R.id.last_selected_store_button);
noSelectedStoreSection=(LinearLayout)theLayout.findViewById(R.id.no_selected_store);
storeInfoName=(TextView)theLayout.findViewById(R.id.store_name);
storeInfoAddress=(TextView)theLayout.findViewById(R.id.store_address);
storeInfoCity=(TextView)theLayout.findViewById(R.id.store_city);
searchStoreLocation=(EditText)theLayout.findViewById(R.id.search_store);
storeFlipper = (ViewFlipper)theLayout.findViewById(R.id.store_flipper);
store=new Store();
store.setName("store1");
store.setAddress("california");
store.setCity("newyork");
store.setZipCode("23");
store.setState("california");
phoneLocationButton.setOnClickListener(this);
mapSwitcher.setOnClickListener(this);
lastSelectedStoreButton.setOnClickListener(this);
return theLayout;
}
#Override
public void onClick(View view) {
int id = view.getId();
if (id == R.id.phone_location_button) {
storeList = (ListView)storeFlipper.inflate(getActivity(), R.layout.store_list, storeFlipper);
storeFlipper.addView(storeList);
StoreAdapter adapter = new StoreAdapter(getActivity(), R.layout.store_list_item);
storeList.setAdapter(adapter);
}
else if(id == R.id.switch_state_button){
}
else{
}
}
}
i want to inflate storelist to storeFlipper.i tried to do that.but it is not working.given below is the corresponding layout for that tab.
<?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:background="#android:color/white" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/find_a_store_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:layout_alignParentLeft="true"
android:text="#string/tab_find_a_store_title"
android:textColor="#android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
<Button android:id="#+id/switch_state_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#drawable/switch_selector" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#E8E2D3" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#C5BFAD" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/last_selected_store_button" android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#android:style/Widget.Button"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="5dp"
android:background="#drawable/last_selected_store_background" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="#8E8672"
android:text="#string/last_selected_store_label"
android:textSize="12sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:orientation="vertical" >
<TextView android:id="#+id/store_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textSize="12sp" />
<TextView android:id="#+id/store_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textSize="12sp" />
<TextView android:id="#+id/store_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textSize="12sp" />
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/arrow" />
</RelativeLayout>
</LinearLayout>
<LinearLayout android:id="#+id/no_selected_store"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="5dp"
android:background="#drawable/last_selected_store_background" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#8E8672"
android:text="#string/no_selected_store_label"
android:textSize="12sp" />
</LinearLayout>
</FrameLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp" >
<EditText android:id="#+id/search_store"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:singleLine="true"
android:textSize="14sp"
android:hint="#string/search_store_placeholder"
android:layout_toLeftOf="#+id/phone_location_button"
android:background="#drawable/search_store_background" />
<RelativeLayout android:id="#id/phone_location_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:background="#drawable/get_location_background" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/get_location" />
</RelativeLayout>
</RelativeLayout>
<ViewFlipper android:id="#+id/store_flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
can anybody help me to inflate the list to flipper.
it is simple.just put this code.
storeList = (ListView) inflater.inflate(R.layout.store_list, null);
it is working