Android View Not Comming as LayOut I design? - android

I beginner of Android development , I created sample view using Android studio.but when i run not give proper out put.please , refer following image.
I create my Home page LayOut as Right Picture.But when I run Come View Like Left Hand Picture.I run different Emulators but out put is same. Please ,hope some expert help .
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.17">
<ImageButton
android:layout_width="136dp"
android:layout_height="wrap_content"
android:id="#+id/imageButtonService"
android:layout_gravity="center_horizontal"
android:layout_weight="0.17"
android:src="#drawable/services"
android:background="#null"
android:layout_marginBottom="36dp"
android:layout_above="#+id/imageButtontransaction"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Service"
android:id="#+id/textView"
android:layout_alignTop="#+id/tvProfile"
android:layout_alignRight="#+id/imageButtonService"
android:layout_alignEnd="#+id/imageButtonService"
android:layout_marginRight="24dp"
android:layout_marginEnd="24dp" />
<ImageButton
android:layout_width="136dp"
android:layout_height="wrap_content"
android:id="#+id/imageButtonProfile"
android:layout_gravity="center_horizontal"
android:layout_weight="0.17"
android:src="#drawable/profile"
android:background="#null"
android:layout_alignTop="#+id/imageButtonService"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="58dp"
android:layout_marginEnd="58dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Profile"
android:id="#+id/tvProfile"
android:layout_alignBottom="#+id/imageButtonService"
android:layout_alignRight="#+id/imageButtonProfile"
android:layout_alignEnd="#+id/imageButtonProfile"
android:layout_marginRight="26dp"
android:layout_marginEnd="26dp" />
<ImageButton
android:layout_width="136dp"
android:layout_height="wrap_content"
android:id="#+id/imageButtontransaction"
android:layout_gravity="center_horizontal"
android:layout_weight="0.17"
android:src="#drawable/transaction"
android:background="#null"
android:layout_alignTop="#+id/imageButtonwallet"
android:layout_alignRight="#+id/tvTransaction"
android:layout_alignEnd="#+id/tvTransaction" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Transaction"
android:id="#+id/tvTransaction"
android:layout_marginBottom="97dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/imageButtonService"
android:layout_alignEnd="#+id/imageButtonService" />
<ImageButton
android:layout_width="136dp"
android:layout_height="wrap_content"
android:id="#+id/imageButtonwallet"
android:layout_gravity="center_horizontal"
android:layout_weight="0.17"
android:src="#drawable/wallet"
android:background="#null"
android:layout_above="#+id/tvWallet"
android:layout_alignRight="#+id/imageButtonProfile" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Wallet"
android:id="#+id/tvWallet"
android:layout_alignTop="#+id/tvTransaction"
android:layout_alignLeft="#+id/tvProfile"
android:layout_alignStart="#+id/tvProfile" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Welcome SCPP Home"
android:id="#+id/tvWelcom"
android:layout_above="#+id/imageButtonService"
android:layout_alignRight="#+id/tvProfile"
android:layout_alignEnd="#+id/tvProfile"
android:layout_marginBottom="36dp" />
</RelativeLayout>
</LinearLayout>
My Activity Class.
public class Home extends Activity implements View.OnClickListener{
private final int SPLASH_DISPLAY_LENGTH = 2000;
private ImageButton profileimgButton;
private ImageButton transctionimgButton;
private ImageButton walletimgButton;
private ImageButton serviceimgButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
initUi();
}
private void initUi() {
profileimgButton = (ImageButton) findViewById(R.id.imageButtonProfile);
transctionimgButton=(ImageButton) findViewById(R.id.imageButtontransaction);
walletimgButton=(ImageButton) findViewById(R.id.imageButtonwallet);
serviceimgButton=(ImageButton) findViewById(R.id.imageButtonService);
profileimgButton.setOnClickListener(Home.this);
transctionimgButton.setOnClickListener(Home.this);
walletimgButton.setOnClickListener(Home.this);
serviceimgButton.setOnClickListener(Home.this);
}
}

Related

Cannot get Button location in Android App

