I'm trying to programmatically generate some UI elements onto an android app with Java. I have a ScrollView created in the XML file, and I'm trying to create and add a bunch of UI elements to that ScrollView. All of the UI elements make up 1 bounding box that looks like this:
The bounding box is just a linear layout with an image background, and some text boxes and labels sitting on top of it. I want my code to be modular, so I'm making a class called BoundingBox which should create and add a bounding box to the app every time I instantiate the BoundingBox class.
Here's what I have so far for the BoundingBox class (it's not complete, all it adds so far is a textview with text on it).
public class BoundingBox {
//Instantiate the main app activity.
MainActivity theMainActivity = new MainActivity();
//Create Scrollview object and set it to the XML GUI scrollview
ScrollView sv = (ScrollView) theMainActivity.findViewById(R.id.theMainScrollView);
public BoundingBox(){
//Create linear layout that will sit in the scrollview.
LinearLayout mainLinearLayout = new LinearLayout(theMainActivity);
TextView exerciseName = new TextView(theMainActivity);
TextView repBox1 = new TextView(theMainActivity);
TextView repBox2 = new TextView(theMainActivity);
TextView repBox3 = new TextView(theMainActivity);
TextView repBox4 = new TextView(theMainActivity);
//Add the Linear Layout to the existing scroll view.
sv.addView(mainLinearLayout);
//Add text box to linear layout
mainLinearLayout.addView(exerciseName);
exerciseName.setText("This doesn't work.");
}
}
Here's how I instantiate it in my main Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
BoundingBox sc = new BoundingBox();
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
The code compiles successfully but no text shows up in my Android app. If I instantiated the class then it should have already ran the code in the BoundingBox constructor, which should have created a text view. I'm not sure what I'm doing wrong, and since the code compiles successfully logcat doesn't tell me anything. If I instantiate the bounding box class in the onCreate() function, the app crashes when starting it. If I instantiate it in the onCreateOptionsMenu function, it runs but it doesn't create a text view.
Your code is so messed up it isn't even close to working.
1)You should never create an activity via new. Ever. It isn't valid.
2)You create a LinearLayout programmatically. But you add it nowhere. If it isn't added to anywhere, where would you expect it to show?
3)Why would you ever want to create a view in options menu code? And once again, you're creating the box but not doing anything with the views it creates. Also, if you want to create a class that holds a bunch of views, it really ought to derive from ViewGroup.
4)Even if 1 was valid (it isn't), doign a findViewById there would return null, because onCreate wasn't called so no contentView was set. But that isn't valid anyway, so don't try to just call it. Instead you want to either pass in the parent view via constructor, or better yet make BoundingBox a ViewGroup subclass and add it to the parent directly.
Related
In my activity I try to call a method from a class but it's not getting the right values.
This is ActivityTwo:
int id = intent.getIntExtra("id", 0);
LayoutInflater inflater = getLayoutInflater();
View mainActivity = inflater.inflate(R.layout.activity_main, null);
LinearLayout eventsLayout = mainActivity.findViewById(R.id.eventsLayout);
Log.d("ACTIVITY_eventlayout", String.valueOf(eventsLayout)); // gives the layout perfectly
Event.RemoveSpecific(eventsLayout, id);
finish();
This is the class(Event) with the method:
public static void RemoveSpecific(LinearLayout layout, int id){
View event = layout.findViewById(id);
Log.d("INSIDE_removespecific", String.valueOf(event));// event is null
layout.removeView(event);
}
And in my MainActivity it's working fine:
LinearLayout eventsLayout = findViewById(R.id.eventsLayout);
View event = eventsLayout.findViewById(id);
//Log.d("MAIN_event", String.valueOf(event1)); // gives it perfectly
eventsLayout.removeView(event);
I also add this view in my MainActivity and use .setId(id) to give it the right id. So my question is, why is the View in my class method null, while I pass the right LinearLayout from my activityTwo?
Where does the id come from?
I have the Event class which contains an id, name, date, & description. This class also has a static ArrayList called eventsList. Whenever the user creates a new reminder I create a new Event using my class and giving it an id as Event.eventsList().size() (after adding it to my eventsList [so the first event id is always 1]), then I create a new View and pass the recently created Event details and use setId() to give it an id [1]. Then in this View I have a button which has an onClickListener which passes the given View id and the LinearLayout (where the View was added) to my Event.removeSpecific() method.
App flow:
The user clicks a button in MainActivity to create an event, then the button click then use startActivityForResult() with an intent to open a new Activity where the user puts in a name,description and date for the event, then I create an intent with putExtra() methods and then use setResult(RESULT_OK, resultintent) to send the data back to MainActivity. Now when the MainActivity gets the data back I use .getStringExtra() to create a new Event object with the data and add it to may Event.eventsList ArrayList, then I use the following:
LayoutInflater inflater = getLayoutInflater();
View event_Exmaple = inflater.inflate(R.layout.event_example, null);
and set the textView's with the data I got, and use event_example.setId(Event.eventsList.size()) to give it an id.
After that I add this to my LinearLayout (which I declared already in the MainActivity):
eventsLayout.addView(event_example)
Now I mentioned that the Event class has a date field. I use that to set up an alarm with AlarmManager. With the AlarmManager I send the current Event object's data through the intent and when the user gets the notification it opens up a new Activity which gets the given Event object data (with intent.putExtra()'s) and that's the part where the user clicks on a Button. I want to remove the given Event object's View from my LinearLayout in the MainActivity XML.
well since you didn't share where the id value come from (from where the intent gets its data/how you put the values in it/where the values come from), I assumed this solution :
whenever I use a 'switch' statement with View Ids it tells me a very important warning which is
Resource IDs will be non-final in Android Gradle Plugin version 5.0, avoid using them in switch case statements
that means that view Ids doesn't have a stable id in some cases that you shouldn't consider them to be final.
I guess in your case when you get an id to your event view and then inflate the layout view, the inflate procedure involve changing the 'int' id value of the event view so whenever you query the old id it returns null since the event view inside the inflated layout view now has a new 'int' id value.
I don't know why it works in main activity since you didn't share full code where you get the id from.
so I guess the only solution is that you should find a way to get a fresh copy of the id after you inflate the view you want to work on in the method.
Update
the problem is with this block of code
View mainActivity = inflater.inflate(R.layout.activity_main, null);
everytime you inflate a new layout view thinking that you have the one used by main activity back there but instead you're creating an entirely new one with new views and new Id's.
that would be why you could get the view in main activity but you can't get it from a newly inflated view.
Update
if you need to remove this item using the id and have a static array stored in the view custom class, call a getter on the static array and remove the item from it instead of trying to remove the view itself.
a better implementation of the whole situation is to store these events in a database using room instead of the static array, that way you could delete/add/edit any event anytime with just one line of code, if you're willing to do this you can start by reading the developers documentation of room.
I'm making a to-do list app and after user presses the button I create a new GridLayout(and all the data about time and name of the task inside of it) and add it into my RelativeLayout. How do I save those GridLayouts in UI so after the activity is destroyed and launched again those layouts are there.
After pressing the button I trigger the Create Activity method
public void CreateActivity(String name,int hours, int minutes,int i)
{
RelativeLayout.LayoutParams relparams= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
relparams.addRule(RelativeLayout.BELOW,i);
relparams.setMargins(0,50,0,100);
Glayouts.add(new GridLayout(this));
Glayouts.get(i+1).setBackgroundColor(Color.GRAY);
Glayouts.get(i+1).setMinimumWidth(relative.getWidth());
Glayouts.get(i+1).setId(i+1);
Glayouts.get(i+1).setPadding(10,0,0,0);
GridLayout.LayoutParams namee = new GridLayout.LayoutParams();
namee.columnSpec = GridLayout.spec(0);
namee.rowSpec = GridLayout.spec(0);
namee.setGravity(Gravity.LEFT);
final TextView Actname = new TextView(this);
Actname.setText(name);
GridLayout.LayoutParams checkbox = new GridLayout.LayoutParams();
checkbox.columnSpec = GridLayout.spec(1);
checkbox.rowSpec = GridLayout.spec(0);
checkbox.setGravity(Gravity.RIGHT);
CheckBox check = new CheckBox(this);
// ADDING TO LAYOUT
Glayouts.get(i+1).addView(Actname,namee);
Glayouts.get(i+1).addView(check,checkbox);
relative.addView(Glayouts.get(i+1),relparams);
Theoretically when you extends View, then you can also override onSaveInstanceState and onRestoreInstanceState methods, where you must provide your own SavedState class that typically extends BaseSavedState. You can find info on that here
In your case, your layout is dynamic, therefore this doesn't really work. To tell you the truth, your layout probably shouldn't be constructed this way, you should be rendering the grid using a RecyclerView based on a "model" that describes this layout, render the items of the grid via the RecyclerView.Adapter, and you should persist either the "model", or the data you use to construct this model along with the user-inputted state so that you can re-construct the model that will be rendered via your RecyclerView.
You can read more about RecyclerView here.
You can read more about data persistence here.
You can read about using onSaveInstanceState to save data in Activities/Fragments across config change and process death (but not finishing then restarting the app) here.
You can’t. The best way to save state is to use some persistence mechanism, for example database (I’d recommend Room as it is officially supported by Google).
After clicking a button, you should put all the needed information (name, hours, minutes) in the database and when Activity is created, you can read all persisted data and - basing on it - create all needed layouts again.
Another option is storing data in SharedPreferences - it is much easier to setup, so you can also start with this solution. Please note, I'm suggesting it as a first step in the world of persistency in Android, not as a preferred solution for storing data.
I'm attempting to implement accessibility on a few custom views for an Android app.
I've condensed what is done in the Google Authenticator app with no luck:
public class CardView extends RelativeLayout {
// ...
#Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
event.setClassName(this.getClass().getName());
event.setPackageName(this.getContext().getPackageName());
event.getText().add("Card Test");
return true;
}
}
All TalkBack reports back is "Double-tap to select" when it's inside a ListView or ViewPager.
Does ViewPager override accessibility events?
What do I need to do in order to have TalkBack say "Card Test" inside ViewPagers and ListViews like I expect it to?
For current versions of Android, you need to set the content description of the view.
myView.setContentDescription("Card Test");
ListView and associated classes expect you to use the onItemSelectedListener instead of assigning an onClickListener to each View (and rightfully so).
If incorporating alanv's suggestion, try to convince android system to read out the content description
by either
If(accessibilityModeIsEnabled())//custom method that checks context.getSystemService(Context.ACCESSIBILITY_SERVICE).isEnabled()
myView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
or AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED.
or requestFocus
Above should done when myView is visible. May be during onMesasure when width and height are both positive
If list view is still unable to do so, then try doing the above tricks on the first element of list view. Accessibility in Android varies devices to device and not one strategy fits all
I am creating a feature inside my Android app that will allow users to see their 6 last used apps in a gridview with only the application image. So far I have tried this:
//Load recent used apps
ActivityManager result = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
ArrayList<RecentTaskInfo> apps = (ArrayList<RecentTaskInfo>) result.getRecentTasks(10, ActivityManager.RECENT_WITH_EXCLUDED);
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,
android.R.layout.simple_list_item_1, apps);
recentappsGridView.setAdapter(adapter);
But this only shows a lot of text in each row and column. How can I fix this/make it happen? Please note that I am already using a ListView within the same activity with a method for its click events.
Create a subclass of ArrayAdapter. Override getView(). Set up your cells to be images, not TextView. Use resolveActivityInfo() on the Intent you get from baseIntent in the RecentTaskInfo to get an ActivityInfo object. Use icon on ActivityInfo to populate your cell.
I haven't tried that recipe, but it should get you closer.
On an app that I'm working on, I need a context menu to show up if a user performs a longClick on a tab, which would allow them to close the tab. I can't seem to find a way to add a listener to a tab though. I either need each tab to have its own listener or the listener needs to be able to tell which tab had the longClick performed on it, as it won't always be the active tab.
Any ideas?
I appreciate that an answer has been accepted but if you want to utilise the built-in ContextMenu capabilities rather than set onLongClickListeners on the TabWidget itself, you can do this as follows...
Example, my current TabActivity adds tabs in a for loop and to register each for context menu I do the following.
for (int tabNumber = 1; tabNumber < 8; tabNumber++) {
...
spec = tabHost.newTabSpec(tag).setIndicator(indicator).setContent(intent);
tabHost.addTab(spec);
View v = tabWidget.getChildAt(tabNumber - 1);
registerForContextMenu(v);
...
}
Then in my Activity I simply override onCreateContextMenu(...) and onContextItemSelected (MenuItem item)
#Override
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo) {
...
// The parameter v is the actual tab view and not the TabWidget
// this makes it easy to get the indicator text or its tag in order
// to easily identify which tab was long-clicked and build the menu
...
}
#Override
public boolean onContextItemSelected (MenuItem item) {
...
// Process selected item here
...
}
There's no need to set an OnLongClickListener on any views explicitly as that is done by the call to registerForContextMenu(...). Also, the ContextMenu creation and selection handling is all handled for you by the ContextMenu methods exposed by Activity.
Unless you need to handle all of this stuff yourself (for a custom context menu layout for example) it seems easier to just use what's buit-in to Activity.
A TabWidget is a View like any other; you should be able to register an OnLongClickListener with it via myTabWidget.setOnClickListener and use the View argument of OnLongClickListener.onLongClick(View v) to determine which tab was clicked.
When you use a TabSpec to register the indicator for each tab with your TabHost, if the resource you pass in has an associated ID, you should be able to use that ID to look up the tab itself. This may mean you could have to start using Views or layouts as your TabSpec.setContent or TabSpec.setIndicator arguments (if you aren't already) so you can look them up by ID later.