how to make a view stay on top alwayson all screens - java

I have got display over other apps permission in android now how can I make a view that always stays on top and responses to clicks or some gestures

I did this by getting the window.decorView.rootView and adding my inflated layout there.
// parent of activity,
val parent = (context as AppCompatActivity).window.decorView.rootView as? ViewGroup
// inflate view,
view = LayoutInflater.from(context).inflate(R.layout.dialog, parent, false)
// add the view to the parent container,
parent?.addView(dialogView, params)
You can add onClick listeners to the inflated view and its children.

Related

How to create a dynamic UI with Kotlin in Android Studio

create widget at runtime as texts and buttons, with kotlin language
For example, when clicking a button, a new text is created
The answer for XML-based UI:
To add a view programmatically on Android, you can use the addView method of a ViewGroup. Here are the basic steps:
Get a reference to the parent ViewGroup where you want to add the new view. You can do this by calling findViewById on the parent Activity or Fragment.
Create a new instance of the View you want to add to the parent ViewGroup. You can do this by calling the constructor of the View class that matches the type of View you want to create.
Set any necessary properties on the new View, such as its layout parameters or content.
Add the new View to the parent ViewGroup by calling the addView method on the parent ViewGroup and passing in the new View.
Here's an example of how to add a TextView to a LinearLayout programmatically:
val linearLayout = findViewById(R.id.linear_layout) // get a reference to the parent LinearLayout
val textView = TextView(this) // create a new TextView in Activity
// or TextView(requireContext) in Fragment
textView.text = "Hello, world!" // set the text of the TextView
linearLayout.addView(textView) // add the TextView to the LinearLayout
lets assume you have a ViewGroup such as LinearLayout and lets name it group
and you have a button.
Based on this assumption. I would do the following
mButton.setOnClickListener{
group.addView(TextView(context)
.also{
it.text = "your text"
})
}
of course you will have to work on positioning it too

Android custom expandable listview expands every nth item

I have custom listview and array adapter with ViewHolder. When click listview item, it expands new layout below. Problem is: unfortunately it is opened for every +9th item in listview.. For example: if item 0 is clicked; 0,9,18th elements opens their expand layouts. Any idea without looking code ?
I have no idea, but this sounds familair with an issue I had. I had a listview with TextView objects and multiple were selected and got typed in the same value.
I had a very, very dirty fix for that, in my custom adapter I'd always make a new View, no matter what:
#Override
public View getView (final int position, View convertView, final ViewGroup parent) {
//if (convertView == null) {
// Inflate the view from the converter
final LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
convertView = layoutInflater.inflate(converter.getLayout(), parent, false);
//}
// Populate the view from the converter
converter.populateInflatedView(convertView, getItem(position));
return convertView;
}
Source can be found here if you want to know what the converter is about.
On a side note, I have created a sort of interface type of thing for a TreeView, however this is in Activity form which uses a ScrollView. So this is not really a ListView type of deal, but might help you. The Tree part can be found on the Tree-link, with an implementation in the subject package.

ListView is empty/disapears after Orientation Change even if Adapter contains Items

I got a ListView with a custom Adapter.
In the Adapter a layout is inflated from xml.
All works fine, and I can see the items, until the Screen Orientation is changed.
I know that the Activity is recreated (or resumed) then, and the ListView is recreated too, as well as the Adapter.
But there are no items in the ListView now. The Adapter isn't empty, I use toasts to display the count of items in the Adapter.
I guess there is an inflating problem, because if I use the same Adapter (or an adapter with the same data) to a new ListView nothing is shown as well.
But the most crazy thing I don't understand is, that if I let my getView() method return a simple TextView, all works fine, even after orientation change.
I tried several things, like don't recycle a View so that it is inflated every time, or save the View to the matching Item (from getItem(position) from the Adapter).
I'm grateful for all hints :)
EDIT: so I was asked for some code.
Here is the getView() of my Adaptar
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View view = convertView;
final Event event = getItem(position);
if (view == null) {
view = inflater.inflate(R.layout.new_event_item_layout, parent,
false);
view.setTag(R.id.eventDate, view.findViewById(R.id.eventDate));
view.setTag(R.id.eventTime, view.findViewById(R.id.eventTime));
view.setTag(R.id.eventName, view.findViewById(R.id.eventName));
view.setTag(R.id.eventBemerkungen,
view.findViewById(R.id.eventBemerkungen));
view.setTag(R.id.eventIcon, view.findViewById(R.id.eventIcon));
}
((TextView) view.getTag(R.id.eventDate)).setText(event.getDate());
((TextView) view.getTag(R.id.eventTime)).setText(event.getName());
((TextView) view.getTag(R.id.eventName)).setText(event.getTime());
((TextView) view.getTag(R.id.eventBemerkungen)).setText(event
.getDescription());
SquaredImageView icon = (SquaredImageView) view.getTag(R.id.eventIcon);
Picasso.with(context).load(event.getUri())
.placeholder(R.drawable.ic_reload).into(icon);
view.setBackgroundColor(event.getBackgroundColor());
view.setVisibility(View.VISIBLE);
return view;
//return getDummyTextView();
}
public TextView getDummyTextView()
{
TextView tv=new TextView(context);
tv.setText("YOLO BIATCHSES");
tv.setTextColor(Color.WHITE);
return tv;
}
I have had the same exact problem although it wasn't because of orientation change and I found the solution by just setting listView.setAdapter(adapter) again after the recreation of the activity or whatever case you have. I suspect the listview is basically losing the pointer to the adapter.