I am trying to get the absolute location of my buttons after they have been drawn into view by the RelativeLayout. This is so that I can draw a pulsing glow animation at the location of the buttons. I have tried Button.getX(); and Button.getLocationOnScreen(); and both have returned an integer value of 0. I tried putting the buttons into a LinearLayout and was still unable to get coordinates for the button location on screen. I also tried running onPostCreate() to ensure the buttons were on screen before checking their location. I want to get their location on application boot, so theat I can create an animation to run underneath them. It seems that Button locations cannot be determined during onCreate. Anything I might be missing?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button playButton = findViewById(R.id.play);
System.out.println(playButton.getX());
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
play();
}
});
playButton.getX();
playButton.getY();
*OR*
int playLoc = int[2] //<--- create two dimensional array for x and y
playLoc = playButton.getLocationOnScreen();
and The XML Layout file:
<RelativeLayout 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:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<!--<com.example.macyg.androidmediaplayer.CustomView
android:layout_width="wrap_content"
android:layout_height="140dp"
android:layout_alignParentBottom="true"
android:background="#000000"/>-->
<android.support.v7.widget.AppCompatSeekBar
android:id="#+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:layout_marginBottom="20dp" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/play"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="#id/seekbar"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:background="#android:drawable/ic_media_play" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/forward"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="#id/seekbar"
android:layout_gravity="end"
android:layout_toEndOf="#id/play"
android:background="#android:drawable/ic_media_next" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/backward"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="#id/seekbar"
android:layout_toStartOf="#id/play"
android:background="#android:drawable/ic_media_previous"
android:shadowColor="#color/white"
android:shadowRadius="50" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/aButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_above="#+id/seekbar"
android:layout_alignStart="#id/backward"
android:layout_alignParentStart="true"
android:layout_marginTop="?attr/actionBarSize"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:fontFamily="sans-serif"
android:text="A" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/bButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_above="#+id/seekbar"
android:layout_toRightOf="#id/aButton"
android:layout_alignParentRight="false"
android:layout_marginBottom="5dp"
android:fontFamily="sans-serif"
android:text="B" />
<ImageView
android:id="#+id/album_art"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="?attr/actionBarSize"
android:layout_marginEnd="3dp"
android:background="#android:color/darker_gray"
android:visibility="invisible" />
<TextView
android:id="#+id/currTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginLeft="5dp"
android:layout_marginBottom="5dp"
android:fontFamily="sans-serif"
android:text="#string/default_time"
android:textColor="#android:color/white"
android:textSize="12sp" />
<TextView
android:id="#+id/trackLength"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:fontFamily="sans-serif"
android:text="#string/default_time"
android:textColor="#android:color/white"
android:textSize="12sp" />
<TextView
android:id="#+id/album_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/artist_name"
android:layout_alignParentRight="true"
android:fontFamily="sans-serif"
android:singleLine="true"
android:textColor="#ffffff"
android:textSize="18sp" />
<TextView
android:id="#+id/songName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/album_art"
android:layout_alignParentRight="true"
android:fontFamily="sans-serif"
android:singleLine="true"
android:textColor="#ffffff"
android:textSize="18sp" />
<TextView
android:id="#+id/artist_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/songName"
android:layout_alignParentRight="true"
android:layout_marginTop="0dp"
android:fontFamily="sans-serif"
android:singleLine="true"
android:textColor="#ffffff"
android:textSize="18sp" />
I found an answer to my problem. Rather than use the onPostCreate() method I use a post method (which is a function of the Button class) inside the onCreate() method to state that AFTER the button has been drawn into view by the relative layout so that I can get the locations of the buttons after they have been drawn in view. This is useful for when you run the application on devices with different screen sizes you will always know the coordinates of where the button was drawn.
How get Button coordinate?
I hope this saves people the amount of time I spent trying to solve this! and thank you to Vektor88 for the solution!
Also here is the code in the onCreate setup phase. This worked correctly for me:
playButton = findViewById(R.id.play);
playButton.post(new Runnable() {
#Override
public void run() {
playx = playButton.getX();
playy = playButton.getY();
System.out.println("play x = " + playx + " play y = " + playy);
}
});
NOTE
Why am I getting negative ratings on this post? I felt I gave very clear posts leading up to the answer and I don't feel I should have been answer banned.

How to set popup window style for responsive UI in android

