Resize DialogFragment when keyboard is visible - java

(I am working with Xamarin C# but the code is identical to Java)
I created a DialogFragment which inflates a custom layout. The OnCreateView looks like this :
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.dialog_feedback, container);
int scale = Context.Resources.DisplayMetrics.HeightPixels;
int height = scale / 2;
Dialog.Window.SetLayout(ViewGroup.LayoutParams.MatchParent, height);
Dialog.Window.SetGravity(GravityFlags.Bottom | GravityFlags.CenterHorizontal);
return view;
}
And the OnCreate looks like this :
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetStyle(0, Resource.Style.dialog_no_border);
}
Here's the style :
<style name="dialog_no_border" parent="#android:style/Theme.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowTitleStyle">#null</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
The layout that the dialog is inflating contains a ConstraintLayout as the parent and has a AppCompatEditText inside it.
When i open the dialog and click on the EditText, the keyboard overlaps the Edittext. What i'm trying to do is, when the keyboard pops up, it should push up the DialogFragment and show the EditText above itself. How do i achieve that ?
I tried setting android:windowSoftInputMode="adjustNothing", android:windowSoftInputMode="adjustPan", android:windowSoftInputMode="adjustResize" in the activity tag inside the manifest, but with no luck!
So, how do i achieve this ?

Related

How to set round corners for a custom Dialog?

This is code for my custom Dialog:
public class DialogBrightness extends AppCompatDialogFragment {
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.layout_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
/*Build and create the dialog here*/
}
}
I followed instructions from other answers by first creating this drawable xml called dialog_bg:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#fff5ee"/>
<corners
android:radius="30dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
Then setting it as the background of the layout_dialog xml:
android:background="#drawable/dialog_bg"
But I can't do the final step which is to set the dialog's root view to transparent:
dialogBrightness.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
because there's no getWindow() function.
Also when they say root view do they mean the one that I set to null in the inflate function above?
You can use the MaterialAlertDialogBuilder included in the Material Components library:
import androidx.fragment.app.DialogFragment;
public class CustomDialog extends DialogFragment {
//...
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
return new MaterialAlertDialogBuilder(getActivity(),R.style.MaterialAlertDialog_rounded)
.setTitle("Title")
.setMessage("Message")
.setPositiveButton("OK", null)
.create();
}
}
with:
<style name="MaterialAlertDialog_rounded" parent="#style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="shapeAppearanceOverlay">#style/ShapeAppearanceOverlay.MyApp.Dialog.Rounded</item>
</style>
<style name="ShapeAppearanceOverlay.MyApp.Dialog.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">8dp</item>
</style>
I highly recommend #GabrieleMariotti's answer but I am writing here to see how you could have implemented in your original way.
there's no getWindow() function.
You could have used requireView() instead as :
dialogBrightness.requireView().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Also when they say root view do they mean the one that I set to null in the inflate function above?
Yes, it is. Generally, it is not recommended to pass null as a root parameter as root view is needed to calculate the layout parameters of the view that is currently being inflated by the LayoutInflater. Instead, you should use it like this :
View view = LayoutInflater.from(requireContext()).inflate(R.layout.layout_dialog, rootView, false);
Here, 3rd parameter is attachToRoot which is passed as false.

How to style a TabLayout

I'm trying to style text inTabLayout. I'd like to managed the styles such as font color, size and weight, change the text therein from being set to ALL CAPS
How can I go about it?
TabLayout tabLayout = new TabLayout(this);
tabLayout.addTab(tabLayout.newTab().setText("Upload"));
tabLayout.addTab(tabLayout.newTab().setText("Pics"));
tabLayout.addTab(tabLayout.newTab().setText("Vids"));
tabLayout.addTab(tabLayout.newTab().setText("Papers"));
I would like to attach the TabLayout to styles set in XML: something like:
Then I call it:
Button button = new Button(mContext);
button.setText("Add New BAG");
button.setTextAppearance(R.style.bttn_style);
button.setTextAppearance(R.style.custom_bttn_style);
res/values/styles.xml
<style name="custom_bttn_style" parent="Widget.AppCompat.Button">
<item name="android:textAllCaps">false</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#00600f</item>
<item name="android:textSize">18dp</item>
<item name="android:fontFamily">\'Zilla Slab\', serif</item>
</style>
Vielen dank im voraus.
You need to inflate a custom view, where you will add all styles you want
View view = LayoutInflater.from(this).inflate(R.layout.icon_tab, null);
AppCompatTextView tv = (AppCompatTextView) view.findViewById(R.id.tab);
ImageView imgTab1 = (ImageView) view.findViewById(R.id.imgTab);
imgTab1.setImageResource(R.drawable.icon_tab_search);
imgTab1.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
tv.setText(titles[0]);
tabLayout.getTabAt(0).setCustomView(view);

Android Action Bar options menu display issue

