I have a date picker dialog ,
when ever dialog opens it shows the white space as shown in image(in right side of datePicker) ,
i tried setting layout Params
DatePickerDialog dialog = new DatePickerDialog(_context, this,
_birthYear, _month,
_day);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.getDatePicker().setLayoutParams(params);
dialog.setCancelable(false);
dialog.getDatePicker().setSpinnersShown(false);
dialog.getDatePicker().setCalendarViewShown(true);
still it aint working
any suggestions??
Here is the best way to fit this is to use scale in your code.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<DatePicker
android:id="#+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="37dp"
android:scaleX="1.2"
android:scaleY="1.2"/>
</LinearLayout>
Try out this code I hope that will help you
Try to set the width to MATCH_PARENT over here.
ViewGroup.LayoutParams.WRAP_CONTENT,
TO:
ViewGroup.LayoutParams.MATCH_PARENT,
Updated ans:
There seems to be a size mismatch with the datepicker dialog and your
view.
Try setting the size of the dialog like this:
datepikerdialog.getWindow().setLayout(width,height)
Related
I have created a TextView dynamically and added it to a linear layout. The TextView is supposed to have an icon on its left. I have added the icon and wanted the TextView to be in the center.
The code:
TextView valueTV = new TextView(getContext());
valueTV.setText(richPost.getPostCreateDate());
valueTV.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher, 0, 0, 0);
valueTV.setGravity(Gravity.CENTER);
llInner.addView(valueTV);
The output:
I want the image to be right before the text and want to remove the huge gap in between. The text is in the proper place, I want the image to be centered beside the text. Where am I going wrong? What should I do?
First, make yoúr TextView wrap its content by adding this to your code:
valueTV.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Additionally, to get the icon and text centered in the LiniarLayout, which you can do by adding this to the LinearLayout:
yourLinearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
yourLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
This may be caused by TextView layout parameters. It seems to me that in your case width of TextView is match_parent, so the drawableLeft position is defined by left side of LinearLayout.
Here is simple example to feel the difference
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:drawableLeft="#drawable/ic_send_white_24dp"
android:drawablePadding="8dp"
android:text="some text"/>
</LinearLayout>
and try the same layout with match_parent width of TextView
I want to make an app with a login activity/layout similar to what Facebook app has. What I mean is when text field is focused soft keyboard pushes the entire view up but not squashing a logo. I have tried android:windowSoftInputMode="adjustPan/adjustResize" but it is not what I was trying to achieve.
I found this question on SO perhaps it will make things clearer, but it has no solution to the problem.
I have also tried various layouts types but it soft keyboard only pushes the focused < EditText > up. Please guide me.
UPDATE:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#DDDDDD">
<RelativeLayout
android:height="0dp"
android:layout_weight="1"
android:layout_height="0dp"
android:layout_width="fill_parent"
android:background="#ff0000">
<ImageView
android:layout_centerVertical="true"
android:layout_height="fill_parent"
android:layout_width="fill_parent"></ImageView>
</RelativeLayout>
<RelativeLayout
android:height="0dp"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="fill_parent"
android:background="#00ff00">
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#0000ff"
android:height="0dp" >
<Button
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Log in"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="4dp"
android:hint="password"
android:inputType="textPassword" >
</EditText>
<EditText
android:id="#+id/editText1"
android:hint="login"
android:padding="4dp"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" ></EditText>
</RelativeLayout>
</LinearLayout>
UPDATE working solution
I can't paste here the entire xml file, but the structure should be enough.
Based on Gabe Sechan's answer.
Layout{
Layout top weight 1
Layout mid weight 1
Layout bot weight 1
}
Child layouts have set to:
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" // should be changed accordingly to your layout design.
And here is a Java code for the activity(keyboard up/down):
View top, mid, bot;
final View activityRootView = findViewById(R.id.loginLayout);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView()
.getHeight() - activityRootView.getHeight();
if (heightDiff > 100) { //keyboard up
mid.setVisibility(View.INVISIBLE);
top.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, 0, 0f));
bot.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, 0, 1f));
} else {// keyboard down
// v.setVisibility(View.VISIBLE);
mid.setVisibility(View.VISIBLE);
top.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, 0, 2f));
bot.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, 0, 3f));
}
}
});
On keyboard up you need to change weights accourding to keyboard up design and on keyboard down change back to the default(layout that you've set via xml/java). I've tested the code on 2.3.x and up.
And don't forget to use android:inputType="textFilter" for the login&password EditText's to remove suggestions on input and save some pixels. In your manifest for the activity android:windowSoftInputMode="adjustResize|stateHidden". stateHidden is used so that keyboard won't be up when activity loads. Hope it helps. Good luck.
They're doing it with relative layouts, adjustResize, and android:layout_centerVertical. Basically, they have a linear layout for their main layout, with 3 equally weighted relative layouts inside of it. Each is set to 0dp height, so they take up equal thirds of the screen. The top RelativeLayout holds the logo, centered vertically. The middle holds the login fields and button, centered vertically one on top of the other. The bottom one holds the copyright text, aligned to bottom. The end result is that when the keyboard comes up, the 3 relative layouts get resized to take 1/3 of the new screen. Then their elements are centered in the new screen.
Remember you need the adjustResize window mode to get this, if you use pan it will just move up and the logo will scroll off center.
In Eclipse, go to File|New|Other and in the Wizard that follows, select Android Activity, then on the next page, select LoginActivity from the list of activities. This has the exact layout you're talking about, and you can use that as a framework. It uses a ScrollView to achieve the effect you're looking for.
I have got something like this:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/chooseBirthDateText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
style="#style/mediumText"
android:gravity="center_horizontal"
android:text="#string/choose_birth_date" />
</TableRow>
</TableLayout>
</ScrollView>
i would like to add some more rows, but only after some time(lets say 10 sec). I thought to make those rows invisible at first and then make them appear. But in that way my scrollView is not working as i want(i can scroll down even if nothing is below - since rows are invisible)
So i thought to make rows that i want to add in second file, load them in middle time and add them after tableRow1 in moment they have to appear. Unfortunately i have got no idea how to do that(i am talking about connecting 2 files, not creating second one in java) :/
There are two modes to hide a view:
INVISIBLE: This view is invisible, but it still takes up space for layout purposes.
GONE: This view is invisible, and it doesn't take any space for layout purposes.
http://developer.android.com/reference/android/view/View.html#setVisibility(int)
I think GONE will help you.
You should try like this.let me know it is helpful or not?
for(int i = 0; i < _attributeList.size();i++){
TableRow newRow = new TableRow(getApplicationContext());
newRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
newCheckBox = new CheckBox(getApplicationContext());
TextView feeTypeText = new TextView(getApplicationContext());
feeTypeText.setText(jsonObj.getString("feeType"));
feeTypeText.setTextColor(Color.BLACK);
feeTypeText.setTextSize(16f);
feeTypeText.setTypeface(tf);
TextView dueAmountText = new TextView(getApplicationContext());
dueAmountText.setText(jsonObj.getString("dueAmount"));
dueAmountText.setTextColor(Color.BLACK);
dueAmountText.setTextSize(16f);
dueAmountText.setTypeface(tf);
TextView dueDateText = new TextView(getApplicationContext());
dueDateText.setText(jsonObj.getString("dueDate"));
dueDateText.setTextColor(Color.BLACK);
dueDateText.setTextSize(16f);
dueDateText.setTypeface(tf);
newRow.addView(newCheckBox,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,0.8f)));
newRow.addView(feeTypeText,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,0.8f)));
newRow.addView(dueAmountText,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,1.0f)));
newRow.addView(dueDateText,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,1.0f)));
attributeTable.addView(newRow); }
I already tried all things I could find (on stackoverflow and the net) but I somehow cannot set my dialog to fullscreen. It has a scrollview with a textview in it, and when there is not much text in the textview the scrollview is not fullscreen. How can I force it to be fullscreen even when there is not much text visible ?
I create the dialog like this:
final TextView box;
final Dialog info = new Dialog(cx);
final ScrollView scroll;
info.setContentView(R.layout.dialoginfo);
info.setTitle("Info");
info.setCancelable(true);
info.getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN, LayoutParams.FLAG_FULLSCREEN);
scroll = ((ScrollView) info.findViewById(R.id.scrollviewinfo));
scroll.setPersistentDrawingCache(ScrollView.PERSISTENT_NO_CACHE);
scroll.setScrollbarFadingEnabled(false);
box = (TextView) info.findViewById(R.id.infotext);
box.setText(text);
info.show();
This is the xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/infolayout"
>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/ImageView01"
android:layout_weight="1.0"
android:id="#+id/scrollviewinfo">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/infolayout2">
<TextView android:layout_margin="5dip" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="#+id/infotext"
android:textSize="8sp"
android:text=""/>
</LinearLayout>
</ScrollView>
</LinearLayout>
It looks like your ScrollView has its height set to wrap_content, I'd first try replacing it with fill_parent.
Here are some other resources that may help you:
How can I get a Dialog style activity window to fill the screen?
I've never used the setFlags() method, so this may give you the same results. Basically try replacing
info.getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN, LayoutParams.FLAG_FULLSCREEN);
with
info.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
Alternatively, if you wanted more exact control over the height, you can create an instance of layout params, as the first answer here describes:
How to make an alert dialog fill 90% of screen size?
You can also use this code to get the screen size, if you wanted to modify it to be slightly smaller than full screen.
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
There are quite a few other ways to get the current screen size, too, this is just one example. Good luck!
Have you tried setting layout_height of the ScrollView to "match_parent" (and you can set the LinearLayout to "match_parent" too, though I don't think it's necessary)?. I'm guessing it shrinks when there isn't much text because it's set to "wrap_content". Try this:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#+id/ImageView01"
android:layout_weight="1.0"
android:id="#+id/scrollviewinfo">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/infolayout2">
...
</LinearLayout>
</ScrollView>
Try wrapping your dialog custom layout into RelativeLayout instead of Linear Layout. That worked for me.
I'm stuck on a problem and I don't know, what causes it.
I have a very simple Layout like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="NOTE_THIS"
android:textColor="#FFFFFF"
android:textSize="22dp"
android:text="TestText"/>
</LinearLayout>
which is included inside another layout. If I change the gravity inside the xml I see same Result in Layout-Editor and on my phone. If I wanna apply the Gravity programatically like with myTextView.setGravity(Gravity.CENTER) it doesn't Change anything. And I cannot set LayoutGravity in Java on a TextView
I'll tried for debug purposes to include them three times each with another gravity which does work even. So I assume everything is alright with my Layout and there has to be a Bug or something else I miss.
Can someone give me a hint what I also can try, or what causes this problem?
Set your TextView's width as android:layout_width="fill_parent" then you can set it programmatically using myTextView.setGravity(Gravity.CENTER)
You need to set the gravity of the LayoutParams object instead of the View itself:
TextView tv = new TextView(getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
lp.gravity = Gravity.CENTER;
tv.setLayoutParams(lp);
When you use LinearLayout as parent, then layout_gravity comes in picture which align control but not content inside the control so,
Instead using android:layout_gravity use android:gravity.