Get name of current fragment in activity. Android Studio - android-studio

I have an framelayout which loads 6 different fragment. Is there any way to find the name of the current fragment in framelayout from activity.
Fragment1
getFragmentManager().beginTransaction().replace(
R.id.main,Fragment.instantiate(LoadingScreen.this,
"com.myapp.fragments.fragment1",bundle)).commit();
Fragment2
getFragmentManager().beginTransaction().replace(
R.id.main,Fragment.instantiate(LoadingScreen.this,
"com.myapp.fragments.fragment2",bundle)).commit();
I need to know which fragment is live in the framelayout, from activity. Since two different fragments are loaded in the same layout.

If you want to know which fragment is loaded in the layout, you can do something like this:
Fragment f = getSupportFragmentManager().findFragmentById(R.id.main);
if(f instanceof Fragment1){
//do something
}
else if(f instanceof Fragment2){
//do something
}
If you want to know class name then use:
String name = f.getClass().getCanonicalName()

You can use findFragmentById() it will return the current fragment in the container.

Related

How do I add map markers to a separate class file?

In android studio I've created a map with hundreds of markers on it. I want to separate these into individual classes and put them in a separate package, so that there isn't one massive list of markers in my main code. Is there a way of doing this? I'm using Kotlin.
so what I think you are trying to say is.
There is an Activity let's say MainActivity and it has maps in it and has let's say some 200 markers on it.
and all are individually initialized and assigned and you want to club them all together so that you'll be able to use them just by searching for one.
if that's the case, what I would suggest is.
make a separate Data Class that stores Marker and other data related to it.
data class MarkerInfo(
//marker ID
val id:Int,
//marker Data
val markerData:Marker,
//other Data
var otherData:String
)
now coming to storing and accessing data.
class MainActivity(){
//at the top level, inside Activity
// This will create an empty list of Marker Info
var markerData = mutableListOf<MarkerInfo>()
//Now take any function, let's say x
private fun x(){
//Mark a Marker on Map and assign it to a variable.
val markerA : Marker = map.addMarker( MarkerOptions.position(somePosition))
//we assign a function with id and other relevant data
val x= markerData.size
//store data in list
markerData.add(MarkerInfo(x, markerA, "This is a very useful marker"))
}
}
//now to access that marker.
//let's say there is a function named y.
private fun y(){
//say we want to access the first marker
//there are two ways to do so.
//first method: you know some data which is already in there let's say we know id
for(i in markerData){
if(i.id == 1){
Log.d("TAG", i.toString())
}
}
//second method: directly
Log.d("TAG",markerData[0].toString())
}

How to set specific BottomNavigation Menu Fragment after redirecting from an Activity

