i am using a Dialog to show user some options. i have inflated a xml which content some button. in landscape mode the buttons are showing like this:
in portrait mode buttons are showing in a line. but as the screen cannot hold all the buttons, some buttons are not showing. i want the buttons in portrait mode like this:
that means responsive. but how can i do that? my xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/jinish" >
<Button
android:id="#+id/sound2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:background="#drawable/sound_xml"/>
<Button
android:id="#+id/review2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:background="#drawable/review_xml"/>
<Button
android:id="#+id/instraction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:background="#drawable/instraction_xml"/>
<Button
android:id="#+id/solve"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/solve_xml"
android:layout_marginRight="2dp"/>
<Button
android:id="#+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/reset_xml"
android:layout_marginRight="2dp"/>
</LinearLayout>
and my java code:
private LinearLayout options_layout;
private Dialog options_show;
options_layout = (LinearLayout) getLayoutInflater().inflate(
R.layout.puzzle_options, null);
options_show = new Dialog(this);
options_show.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
options_show.setContentView(options_layout);
options_show.setCancelable(true);
options_show.setCanceledOnTouchOutside(true);
options_show.getWindow().setBackgroundDrawable(new ColorDrawable(0));
options_show.show();
Use a GridView, and set its columns to a dimen: android:numColumns="#integer/num_columns"
Add to your dimensions declarations (values/dimens.xml for example): <integer name="num_columns">3</integer>
Add another dimensions file with the orientation set to landscape (values-land/dimens.xml for example): <integer name="num_columns">6</integer>
Now, depending on the device orientation, Android will pick the value 3 or 6 for the number of columns.
You will need to create a basic adapter to bind data to the GridView.
Related
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="32px"
android:layout_marginTop="32px"
android:layout_marginRight="32px"
android:background="#44C4C5"
android:drawableEnd="#drawable/ic_ON"
android:gravity="center"
android:text="ON"
android:textFontWeight="300"
android:textSize="32px"></Button>
If the Button is off, android drawable should change to android:drawableLeft= "#drawable/ic_Off" and text is OFF.
If the Button is On, android drawable should change to android:drawableEnd="#drawable/ic_ON" and text is ON.
Create a custom layout for this, where there is an ImageView to the left and right hand side of a TextView. You can then set the visibility of the left/ right ImageViews programmatically based on whether the button is in the ON or OFF state.
on_off_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#44C4C5">
<ImageView
android:id="#+id/offImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_OFF"/>
<TextView
android:id="#+id/onOffTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ON"
android:textFontWeight="300"
android:textSize="32sp"/>
<ImageView
android:id="#+id/onImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_ON"/>
</LinearLayout>
In your layout file where you want the button to be:
...
<include
android:id="#+id/onOffButton"
layout="#layout/on_off_button"/>
...
In your activity or fragment then you will need to get a reference to the button with findViewById<View>(R.id.onOffButton) and set up an on click listener to set the visibility of the offImageView or onImageView based on the state.
I have a layout with several image and toggle buttons. How can I make them rotate to match the screen orientation? I do not with to rotate the layout, just these elements. Example: The screen is in portrait orientation, the buttons are perfectly vertically aligned. The device is rotated and switches to landscape orientation and so the buttons rotate to adapt to the users point of view. How can that effect be achieved? How can a degree rotation animation be linked not only to the element it affects, but also be orientation sensor sensitive?
You have to set "android:gravity to center".
The following layout will hekp for you:
<?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:gravity="center"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cool Button" />
</LinearLayout>
There are other ways to do it.
Using relative layout
Using 2 layouts : 1 one for portrait and and 1 for landscape.
Example for using two layouts:
XML file for portrait mode (found in the res/layout folder)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/Apple"
android:text="Apple"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:padding="20dip"
android:layout_marginTop="20dip" />
<Button
android:id="#+id/Mango"
android:text="Mango"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:padding="20dip"
android:layout_marginTop="20dip" />
<Button
android:id="#+id/Banana"
android:text="Banana"
android:layout_width="300dip"
android:layout_height="wrap_content"
android:padding="20dip"
android:layout_marginTop="20dip" />
<Button
android:id="#+id/Grapes"
android:text="Grapes"
android:layout_width="300dip"
android:layout_height="wrap_content"
android:padding="20dip"
android:layout_marginTop="20dip" />
<Button
android:id="#+id/Kiwi"
android:text="Kiwi"
android:layout_width="300dip"
android:layout_height="wrap_content"
android:padding="20dip"
android:layout_marginTop="20dip" />
</LinearLayout>
We can see that five Button controls are vertically arranged in a LinearLayout container, one below the other. This vertical arrangement makes a few of the Button controls disappear when the screen is in Landscape mode.
To use the blank space on the right side of the screen in Landscape mode, we need to define another layout file, activity_screen_orientation_app.xml, created in the res/layout-land folder. The layout-land folder has to be created manually inside the res folder. Right-click on the res folder in the Package Explorer window and select the New, Folder option. A dialog box opens, asking for the name for the new folder. Assign the name layout-land to the new folder, and click the Finish button.
XM file for landscape mode (found in the res/layout-land folder)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/Apple"
android:text="Apple"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:padding="20dip"
android:layout_marginTop="20dip" />
<Button
android:id="#+id/Mango"
android:text="Mango"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:padding="20dip"
android:layout_marginTop="20dip"
android:layout_toRightOf="#id/Apple" />
<Button
android:id="#+id/Banana"
android:text="Banana"
android:layout_width="250dip"
android:layout_height="wrap_content"
android:padding="20dip"
android:layout_marginTop="20dip"
android:layout_below="#id/Apple" />
<Button
android:id="#+id/Grapes"
android:text="Grapes"
android:layout_width="250dip"
android:layout_height="wrap_content"
android:padding="20dip"
android:layout_marginTop="20dip"
android:layout_below="#id/Apple"
android:layout_toRightOf="#id/Banana" />
<Button
android:id="#+id/Kiwi"
android:text="Kiwi"
android:layout_width="250dip"
android:layout_height="wrap_content"
android:padding="20dip"
android:layout_marginTop="20dip"
android:layout_below="#id/Banana" />
</RelativeLayout>
We can also detect the screen orientation via Java code. Let’s modify the activity file ScreenOrientationAppActivity.java to display a toast message when the screen switches between landscape mode and portrait mode.
package com.androidunleashed.screenorientationapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class ScreenOrientationAppActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_orientation_app);
if(getResources().getDisplayMetrics().widthPixels>getResources().getDisplayMetrics().
heightPixels)
{
Toast.makeText(this,"Screen switched to Landscape mode",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this,"Screen switched to Portrait mode",Toast.LENGTH_SHORT).show();
}
}
}
I'm posting here in order to know if it was possible to make this design in Android :
If yes, could you help me how to do it (Just Guidelines).
Thank you
EDIT :
if I use only one Relative Layout I have this xml file :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/background_cross"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="F1\n" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="300dp"
android:layout_marginLeft="50dp"
android:text="Left\nbutton" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right\nbutton"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="300dp"
android:layout_marginRight="50dp"/>
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="380dp"
android:text="Bottom\nbutton" />
</RelativeLayout>
But declaring static dp is very bad and it isn't good to apply on all devices..
My xml code is shit ?
Do I have to get width and height screen of the device programmatically and set my buttons positions programmatically too ?
May be I could use this picture in Background and bind buttons on it ? (But how ?)
You can do this using a RelativeLayout and add the four buttons in it. Android supports adding custom fonts to your app and you will be able to set the button's font as well, you just need the font file. See here or here an example.
You will need the buttons background just as a white/red ring with transparent background.
You can place the buttons inside the RelativeLayout using margin properties (layout_marginLeft/Right/Top/Bottom) see here other properties.
The buttons text should have two lines (see android:lines xml property for a Button).
Good luck! I hope it helps ;).
I have a custom dialog which has 3 buttons and sometimes it has one button only. When I have 3 buttons, the LinearLayout fits those buttons well in itself. But when I have just one button, it gives the whole width available to a single button making that button look too big. I want that, if there's only one button, it should only take half the the complete width available or should wrap content (Button image.) See following images for reference-
Refer this XML file which is similar to your requirement. Just visibility to gone to not required button.
<?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="horizontal" android:weightSum="3" android:gravity="center_horizontal">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button"/>
</LinearLayout>
In order to have one button take up half the available screen width you need to
set android:weightSum="2" in the parent LinearLayout
set android:layout_weight="1" in the Button
I am creating a custom dialog in my app and it looks fine in the Layout Editor, but is not the right size on the device. Here's the layout for the dialog:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/dialog_border"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:src="#drawable/wifi" />
<TextView
android:layout_height="wrap_content"
android:id="#+id/heading"
android:layout_below="#+id/image"
android:layout_width="wrap_content"
android:layout_margin="10dp"
android:layout_alignLeft="#+id/cancel_button"
android:layout_alignRight="#+id/continue_button"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/lost_connection"
android:layout_centerHorizontal="true" />
<TextView
android:layout_height="wrap_content"
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_below="#+id/heading"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_centerHorizontal="true"
android:layout_alignLeft="#+id/cancel_button"
android:layout_alignRight="#+id/continue_button"
android:gravity="center"
android:layout_margin="10dp"
android:text="#string/try_again" />
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="#+id/cancel_button"
android:layout_below="#+id/description"
android:layout_centerHorizontal="true"
android:id="#+id/dismiss_button"
android:text="#string/dismiss" />
</RelativeLayout>
This is what it looks like in the Layout Editor:
But this is what it looks like on the device:
It has that weird bit of extra space at the top and right side and the button is compress vertically.
That blank area is the dialog title. It can be turned off when creating the DialogFragment as:
DialogFragment df = new MyDialogFragment();
df.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
df.show(ft, "dialog");
Where the 0 in set style lets the platform choose an appropriate style and "dialog" is whatever tag you'd like to set for your dialog fragment.
I think that extra weird space at the top is the place for the dialog title. As i see on the screenshots you used a dialog and inflated a custom view into that dialog ?
I guess worth a try to hide the title of the dialog with yourDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);