In My android app, I used popup window.
There is one issue regarding popup window.
In bigger device (height or width) It will show clearly and automatically set margin left or right.
In smaller device like nexus one , Popup window stick with device not set margin left or right.
Or another issue is that In lollipop or marshmallow there is one button look like floating button. You can view in screen shot which I attached below.
That button look good in marshmallow or higher version.
But in kitkat there is only look like a simple arrow how to resolved it.
Here i specified My popup window source code or screenshots of kitkat device UI and Marshmallow UI.
please any one let me know how to resolved it. In advance, Thank you for your support.
raw_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/demo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorWhite"
android:orientation="horizontal"
android:padding="16dp">
<TextView
android:id="#+id/txtMainHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:text="#string/headertext"
android:textColor="#color/colorBlack"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:padding="10dp"
android:text="#string/popupsubtext"
android:textColor="#color/colorBlack" />
<View
android:id="#+id/viewMain"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="#+id/txtText"
android:background="#color/bg_border" />
<View
android:id="#+id/view"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_below="#+id/LayoutDetail"
android:layout_marginLeft="95dp"
android:layout_marginRight="10dp"
android:background="#color/bg_border" />
<LinearLayout
android:id="#+id/LayoutDetail1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/view"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/txtLastNameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:text="#string/puplastname"
android:textColor="#color/colorBlack"
android:textSize="15sp" />
<TextView
android:id="#+id/txtLastName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:focusableInTouchMode="true"
android:text="#string/txtLastnametext"
android:textColor="#color/button_text_dialog"
android:textSize="16sp" />
</LinearLayout>
<View
android:id="#+id/view1"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_below="#+id/LayoutDetail1"
android:layout_marginLeft="95dp"
android:layout_marginRight="10dp"
android:background="#color/bg_border" />
<LinearLayout
android:id="#+id/LayoutDetail2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/view1"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/txtEmailText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:text="#string/pupEmail"
android:textColor="#color/colorBlack"
android:textSize="15sp" />
<TextView
android:id="#+id/txtEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:text="#string/txtemailtext"
android:textColor="#color/button_text_dialog"
android:textSize="16sp" />
</LinearLayout>
<View
android:id="#+id/view2"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_below="#+id/LayoutDetail2"
android:layout_marginBottom="10dp"
android:layout_marginLeft="70dp"
android:layout_marginRight="10dp"
android:background="#color/bg_border" />
<ImageView
android:id="#+id/btnNext"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/LayoutDetail2"
android:layout_margin="#dimen/fab_margin"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="14dp"
android:background="#drawable/shape_oval"
android:elevation="2dp"
android:scaleType="center"
android:src="#drawable/ic_icon_right_1" />
<LinearLayout
android:id="#+id/LayoutDetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/txtText"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/txtFirstNameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:text="#string/pupfirstname"
android:textColor="#color/colorBlack"
android:textSize="15sp" />
<TextView
android:id="#+id/txtFirstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:focusableInTouchMode="true"
android:text="#string/txtFirstnametext"
android:textColor="#color/button_text_dialog"
android:textSize="16sp" />
</LinearLayout>
Popupwindow.java
private Context mContext;
private Activity mActivity;
private android.widget.PopupWindow mPopupWindow;
private RelativeLayout mRelativeLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_countrycode);
mContext = getApplicationContext();
// Get the activity
mActivity = CountrycodeActivity.this;
// Get the widgets reference from XML layout
mRelativeLayout = (RelativeLayout) findViewById(R.id.mRelativeLayout);
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
// Inflate the custom layout/view
View customView = inflater.inflate(R.layout.raw_layout, null);
mPopupWindow = new android.widget.PopupWindow(
customView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
// mPopupWindow.setContentView(findViewById(R.id.activity_view_pager));
mPopupWindow.setAnimationStyle(R.style.PopupAnimation);
if (Build.VERSION.SDK_INT >= 21) {
mPopupWindow.setElevation(24f);
}
new Handler().postDelayed(new Runnable() {
public void run() {
mPopupWindow.showAtLocation(mRelativeLayout, Gravity.CENTER, 0, 0);
}
}, 100L);
}
#Override
protected void onStop() {
super.onStop();
mPopupWindow.dismiss();
}
Screenshots of UI
In marshmallow as well as bigger device nexus 5.1
In nexus one as well as kitkat device or smaller
android:layout_width="fill_parent"
set RelativeLayout width property fill_parent

TextView not showing up after activity switch

