I'm facing an issue regarding intro screen in android.
I create button in one fragment but i'm trying to call this button in welcomeActivity through layoutinflate in activity but it doesn't perform any functionality.
XML Fragment(custom_intro):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id ="#+id/explore"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Explore"
android:background="#drawable/round_button"
android:padding="15dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="114dp" />
</RelativeLayout>
WelcomeActivityLayout
<?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="#color/colorPrimaryDark">
<!--tools:showIn="#layout/welcome_activity"-->
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
WelcomeActivityJava:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_activity);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
customView = inflater.inflate(R.layout.custom_intro, null);
Button button=(Button)customView.findViewById(R.id.explore);
button.setText("Test");
Int[] layouts = new int[]{
R.layout.custom_intro,
R.layout.custom_intro1,
R.layout.custom_intro2,
R.layout.custom_intro3,
R.layout.custom_intro4};
myViewPagerAdapter = new MyViewPagerAdapter();
viewPager.setAdapter(myViewPagerAdapter);
viewPager.addOnPageChangeListener(viewPagerPageChangeListener);
viewPagerIndicator.setupWithViewPager(viewPager);
viewPagerIndicator.addOnPageChangeListener(viewPagerPageChangeListener);
}
If you want to access the Button of Fragment inside its parent Activity then you should define a method inside your Fragment class like this:
public class MyFragment extends Fragment {
TextView mTextView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, container, false);
mButton = (Button) view.findViewById(R.id.textView1);
return view;
}
public void setButtonText(String value){
mTextView.setText(value);
}
}
Then you can use this inside your Activity like this:
myFragment.setButtonText("Test");
Related
I am trying to have dynamic number of cardviews in my layout.For test i am trying to insert 3 cardviews in my layout,but instead of three only one card is added with "hello 3" written on it.
Here's my code for activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int i;
LinearLayout layout=(LinearLayout)findViewById(R.id.mainl);
for(i=1;i<=3;i++)
{
setContentView(R.layout.card);
LayoutInflater layoutInflater=(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView=layoutInflater.inflate(R.layout.card,null);
TextView t=(TextView)findViewById(R.id.text1);
t.setText("hello "+String.valueOf(i));
layout.addView(addView);
}
}
}
`
code for cardlayout
<android.support.v7.widget.CardView
android:id="#+id/cardv"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="50dp"
android:layout_width="match_parent"
android:layout_margin="10dp"
card_view:cardCornerRadius="10dp">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="Hello"
android:layout_gravity="center"/>
</android.support.v7.widget.CardView>
For contentmain
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main"
android:orientation="vertical"
android:id="#+id/mainl">
</LinearLayout>
Because in loop cycle, you set content view each loop.
Please just remove this line setContentView(R.layout.card);
So your onCreate() method will look like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int i;
LinearLayout layout=(LinearLayout)findViewById(R.id.mainl);
for(i=1;i<=3;i++)
{
LayoutInflater layoutInflater=(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView=layoutInflater.inflate(R.layout.card,null);
TextView t=(TextView) addView.findViewById(R.id.text1);
t.setText("hello "+String.valueOf(i));
layout.addView(addView);
}
}
I want to go from fragment5 to fragment6 by clicking on imageview.
Here is my fragment5 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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"/>
<TextView
android:id="#+id/textfrag5label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="header" />
<TextView
android:id="#+id/textfrag5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="39dp"
android:textStyle="bold"
android:text="foo "
android:layout_below="#+id/toolbar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:id="#+id/forwardfrag5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textfrag5label"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="23dp"
android:layout_marginRight="23dp"
app:srcCompat="#drawable/ic_arrow_forward_black_24dp"
android:onClick="clickEvent"/>
<FrameLayout
android:id="#+id/show_fragment"
android:layout_width="match_parent"
android:layout_height="0dp">
</FrameLayout>
</RelativeLayout>
And this is my fragment5 class in which I have declared an Imageview called forward5. So, if I click on this Imageview I want the app to show fragment6's layout. But if I click on the Imageview, nothing happens. I don't know why. Can anyone help me please?
public class Fragment5 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragmentdescrip5, container, false);
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
ImageView forward5 = (ImageView) rootView.findViewById(R.id.forwardfrag5);
toolbar.setNavigationIcon(R.drawable.ic_keyboard_backspace_black_24dp);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().onBackPressed();
} // this is for going back, e.g from fragment5 to fragment 4, this works
});
forward5.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.show_fragment, new Fragment6(), "NewFragmentTag");
ft.commit();
}
});
return rootView;
}
}
that's because you set you container's height is 0dp,In fact you fragment6 had attached, but you can't see it.
you can modify the height of container
<FrameLayout
android:id="#+id/show_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"><!--modify 0dp to some you need-->
</FrameLayout>
I have a fragment with OnCreateView and onCreate. In onCreate() I download some icons and set the in ImageView. The first time the program runs it's ok. The icons are downloaded and set but as soon as I switch tabs and come back onCreateView() is called and resets the interface to exactly how is described in the xml file associated.
I was wondering if its possible to stop this thing to happen. Moving the code from onCreate to onCreateView is not what I want since it just keeps downloading the icons again and again.
Fragment
public class LiveGameTab1Fragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.live_game_tab1, container, false);
return V;
}
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
//download an icon from the internet
Bitmap bitmap = BitmapFactory.decodeByteArray(icon.getIcon(), 0, icon.getIcon().length);
Drawable iconDrawable = new BitmapDrawable(getResources(), bitmap);
imgButton.setImageDrawable(iconDrawable);
}
}
FragmentActivity
public class LiveGameStats extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_game_stats);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab1Fragment()).commit();
getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent2, new LiveGameTab2Fragment()).commit();
getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent3, new LiveGameTab3Fragment()).commit();
}
FragmentTabHost mTabHost = (FragmentTabHost) findViewById(R.id.liveGameTabhost);
mTabHost.setup(LiveGameStats.this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("You"),
LiveGameTab1Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Your Team"),
LiveGameTab2Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Your Enemies"),
LiveGameTab3Fragment.class, null);
}
}
live_game_tab1.xml
<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:orientation="vertical"
tools:context=".DeviceFragment">
<ImageButton
android:id="#+id/champBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_shape_champ"
/>
</LinearLayout>
activity_live_game_stats.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_live_game_stats"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_light"
tools:context="lucian.leagueassistant.activity.LiveGameStats">
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/liveGameTabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="#+id/liverealtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<FrameLayout
android:id="#+id/liverealtabcontent2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<FrameLayout
android:id="#+id/liverealtabcontent3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
UPDATE
Read the comments for the right answer
Sorry if the code looks out of line I'm answering from my phone
public class LiveGameTab1Fragment extends Fragment {
Bitmap bitmap;
Drawable iconDrawable;
public LiveGameTab1Fragment() {}
public LiveGameTab1Fragment(Context context
//add this if you need something from resources) {
//I don't know what you mean by icon.getIcon()
bitmap = BitmapFactory.decodeByteArray(icon.getIcon(), 0, icon.getIcon().length);
iconDrawable = new BitmapDrawable(context.getResources(), bitmap);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.live_game_tab1, container, false);
return V;
}
#Override //you need the onViewCreated function again I'm on my phone so I might be wrong about the variables inside so make sure to take the right code and the functions inside
public void onViewCreated(View v) {
super.onViewCreated(v);
//code moved to constructor so the image is initialize only ones
ImageButton imgButton = (ImageButton) v.findViewById(R.id.yourButtonId);
imgButton.setImageDrawable(iconDrawable);
}
}
I am practicing to multi-pane screen using fragment, but it's not working properly. I want two frame in single activity. One should be static and another will dynamic list.
main_biddingwindow.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="200dp"
android:weightSum="1"
android:id="#+id/ag">
<fragment
android:layout_width="163dp"
android:layout_height="match_parent"
android:name="driver.project_detail"
android:id="#+id/fragment"
android:layout_row="0"
android:layout_column="0" />
<fragment
android:layout_width="184dp"
android:layout_height="match_parent"
android:name="driver.bid_list"
android:id="#+id/fragment2"
android:layout_row="0"
android:layout_column="19" />
</GridLayout>`
main.class
public class biddingWindow extends Activity{
protected void OnCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.main_biddingwindow);
}
}
bidlist.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
bid_list.java
public class bid_list extends Fragment {
public View onCreateView(LayoutInflater inflater ,ViewGroup container ,Bundle s){
return inflater.inflate(R.layout.bidlist,container, false);
}
}
output screen
Just Create the objects of both the fragments in activity and you can access everything in fragment
public class bid_list extends Fragment {
public ListView listView;
public View onCreateView(LayoutInflater inflater ,ViewGroup container ,Bundle s){
View view = inflater.inflate(R.layout.bidlist,container, false);
listView = (ListView)view.findViewById(R.id.listView);
return view ;
}
}
public class bid_list extends Fragment {
public ListView listView;
public View onCreateView(LayoutInflater inflater ,ViewGroup container ,Bundle s){
View view = inflater.inflate(R.layout.bidlist,container, false);
listView = (ListView)view.findViewById(R.id.listView);
return view ;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ag">
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="driver.project_detail"
android:id="#+id/fragment"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:name="driver.bid_list"
android:id="#+id/fragment2"/>
</LinearLayout>
class Main extends Activity
{
protected void OnCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.main_biddingwindow);
FrameLayout frame1 = (FrameLayout)findViewById(R.id.fragment);
FrameLayout frame2 = (FrameLayout)findViewById(R.id.fragment);
Fragment1 fragment1 = new Fragment1();
Fragment2 fragment2 = new Fragment2();
frame1.removeAllView();
frame1.addView(fragment1)
frame2.removeAllView();
frame2.addView(fragment2)
}
}
The slidingPaneLayout does not slide!
pane.isSlideable() is false!
I inflate the layout in this way:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
slidingView = inflater.inflate(R.layout.available_nodes_fragment, container, false);
SlidingPaneLayout pane = (SlidingPaneLayout)slidingView.findViewById(R.id.sliding_pane_layout);
Fragment fragmentA = new ListNodesFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.child_fragmentA, fragmentA );
Fragment fragmentB = new ReserveNodesFragment();
transaction.add(R.id.child_fragmentB, fragmentB);
transaction.commit();
//pane.setPanelSlideListener(new SliderListener());
pane.openPane();
Log.i("Available Nodes","PANE IS SLIDABLE" + pane.isSlideable());
return slidingView;
}
The available_nodes_fragment layout that i inflate is :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sliding_pane_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="#+id/child_fragmentA"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="left" />
<FrameLayout
android:id="#+id/child_fragmentB"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="right" />
</LinearLayout>
</android.support.v4.widget.SlidingPaneLayout>
I do not know why the SlidingPaneLayout does not slide. I do not know if this problem has to do with the fact that i use nested fragments.
I have some progress.
I changed the available_nodes_fragment layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/reserve_nodes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sliding_pane_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/child_fragmentA"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_gravity="left"
android:layout_marginRight="25dp"
android:background="#A588DC" />
<FrameLayout
android:id="#+id/child_fragmentB"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:background="#B6D4D3"
android:layout_gravity="right" />
</android.support.v4.widget.SlidingPaneLayout>
<Button
android:id="#+id/btnReserveNodes"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:text="Reserve"/>
</RelativeLayout>
Now, the SlidePaneLayout layout slides. I do not know why. When i changed the fixed layout_width of the FrameLayouts to wrap_content, the SlidePaneLayout started to slide.
My problem now is that i want the FrameLayout with id= "child_fragmentA" with the ListView to get the 30% of the screen horizontally. I did not found a way to do it, since FrameLayout does not support weights.
I also provide the code of ListNodesFragment and ReserveNodesFragment.
ListFragments.java
public class ListNodesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View listNodesView = inflater.inflate(R.layout.nodes_categories_listview, container, false);
String[] items = { getResources().getString(R.string.orbit_nodes),
getResources().getString(R.string.grid_nodes),
getResources().getString(R.string.usrp_nodes),
getResources().getString(R.string.diskless_nodes),
getResources().getString(R.string.icarus_nodes) ,
getResources().getString(R.string.base_station)
};
ListView nodesListView = (ListView) listNodesView.findViewById(R.id.nodes_list);
ArrayAdapter<String> adapt = new ArrayAdapter<String>(getActivity(), R.layout.nodes_categories_listview_item, items);
nodesListView.setAdapter(adapt);
return listNodesView;
}
}
ReserveNodesFragment.java
public class ReserveNodesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View reserveNodesView = inflater.inflate(R.layout.reserve_nodes_fragment, container, false);
TextView txtView = (TextView) reserveNodesView.findViewById(R.id.textView1);
txtView.setText("fragment 2");
return reserveNodesView;
}
}