I've been trying to programatically inflate a linearlayout of buttons using a loop, but it doesn't show up in the ui at all. The array gets populated, however.
My button xml:
<Button xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/BlackKey">
</Button>
My style resource:
<style name="BlackKey">
<item name="android:layout_height">0dp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_weight">2</item>
<item name="android:background">#color/black</item>
<item name="android:layout_margin">3dp</item>
</style>
My initialization code:
container = (FrameLayout) findViewById(R.id.mainframe);
public Button [] BLACKKEYS = new Button[5];
LinearLayout blackkeys = new LinearLayout(this);
blackkeys.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
blackkeys.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < 8; i++){
Button newbutton = (Button) LayoutInflater.from(this).inflate(R.layout.blackkey, blackkeys, false);
blackkeys.addView(newbutton);
BLACKKEYS[i] = newbutton;
}
container.addView(blackkeys);
So I went ahead and tried the code you provided using Android Studio and I have also received a blank screen. Main point that I saw that caused this is in your BlackKey style, the layout_weight is provided, the layout_height is set to 0dp, and the layout_width is MATCH_PARENT, but the orientation is horizontal -- if ever the button shows up, this will make it only show a single button.
If how I understood on how you want this to show up (five buttons alongside each other in horizontal orientation, with equal widths) something like this:
Then you can just modify the BlackKey styles like this:
<style name="BlackKey">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_weight">2</item>
<item name="android:background">#color/black</item>
<item name="android:layout_margin">3dp</item>
</style>
And also a tip, if ever you're using Android Studio, you can check first if the Button is showing in the Design Tab to see if it shows up properly on the screen. If it doesn't show up there, then try to modify it. Hope this helps you. If you have further questions, or if this is similar to the answer you're looking for but is not complete, just post a comment and I'll try to help you out. :)
Related
I am pro grammatically creating buttons and specifying the height and width of each prior to adding it to a Grid View:
Button discountButton = new Button(this, null, 0, R.style.discount_select_button);
discountButton.setText("blah");
discountButton.setWidth(300);
discountButton.setHeight(300);
discountsGrid.addView(discountButton);
The button utilises this style, all items defined within the style get applied except for "layout_margin", why could this be?
<style name="discount_select_button" parent="#android:style/Widget.Button">
<item name="android:layout_margin">10dp</item>
<item name="android:textAlignment">center</item>
<item name="android:textColor">#color/white_light_background_focusable_color</item>
<item name="android:background">#drawable/discount_selection_button_shape</item>
<item name="android:gravity">fill_horizontal</item>
<item name="android:autoSizeMinTextSize">1sp</item>
<item name="android:autoSizeMaxTextSize">25sp</item>
<item name="android:autoSizeTextType">uniform</item>
<item name="android:autoSizeStepGranularity">1sp</item>
</style>
When I apply all the above properties programmatically, all applies successfully but not when defined within a style.
The minimum API level I am targeting is 26
Attributes beginning with layout_ are not actually part of the view, they are part of that view's LayoutParams object, and they define the behavior of the parent.
If you use that style definition directly in XML, the margins will work, but from Java they will not.
You can create a ViewGroup.MarginLayoutParams object and set the params.topMargin field to get your desired result.
I want to build a BottomSheet to display a view with button and edit text. If I click on the Edittext the Keyboard shows up but it hides the button which is below the edit text.
These pics should show my problems
1)BottomSheet in normal view-First Image
2)BottomSheet hides the button- Second Image
Here is my code for creating BottomSheet
addNewListBottomSheet = new BottomSheetDialog(this, R.style.BottomSheetDialogTheme);
View bottomSheetRootView = getLayoutInflater().inflate(R.layout.addnewlist_bottomsheet_layout, null);
addNewListBottomSheet.setContentView(bottomSheetRootView);
I also added a specific style
<style name="BottomSheet" parent="#style/Widget.Design.BottomSheet.Modal">
<item name="android:background">#drawable/bottomsheet_round_bg</item>
</style>
<style name="BaseBottomSheetDialog" parent="#style/Theme.Design.Light.BottomSheetDialog">
<item name="android:windowIsFloating">true</item>
<item name="bottomSheetStyle">#style/BottomSheet</item>
</style>
<style name="BottomSheetDialogTheme" parent="BaseBottomSheetDialog">
i also tried to change the behavior with
addNewListBottomSheet.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
but it doesn't work. Would appreciate an answer.
Put all the content of the bottomsheet in the scrollview and try.
<ScrollView layout_behavior = "#string/bottom_sheet....">
<your layout/>
<ScrollView>
I am displaying a snackbar with a fairly long text message and on the phone in portrait mode it looks fine.
But on the tablet it seems to only allow 1 line of text so it gets ellipsis-ed
Is there anyway I can get it to be 2 line in tablet landscape?
What's important and not stated in other answers is that you need to use Snackbar's view.
So:
Snackbar snackbar = Snackbar.make(rootView, R.string.message, Snackbar.LENGTH_LONG);
View snackbarView = snackbar.getView();
TextView snackTextView = (TextView) snackbarView.findViewById(com.google.android.material.R.id.snackbar_text);
snackTextView.setMaxLines(2);
snackbar.show();
Note: This may also help with devices with lower screen density.
P.S If you're not using AndroidX (which is recommended), and are still using the legacy support libraries, please use android.support.design.R.id.snackbar_text instead of com.google.android.material.R.id.snackbar_text for getting the Snackbar's internal TextView.
You can reference the SnackBar's TextView like this:
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
And then operate on the TextView itself by changing its max lines:
tv.setMaxLines(3)
Based on Snackbar source code you can see that on devices with width more or equal than 600 dp they change max lines to 1. So You can also just add:
<integer name="design_snackbar_text_max_lines">3</integer>
into your res\values\*.xml values
A more elaborate and thorough example with context would be this:
// Create on click listener
final OnClickListener positiveButtonClickListener = new OnClickListener()
{
#Override
public void onClick(View v)
{
// Do your action - e.g. call GooglePlay to update app
openGooglePlayAppUpdate();
}
};
// Create snack bar instance
Snackbar sBar = Snackbar.make(findViewById(R.id.some_view_to_bind), // You bind here e.g. layout, or form view
R.string.snack_bar_message,
Snackbar.LENGTH_INDEFINITE)
// Set text and action to the snack bar
.setAction(android.R.string.ok, positiveButtonClickListener);
// Now get text view of your snack bar ...
TextView snckBarTv = (TextView) offerUpdate.getView().findViewById(android.support.design.R.id.snackbar_text);
snckBarTv.setMaxLines(5); // ... and set max lines
sBar.show(); // and finally display snack bar !
For those using Kotlin you can make use of extensions
fun Snackbar.setMaxLines(lines: Int): Snackbar = apply {
view.findViewById<TextView>(com.google.android.material.R.id.snackbar_text).maxLines = lines
}
For me, the best solution was auto-size within 2 lines:
<style name="ThemeOverlay.MyApp.SnackBar.Text" parent="#style/Widget.MaterialComponents.Snackbar.TextView" >
<item name="autoSizeStepGranularity">0.5dp</item>
<item name="autoSizeMaxTextSize">16dp</item>
<item name="autoSizeMinTextSize">10dp</item>
<item name="autoSizeTextType">uniform</item>
</style>
You can also increase the max number of lines or change any other TextView attribute:
<style name="ThemeOverlay.MyApp.SnackBar.Text" parent="#style/Widget.MaterialComponents.Snackbar.TextView" >
<item name="android:maxLines">3</item>
</style>
And customize the button and paddings:
<style name="ThemeOverlay.MyApp.SnackBar.Button" parent="#style/Widget.MaterialComponents.Button.TextButton.Snackbar" >
<item name="android:paddingStart">16dp</item>
<item name="android:paddingEnd">16dp</item>
<item name="android:textSize">14dp</item>
<item name="android:textAllCaps">false</item>
<item name="android:letterSpacing">0.0</item>
</style>
<style name="ThemeOverlay.MyApp.SnackBar" parent="#style/Widget.MaterialComponents.Snackbar" >
<item name="android:paddingStart">8dp</item>
<item name="android:paddingEnd">8dp</item>
</style>
Your theme:
<style name="Theme.MyApp" parent="#style/Theme.MaterialComponents.DayNight.NoActionBar">
<item name="snackbarStyle">#style/ThemeOverlay.MyApp.SnackBar</item>
<item name="snackbarButtonStyle">#style/ThemeOverlay.MyApp.SnackBar.Button</item>
<item name="snackbarTextViewStyle">#style/ThemeOverlay.MyApp.SnackBar.Text</item>
</style>
I need to customize ScrollView so it will look like in the picture:
I already successfully customized the Thumb and the Track, but I don't know how to add Arrows like in the picture I provided.
Here is the style I'm using:
<!-- Scrollbar -->
<style name="scroll_view">
<item name="android:scrollbarAlwaysDrawVerticalTrack">true</item>
<item name="android:scrollbars">vertical</item>
<item name="android:fadeScrollbars">false</item>
<item name="android:scrollbarThumbVertical">#drawable/sb_thumb</item>
<item name="android:scrollbarTrackVertical">#drawable/sb_track_vertical_bg</item>
</style>
The Arrows should be a part of the ScrollView style to avoid extra spacing and also they need to be simple, non clickable and e.t.c.
If you want to add buttons for up and down, you can place two buttons above and below of Scrollview.And programatically scroll the scroll bar on button click.
For that purpose, you can use following code,
scrollview.scrollTo(5, 10);
I am simply trying to create a spinning progress dialog. No text, no borders, no background. Just a spinning notification that lightly is center on the screen on top of content.
I have seen two different Stack Overflow questions in creating this custom Dialog.
They both use this styling:
<style name="NewDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowTitleStyle">#null</item>
<item name="android:colorBackground">#ffffff</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:width">600dip</item>
<item name="android:height">100dip</item>
<item name="android:textColor">#FF0000</item>
</style>
But in the Java side, one extends Dialog and contains lots of custom content, the other one is simply this:
public class MyProgressDialog extends ProgressDialog {
public MyProgressDialog(Context context) {
super(context, R.style.NewDialog);
}
}
On the first (extends Dialog), I get a blank. No dialog. Nothing. On the code sample just above, I get a shrunken small dialog spinning inside a black box that is off centered.
Which method is more correct and can I have a sample on how to implement this?
Also, how can I make that dialog transparent/borderless?
The 'right way' to do this now would be to extend DialogFragment http://developer.android.com/reference/android/app/DialogFragment.html
If you are not using the compatibility library, you are not programming in the current Android world.
If you want to extend Dialog, then I have some example code here: https://github.com/tom-dignan/nifty/tree/master/src/com/tomdignan/nifty/dialogs In this code, I implemented a kind of 'progress dialog' by extending Dialog. I extended Dialog because I wanted full control and wanted my Dialog to be full-screen.
I would recommend reading the above DialogFragment doc and using that, though.
So really, there is no right or wrong way here. Extending any of the three classes will work, but the cleanest and most current would be the DialogFragment approach.
One way of doing it is to use a custom dialog, request feature No_Title and set the background drawable resource to transparent.
You can use the following code, I just wrote and tested it out on Android 4.0.3 ICS.
The xml layout in layout folder is, say dialog_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ProgressBar
android:id="#+id/dialogProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
The java code to display the dialog and make it transparent is as follows.
Dialog dialog = new Dialog (this);
dialog.requestWindowFeature (Window.FEATURE_NO_TITLE);
dialog.setContentView (R.layout.dialog_progress);
dialog.getWindow ().setBackgroundDrawableResource (android.R.color.transparent);
dialog.show ();
Something similar can be achieved very easily by using frame-by-frame animation on an ImageView instead of making custom progress dialog. For this you just need a sequence of images(can be extracted from any gif). And then you can apply frame-by-frame animation on it. It will do exactly what you want.
I have done something similar with the code below -:
// XML for frame by frame animation
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="#drawable/frame00"
android:duration="150"/>
<item
android:drawable="#drawable/frame01"
android:duration="150"/>
<item
android:drawable="#drawable/frame02"
android:duration="150"/>
<item
android:drawable="#drawable/frame03"
android:duration="150"/>
<item
android:drawable="#drawable/frame04"
android:duration="150"/>
<item
android:drawable="#drawable/frame05"
android:duration="150"/>
<item
android:drawable="#drawable/frame06"
android:duration="150"/>
Java code for starting animation :-
ImageView iv = (ImageView)findViewById(R.id.progressDialog);
iv.setBackgroundResource(R.anim.progress_dialog_icon_drawable_animation);
AnimationDrawable aniFrame = (AnimationDrawable) iv.getBackground();
aniFrame = (AnimationDrawable) iv.getBackground();
aniFrame.start();
Hope it helps !