I wrote a simple cards game where the user plays his card
doing TAP on one of the three imageviews (the cards)
I'd like to add the possibility of playing the card
by dragging the ImageView on the "table"
(the table is another layout or simply another part of the screen).
I tryed to use the techinc indicated at
http://blahti.wordpress.com/2011/01/17/moving-views-part-2/
but as it uses AbsoluteLayout, it introduces a lot of limitations
on my current layout, more it requires adjustments depending
to the device screen resolution where the app runs.
I'd like to avoid this continue using
-if possibile- the RelativeLayout.
Maybe the starting point is extenting the ImageView
adding the onTouch support but i couldn't reproduce
the wished effect (drag)
Any idea or suggestion or sample code?
Thanks!
Just to give an idea, this is the draft of layout.
The 3 cards below should be moved via drag.
<?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"
>
<RelativeLayout android:id="#+id/player_cards_layout"
android:layout_width="fill_parent" android:layout_height="150dip"
android:gravity="center_horizontal"
android:background="#55335588"
android:layout_above="#+id/player_cards_layout"
>
<ImageView android:id="#+id/img_pc_card_1"
android:layout_width="100dip" android:layout_height="150dip"
android:src="#drawable/icon"
/>
</RelativeLayout>
<RelativeLayout android:id="#+id/player_cards_layout"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:background="#336699"
>
<ImageView android:id="#+id/img_user_card_1"
android:layout_width="100dip" android:layout_height="150dip"
android:src="#drawable/icon"
/>
<ImageView android:id="#+id/img_user_card_2"
android:layout_width="100dip" android:layout_height="150dip"
android:src="#drawable/icon"
android:layout_toRightOf="#+id/img_user_card_1"
/>
<ImageView android:id="#+id/img_user_card_3"
android:layout_width="100dip" android:layout_height="150dip"
android:src="#drawable/icon"
android:layout_toRightOf="#+id/img_user_card_2"
/>
</RelativeLayout>
</RelativeLayout>
here is nice example from link. i hope this will help you.
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="center" android:id="#+id/LinearLayout01">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/btn"
android:text="Drag Me"></Button>
</FrameLayout>
src/com/beanie/example/Home.java
package com.beanie.example;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.FrameLayout.LayoutParams;
public class Home extends Activity implements OnTouchListener {
private final static int START_DRAGGING = 0;
private final static int STOP_DRAGGING = 1;
private Button btn;
private FrameLayout layout;
private int status;
private LayoutParams params;
private ImageView image;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (FrameLayout) findViewById(R.id.LinearLayout01);
// layout.setOnTouchListener(this);
btn = (Button) findViewById(R.id.btn);
btn.setDrawingCacheEnabled(true);
btn.setOnTouchListener(this);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}
#Override
public boolean onTouch(View view, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
status = START_DRAGGING;
image = new ImageView(this);
image.setImageBitmap(btn.getDrawingCache());
layout.addView(image, params);
}
if (me.getAction() == MotionEvent.ACTION_UP) {
status = STOP_DRAGGING;
Log.i("Drag", "Stopped Dragging");
} else if (me.getAction() == MotionEvent.ACTION_MOVE) {
if (status == START_DRAGGING) {
System.out.println("Dragging");
image.setPadding((int) me.getRawX(), (int) me.getRawY(), 0, 0);
image.invalidate();
}
}
return false;
}
}
Related
I am developing a game application using android.
The Object(a box) slides up and down. And it has to hit the
objects(orange and pink balls) coming towards it from the right end of
the screen that would increase his score.
There will be black balls as well(shot from the right end of the
screen) which he should avoid hitting.
I am having problem with
onTouchEvent(MotionEvent me)
function while implementing the code.
I am following this tutorial in this series of tutorials.
My Questions:
To use the onTouchEvent(MotionEvent me) function do I need to import any class?
The tutorial has declared theonTouchEvent(MotionEvent me) outside the onCreate method. Which is okay. But the program has not called it anywhere. How does it work then?
After writing the code as mentioned in the tutorial, the program is not working as intended. The box appears when the activity starts. However, it disappears as soon as I click on the screen. What could be the problem?
ActivityMain.XML
<?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"
tools:context=".MainActivity">
<TextView
android:id="#+id/scoreLabel"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text=" : 300"
android:paddingLeft="10dp"
android:gravity="center_vertical" />
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/startLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="130dp"/>
<ImageView
android:id="#+id/box"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/box"
android:layout_gravity="center_vertical" />
<ImageView
android:id="#+id/orange"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/orange" />
<ImageView
android:id="#+id/black"
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/black" />
<ImageView
android:id="#+id/pink"
android:layout_width="16dp"
android:layout_height="16dp"
android:src="#drawable/pink" />
</FrameLayout>
</RelativeLayout>
MainActivity.java
package com.example.catcheggs1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView scoreLabel;
private TextView startLabel;
private ImageView box;
private ImageView orange;
private ImageView black;
private ImageView pink;
//Position
private int boxY;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scoreLabel=(TextView)findViewById(R.id.scoreLabel);
startLabel=(TextView)findViewById(R.id.startLabel);
box=(ImageView)findViewById(R.id.box);
orange=(ImageView)findViewById(R.id.orange);
pink=(ImageView)findViewById(R.id.pink);
black=(ImageView)findViewById(R.id.black);
//Move To Out of Screen
orange.setX(-80);
orange.setY(-80);
pink.setX(-80);
pink.setY(-80);
black.setX(-80);
black.setY(-80);
//Temporary
startLabel.setVisibility(View.INVISIBLE);
boxY=500;
}
public boolean onTouchEvent(MotionEvent me)
{
if(me.getAction()==MotionEvent.ACTION_DOWN)
{
boxY -= 1 ;
}
box.setY(boxY);
return true;
}
}
1.) No, you do not need to import anything.
2.) For detailed information you can see the link.
It is an event method, it is automatically called, you do not need to call it.
https://developer.android.com/guide/topics/ui/ui-events#java
3.) you use boxY variable when touched and it is assigned an arbitrary number. Your problem is much likely to be caused by that. Rather than that, you should first get the current position than tweak it.
You can get current position with this method.
int[] location = new int[2];
imageView.getLocationOnScreen(location);
https://developer.android.com/reference/android/view/View.html#getLocationOnScreen(int[])
Bonus.) onTouchEvent is an activity method, so you should use view's own event listeners for specific tasks rather than onTouchEvent.
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
I am new to Android Studio/Development, but not programming itself. I am struggling with the syntax (as I do think it is my problem) yet I cannot find a solution anywhere on here or on Google. I have this 2nd activity named FilterActivity. FilterActivity currently has 2 TextViews. Both create dynamic CheckBoxes. Right now, I only have one doing this so I can get one right before I go onto another. Here's the issue, I click on the TextView to get the dynamically created CheckBox and it shows perfectly fine. However, clicking on it again just adds the same values until it appears to completely fill the parent via xml.
No matter what I search, what I do, it all does the same exact thing. I know, I have nothing for onCheckedChanged, but I have previously before and it did not work either. So question(s), should I not be using LinearLayout and do like a container of sorts? There will be a decently big database for the checkboxes (500+) so I was thinking I would have to implement ScrollView at some point. Also, if LinearLayout is the correct way to go, what am I doing wrong? I am just completely spinning and I know it should not be this hard.
Thank you to anyone that gives feedback!
Here's XML:
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="xxx.AppEx.FilterActivity"
tools:layout_editor_absoluteY="81dp"
tools:showIn="#layout/activity_filter">
<View
android:id="#+id/firstView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true" />
<View
android:id="#+id/secondView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/AllTextView"
android:clickable="true"
android:text="All"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="50dp"
android:layout_toLeftOf="#id/firstView"
android:layout_alignParentTop="true"
android:gravity="center"
android:textStyle="bold"
android:textAllCaps="true"/>
<TextView
android:id="#+id/DisTextView"
android:clickable="true"
android:text="Dis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="50dp"
android:layout_toRightOf="#+id/firstView"
android:layout_alignParentTop="true"
android:gravity="center"
android:textStyle="bold"
android:textAllCaps="true"/>
<LinearLayout
android:id="#+id/CheckBoxLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#id/secondView">
</LinearLayout>
</RelativeLayout>
Here's the main code:
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.content.Intent;
public class FilterActivity extends AppCompatActivity implements
CompoundButton.OnCheckedChangeListener {
LinearLayout CheckBoxLayout;
CheckBox checkBox;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filter);
CheckBoxLayout = (LinearLayout) findViewById(R.id.CheckBoxLayout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TextView AllTextView = (TextView) findViewById(R.id.AllTextView);
AllTextView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
AllTextView.setBackgroundColor(Color.BLUE);
onAllClick();
}
});
TextView DisTextView = (TextView) findViewById(R.id.DisTextView);
DisTextView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//onAllClick();
}
});
}
public void onAllClick()
{
Intent intent = getIntent();
dummy();
}
public void dummy()
{
String[] array = new String[]
{
"Rice", "Beans"
};
LinearLayout CheckBoxLayout = (LinearLayout)findViewById(R.id.CheckBoxLayout);
for (int i = 0; i < array.length; i++)
{
CheckBox checkBox = new CheckBox(this);
checkBox.setText(array[i]);
CheckBoxLayout.addView(checkBox);
}
}
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
}
}
i'm trying to create a widget called DecoView dynamicaly every time i press a button.
if i just put two deco views in the XML i can see them both and they both look fine on ontop of the other.
but when i try to add it dynamicly in the code, i only create the first one,
and all the rest just aren't get created
what am i missing here ?
can someone help me with this ?
my mainActivity.java
package com.example.shay_v.dynamicdecoviewexample;
import android.graphics.Color;
import android.graphics.PointF;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.hookedonplay.decoviewlib.DecoView;
import com.hookedonplay.decoviewlib.charts.SeriesItem;
import com.hookedonplay.decoviewlib.events.DecoEvent;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button mainMenuButton;
int widgetInteger = 1;
LinearLayout ll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//pointing to display
mainMenuButton = (Button) findViewById(R.id.button);
mainMenuButton.setOnClickListener(this);
//points to the linear layout in the xml
ll = (LinearLayout)findViewById(R.id.mainMenu_mainLayout);
}
private void createDecoViewWidget (int i) {
//adds params to the linear layout
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
//deco view widget
DecoView decoViewWidget = new DecoView(this);
//adding to view
decoViewWidget.setId(i);
ll.addView(decoViewWidget, params);
//decoViewWidget.configureAngles((int) (Math.random() * 360) + 1, (int) (Math.random() * 100));
//Create data series track
SeriesItem seriesItem = new SeriesItem.Builder(Color.argb(255, (int) (Math.random()*255), (int) (Math.random()*255), (int) (Math.random()*255)))
//third controller is end point
.setRange(0, 100, 0)
.setLineWidth(60f)
.setInset(new PointF(120f, 120f))
.build();
int series1Index = decoViewWidget.addSeries(seriesItem);
decoViewWidget.addEvent(new DecoEvent.Builder((float) (Math.random() * 100)).setIndex(series1Index).setDelay(1000).build());
}
//button listener
#Override
public void onClick(View v) {
createDecoViewWidget (widgetInteger);
widgetInteger++;
}
}
my activity_main.xml >
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.shay_v.dynamicdecoviewexample.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<LinearLayout
android:orientation="vertical"
android:layout_width="350dp"
android:layout_height="350dp"
android:layout_centerVertical="true"
android:id="#+id/mainMenu_mainLayout"
android:layout_centerHorizontal="true"></LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
anyone ?
When you are creating the Views they are being added into a LinearLayout. This type of layout will position the views one after the other, so the second view is being drawn off the visible area of the screen.
As you want all views to be drawn on top of each other you should use a RelativeLayout.
I have 4 different buttons. I want to change the background of the buttons at a fixed time (say 1 sec i.e. one button changes its color for one sec then retains its previous color and then other button does the same and so on) in certain random pattern, and then the user will repeat this pattern. However I am unable to change the background of the buttons randomly. I know that a timer or handler will b used but I have no idea ho to use it. Can anyone post a example program that shows how to change the background of buttons randomly?
here is my xml file:
`
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
>
<TextView
android:id="#+id/levelText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="50dp"
android:textColor="#FFFFFF"
android:text = "" />
<TextView
android:id="#+id/countDnText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="100dp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:text=""
/>
<Button
android:layout_width="160dp"
android:layout_height="200dp"
android:background="#000000"
android:id="#+id/button5"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="79dp" />
<Button
android:layout_width="160dp"
android:layout_height="200dp"
android:background="#000000"
android:id="#+id/button6"
android:layout_alignTop="#+id/button5"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="160dp"
android:layout_height="200dp"
android:background="#000000"
android:id="#+id/button7"
android:layout_below="#+id/button5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<Button
android:layout_width="160dp"
android:layout_height="200dp"
android:background="#000000"
android:id="#+id/button8"
android:layout_alignTop="#+id/button7"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
`
here is my Activity:`
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class EasyGameActivity extends AppCompatActivity
{
private int counter;
private TextView text;
private boolean flag = false;
private Button button = null;
private int i;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_easy_game);
startGame();
}
public void startGame()
{
counter = 3;
int temp;
final Random rand = new Random();
Handler handler = new Handler();
while(true)
{
BinaryTree binaryTree = new BinaryTree(counter);
for(int i = 0; i<counter; ++i)
{
temp = rand.nextInt(3);
// yellow color button...
if(temp == 0)
{
button = (Button) findViewById(R.id.button5);
button.setBackgroundColor(Color.YELLOW);
}
else if(temp == 1)
{
button = (Button) findViewById(R.id.button6);
button.setBackgroundColor(Color.BLUE);
}
else if(temp == 2)
{
button = (Button) findViewById(R.id.button7);
button.setBackgroundColor(Color.RED);
}
else if(temp == 3)
{
button = (Button) findViewById(R.id.button8);
button.setBackgroundColor(Color.GREEN);
}
//button.setBackgroundColor(Color.BLACK);
}
break;
}
}
}`
You could try something like this:
Button[] changingButtons = new Button[4];
changingButtons[0] = (Button)findViewById(R.id.button1_id);
changingButtons[1] = (Button)findViewById(R.id.button2_id);
changingButtons[2] = (Button)findViewById(R.id.button3_id);
changingButtons[3] = (Button)findViewById(R.id.button4_id);
You can then access the corresponding button in the array and change the background colour using changingButtons[<desired index>].setBackgroundResource(<color resource>)
To retain the previous color, you can use ColorDrawable previousBackground = (ColorDrawable)changingButtons[<desired index>].getBackground();
Then, for the "set time" part, use a timer, and do the colour switching in the TimerTask.
If you wish to use a TimerTask would look something like this inside the method calling it:
timer = new Timer();
timer.schedule(new MyTask(buttonNumber), numMilliseconds);
And then MyTask would be an extension class of TimeTask
class MyTask extends TimerTask
{
private int buttonId;
public MyTask(int buttonId)
{
super();
this.buttonId = buttonId;
}
public void run()
{
//Do your button alterations here
}
}
Here's a sample I made using the above code on Ideone
Hopefully this points you in the right direction!
For more information, check out these: Changing the background color programmatically, Getting the background color, Java Timer documentation
You can use Collections.shuffle(colorList);
List<String> colorList=new ArrayList<>();
colorList.add("#108E44");
colorList.add("#000000ff");
colorList.add("#108E44");
for() {
giveMeButtons(i).setBackgorundColor(Color.parseColor(colorList.get(i)));
}
And you must make other method. It will return random View button.
I'm really confused with this whole measuring thing that android does for layouts. Basically, I want to get the actual computed height and width of a view when it has been laid out BEFORE it gets laid out. I need to get the computed height and width because I have a hidden LinearLayout that I want to animate when opening using viewpropertyanimator. I need to supply the target width (which is the computed width) to the animator before it animates it. The width of this layout is dynamic because it relies on its android:weight parameter for the width, so I can't give a static value.
Here's my activity class:
package com.example.testproject;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class TestActivity extends Activity
{
private LinearLayout gmailListContainer, yahooMailListContainer, windowsLiveListContainer;
private LinearLayout emailMessageContainer;
private Display display;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_test);
gmailListContainer = (LinearLayout) this.findViewById(R.id.email_activity_gmail_layout);
yahooMailListContainer = (LinearLayout) this.findViewById(R.id.email_activity_yahoo_mail_layout);
windowsLiveListContainer = (LinearLayout) this.findViewById(R.id.email_activity_windows_live_layout);
emailMessageContainer = (LinearLayout) this.findViewById(R.id.email_activity_email_content_layout);
gmailListContainer.setBackgroundColor(Color.CYAN);
yahooMailListContainer.setBackgroundColor(Color.MAGENTA);
windowsLiveListContainer.setBackgroundColor(Color.GREEN);
emailMessageContainer.setBackgroundColor(Color.RED);
display = getWindowManager().getDefaultDisplay();
setMeasuredDimensions();
}
private void setMeasuredDimensions()
{
View v = this.findViewById(R.id.email_activity_layout);
v.measure(display.getWidth(), display.getHeight());
Log.v("EmailActivity", v.getMeasuredHeight() + ", " +v.getMeasuredWidth());
}
private void setWeight(LinearLayout container, float weight)
{
LinearLayout.LayoutParams params = (LayoutParams) container.getLayoutParams();
params.weight = weight;
container.setLayoutParams(params);
container.setVisibility(View.VISIBLE);
}
}
Here's the relevant layout resource:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/email_activity_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:id="#+id/email_activity_gmail_layout"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
<LinearLayout
android:id="#+id/email_activity_yahoo_mail_layout"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
<LinearLayout
android:id="#+id/email_activity_windows_live_layout"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
<LinearLayout
android:id="#+id/email_activity_email_content_layout"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="0"
/>
Add the below method to your Activity and call your method from it.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
setMeasuredDimensions();
super.onWindowFocusChanged(hasFocus);
}
In onCreate() you can't assure that your view is drawn. It might take some time. But this above method gets called only when the view is drawn completely. So YOu should be able to get the width and height exactly here.