In my app, I have many menus. Example A Fragment, B Fragment, C Fragment, & D Fragment. This all are the Bottom Navigation menu. So, my question is how do I get back to one of this particular Fragment of BottomNavigation.
Example: I have selected menu A, now Fragment A is live, there I have some object, I have seleted the Object and now I am redirected to the Object Activity. Now, here in Object Activity I have a back button, so when I click on this back button I want to go back to Fragment A not the default home menu of BottomNavigation, I want to be redirected to where this Object was displaying which is Fragment A.
So, how can I do that. Any solution for that.
I have tried by passing extra from Object Activity, and receiving on HomeActivity where the BottomNavigation is setup.
if(getIntent().getExtras.get("FUN").toString.equals(null){
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.nav_graph_fragment,new WineShopFragment());
fragmentTransaction.commitNow();
bottomNavigationView.setSelectedItemId(R.id.wineShopFragment);
In this way, it works but after running, it thus goes back to the Fragment A, however, when I select the Fragment B or Fragment c or Fragment D, the Content of Fragment A also gets display along with the other selected Fragment. If I select Fragment B after that the Fragment A and B will display together overlapping the display content.
So, can anyone tell me how can I do it successfully without the error mentioned above?
Please add addToBackStack() method in fragment
if(getIntent().getExtras.get("FUN").toString.equals(null){
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.nav_graph_fragment,new WineShopFragment());
fragmentTransactin.addToBackStack(null);
fragmentTransaction.commitNow();
bottomNavigationView.setSelectedItemId(R.id.wineShopFragment);
}
Add popBackStack() method in backpress click in object activity rather than calling intent of homePage
public void backPressClick(){
//this will pop the last added fragment which is fragmentA in your case
getSupportFragmentManager().beginTransaction().popBackStack();
}

Class extending AbstractDetailsDescriptionPresenter text gets cut off

I'm using a class extending AbstractDetailsDescriptionPresenter. The summary text is relatively long. For some reason the text gets cut off after a certain length. I could not figure out how to display the entire text without it being cut off.
I tried viewHolder.getBody().setLines(20); and other property changes but nothing seemed to have the desired effect.
This it the Presenter class I'm using:
public class MovieDetailPresenter extends AbstractDetailsDescriptionPresenter {
#Override
protected void onBindDescription(ViewHolder viewHolder, Object item) {
Video video = (Video) item;
if (video != null) {
viewHolder.getTitle().setText(video.title);
viewHolder.getSubtitle().setText(video.subtitle);
viewHolder.getBody().setText(video.summary);
}
}
}
How can I remove the text length limit/cutting off?
Here a picture to better illustrate what I mean. The text at the bottom right isn't displayed in its full length but gets cut off and adds three dots (...) at the end.
Thanks for any hints/help.
Finally found a solution: Making a custom “AbstractDetailsDescriptionPresenter” without the addPreDrawListener() method (which is causing the problem) and use it in the “DetailsDescriptionPresenter”.
body.setMaxLines(Integer.MAX_VALUE) should do the trick unless you are forcing a specific height somewhere in your LayoutParams. I assume you're setting height to wrap_content? You could try enabling Show Layout Bounds in the developer options to see if your changes have any effect.

Custom selector challenges

I have a custom screen with a multiple custom selectors, which change what they select based on dropdown lists.
The solution I implemented is shown in a previous case:
Dynamically changing PXSelector in Acumatica (thanks).
My challenge is twofold:
1.) If the dropdown selection is "No Lookup", then I want the PXSelector Attribute to essentially be removed - leaving just a text entry. Not sure if this is even possible...
2.) If one of the selectors (let's say Projects) is selected, I'd like the selection of the following selector (let's say Tasks) to filter based on the Project selected.
Thanks much...
1) I think the only way to do this is to create your own attribute.
Something like that:
public class PXSelectorTextEditAttribute : PXSelectorAttribute
{
bool selectorMode;
public PXSelectorTextEditAttribute(Type type, bool selectorOn):base(type)
{
selectorMode = selectorOn;
}
public override void FieldVerifying(PXCache sender, PXFieldVerifyingEventArgs e)
{
if(selectorMode)
base.FieldVerifying(sender, e);
}
public static void SwitchSelectorMode(PXSelectorTextEditAttribute attribute, bool onOff)
{
attribute.selectorMode = onOff;
}
}
You will be able to turn on and off the 'selector' part of the attribute. With the field verifying turned off you will be able to put any value to the field just like in simple TextEdit field. However, the lookup button in the right end of the field still will be visible. I have no idea how to hide it.
2) This behavior can be implemented easily. You will need something like that(example based on cashaccount):
[PXSelector(typeof(Search<CABankTran.tranID, Where<CABankTran.cashAccountID, Equal<Current<Filter.cashAccountID>>>>))]
If you want to see all records when the cashaccount is not defined then you just modify the where clause by adding Or<Current<Filter.cashAccountID>, isNull>
Also don't forget to add AutoRefresh="true" to the PXSelector in the aspx. Without it your selector will keep the list of the records untill you press refresh inside of it.

How can I read a string directly into a variable? Java, slick

I want to program some kind of game where the player has to name loacations shown on a map. I am using the slick library.
My problem is that I need some way to get the keyboard input from the player. I tried it with InputDialog from JOptionPane but I do not really like it. I would rather have the string appear on some part of the screen. But I do not have any idea how I can read from the keyboard directly into a variable that should be drawn on the screen. I thought that it would be possible to use streams but if I try to get some examples, they are always about reading from files and I do not know how to use that for reading from keyboard.
String answer;
public void render(GameContainer gameContainer, StateBasedGame sbGame, Graphics g){
g.drawString(answer, 50, 50);
}
public void update(GameContainer gameContainer, StateBasedGame sbGame, int delta){
//user types something which I now call "inputFromUser"
//it does not appear anywhere before the string is drawn on the screen.
answer = inputFromUser;
}
Something like a Scanner does not work for me because the user has to type that into the console, and I want him to type it "directly into the game" like it works with a textfield. (But I do not want to use a textfield.)
I have one way to accomplish that but it's fairly resource intensive. First create a global variable to keep track of the current letters. now create a new method that runs through all of the keys to check if they are down
private String totalString;
private void handelUserInput(Input input){
if(input.isKeyDown(Input.KEY_BACK)){
totalString = totalString.substring(0, totalString.length() - 1);
}
if(input.isKeyDown(Input.KEY_A)){
totalString += "a";
}
if(input.isKeyDown(Input.KEY_B)){
totalString += "b";
}
...etc
}
next create an input handler in the update loop to pass into the handle input method.
public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException {
Input input = gc.getInput();
handelUserInput(input);
}
and then print the string somewhere on your window in the render loop. by the way the Input class is built into slick.

Resources