Different fragment layout based on device android

I am working on an android application that makes use of fragments. I used the standard template for fragments that is generated in the create-wizard. Now, I want to use a different 'ItemListFragment' layout for when 'mTwoPane' is true(screen sizes above 600dp). The layout on single pane devices will have a title and a 2x3 icon layout, while the mTwoPane devices won't have a title and will have a 1x6 layout, so all the icons will be under each other.
In my ItemListActivity.java, there is checked or the item_detail_container is present. If so, mTwoPane is set to true.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
if (findViewById(R.id.item_detail_container) != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-large and
// res/values-sw600dp). If this view is present, then the
// activity should be in two-pane mode.
mTwoPane = true;
// In two-pane mode, list items should be given the
// 'activated' state when touched.
((ItemListFragment) getSupportFragmentManager()
.findFragmentById(R.id.item_list))
.setActivateOnItemClick(true);
}
}
My first thought was to check for mTwoPane in my ItemListFragment too, and pick the RootView based on this. But I couldn't get this to work. Here is what I did in my ItemListFragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view;
boolean mTwoPane;
if (getView().findViewById(R.id.item_detail_container) != null) {
mTwoPane = true;
}
else mTwoPane = false;
if(!mTwoPane){
view = inflater.inflate(R.layout.mobile_list, container, false);
}
else{
view = inflater.inflate(R.layout.tablet_list, container, false);
}
return view;
}
But this obviously wouldn't work, because the view needs to be instantiated before there could be checked or item_detail_container was present.
Then I started to think, should fragments be aware of the layout? Maybe I should have 2 list layouts, one for tablets and one for mobile. But then again,
setContentView(R.layout.activity_item_list);
Has to be called first to check if mTwoPane is true.
What would be the best approach to do this?
Android will pick the layout resource to use based on resource qualifiers, or buckets.
e.g.
layout-mdpi
layout-hdpi
layout-xlarge
layout-port
layout-sw600dp
These are all resource buckets, than can all contain a layout file named activity_item_list. Android will then pick the layout file to use based on the current devices attributes.
So the solution is to create a list_fragment layout file that exists in the layout folder, and the layout-sw600dp folder (or whatever you currently have). The layout in the layout-sw600dp folder can be adjusted for a tablet.
Have a read up on these articles for more info:
http://android-developers.blogspot.co.uk/2011/07/new-tools-for-managing-screen-sizes.html
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/guide/topics/resources/providing-resources.html

Set id and get id of inflated view?

I have to inflate the group of items in a listview and i need to find inflated view anywhere in the program .
May I know how to find the view.
How to set id and get id of inflated listview.
Thanks in Advance
In the Adapter of your list view
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.newlistrow, null);
//Access your inflated layout using vi
TextView sometextview= (TextView) vi.findViewById(R.id.textview); // sometextview
Typically you don't find views in a list view. The adapter behind the list view will pass you the view plus the underlying position of your data that you need to render via the getView method.
To answer your question of how to get or set id of inflated view you should call getId or setId on the inflated view.
http://developer.android.com/reference/android/view/View.html#setId(int)
http://developer.android.com/reference/android/view/View.html#getId()
View myView = LayoutInflater.from(activity).inflate(R.layout.list_item_punch_log, null);
myView.setId(android.R.id.button1);
int id = myView.getId();
However doing this and finding views in a list view is a very dangerous act as it will cause some really funny behaviors on the listview. Maybe you can tell us what you are trying to do and we can guide you from there.

Categories