My main activity(one xml file, and one java file) contains a help button, and I want to display the whole layout(including the paragraph I typed on a Textview) after button clicked. It was successfully launched, all imageview were displayed but not the Textview.
Here's the content of the xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="#drawable/tbs_background"
tools:context="limitless.the_bat_signal.aboutPage">
<TextView
tools:text="about the app"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tbs_text_about"
android:textStyle="normal|italic"
android:textAlignment="center"
android:textColor="#android:color/darker_gray"
android:fontFamily="sans-serif"
android:layout_below="#+id/tbs_title_text"
android:layout_centerHorizontal="true"
tools:ignore="UnusedAttribute" />
<TextView
tools:text="#string/about"
android:id="#+id/tbs_text_aboutDetails"
android:layout_width="300dp"
android:textColor="#android:color/darker_gray"
android:textSize="16sp"
android:layout_height="wrap_content"
android:layout_below="#+id/tbs_text_about"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp" />
<ImageView
android:layout_height="wrap_content"
app:srcCompat="#drawable/tbs_title_text"
android:id="#+id/tbs_title_text"
android:adjustViewBounds="true"
android:layout_width="250dp"
android:layout_below="#+id/tbs_icon"
android:layout_centerHorizontal="true"
tools:ignore="ContentDescription" />
<ImageView
app:srcCompat="#drawable/tbs_icon"
android:layout_marginTop="14dp"
android:id="#+id/tbs_icon"
android:layout_width="128dp"
android:layout_height="128dp"
tools:ignore="ContentDescription"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/tbs_home"
android:background="#drawable/tbs_home"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/tbs_title_text"
android:layout_toEndOf="#+id/tbs_title_text"
android:layout_marginBottom="10dp" />
</RelativeLayout>.
Then my Activity 2 named aboutPage calling out the XML file above:
public class aboutPage extends AppCompatActivity {
Button btn_help;
TextView tbs_title_text;
TextView tbs_text_about_details;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
}
}.
I'm new to android, please tell me what am I missing. Thanks!
The text attribute in your code is wrong, the namespace should be android:text="#string/about" instead of tools:text=="#string/about".
So the resulting textview will look like,
<TextView
android:text="about the app"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tbs_text_about"
android:textStyle="normal|italic"
android:textAlignment="center"
android:textColor="#android:color/darker_gray"
android:fontFamily="sans-serif"
android:layout_below="#+id/tbs_title_text"
android:layout_centerHorizontal="true"
tools:ignore="UnusedAttribute" />
Fix it and you will be good to go.
The problem is you're using tools:text this is only for design view and will not translate to code. Use android:text instead.

Android GridView with Both Horizontal and Vertical Scrolbars at the same time

