I'm trying to create a quiz app with a sidebar that consists of flags (image buttons), and when you click on each flag, you can type in the country it belongs to in a fragment on the right hand side of the screen. However I'm getting an error inside the if statement in my Play.java file where it says:
fragment = new FragmentOne();
and
fragment = new FragmentTwo();
Play.java
import android.os.Bundle;
import android.app.FragmentManager;
import android.view.View;
import android.view.Menu;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import layout.FragmentOne;
import layout.FragmentTwo;
public class Play extends MainActivity {
Fragment fragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
}
public void ChangeFragment(View view) {
if(view == findViewById(R.id.imageButton10)) {
fragment = new FragmentOne();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_place, fragment);
ft.commit();
}
if(view == findViewById(R.id.imageButton9)) {
fragment = new FragmentTwo();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_place, fragment);
ft.commit();
}
}
}
activity_play.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_play"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.karolinawullum.quizapp.Play">
<LinearLayout
android:layout_width="80dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/LinearLayout1">
<TextView
android:text="Which country does this flag belong to? Press flag to answer."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="10sp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/germany"
android:id="#+id/imageButton10"
android:layout_marginBottom="20dp"
android:layout_marginTop="50dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:onClick="ChangeFragment" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/greece"
android:id="#+id/imageButton9"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:onClick="ChangeFragment"
android:scaleType="fitXY"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/france"
android:id="#+id/imageButton8"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/finland"
android:id="#+id/imageButton7"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/denmark"
android:id="#+id/imageButton6"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/cyprus"
android:id="#+id/imageButton5"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/belgium"
android:id="#+id/imageButton4"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/austria"
android:id="#+id/imageButton3"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
</LinearLayout>
<fragment
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:name="layout.FragmentOne"
android:id="#+id/fragment_place"
android:layout_weight="0.72" />
</LinearLayout>
FragmentOne.java
package layout;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_one, container, false);
}
}
Does anyone know what I'm doing wrong?
In your Play.java class, your Fragment variable is of type android.app.Fragment. Though your FragmentOne.java (and I assume your FragmentTwo.java as well) class is of type android.support.v4.app.Fragment.
So in your Play.java class you're trying to initialize the Fragment variable with the wrong type.
Either :
Make your FragmentOne.java and FragmentTwo.java extends android.app.Fragment instead of the support version one
Make your Fragment variable in Play.java of type android.support.v4.Fragment by changing your import.
Related
RecycleView works fine in the first time on my "home" Activity, but when i go to another activity (through the navigation menu) and comeback to "home" Activity the recycle view don't show anything ?
the items in that my recycleview shows are a linearlayouts (user_row.xml), when i use log.e i can see that the data is there but the recyleview fails to list the items (in the second time)
The home activity :
package com.example.forum;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.navigation.NavigationView;
import java.util.HashMap;
public class Home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
//Variables
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
DrawerLayout drawerLayout;
NavigationView navigationView;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//HOOKS OF NAVIGATION MENU
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
toolbar = findViewById(R.id.toolbar);
//DATA AND RECYCLERVIEW
recyclerView = findViewById(R.id.recycler_view);
adapter = new ForumAdapter(getApplicationContext());
layoutManager = new LinearLayoutManager(this);
//TOOL BAR
setSupportActionBar(toolbar);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
//NAVIGATION MENU
navigationView.bringToFront();
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout , toolbar, R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
if(drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}
else{
super.onBackPressed();
}
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_home:
Intent intent33 = new Intent(Home.this,Home.class);
startActivity(intent33);
break;
case R.id.nav_user:
Intent intent = new Intent(Home.this,Myprofile.class);
startActivity(intent);
break;
case R.id.nav_logout:
SessionManager sessionManager = new SessionManager(Home.this);
sessionManager.logout();
Intent intent2 = new Intent(Home.this,Login.class);
startActivity(intent2);
break;
}
return true;
}
}
The home xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient"
android:id="#+id/drawer_layout"
tools:context=".Home"
tools:ignore="ExtraText"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/nav_view"
app:headerLayout="#layout/header"
app:menu="#menu/main_menu"
android:layout_gravity="start"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="675dp"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.006"
app:navigationIcon="#drawable/ic_action_menu"
app:title="Home"
app:titleMarginBottom="10dp"
app:titleTextAppearance="#style/TextAppearance.AppCompat.Large"
app:titleTextColor="#00BCD4" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="60dp"
android:layout_marginBottom="24dp"
android:fontFamily="#font/aldrich"
android:text="#string/ensak_business_forum"
android:textColor="#E4E4E4"
android:textColorLink="#FFFFFF"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="#+id/linearLayoutxxx"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:translationY="130dp"
android:id="#+id/recycler_view"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="175dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:background="#00FFFFFF"
android:orientation="horizontal"
android:padding="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/recycler_view">
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1"
app:cardBackgroundColor="#00FFFFFF"
app:cardElevation="0dp">
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1"
app:cardBackgroundColor="#00FFFFFF"
app:cardCornerRadius="20dp"
app:cardElevation="0dp">
</androidx.cardview.widget.CardView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.drawerlayout.widget.DrawerLayout>
The view row (user_row.xml) :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linearLayoutxxx"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:background="#drawable/rounding_relative_layouts"
android:orientation="horizontal"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:padding="10dp">
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:id="#+id/firstcardxxx"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/imagelayoutxxx"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView4xxx"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:id="#+id/secondcardxxx"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
>
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/infoslayoutxxx"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView4xxx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:fontFamily="#font/aldrich"
android:textColor="#000000"
android:textSize="24sp" />
<Button
android:id="#+id/button4xxx"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#id/textView4xxx"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:background="#drawable/rounded_green_button"
android:fontFamily="#font/aldrich"
android:text="VIEW MORE"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:textColor="#FFFFFF"
android:textColorHighlight="#FFFFFF"
android:textColorHint="#FFFFFF" />
<Button
android:id="#+id/button5xxx"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#id/button4xxx"
android:layout_centerHorizontal="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="#drawable/rounded_red_button"
android:fontFamily="#font/aldrich"
android:text="SUBSCRIBE"
android:textColor="#FFFFFF" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
The home page :
The home page
the menu :
The menu :
when i go to another activity :
when i go to another activity :
when i come back through menu :
when i come back through menu :
This is happening because all of your logic is inside the onCreate() method of the Home activity. onCreate() is only invoked once when an activity is created.
Activities are kept on the activity stack, with the most recent activities being at the top of the stack.
In your case, the Home activity is placed into a stopped state while the other activity is active.
When you navigate back to the Home Activity, onCreate() is not called because it is already created. In this case onRestart() is called followed by onStart().
My suggestion is to move the code for the RecyclerView in onCreate() to onStart().
You should read about the Activity Lifecycle here.
And for future reference, there is also a Fragment Lifecycle which is slightly different. You can read about the Fragment Lifecycle here.
I found the response to my question, so i'll just share the answer in case any one faces the same problem, the problem i had is that the activity launches with no data in it, i thought the problem was from the recycler view or from the layout manager, but actually the problem was so simple i just have to wait for the data to download first!, if the activity life starts and the data hasn't been loaded yet it will show nothing, so i added a simple ----- WAIT() ----- function to dellay the data loading first then the data can be bined to recycler view and so on ...
In my app I have an Activity containing a simple container (LinearLayout) in which I want to display a Fragment. It all works and the fragment is shown but it is kind of "cut off" at the end of the screen. Meaning if in the fragment the screen is completely filled out with buttons, in the actual running app the last button is not or only partly shown.
To make it easier to test I created this new project where in the fragment there are only 3 big buttons which, in the design function in android studio, do not extend the screen but then when I run it are cut off.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".BlankFragment">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Button"
android:textSize="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Button"
android:textSize="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button" />
<Button
android:id="#+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Button"
android:textSize="70dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.51"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button9" />
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"
android:orientation="vertical"
tools:context=".MainActivity"/>
package com.example.fragmenttestapp;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BlankFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
}
package com.example.fragmenttestapp;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
BlankFragment fragment = new BlankFragment();
fragmentTransaction.add(R.id.container, fragment);
fragmentTransaction.commit();
}
}
Would appreciate some help! :)
I am 99% sure that your views are not fitting into your screen because of your huge text size on your views (100dp), large text size will resize your view, and you are using a fixed size for you texts size.
Remember that fixed size on views makes your layout non-responsive
After I removed your text size from the views it was alright, here is an example:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="#+id/button9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button9"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="#+id/button10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button" />
<Button
android:id="#+id/button10"
android:layout_width="w"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="#+id/guideline3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button9" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5"/>
</androidx.constraintlayout.widget.ConstraintLayout>
And if you want to use large text maybe use Autosizing TextViews
I have a simple LinearLayout with several buttons, whos state color/text change based on the state of an underlying service, thats working fine.
However the buttons, are only clickable on the right corner ???
The button allSystemServicesToggleButton which i have included the implementation for in this post and only be clicked on the right side/right corner???
Here is my fragment xml layout & Actual screen shot with “Show Layout bounds” set to true:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:text="All System Services"
android:textColor="#000000"
android:textSize="20sp"
android:layout_weight="1"
android:layout_width="0dp"
android:singleLine="true"
android:onClick="onClick"/>
<Button
android:id="#+id/allSystemServicesToggleButton"
android:layout_height="wrap_content"
android:text="#string/stopped"
android:layout_weight="1"
android:layout_width="0dp"
android:backgroundTint="#color/stoppedServiceColor"
android:enabled="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:text="#string/shutdown_all_services"
android:textColor="#000000"
android:textSize="20sp"
android:layout_weight="1"
android:layout_width="0dp"
android:singleLine="true"
android:onClick="onClick"/>
<Button
android:id="#+id/shutdownAllServicesToggleButton"
android:layout_height="wrap_content"
android:text="#string/shutdown"
android:layout_weight="1"
android:layout_width="0dp"
android:enabled="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="Networks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:textColor="#000000"/>
<View
android:id="#+id/viewServicesDivider1"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#808080"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="Bluetooth Service"
android:textColor="#000000"
android:textSize="20sp"
android:singleLine="true"/>
<Button
android:id="#+id/btServicesToggleButton"
android:layout_height="wrap_content"
android:text="#string/stopped"
android:layout_weight="1"
android:layout_width="0dp"
android:backgroundTint="#color/stoppedServiceColor"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--TODO: get requirements for showing paired devices & pairing devices-->
<TextView
android:id="#+id/textPairedText"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="Paired Bluetooth Devices"
android:textColor="#000000"
android:singleLine="true"
android:textSize="20sp"
android:layout_width="0dp"
/>
<TextView
android:id="#+id/textViewNumberOfConnectedDevices"
android:layout_height="wrap_content"
android:text="0"
android:layout_width="0dp"
android:layout_weight="1" />
<Button
android:id="#+id/btDevicesToggleButton"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="2"
android:text="Pair"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="MQTT Service"
android:textColor="#000000"
android:textSize="20sp"
android:singleLine="true"/>
<Button
android:id="#+id/MQTTserviceToggleButton"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:backgroundTint="#color/stoppedServiceColor"
android:text="#string/stopped" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="Location Services"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:textColor="#000000"/>
<View
android:id="#+id/viewServicesDivider3"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#808080"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="GPS"
android:textColor="#000000"
android:textSize="20sp"
android:singleLine="true"/>
<Button
android:id="#+id/gpsServiceToggleButton"
android:layout_height="wrap_content"
android:text="#string/stopped"
android:layout_weight="1"
android:layout_width="0dp"
android:backgroundTint="#color/stoppedServiceColor"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="Command Services"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:textColor="#000000"/>
<View
android:id="#+id/viewServicesDivider4"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#808080"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="Voice Recognition"
android:textColor="#000000"
android:textSize="20sp"
android:singleLine="true"/>
<Button
android:id="#+id/voiceRecognitionToggleButton"
android:layout_height="wrap_content"
android:text="#string/stopped"
android:layout_weight="1"
android:layout_width="0dp"
android:backgroundTint="#color/stoppedServiceColor"
/>
</LinearLayout>
Relevant fragment java:
package x.core.fragments;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.Fragment;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import x.core.Application.NgfrApp;
import x.core.R;
import x.core.helpers.Util;
import x.core.services.BluetoothService;
import x.core.services.LocationService;
import x.core.services.MqttBrokerService;
import x.core.services.ServicesStateBroadcastReceiver;
import x.core.services.SpeechRecognitionService;
import x.core.services.UIService;
public class ServicesFragment extends Fragment implements View.OnClickListener {
private static final String TAG = "ServicesFragment";
public static ServicesFragment newInstance() {
return new ServicesFragment();
}
private static Button btServicesToggleButton;
private static Button mqttServicesToggleButton;
private static Button gpsServiceToggleButton;
private static Button voiceServiceToggleButton;
private static Button allServiceToggleButton;
private static String stopped = null;
private static String running = null;
private static int runningColorId, stoppedColorId = -1;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_services, container, false);
btServicesToggleButton = rootView.findViewById(R.id.btServicesToggleButton);
mqttServicesToggleButton = rootView.findViewById(R.id.MQTTserviceToggleButton);
gpsServiceToggleButton = rootView.findViewById(R.id.gpsServiceToggleButton);
voiceServiceToggleButton = rootView.findViewById(R.id.voiceRecognitionToggleButton);
allServiceToggleButton = rootView.findViewById(R.id.allSystemServicesToggleButton);
stopped = getResources().getString(R.string.stopped);
running = getResources().getString(R.string.running);
runningColorId = getResources().getColor(R.color.runningServiceColor);
stoppedColorId = getResources().getColor(R.color.stoppedServiceColor);
allServiceToggleButton.setEnabled(true);
allServiceToggleButton.setClickable(true);
allServiceToggleButton.setOnClickListener(this);
return rootView;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.allSystemServicesToggleButton:
if (ServicesStateBroadcastReceiver.BT_SERVICE_STATE_VALUE==false || ServicesStateBroadcastReceiver.MQTT_STATE_VALUE==false || ServicesStateBroadcastReceiver.NGFR_GPS_SERVICE_STATE_VALUE==false || ServicesStateBroadcastReceiver.VOICE_SERVICE_STATE_VALUE==false)
{
Toast.makeText(NgfrApp.getContext(),NgfrApp.getContext().getResources().getString(R.string.restarting_services),Toast.LENGTH_SHORT).show();
//restartingServices();
}
else
{
Toast.makeText(NgfrApp.getContext(),NgfrApp.getContext().getResources().getString(R.string.all_already_running),Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
}
MainActivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<android.support.design.widget.TabLayout
android:id="#+id/activity_main_tabLyout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/activity_main_viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
MainActivity.java, I only included relevant code:
public class MainActivity extends AppCompatActivity {
private static String TAG = "Main";
private static final int CHECK_BT_CODE = 1;
private static final int CHECK_TTS_CODE = 2;
//global boolean flags that will communicate the state of the system at all times
//bluetooth related flags
public boolean isBleSupported = false;
public boolean isBluetoothEnabled = false;
public boolean accessBluetoothManager= false;
public boolean nearbyDevices = false;
//configuration data related
public boolean isConfigurationLoadedCorrectly = false;
//text to speech related
public boolean isTextToSpeechSupported = false;
private Context context = null;
private ServicesStateBroadcastReceiver servicesStateBroadcastReciever = null;
private ViewPager mainViewPager;
private TabLayout tabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "Activity started!!");
context = this;
setContentView(R.layout.activity_main);
MainActivityViewPager adapter = new MainActivityViewPager(getSupportFragmentManager());
mainViewPager = (ViewPager) findViewById(R.id.activity_main_viewPager);
mainViewPager.setAdapter(adapter);
tabLayout = (TabLayout) findViewById(R.id.activity_main_tabLyout);
tabLayout.setupWithViewPager(mainViewPager );
}
}
The adapter for my fragments, FragmentStatePagerAdapter:
package x.core.views;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import x.BiometricsFragment;
import x.ServicesFragment;
public class MainActivityViewPager extends FragmentStatePagerAdapter {
public MainActivityViewPager(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment returnFragment;
switch(position) {
case 0:
returnFragment = ServicesFragment.newInstance();
break;
case 1:
returnFragment = BiometricsFragment.newInstance();
break;
default:
return null;
}
return returnFragment;
}
#Override
public int getCount() {
return 2;
}
public CharSequence getPageTitle(int position) {
CharSequence title;
switch (position) {
case 0:
title = "Services";
break;
case 1:
title = "Biometrics";
break;
default:
return null;
}
return title;
}
}
Thanks
only for corner clicking use this kind of logic
<FrameLayout
android:layout_width="50dp"
android:layout_height="50dp">
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#color/colorAccent" />
<TextView
android:id="#+id/tvtttt"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_gravity="right"
android:background="#F00" />
</FrameLayout>
1- I have a fragment which has 2 EditText and 1 Button (below the code is)
<android.support.constraint.ConstraintLayout 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.junior_yao.fragmentfrombeginning.Top_Fragment"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="information"
android:inputType="text|textShortMessage"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintHorizontal_bias="0.503" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="information"
android:inputType="textPersonName"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="#+id/editText1"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="#+id/editText1"
android:layout_marginTop="23dp"
app:layout_constraintTop_toBottomOf="#+id/editText1"
app:layout_constraintHorizontal_bias="0.0" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Info"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="#+id/editText1"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="#+id/editText1"
android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="#+id/editText2"
app:layout_constraintHorizontal_bias="0.495" />
</android.support.constraint.ConstraintLayout>
2- I have another fragment which has one Image View and 2 TextView
<android.support.constraint.ConstraintLayout 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.junior_yao.fragmentfrombeginning.Down_Fragment"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/androidtest"
tools:layout_constraintTop_creator="1"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:id="#+id/imageView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text2"
android:textSize="25dp"
android:textColor="#7FFF00"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="30dp"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text1"
android:textSize="25dp"
android:textColor="#7FFF00"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="44dp" />
</android.support.constraint.ConstraintLayout>
On the 3rd part I add those 2 fragment in main activity layout
3 - if I add the first fragment only (I got no Error )
4 - Here the issue
If I add the 2nd fragment the app does not start , please help me to figure it out
<?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.junior_yao.fragmentfrombeginning.MainActivity">
<fragment
android:id="#+id/fragment"
android:name="com.example.junior_yao.fragmentfrombeginning.Top_Fragment"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
/>
<fragment
android:id="#+id/fragment12"
android:name="com.example.junior_yao.fragmentfrombeginning.Down_Fragment"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
/>
</RelativeLayout>
You need to add more information so you can be helped, the java code where the fragments are being called and created and the logcat as well to help solve the problem, the XML design code is not helpful.
here the java code for the 1st fragment
package com.example.junior_yao.fragmentfrombeginning;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Top_Fragment extends Fragment {
public Top_Fragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_top, container, false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
The java code for the 2nd fragment
package com.example.junior_yao.fragmentfrombeginning;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Down_Fragment extends Fragment {
public Down_Fragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_down, container, false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
Now the java code of the main Activity
package com.example.junior_yao.fragmentfrombeginning;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I have image-buttons in one of the fragments of an activity and want to open corresponding fragments containing webViews on button clicks in another activity.
I am a beginner so please do give appropriate codes for this.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.administrator.hiha.MainActivity"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="#+id/toolbar"
layout="#layout/tool_bar">
</include>
<fragment
android:id="#+id/upperFragment"
android:layout_width="match_parent"
android:layout_height="200dp"
android:name="com.example.administrator.hiha.Upper_main_Fragment"
android:layout_below="#id/toolbar"
>
</fragment>
<fragment
android:layout_below="#id/upperFragment"
android:id="#+id/lowerFragment"
android:layout_width="match_parent"
android:layout_height="140dp"
android:name="com.example.administrator.hiha.Lower_main_Fragment"
>
</fragment>
<fragment
android:layout_below="#id/lowerFragment"
android:id="#+id/Bottom_Most_Fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.administrator.hiha.BottomMostFragment"
tools:layout="#layout/fragment_bottom_most">
</fragment>
<View
android:id="#+id/hLastRow"
android:layout_centerHorizontal="true"
android:layout_below="#id/Bottom_Most_Fragment"
android:layout_width="300dp"
android:layout_height="2dp"
android:background="#EEEEEE"
/>
<TextView
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/hLastRow"
android:layout_centerHorizontal="true"
android:text="Designed \u0026 Developed by me © 2016."/>
</RelativeLayout>
</ScrollView>
MainActivity.java
package com.example.administrator.hiha;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
//Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//toolbar=(Toolbar)findViewById(R.id.toolbar);
}
}
fragment_lower_main.xml
<FrameLayout 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"
tools:context="com.example.administrator.hiha.Lower_main_Fragment">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/about_board"
android:id="#+id/about_img_btn"
android:onClick="onAboutBoardClick"
/>
<TextView
android:id="#+id/text_about_img_btn"
android:text="About Board"
android:layout_below="#id/about_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/sarasvati"
android:id="#+id/sarasvati_img_btn"
android:layout_toRightOf="#id/about_img_btn"
android:onClick="onSarasvatiClick"
/>
<TextView
android:layout_toRightOf="#id/text_about_img_btn"
android:id="#+id/text_sarasvati_img_btn"
android:text="Sarasvati"
android:layout_below="#id/sarasvati_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="#id/sarasvati_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/scientific_evidences"
android:id="#+id/scientific_evidences_img_btn"
android:onClick="onScientificEvidencesClick"
/>
<TextView
android:layout_toRightOf="#id/text_sarasvati_img_btn"
android:id="#+id/text_scientific_evidences_img_btn"
android:text="Scientific EVidences"
android:layout_below="#id/scientific_evidences_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="#id/digital_library_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/affltd_organization"
android:id="#+id/affltd_oragnization_img_btn"
android:onClick="onAffltdOrganizationClick"
/>
<TextView
android:layout_toRightOf="#id/text_digital_library_img_btn"
android:id="#+id/text_affltd_oragnization_img_btn"
android:text="Affiliated Organizations"
android:layout_below="#id/affltd_oragnization_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="#id/affltd_oragnization_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/tender"
android:id="#+id/tender_img_btn"
android:onClick="onTenderClick"
/>
<TextView
android:layout_toRightOf="#id/text_affltd_oragnization_img_btn"
android:id="#+id/text_tender_img_btn"
android:text="Tenders"
android:layout_below="#id/tender_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
</RelativeLayout>
</LinearLayout>
</HorizontalScrollView>
</FrameLayout>
Lower_Main_Fragment.java
package com.example.administrator.hiha;
import android.content.Context;
import android.net.Uri;
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.ImageButton;
public class Lower_main_Fragment extends Fragment {
ImageButton about_img_btn;
ImageButton sarasvati_img_btn;
ImageButton scientific_evidences_img_btn;
ImageButton digital_library_img_btn;
ImageButton affltd_oragnization_img_btn;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView=inflater.inflate(R.layout.fragment_lower_main_,container,false);
//cast image buttons
about_img_btn=(ImageButton)getView().findViewById(R.id.about_img_btn);
sarasvati_img_btn=(ImageButton)getView().findViewById(R.id.sarasvati_img_btn);
scientific_evidences_img_btn=(ImageButton)getView().findViewById(R.id.scientific_evidences_img_btn);
digital_library_img_btn=(ImageButton)getView().findViewById(R.id.digital_library_img_btn);
affltd_oragnization_img_btn=(ImageButton)getView().findViewById(R.id.affltd_oragnization_img_btn);
return rootView;
}
//onclick methods of image buttons
public void onAboutBoardClick(View view){
}
}
These are the files in which i want that if i click on about_img_btn then it should open a fragment containing a webView in another activity. The same should be repeated for all the buttons.
Please define the further code that i have to use and mention the files where i have to use it. Thanks!
try this:
in your fragment_lower_main xml:
Give an id to the Framelayout:
<?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"
tools:context="com.example.administrator.hiha.Lower_main_Fragment">
<FrameLayout
android:id="+#id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<--Rest of the code -->
/>
</LinearLayout>
Now in code open new Fragment during button click using:
about_img_btn=(ImageButton)rootView.findViewById(R.id.about_img_btn);
about_img_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AboutUS fragment = new AboutUS();
Replace_fragment(fragment); //pass the fragment u want to replace
}
});
public void ReplaceFragment(Fragment r_fragment) {
FragmentManager fragmentManager = getActivity.getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_container, r_fragment).commit();
}