Okay, so i got a small problem that i really don't know how to fix this.
So i have an application where i use my own style for an Action Bar
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionModeOverlay">true</item>
<item name="actionBarStyle">#style/AppTheme.ActionBar</item>
<item name="android:textColorSecondary">#color/action_bar_bg</item>
</style>
<style name="AppTheme.ActionBar" parent="Widget.AppCompat.ActionBar">
<item name="height">30dp</item>
<item name="background">#color/action_bar_bg</item>
</style>
</resources>
The menu that is displayed
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/sortByStatus"
android:title="#string/sort_by_status"
app:showAsAction="never" />
<item android:id="#+id/sortByName"
android:title="#string/sort_by_name"
app:showAsAction="never" />
<item android:id="#+id/sortByCompany"
android:title="#string/sort_by_company"
app:showAsAction="never" />
</menu>
This works great for almost all places, except when i try to display my options menu. I have added the options menu and the handler for it, but when i click on the options menu then the default top icons (from the notification bar) also shows inside my options menu, and i don't want those to show there. It also starts showing the default bottom navigation buttons for Sony phones.
Enabling the menu from onResume
#Override
public void onResume() {
super.onResume();
AppCompatActivity appCompatActivity = (AppCompatActivity) getActivity();
if (appCompatActivity != null && appCompatActivity.getSupportActionBar() != null)
appCompatActivity.getSupportActionBar().setTitle(getResources().getString(R.string.presense_status));
setHasOptionsMenu(true);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
and then the menu onCreate
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_presense_status, menu);
}
and then the handler for selected item
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
getActivity().getFragmentManager().popBackStackImmediate();
break;
case R.id.sortByStatus:
if (null != mDataSource) {
// update data list
reloadData();
}
break;
case R.id.sortByName:
if (null != mDataSource) {
// update data list
reloadData();
}
break;
case R.id.sortByCompany:
if (null != mDataSource) {
// update data list
reloadData();
}
break;
default:
super.onOptionsItemSelected(item);
}
return true;
}
I really can't see where the problem is coming from or how to remove it so that this does not happen. This happens inside a Fragment that is started by my main Activity, meaning i only have one Activity that spawns different fragments for the different in-app tasks.
Any help or thought would be greatly appreciated.
I still don't really know why the notification bar was affected by what i did, but forcing the application to fullscreen in this view stops the problem, so i guess that's an acceptable workaround.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

Can't get my DialogFragment background to be transparent

I have created a custom DialogFragment like it is described in the developer guide.
Now what I am trying to do sounds simple enough, but I cannot get it to work.
I have defined: android:background="#android:color/transparent" in my layout xml which I am loading like this (in my onCreateDialog):
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.pausedialog, null);
setStyle(STYLE_NO_FRAME, R.style.CustomDialog);
As you can see I also tried to set a custom style in the DialogFragment which is defined like this:
<style name="CustomDialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:alwaysDrawnWithCache">false</item>
<item name="android:windowContentOverlay">#null</item>
</style>
And I also tried getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));
which leads to a null pointer exception.
I am using android.support.v4.app.DialogFragment. Can this be the cause?
Or am I doing something else wrong?
Try to set style and theme in onCreate method of your dialogFragment class implementation.
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
int style=DialogFragment.STYLE_NO_TITLE;
int theme=android.R.style.Theme_Translucent;
setStyle(style, theme);
}
Or
if you are using Dialog class then you can also set Style and theme on dialog instance.
This worked for me. I created a new style:
<style name="CustomDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
</style>
And then set this style in my DialogFragment's onCreate() method like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.CustomDialog);
}
This is the style I use to totally remove the Dialog`s background.-
<style name="Theme.Dialog" parent="#android:style/Theme.Translucent.NoTitleBar">
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
As for the Dialog creation, you're creating a DialogBuilder but then you manually inflate a view, I guess that's the problem. Try this instead.-
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.customTheme));
AlertDialog dialog = builder.create();
dialog.show();
EDIT
Another approach is extending AlertDialog.-
public class CustomDialog extends AlertDialog {
public DialogParent(Context context) {
super(context, R.style.CustomDialog);
setContentView(R.layout.pausedialog);
// More initialization stuff
}
}
And then 'manually' instantiate it.-
AlertDialog dialog = new CustomDialog(getActivity());
dialog.show();

change icon on tabhost android

i want to change icon when click on tabhost
below is the source code
private void setTabs() {
addTab("Home", TabHome.class, R.drawable.home);
addTab("Performers", TabPerformers.class, R.drawable.performers);
addTab("Tickets", TabTickets.class, R.drawable.tickets);
addTab("Info", TabInfo.class, R.drawable.info);
}
private void addTab(String labelId, Class<?> c, int drawableId) {
tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
i want to change tab's icon when user press tab for that i have used below tabhost event
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
if (tabId.equals("tabHome")) {
but could not success
there is also a tab_indicater.xml file but from that only background will be change not icon
below is the xml code
<?xml version="1.0" encoding="utf-8"?>
<!-- Non focused states -->
<item android:drawable="#drawable/tab_unselected" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="#drawable/tab_bg_selector" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
<!-- Focused states -->
<item android:drawable="#drawable/tab_bg_selector" android:state_focused="true" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="#drawable/tab_bg_selector" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>
<!-- Pressed -->
<item android:drawable="#drawable/tab_bg_selector" android:state_pressed="true" android:state_selected="true"/>
<item android:drawable="#drawable/tab_press" android:state_pressed="true"/>
below is the screen shot
when we click on any one tab it's icon should be change like here it must become orange..
can any body help me...
There's no straightforward way to change the TabSpec icon. You have to put your icons in the selector drawables.

Categories

Resources