I am new to Android and Have to Port a Windows Mobile Application to an Android Application.
Issue: I need GridView in Android similar to .net GridView which is able to scroll in both Horizontal and Vertical directions at the same time.
As there are many rows and many columns in my data. So, all the columns are not shown as screen width is small on mobile. Multiple rows are shown as vertical scroll is enabled. What i want is that this GridView is somehow scrollable in both directions at the same time. So that user could view data in tabular form.
I am using GridView in Android with the help of TableLayout and SimpleAdapter. Following is my code:
Main Activity Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- This Table Layout is header followed by the gridview -->
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp" >
<TableRow android:layout_width="wrap_content" >
<TextView
android:id="#+id/schemeTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Scheme"
android:textStyle="bold" />
<TextView
android:id="#+id/nameTitle"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Name"
android:textStyle="bold" />
<TextView
android:id="#+id/productTitle"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Product"
android:textStyle="bold" />
<TextView
android:id="#+id/channelTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Channel"
android:textStyle="bold" />
<TextView
android:id="#+id/typeTitle"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Type"
android:textStyle="bold" />
<TextView
android:id="#+id/customerSelectionTitle"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Customer Selection"
android:textStyle="bold" />
<TextView
android:id="#+id/brandTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Brand"
android:textStyle="bold" />
<TextView
android:id="#+id/wsTitle"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="W/S"
android:textStyle="bold" />
<TextView
android:id="#+id/startDateTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Start Date"
android:textStyle="bold" />
<TextView
android:id="#+id/endDateTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="End Date"
android:textStyle="bold" />
<TextView
android:id="#+id/codeTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Code"
android:textStyle="bold" />
<TextView
android:id="#+id/tsidTitle"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="TSID"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<GridView
android:id="#+id/schemeGridView"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:clickable="true"
android:numColumns="1"
android:columnWidth="900dp" >
</GridView>
</LinearLayout>
Grid Layout:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<TableRow android:layout_width="wrap_content" >
<TextView
android:id="#+id/scheme"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/name"
android:layout_width="200dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/product"
android:layout_width="200dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/channel"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/type"
android:layout_width="100dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/customerSelection"
android:layout_width="150dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/brand"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/ws"
android:layout_width="50dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/startDate"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/endDate"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/code"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tsid"
android:layout_width="50dp"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
Source Code:
public class Schemes extends BaseMainActivity {
private String obID = "";
private String obName = "";
ArrayList<HashMap<String, Object>> dtSchemes = null;
GridView schemeGridView = null;
Activity thisAct = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schemes);
ProgressDialog d = ProgressDialog.show(this, "Inventory",
"Loading Inventory...");
OrderBookingDAL dl = new OrderBookingDAL();
obID = dl.getConfigValue("OBID");
obName = dl.getConfigValue("OBName");
dtSchemes = dl.getHHTSchemes(obID, obName, dtSchemes);
schemeGridView = (GridView) findViewById(R.id.schemeGridView);
SimpleAdapter sa = new SimpleAdapter(this, dtSchemes,
R.layout.schemes_grid, new String[] { "Scheme", "Name",
"Product", "Channel", "Type", "CustomerSelection",
"Brand", "[W/S Approved]", "[Start Date]",
"[End Date]", "Code", "TSID" }, new int[] {
R.id.scheme, R.id.name, R.id.product, R.id.channel,
R.id.type, R.id.customerSelection, R.id.brand, R.id.ws,
R.id.startDate, R.id.endDate, R.id.code, R.id.tsid });
schemeGridView.setAdapter(sa);
schemeGridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.d("Grid Item Click", "Clicked pos:"+position);
TextView txt = (TextView) view.findViewById(R.id.tsid);
if (txt != null && txt.getText() != null) {
String tsid = txt.getText().toString();
Intent intent = new Intent(thisAct,SchemeDetails.class);
intent.putExtra(TSID_PARAM, tsid);
thisAct.startActivity(intent);
}
}
});
d.dismiss();
}
}
I changed some code and finally able get tabular view.
Solution: I added HorizontalScrollView and now my LinearLayout is its child. Also I added android:stretchColumns="*" in TableLayout. Along with this i had to chnage android:layout_width and android:layout_height of different controls in following ways.
Updated code is below:
Main Activity Layout:
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- This Table Layout is header followed by the gridview -->
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:stretchColumns="*" >
<TableRow android:layout_width="wrap_content" >
<TextView
android:id="#+id/schemeTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Scheme"
android:textStyle="bold" />
<TextView
android:id="#+id/nameTitle"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Name"
android:textStyle="bold" />
<TextView
android:id="#+id/productTitle"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Product"
android:textStyle="bold" />
<TextView
android:id="#+id/channelTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Channel"
android:textStyle="bold" />
<TextView
android:id="#+id/typeTitle"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Type"
android:textStyle="bold" />
<TextView
android:id="#+id/customerSelectionTitle"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Customer Selection"
android:textStyle="bold" />
<TextView
android:id="#+id/brandTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Brand"
android:textStyle="bold" />
<TextView
android:id="#+id/wsTitle"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="W/S"
android:textStyle="bold" />
<TextView
android:id="#+id/startDateTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Start Date"
android:textStyle="bold" />
<TextView
android:id="#+id/endDateTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="End Date"
android:textStyle="bold" />
<TextView
android:id="#+id/codeTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Code"
android:textStyle="bold" />
<TextView
android:id="#+id/tsidTitle"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="TSID"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<GridView
android:id="#+id/schemeGridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:numColumns="1" >
</GridView>
</LinearLayout>
</HorizontalScrollView>
Grid Layout:
Only following tag is chnaged:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*">
Source Code:
No change in Java code.
I got idea from: How can I make my layout scroll both horizontally and vertically? and How to put a very long table layout inside the horizontal Scroll view in the CORRECT way?
The simplest solution is
Use a horizontal scroll view to contain the GridView
Then dynamically re-size the GridView width based on the number of columns that you set for the GridView
Lastly, multiply number of columns with the width of each columns
GridView gridView = (GridView) findViewById(R.id.grid_view);
int numberOfColumns = 3;
int sizeOfWidthPerColumn = 20;
gridView.setNumColumns(numberOfColumns);
ViewGroup.LayoutParams layoutParams = gridView.getLayoutParams();
layoutParams.width = convertDpToPixels(numberOfColumns * sizeOfWidthPerColumn, this);
gridView.setLayoutParams(layoutParams);
Now you are able to scroll the GridView horizontally and vertically.
I am not sure, if it's possible to scroll in both direction at the same time, but here's a library that helps you to scroll a GridView horizontally.
https://github.com/jess-anders/two-way-gridview
Also you can have a look in here, How to make a 2-dimension image gallery with both horizontal and vertical scrolling?

