Replace FrameLayout content from a custom view - java

I'm currently working on creating a custom view which uses a xml layout file to render the view on the canvas. In this file I have a FrameLayout that is meant for dynamic content. I'm trying to change the content and in the custom view class with this method.
private void setFragment(Fragment fragment)
{
((ActionBarActivity) getContext()).getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_container, fragment)
.commit();
}
The problem with this is that I'm using the FragmentManager from the Activity this view is embedded in. Because of this the FragmentManager is referencing the Activity's layout file which does not contain my FrameLayout. I need this to be able to modify the content in the custom view's layout file so it can access the FrameLayout and change the content.
How can I go about accomplishing what I want? Any idea?

use callback pattern like here. But define an interface in your view class and implement it in activity.

Related

Set text in a Text view

I have this adapter which the text view instantiate.
But i want to change the text in the fragment.
what should i do?
Adapter which i used holder:
holder.btnRedeem = vi.findViewById(R.id.btnRedeem);
vi.setTag(holder);
Fragment:
(TextView)getView().findViewById(R.id.btnRedeem)).setText("redeem");
create an interface in your adapter and implement it in your fragment.
check my answer for this question:
https://stackoverflow.com/questions/58521136
check adapter and fragment (not activity)
if you want to handle in adapter
holder.btnRedeem.setText("redeem")
If you want to handle in Fragment, you need to get the position of your item & apply text to the TextView.

Change textview of another layout

I have a class "HomeActivity.class" and its layout "activity_home". I need to change the text of a TextView ("TV8") in another layout ("layout_profile"). I tried to use:
setContentView(R.layout.activity_home);
ConstraintLayout profile=(ConstraintLayout)findViewById(R.id.layout_profile);
TextView TV8 = (TextView) findViewById(R.id.TV8);
TV8.setText("MY TEXT IN TV8");
but my app crush when try to set text at line 4.
Maybe the problem can be the inflated layout: in activty_home layout i have a frame layout that inflate layout_profile where is my textview TV8:
activity_home:
layout_profile :
Debug application
HOW CAN I CHANGE THE TEXT OF A TEXTVIEW (TV8) IN A LAYOUT(layout_profile) FROM A CLASS(activity_home) THAT HAVE ANOTHER LAYOUT(activty_home)??
I think there is an issue in line 3.
Try this:
setContentView(R.layout.activity_home);
ConstraintLayout profile=(ConstraintLayout)findViewById(R.id.layout_profile);
TextView TV8 = (TextView) profile.findViewById(R.id.TV8);
TV8.setText("MY TEXT IN TV8");
You are loading activity_home.xml in the Activity and try to access the view TV8 which is not in activty_home.xml.
R.id.TV8 is part of layout_profile not part of activity_home.xml.
You can access those view which is in layout which is currently loaded in the screen.
Your activity is loading the activity_main layout so you can access the views that are present in the layout.
You are trying to access the view present in layout_profile which is not loaded and not visible. Until its loaded you can't access the view.
And if the view is not visible there is no point changing the text of that view.
Rather you can store the value in a flag and can be used to change the text when layout_profile is loaded.

How to set a temporary fragment to my "home screen" Android studio

I have started coding and I really like it, I have been on/off on diffrent project (to learn diffrent things) But now i got a problem, I "app"reciate all the help i can get.
I am using the navigation drawer and got three fragments installed. I can navigate through the navigation drawer and select a fragment and it loads on my screen. But when i start my app the screen is just white and the navigation drawer is on the left and i can pick a fragment. Is it possible to have a fragment on my "home screen" so when I start the app there is a fragment already placed on the screen BUT! when i chosse another fragment in the navigation drawer i would like to have my fragment who loaded at the beginning to disappear. Otherwise both fragments will show on eachother (Text on text). Maybe to have the temporary fragment to "finish" itself somehow. Please look at my imgur image to get my idea.Imgur image press here to view
Here is a generic solution on how to use the FragmentManager. It should give a good idea on how to display a Fragment. Besides that you could include a static Fragment in your layout. It's up to you how to approach it.
So the FragmentManager solution looks like this:
YourActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = AnyFragment.instantiate(this, AnyFragment.class.getName());
fragmentManager.beginTransaction().add(R.id.fragment_placeholder, fragment).addToBackStack(null).commit();
}
The target id is defined in your activity layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/fragment_placeholder"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></FrameLayout>
</LinearLayout>
Later if you want to replace the current Fragment you can use the remove and add method of the FragmentManager (the replace method is kinda buggy).
Fragment currentFragment = fragmentManager.findFragmentById(R.id.fragment_placeholder))
fragmentManager.beginTransaction().remove(currentFragment).add(R.id.fragment_placeholder, yourNewFragment).addToBackStack(null).commit();

ActionBar onClick listener- this is possible?

How to get ActionBar onClick listener? I mean- global listener for whole view, not only ActionBar child views
Try to use a custom view.
Create the layout you want for your action bar
Add the android:onClick attribute in the xml
android:onClick="handleClick"
inflate it somewhere in your code
use ActionBar.setCustomView() method
Implement the onClick method in your Java
public void handleClick(View view) {
// code after click here
}
That should work !

Adding xml widgets to a tab view with java

I have been following this guide and i have it running.
http://www.mkyong.com/android/android-tablayout-example/
I have added a bunch of new widgets to the tabs using java (image views and text fields) but its getting messy and i dont have as much control over the positioning and size as i would like to have since i cant use layouts
If i created a new layout using xml and i placed all my widgets perfectly using layouts etc is there a way i can populate a specific tab with that layout using java?
eg
rather than have code like this to add widgets
TextView textview = new TextView(this);
textview.setText("This is Android tab");
setContentView(textview);
i can just add a reference to an xml file and it will add all them widgets within the xml file
Use LayoutInflater http://developer.android.com/reference/android/view/LayoutInflater.html
LayoutInflater inflater = LayoutInflater.from(context); // if you're inside of activity then context = this
View viewInflatedFromXml = inflater.inflate(R.layout.your_layout, null);
Then you can do what you want with this view.

Categories