what is the equivalent component in android like uiindicator in iphone - iphone

can anybody tell what is the equivalent component in android like uiindicator in iphone give example
Thanks

In activity java class:
protected void startProgress() {
ProgressBar pbSpinner = (ProgressBar)findViewById(R.id.activity_progress_bar);
pbSpinner.setVisibility(View.VISIBLE);
}
protected void stopProgress() {
ProgressBar pbSpinner = (ProgressBar)findViewById(R.id.activity_progress_bar);
pbSpinner.setVisibility(View.INVISIBLE);
}
And in layout xml file you can add progress bar:
<ProgressBar android:id="#+id/activity_progress_bar"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:layout_centerVertical="true">
</ProgressBar>

Related

when I used Intent to navigate to a new activity in kotlin, the new activity did not display anything

I'm new to app development. I was doing this project using android studio. the project utilize flutter as main display. However, there is a function that requires me to use kotlin. I used the method channel to set up a communication line between flutter and kotlin, then I try using Intent to open a new activity to display a page. but the new page did not display anything.
here are my code below.
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
call, result ->
if (call.method == "tokotlin")
{
.
.
.
val intent = Intent(this, calling_page::class.java)
startActivity(intent)
.
.
}
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
// the page 2
class calling_page : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_calling_page)
}
}
// the XML file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".calling_page">
<TextView
android:id="#+id/test_text"
android:layout_width="194dp"
android:layout_height="57dp"
android:text="test view 1"
android:textSize="34sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="201dp" />
<Button
android:id="#+id/test_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="487dp" />
</androidx.constraintlayout.widget.ConstraintLayout>`

App crashes on exit when using ListFragment called from a Sliding Tab's Fragment adapter

I have spend almost two day trying to figure out why this is happening but couldn't find a solution so far. So I am posting it here.
I have a pretty generic Sliding Tab which is created in Eclipse for newer Android APIs. Inside one of the sliding tabs I call a ListFragment. This ListFragment uses CursorLoader to load some data.
Now when the app exits, it gives: 05-28 11:34:00.327: E/AndroidRuntime(31994): java.lang.RuntimeException: Unable to destroy activity {com.example.myapp/com.example.myapp.main.HomeActivity}: java.lang.NullPointerException
I have tried using ChildFragmentManager and also the latest support package, but to no avail.
This is the only tab which calls another fragment, otherwise rest of the tabs which just call static XML content work just fine. App works fine if I remove this tab.
As I understand I need to destroy the CursorLoader or somehow detach this particular fragment before the app exits. CursorLoader seems to get destroyed, and the error is caught in HomeActivty. Maybe I should call onDestroy in HomeActivity, but really don't know how and where exactly.
Code of the calling HomeActivity is pretty standard:
ViewPager mViewPager;
private static final String DEBUG_TAG = "MY APP";
private static boolean logged_in;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
logged_in = sharedPrefs.getBoolean("logged_in", false);
Log.v(DEBUG_TAG, "logged_in: " + logged_in);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment;
switch(position) {
case 0:
fragment = new MySectionFragment();
break;
case 1:
...
The code which calls the ListFragment from the FragmentActivity is as follows:
public static class MySectionFragment extends Fragment {
public MySectionFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_my_section, container, false);
}
public void onDestroyView() {
super.onDestroyView();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.fragment_my_section));
if (fragment != null) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commitAllowingStateLoss();
}
}
}
Finally I found the answer and it was in the most unexpected place in the code. I still don't understand the mechanics, but I have confirmed throughly that it works.
I am using Android 4.0.3 with support library rev 12.
Turned out that my code was perfectly fine and I didn't even need onDestroyView(). Views get destroyed and regenerated fine. There was no need to deal with FragmentManager, SupportFragmentManager or ChildSupportFragmentManager. The default code which is generated in Eclipse when selecting swipable tabs is just fine.
The only place where I made a change, which fixed it all was the XML template. I moved the android:id from 'fragment' section to 'RelativeLayout' section.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/fragment_my_section"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="4dp"
android:background="#drawable/bg">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:cacheColorHint="#00000000"/>
<fragment
android:name="com.example.MyFragmentActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</fragment>
</RelativeLayout>
Previously the id was in the 'fragment' section. This was when there was no RelativeLayout and ListView defined in the XML file.
Needless to say I hate XMLs in this Android development world. Majority of the time it has always been an XML file which has wasted my time, without an exception. IDEs don't catch these XML issues well either. I wish Android development get rid or XMLs altogether..

How to set DialogFragment's width and height?

Let's say I specify the layout of my DialogFragment in an xml layout file named my_dialog_fragment.xml and I specify the layout_width and layout_height values of its root view to a fixed value (e.g. 100dp). I then inflate this layout in my DialogFragment's onCreateView(...) method as follows:
View view = inflater.inflate(R.layout.my_dialog_fragment, container, false);
Sadly, I find that when my DialogFragment appears, it does not respect the layout_width and layout_height values specified in its xml layout file and instead shrinks or expands depending on its content. Anyone know whether or how I can get my DialogFragment to respect the layout_width and layout_height values specified in its xml layout file? At the moment I'm having to specify the width and height of the Dialog again in my DialogFragment's onResume() method as follows:
getDialog().getWindow().setLayout(width, height);
The problem with this is that I have to remember to make any future changes to the width and height in two places.
If you convert directly from resources values:
int width = getResources().getDimensionPixelSize(R.dimen.popup_width);
int height = getResources().getDimensionPixelSize(R.dimen.popup_height);
getDialog().getWindow().setLayout(width, height);
Then specify match_parent in your layout for the dialog:
android:layout_width="match_parent"
android:layout_height="match_parent"
Now you only have to worry about one place (i.e. your DialogFragment.onResume method). It's not perfect but at least it works for having a RelativeLayout as the root of your dialog's layout file.
I ended up overriding Fragment.onResume() and grabbing the attributes from the underlying dialog, then setting width/height params there. I set the outermost layout height/width to match_parent. Note that this code seems to respect the margins I defined in the xml layout as well.
#Override
public void onResume() {
super.onResume();
ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = LayoutParams.MATCH_PARENT;
params.height = LayoutParams.MATCH_PARENT;
getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}
I got a fixed size DialogFragment defining the following in the XML main layout (LinearLayout in my case):
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="1000dp"
android:minHeight="450dp"
UPDATE 2021
For Kotlin users, I've crafted a couple of simple extension methods that will set the width of your DialogFragment to either a percentage of the screen width, or near full screen:
/**
* Call this method (in onActivityCreated or later) to set
* the width of the dialog to a percentage of the current
* screen width.
*/
fun DialogFragment.setWidthPercent(percentage: Int) {
val percent = percentage.toFloat() / 100
val dm = Resources.getSystem().displayMetrics
val rect = dm.run { Rect(0, 0, widthPixels, heightPixels) }
val percentWidth = rect.width() * percent
dialog?.window?.setLayout(percentWidth.toInt(), ViewGroup.LayoutParams.WRAP_CONTENT)
}
/**
* Call this method (in onActivityCreated or later)
* to make the dialog near-full screen.
*/
fun DialogFragment.setFullScreen() {
dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
}
Then in your DialogFragment in or after onActivityCreated:
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
setWidthPercent(85)
}
Consider the remainder of this answer for posterity.
Gotcha #13: DialogFragment Layouts
It's sort of mind numbing really.
When creating a DialogFragment, you can choose to override onCreateView (which passes a ViewGroup to attach your .xml layout to) or onCreateDialog, which does not.
You mustn't override both methods though, because you will very likely confuse Android as to when or if your dialog's layout was inflated! WTF?
The choice of whether to override OnCreateView or OnCreateDialog depends on how you intend to use the dialog.
If you intend to allow the DialogFragment to control the rendering of its own internal Dialog, then you are expected to override OnCreateView.
If you intend to manually control how the DialogFragment's Dialog will be rendered, you are expected to override OnCreateDialog.
This is possibly the worst thing in the world.
onCreateDialog Insanity
So, you're overriding onCreateDialog in your DialogFragment to create a customized instance of AlertDialog to display in a window. Cool. But remember, onCreateDialog receives no ViewGroup to attach your custom .xml layout to. No problem, you simply pass null to the inflate method.
Let the madness begin.
When you override onCreateDialog, Android COMPLETELY IGNORES several attributes of the root node of the .xml Layout you inflate. This includes, but probably isn't limited to:
background_color
layout_gravity
layout_width
layout_height
This is almost comical, as you are required to set the layout_width and layout_height of EVERY .xml Layout or Android Studio will slap you with a nice little red badge of shame.
Just the word DialogFragment makes me want to puke. I could write a novel filled with Android gotchas and snafus, but this one is one of the most insideous.
To return to sanity, first, we declare a style to restore JUST the background_color and layout_gravity we expect:
<style name="MyAlertDialog" parent="Theme.AppCompat.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:layout_gravity">center</item>
</style>
The style above inherits from the base theme for Dialogs (in the AppCompat theme in this example).
Next, we apply the style programmatically to put back the values Android just tossed aside and to restore the standard AlertDialog look and feel:
public class MyDialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View layout = getActivity().getLayoutInflater().inflate(R.layout.my_dialog_layout, null, false);
assert layout != null;
//build the alert dialog child of this fragment
AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
//restore the background_color and layout_gravity that Android strips
b.getContext().getTheme().applyStyle(R.style.MyAlertDialog, true);
b.setView(layout);
return b.create();
}
}
The code above will make your AlertDialog look like an AlertDialog again. Maybe this is good enough.
But wait, there's more!
If you're looking to set a SPECIFIC layout_width or layout_height for your AlertDialog when it's shown (very likely), then guess what, you ain't done yet!
The hilarity continues as you realize that if you attempt to set a specific layout_width or layout_height in your fancy new style, Android will completely ignore that, too!:
<style name="MyAlertDialog" parent="Theme.AppCompat.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:layout_gravity">center</item>
<!-- NOPE!!!!! --->
<item name="android:layout_width">200dp</item>
<!-- NOPE!!!!! --->
<item name="android:layout_height">200dp</item>
</style>
To set a SPECIFIC window width or height, you get to head on over to a whole 'nuther method and deal with LayoutParams:
#Override
public void onResume() {
super.onResume();
Window window = getDialog().getWindow();
if(window == null) return;
WindowManager.LayoutParams params = window.getAttributes();
params.width = 400;
params.height = 400;
window.setAttributes(params);
}
Many folks follow Android's bad example of casting WindowManager.LayoutParams up to the more general ViewGroup.LayoutParams, only to turn right around and cast ViewGroup.LayoutParams back down to WindowManager.LayoutParams a few lines later. Effective Java be damned, that unnecessary casting offers NOTHING other than making the code even harder to decipher.
Side note: There are some TWENTY repetitions of LayoutParams across the Android SDK - a perfect example of radically poor design.
In Summary
For DialogFragments that override onCreateDialog:
To restore the standard AlertDialog look and feel, create a style that sets background_color = transparent and layout_gravity = center and apply that style in onCreateDialog.
To set a specific layout_width and/or layout_height, do it programmatically in onResume with LayoutParams
To maintain sanity, try not to think about the Android SDK.
One way to control your DialogFragment's width and height is to make sure its dialog respects your view's width and height if their value is WRAP_CONTENT.
Using ThemeOverlay.AppCompat.Dialog
One simple way to achieve this is to make use of the ThemeOverlay.AppCompat.Dialog style that's included in Android Support Library.
DialogFragment with Dialog:
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.dialog_view, null);
Dialog dialog = new Dialog(getContext(), R.style.ThemeOverlay_AppCompat_Dialog);
dialog.setContentView(view);
return dialog;
}
DialogFragment with AlertDialog (caveat: minHeight="48dp"):
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.dialog_view, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext(), R.style.ThemeOverlay_AppCompat_Dialog);
builder.setView(view);
return builder.create();
}
You can also set ThemeOverlay.AppCompat.Dialog as the default theme when creating your dialogs, by adding it to your app's xml theme.
Be careful, as many dialogs do need the default minimum width to look good.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- For Android Dialog. -->
<item name="android:dialogTheme">#style/ThemeOverlay.AppCompat.Dialog</item>
<!-- For Android AlertDialog. -->
<item name="android:alertDialogTheme">#style/ThemeOverlay.AppCompat.Dialog</item>
<!-- For AppCompat AlertDialog. -->
<item name="alertDialogTheme">#style/ThemeOverlay.AppCompat.Dialog</item>
<!-- Other attributes. -->
</style>
DialogFragment with Dialog, making use of android:dialogTheme:
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.dialog_view, null);
Dialog dialog = new Dialog(getContext());
dialog.setContentView(view);
return dialog;
}
DialogFragment with AlertDialog, making use of android:alertDialogTheme or alertDialogTheme (caveat: minHeight="48dp"):
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.dialog_view, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setView(view);
return builder.create();
}
Bonus
On Older Android APIs, Dialogs seem to have some width issues, because of their title (even if you don't set one).
If you don't want to use ThemeOverlay.AppCompat.Dialog style and your Dialog doesn't need a title (or has a custom one), you might want to disable it:
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.dialog_view, null);
Dialog dialog = new Dialog(getContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(view);
return dialog;
}
Outdated answer, won't work in most cases
I was trying to make the dialog respect the width and height of my layout, without specifying a fixed size programmatically.
I figured that android:windowMinWidthMinor and android:windowMinWidthMajor were causing the problem. Even though they were not included in the theme of my Activity or Dialog, they were still being applied to the Activity theme, somehow.
I came up with three possible solutions.
Solution 1: create a custom dialog theme and use it when creating the dialog in the DialogFragment.
<style name="Theme.Material.Light.Dialog.NoMinWidth" parent="android:Theme.Material.Light.Dialog">
<item name="android:windowMinWidthMinor">0dip</item>
<item name="android:windowMinWidthMajor">0dip</item>
</style>
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new Dialog(getActivity(), R.style.Theme_Material_Light_Dialog_NoMinWidth);
}
Solution 2: create a custom theme to be used in a ContextThemeWrapper that will serve as Context for the dialog. Use this if you don't want to create a custom dialog theme (for instance, when you want to use the theme specified by android:dialogTheme).
<style name="Theme.Window.NoMinWidth" parent="">
<item name="android:windowMinWidthMinor">0dip</item>
<item name="android:windowMinWidthMajor">0dip</item>
</style>
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new Dialog(new ContextThemeWrapper(getActivity(), R.style.Theme_Window_NoMinWidth), getTheme());
}
Solution 3 (with an AlertDialog): enforce android:windowMinWidthMinor and android:windowMinWidthMajor into the ContextThemeWrapper created by the AlertDialog$Builder.
<style name="Theme.Window.NoMinWidth" parent="">
<item name="android:windowMinWidthMinor">0dip</item>
<item name="android:windowMinWidthMajor">0dip</item>
</style>
#Override
public final Dialog onCreateDialog(Bundle savedInstanceState) {
View view = new View(); // Inflate your view here.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view);
// Make sure the dialog width works as WRAP_CONTENT.
builder.getContext().getTheme().applyStyle(R.style.Theme_Window_NoMinWidth, true);
return builder.create();
}
The only thing that worked in my case was the solution pointed here: http://adilatwork.blogspot.mx/2012/11/android-dialogfragment-dialog-sizing.html
Snippet from Adil blog post:
#Override
public void onStart()
{
super.onStart();
// safety check
if (getDialog() == null)
return;
int dialogWidth = ... // specify a value here
int dialogHeight = ... // specify a value here
getDialog().getWindow().setLayout(dialogWidth, dialogHeight);
// ... other stuff you want to do in your onStart() method
}
When I need to make the DialogFragment a bit wider I'm setting minWidth:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="320dp"
... />
I don't see a compelling reason to override onResume or onStart to set the width and height of the Window within DialogFragment's Dialog -- these particular lifecycle methods can get called repeatedly and unnecessarily execute that resizing code more than once due to things like multi window switching, backgrounding then foregrounding the app, and so on. The consequences of that repetition are fairly trivial, but why settle for that?
Setting the width/height instead within an overridden onActivityCreated() method will be an improvement because this method realistically only gets called once per instance of your DialogFragment. For example:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Window window = getDialog().getWindow();
assert window != null;
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
window.setAttributes(layoutParams);
}
Above I just set the width to be match_parent irrespective of device orientation. If you want your landscape dialog to not be so wide, you can do a check of whether getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT beforehand.
The dimension in outermost layout doesn't work in dialog. You can add a layout where set dimension below the outermost.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="xxdp"
android:layout_height="xxdp"
android:orientation="vertical">
</LinearLayout>
In my case DialogFragment occupied full activity size like a Fragment. The DialogFragment was based on XML-layout, not AlertDialog. My mistake was adding the dialog fragment to FragmentManager as a usual fragment:
fragmentManager?.beginTransaction()?.run {
replace(R.id.container, MyDialogFragment.newInstance(), MyDialogFragment.TAG)
addToBackStack(MyDialogFragment.TAG)
}?.commitAllowingStateLoss()
Instead I need to show the dialog fragment:
val dialogFragment = MyDialogFragment.newInstance()
fragmentManager?.let { dialogFragment.show(it, MyDialogFragment.TAG) }
After some editing (I have ViewPager2 in the layout) the dialog fragment became too narrow:
I used the solution of N1hk:
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
dialog?.window?.attributes?.width = ViewGroup.LayoutParams.MATCH_PARENT
dialog?.window?.attributes?.height = ViewGroup.LayoutParams.MATCH_PARENT
}
Now it has defined width and height, not full activity size.
I want to say about onCreateView and onCreateDialog. If you have a dialog fragment based on layout, you can use any of these 2 methods.
If you use onCreateView, then you should use onActivityCreated to set width.
If you use onCreateDialog instead of onCreateView, you can set parameters there. onActivityCreated won't be needed.
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
super.onCreateDialog(savedInstanceState)
val view = activity?.layoutInflater?.inflate(R.layout.your_layout, null)
val dialogBuilder = MaterialAlertDialogBuilder(context!!).apply { // Or AlertDialog.Builder(context!!).apply
setView(view)
// setCancelable(false)
}
view.text_view.text = "Some text"
val dialog = dialogBuilder.create()
// You can access dialog.window here, if needed.
return dialog
}
I fixed it setting the root element layout parameters.
int width = activity.getResources().getDisplayMetrics().widthPixels;
int height = activity.getResources().getDisplayMetrics().heightPixels;
content.setLayoutParams(new LinearLayout.LayoutParams(width, height));
Here's a way to set DialogFragment width/height in xml. Just wrap your viewHierarchy in a Framelayout (any layout will work) with a transparent background.
A transparent background seems to be a special flag, because it automatically centers the frameLayout's child in the window when you do that. You will still get the full screen darkening behind your fragment, indicating your fragment is the active element.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#color/background_material_light">
.....
You can use percentage for width.
<style name="Theme.Holo.Dialog.MinWidth">
<item name="android:windowMinWidthMajor">70%</item>
I used Holo Theme for this example.
Here is kotlin version
override fun onResume() {
super.onResume()
val params:ViewGroup.LayoutParams = dialog.window.attributes
params.width = LinearLayout.LayoutParams.MATCH_PARENT
params.height = LinearLayout.LayoutParams.MATCH_PARENT
dialog.window.attributes = params as android.view.WindowManager.LayoutParams
}
You can below code to set layout width and height from java.
final AlertDialog alertDialog = alertDialogBuilder.create();
final WindowManager.LayoutParams WMLP = alertDialog.getWindow().getAttributes();
WMLP.gravity = Gravity.TOP;
WMLP.y = mActionBarHeight;
WMLP.x = getResources().getDimensionPixelSize(R.dimen.unknown_image_width);
alertDialog.getWindow().setAttributes(WMLP);
alertDialog.show();
Easy and solid:
#Override
public void onResume() {
// Sets the height and the width of the DialogFragment
int width = ConstraintLayout.LayoutParams.MATCH_PARENT;
int height = ConstraintLayout.LayoutParams.MATCH_PARENT;
getDialog().getWindow().setLayout(width, height);
super.onResume();
}
None of the other answers worked for me. It was solved for me only be creating a style where you can choose percentage of the screen that you want your dialog to take:
<style name="RelativeDialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="windowFixedWidthMajor">90%</item>
<item name="windowFixedWidthMinor">90%</item>
<item name="android:windowMinWidthMajor">90%</item>
<item name="android:windowMinWidthMinor">90%</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation</item>
</style>
Than just set this style to the dialog like:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NO_TITLE, R.style.RelativeDialog)
}
I create the dialog using AlertDialog.Builder so I used Rodrigo's answer inside a OnShowListener.
dialog.setOnShowListener(new OnShowListener() {
#Override
public void onShow(DialogInterface dialogInterface) {
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
dialog.getWindow().setLayout((int)(312 * outMetrics.density), (int)(436 * outMetrics.density));
}
});
Working on Android 6.0, ran into the same issue. AlertDialog would default to predefined width set in the theme regardless of the actual width set in the custom view's root Layout. I was able to get it to set properly adjusting the width of the loading_message TextView. Without investigating further, it seems that sizing the actual elements and having the root Layout wrap around them makes it work as expected. Below is an XML layout of a loading dialog which sets width of the the dialog correctly. Using the this library for the animation.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/custom_color"
android:padding="#dimen/custom_dimen">
<com.github.rahatarmanahmed.cpv.CircularProgressView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/progress_view"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerHorizontal="true"
app:cpv_color="#color/white"
app:cpv_animAutostart="true"
app:cpv_indeterminate="true" />
<TextView
android:id="#+id/loading_message"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="#+id/progress_view"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textSize="18dp"
android:layout_marginTop="#dimen/custom_dimen"
android:textColor="#color/white"
android:text="#string/custom_string"/>
</RelativeLayout>
Set the Parent layout of Custom dialogue layout to RelativeLayout, get common width and height automatically .
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
Add to your FragmentDialog:
public void onResume() {
Window window = getDialog().getWindow();
Point size = new Point();
Display display = window.getWindowManager().getDefaultDisplay();
display.getSize(size);
window.setLayout( (int)(size.x * 0.9), WindowManager.LayoutParams.WRAP_CONTENT );
window.setGravity( Gravity.CENTER );
super.onResume();
}
This will work perfectly.
#Override
public void onResume() {
super.onResume();
Window window = getDialog().getWindow();
if(window == null) return;
WindowManager.LayoutParams params = window.getAttributes();
params.width = 400;
params.height = 400;
window.setAttributes(params);
}
In my case it was caused by align_parentBottom="true" given to a view inside a RelativeLayout. Removed all the alignParentBottom's and changed all the layouts to vertical LinearLayouts and problem gone.
One of the earlier solutions almost worked. I tried something slightly different and it ended up working for me.
(Make sure you look at his solution)
This was his solution.. Click Here
It worked except for: builder.getContext().getTheme().applyStyle(R.style.Theme_Window_NoMinWidth, true);
I changed it to
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get layout inflater
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
// Set layout by setting view that is returned from inflating the XML layout
builder.setView(layoutInflater.inflate(R.layout.dialog_window_layout, null));
AlertDialog dialog = builder.create();
dialog.getContext().setTheme(R.style.Theme_Window_NoMinWidth);
The last line is whats different really.
This is the simplest solution
The best solution I have found is to override onCreateDialog() instead of onCreateView(). setContentView() will set the correct window dimensions before inflating. It removes the need to store/set a dimension, background color, style, etc in resource files and setting them manually.
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.fragment_dialog);
Button button = (Button) dialog.findViewById(R.id.dialog_button);
// ...
return dialog;
}
I took #rmirabella 's answer and updated it to also deal with height:
private fun DialogFragment.setSize(widthPercentage: Int, heightPercentage: Int) {
val newWidth = widthPercentage.toFloat() / 100
val newHeight = heightPercentage.toFloat() / 100
val dm = Resources.getSystem().displayMetrics
val rect = dm.run { Rect(0, 0, widthPixels, heightPixels) }
val percentWidth = rect.width() * newWidth
val percentHeight = rect.height() * newHeight
dialog?.window?.setLayout(percentWidth.toInt(), percentHeight.toInt())
}
Then in your DialogFragment in or after onStart():
override fun onStart() {
super.onStart()
setSize(
widthPercentage = 100,
heightPercentage = 80
)
}
#Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null)
{
dialog.getWindow().setLayout(-1, -2);
dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.dimAmount = 1.0f;
window.setAttributes(params);
window.setBackgroundDrawableResource(android.R.color.transparent);
}
}
Use RelativeLayout as a parent for DialogFragment
public void onResume() {
Window window = Objects.requireNonNull(getDialog()).getWindow();
Point size = new Point();
assert window != null;
Display display = window.getWindowManager().getDefaultDisplay();
display.getSize(size);
window.setLayout( (int)(size.x * 0.9), (int) (size.y * 0.75));
window.setGravity( Gravity.CENTER );
super.onResume();
}
To get a Dialog that covers almost the entire scree: First define a ScreenParameter class
public class ScreenParameters
{
public static int Width;
public static int Height;
public ScreenParameters()
{
LayoutParams l = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
Width= l.width;
Height = l.height;
}
}
Then you have to call the ScreenParamater before your getDialog.getWindow().setLayout() method
#Override
public void onResume()
{
super.onResume();
ScreenParameters s = new ScreenParameters();
getDialog().getWindow().setLayout(s.Width , s.Height);
}

Android Custom Keyboard Implementation

I am developing Android custom keyboard. I imported android.view.inputmethod.InputMethodSubtype in my code while doing this I am getting an error message like this imported one cannot be resolved. Is there any eclipse plugin I need to install, as per my knowledge Android version above 1.6 will support IMF.
The question is very old but I am answering it as it might help another user who sees this.
OP asked if there is any plugin for Eclipse to install to resolve the issue but now we have Android Studio.
For those who want to implement Android Custom Keyboard:
First, download Google sample project for Android Custom Keyboard to start with.
There are three important features to decide which one you want: 1) theme (custom layout), 2) subtype and 3) emoticons.
For themes/ layouts: Create layout files. See the example code below:
<com.domain.keyboard.android.LatinKeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keyboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/kb_bg_9"
android:keyBackground="#drawable/key_bg_fill_white"
android:keyPreviewLayout="#layout/key_preview_layout"
android:keyPreviewOffset="#dimen/keyPreviewOffset"
android:keyTextColor="#color/white"
android:popupLayout="#layout/keyboard_popup_layout" />
And use the following code in SoftKeyboard.java:
#Override
public View onCreateInputView() {
// Set custom theme to input view.
int themeLayout = sharedPreferences.getInt(THEME_KEY, R.layout.input_1);
mInputView = (LatinKeyboardView) getLayoutInflater().inflate(
themeLayout, null);
mInputView.setOnKeyboardActionListener(this);
// Close popup keyboard when screen is touched, if it's showing
mInputView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
mInputView.closing();
}
return false;
}
});
// Apply the selected keyboard to the input view.
setLatinKeyboard(getSelectedSubtype());
return mInputView;
}
For subtype: Create a copy of qwerty.xml and edit it to replace the keys. Create another instance of LatinKeyboard in SoftKeyboard.java and use if or switch logic.
private LatinKeyboard getSelectedSubtype() {
final InputMethodSubtype subtype = mInputMethodManager.getCurrentInputMethodSubtype();
String s = subtype.getLocale();
switch (s) {
case "ps_AF":
mActiveKeyboard = mPashtoKeyboard;
mCurKeyboard = mPashtoKeyboard;
break;
case "fa_AF":
mCurKeyboard = mFarsiKeyboard;
break;
default:
mCurKeyboard = mQwertyKeyboard;
}
return mCurKeyboard;
}
And edit methods.xml to add subtypes:
<input-method xmlns:android="http://schemas.android.com/apk/res/android"
android:settingsActivity="com.sunzala.afghankeyboard.android.ImePreferences"
android:supportsSwitchingToNextInputMethod="true">
<subtype
android:imeSubtypeLocale="en_US"
android:imeSubtypeMode="keyboard"
android:label="#string/label_subtype_generic" />
<subtype
android:imeSubtypeLocale="ps_AF"
android:imeSubtypeMode="keyboard"
android:label="#string/label_subtype_generic" />
<subtype
android:imeSubtypeLocale="fa_AF"
android:imeSubtypeMode="keyboard"
android:label="#string/label_subtype_generic" />
</input-method>
For emoticons: Find library and integrate it with a keyboard. The emoticons will display with a key press.
if (primaryCode == -10000) {
showEmoticons();
}
Where -10000 is keycode.

setOnclilcklistener for images of footer

i am adding a footer having 2 imageviews to my app using include tag.
I am able to view the footer and access the image view ids in source code but am not able to setOnclicklistener to these imageviews.
In main.xml i have
include layout="#layout/common_footer"
In common_footer.xml
?xml version="1.0" encoding="UTF-8"?
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#228b22"
ImageView android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:paddingRight="20px"
android:clickable="false"
ImageView android:id="#+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sample_0"
android:clickable="false"
LinearLayout
In source code
View inflatedView = View.inflate(this, R.layout.common_footer, null);
ImageView b = (ImageView)inflatedView.findViewById(R.id.image1);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT
,LayoutParams.WRAP_CONTENT);
b.setLayoutParams(lp);
b.setClickable(false);
b.setSelected(false);
b.setOnClickListener(mClickListener);
}
private OnClickListener mClickListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(TableImageLayout.this, TableImageLayout2.class);
startActivity(i);
}
};
But my Onclicklistener is not working.
Please forward your valuable suggestions
Thanks in advance:)
Hello i have solved the issue
Earlier i was using viewgroup as null.
View inflatedView = View.inflate(this, R.layout.common_footer, null);
the problem will be solved if we provide the view group as the parent layout.
As my layout is table layout tb.
View inflatedView = View.inflate(this, R.layout.common_footer, tb); works fine.