Why is my relative layout filling the screen rather than wrapping content when I add an image?

I have a RelativeLayout, we'll call this the 'slider', that I want to overlay on another RelativeLayout (by switching visibility="gone" and "visible") when "Add People" is clicked, but the overlay should only take up as much width of the screen as needed. This layout will then be removed when "Cancel" is clicked. Everything is working fine so far.
RelativeLayout slider;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_people);
RelativeLayout add = (RelativeLayout) findViewById(R.id.add_wrapper);
RelativeLayout cancel = (RelativeLayout) findViewById(R.id.cancel_wrapper);
slider = (RelativeLayout) findViewById(R.id.add_people_slider);
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
slider.setVisibility(View.VISIBLE);
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
slider.setVisibility(View.GONE);
}
});
}
The issue arises when I try to add another image to the slider. I am adding this just above the #id/cancel_wrapper RelativeLayout in the XML (full XML at bottom).
<ImageView
android:id="#+id/transparent_add"
android:src="#drawable/ic_add_active_256"
android:layout_height="30dp"
android:layout_width="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true" />
For some reason, this is making the width of the slider the full width of the screen.
What's even more bizarre is if I add android:layout_marginRight="50dp" to this ImageView to move it left a little it starts making the slider smaller from the left. I would like this "transparent_add" image to be lined up with the old "add" image.
My 2 issues, then, are that when I add the "transparent_add" image it changes the width of the slider for an unknown reason, and also when I add marginRight on the image it makes the width of the slider smaller from the left.
<?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="fill_parent"
android:background="#color/white" >
<TextView
android:id="#+id/btn_people"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingLeft="10dp"
android:text="#string/label_people"
android:textColor="#color/blue"
android:textSize="16dp" />
<TextView
android:id="#+id/people_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/btn_people"
android:paddingLeft="10dp"
android:text="#string/label_people_info"
android:textSize="11dp" />
<RelativeLayout
android:id="#+id/add_wrapper"
android:layout_width="65dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#color/blue" >
<ImageView
android:id="#+id/plus_sign"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="19dp"
android:layout_marginTop="6dp"
android:src="#drawable/ic_add_256" />
<TextView
android:id="#+id/add_people"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingBottom="2dp"
android:text="#string/label_add_people"
android:textColor="#color/white"
android:textSize="11dp" />
</RelativeLayout>
<ListView
android:id="#+id/contacts_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="#id/people_info" />
<!-- switch between visible/gone -->
<RelativeLayout
android:id="#+id/add_people_slider"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="#color/blue"
android:visibility="gone" >
<TextView
android:id="#+id/label_add_new_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="65dp"
android:layout_alignParentTop="true"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:text="#string/label_add_new_contact"
android:textColor="#color/white"
android:textSize="16dp" />
<TextView
android:id="#+id/label_add_from_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/label_add_new_contact"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:text="#string/label_add_from_phone"
android:textColor="#color/white"
android:textSize="16dp" />
<TextView
android:id="#+id/label_add_from_facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/label_add_from_phone"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:text="#string/label_add_from_facebook"
android:textColor="#color/white"
android:textSize="16dp" />
<TextView
android:id="#+id/label_add_from_linkedin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/label_add_from_facebook"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:text="#string/label_add_from_linkedin"
android:textColor="#color/white"
android:textSize="16dp" />
<!-- insert image here -->
<RelativeLayout
android:id="#+id/cancel_wrapper"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingBottom="10dp" >
<ImageView
android:id="#+id/image_cancel"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="4dp"
android:src="#drawable/ic_close_256"/>
<TextView
android:id="#+id/label_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/label_cancel"
android:textColor="#color/white"
android:textSize="8dp" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
EDIT: This is happening both in Eclipse and on my Android device.
EDIT: I have tried placing the image above the "Add a new contact" text and then placing the text below that, but the same thing happens

Categories

Resources