[Q] How to achieve this Custom layout - Java for Android App Development

I have tried the frame layout and sliderView all seemed abortive..
my aim is to slide a layout from below when a button is clicked and the layout shouldn't cover the whole screen just the height should wrap_content some thing like the image below
i am a newbie in android development so don't blame my ignorance thnak you as you help

no reply? please guys help a noob...

mashnino said:
no reply? please guys help a noob...
Click to expand...
Click to collapse
question is a little ambiguous and it's just a viewGroup afterall, there are 100's of implementation options. Personally I would use a fragmentDialog, but I could just as easy use any normal viewGroup.

deanwray said:
question is a little ambiguous and it's just a viewGroup afterall, there are 100's of implementation options. Personally I would use a fragmentDialog, but I could just as easy use any normal viewGroup.
Click to expand...
Click to collapse
okay dat was a little out of my league..can u like give me an example...
because this is what i have already... a layout within a layout and then some button in which onClick() it slides the layout from below.
the main problem is the layout doesnt stick to the bottom of the screen.. instead it slides to the top of the screen.
activity_main.xml
PHP:
<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="@color/blue"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#eeeeee"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/action_bar"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="@color/blue" >
<ImageButton
android:id="@+id/qpaper_menu"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/menu_button_blue" />
<TextView
android:id="@+id/app_header_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/qpaper_menu"
android:text="UMAR MASH"
android:textColor="#fff"
android:textSize="18sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_margin="12dp"
android:background="@drawable/bg_card"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Umar mash was ere...."
android:layout_alignParentLeft="true"/>
<Button
android:id="@+id/slider"
android:layout_below="@id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sleider"
android:onClick="slider"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/hidden_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone"
android:background="#000000" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test" />
</LinearLayout>
</RelativeLayout>
and then the main_activity.java class
PHP:
package com.example.kkn;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
ActionBar ab;
ImageButton side;
boolean isPanelDown;
View hiddenpanel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
side = (ImageButton) findViewById(R.id.qpaper_menu);
hiddenpanel = findViewById(R.id.hidden_panel);
ab = getActionBar();
ab.hide();
side.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.qpaper_menu:
Toast.makeText(getApplicationContext(), "This is the menu button", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
public void slider(final View view) {
if (!isPanelShown()) {
// Show the panel
Animation bottomUp = AnimationUtils.loadAnimation(this,R.anim.bottom_up);
hiddenpanel.startAnimation(bottomUp);
hiddenpanel.setVisibility(View.VISIBLE);
}
else {
// Hide the Panel
Animation bottomDown = AnimationUtils.loadAnimation(this,R.anim.bottom_down);
hiddenpanel.startAnimation(bottomDown);
hiddenpanel.setVisibility(View.GONE);
}
}
private boolean isPanelShown() {
return hiddenpanel.getVisibility() == View.VISIBLE;
}
}
so any help please

mashnino said:
okay dat was a little out of my league..can u like give me an example...
so any help please
Click to expand...
Click to collapse
There are many examples of view animation both on the developer website and on SO. Sounds like you just need to set animate Y position to move relative ((screen height - (screen height - height of sliding view)) ?
Or just end at absolute (screen height - height of sliding view)

deanwray said:
There are many examples of view animation both on the developer website and on SO. Sounds like you just need to set animate Y position to move relative ((screen height - (screen height - height of sliding view)) ?
Or just end at absolute (screen height - height of sliding view)
Click to expand...
Click to collapse
i would have known what to do if i actually understood where to put the code... please can you like fork the code so that i can have an idea of what you are saying and from there i can look up other resources... please

mashnino said:
i would have known what to do if i actually understood where to put the code... please can you like fork the code so that i can have an idea of what you are saying and from there i can look up other resources... please
Click to expand...
Click to collapse
sounds like your wanting someone to write it for you ?
If not then you should just look at doing what I said, getting the height and working out the end position for the anim and setting it programmatically. If you need hints ask away but not sure I have the time to write it for you atm...sorry

Related

Developing Game

I have decided to recreate a game I was making on the PC for Google Android but I have run into some snags. Currently I have the menu partially created but am not exactly sure how the views and such work. So I have uploaded my source and was hoping someone could look at my source and help me. I have menu created through xml and have ninepatchdrawable button with a background set for the menu. What I am trying to figure out is clicking start and going to another view.
http://www.bobhoil.com/wp-content/uploads/2010/08/T-Dodger.zip
You can also visit my blog here: http://bobhoil.com/
Thanks! I hope I can get some assistance
I am also having some trouble installing it to my att Samsung Captivate. I tried using a program I have been using to install from the pc but it says wrong SDk version even though it is the same one I have been using.
Please Help?
look at this: http://dl.dropbox.com/u/2817943/T Dodger.rar
i have added an additional layout and an acitivity-class to your project. and i called that activity by clicking you start button!
i hope that helps you
Thanks that helps a ton! That was the main issue I was having. Now I can start connecting parts of my game together. I appreciate it a ton.
Also just a tip, root your captivated and turn on USB debugging. That will allow you to run your program right to your phone, rather than the slow emulator.
Sent from my HTC HD2 using XDA App
Okay that sounds good. One thing though that voids your warrenty and I bought an extended warrenty from At&t. So would it void my warrenty?
Also I am making some progress. I have the code laid out for detecting the accelerometer! Next I just need to use that data and apply it to the image that I will be moving around the screen.
It will not void warrenty as it is software based. Sounds good. Hope to see a beta soon.
Okay sounds great then. I will start working on that then.
I have run into another little problem. Now I have rooted my phone which makes testing a LOT easier! (Thanks Metastable!)
The following code seems to cause it to crash:
package com.bobhoil.tdodger;
import android.app.Activity;
import android.os.Bundle;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.TextView;
public class TestActivity extends Activity implements SensorEventListener {
private TextView accelXValue;
private TextView accelYValue;
private TextView accelZValue;
private SensorManager sensorManager = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.test);
accelXValue = (TextView) findViewById(R.id.accel_x_value);
accelYValue = (TextView) findViewById(R.id.accel_y_value);
accelZValue = (TextView) findViewById(R.id.accel_z_value);
accelXValue.setText("0.00");
accelYValue.setText("0.00");
accelZValue.setText("0.00");
}
public void onSensorChanged(SensorEvent sensorEvent) {
synchronized (this) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
accelXValue.setText(Float.toString(sensorEvent.values[0]));
accelYValue.setText(Float.toString(sensorEvent.values[1]));
accelZValue.setText(Float.toString(sensorEvent.values[2])); }
}
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onStop() {
sensorManager.unregisterListener(this);
super.onStop();
}
}
Click to expand...
Click to collapse
This is supposed to capture movement through the accelerometer.
please zip the eclipse project and tell us the link to it.
i'm too lazy to build my own xml with 3 EditText's
OKay here is the link: http://www.bobhoil.com/wp-content/uploads/2010/09/T-Dodger2.zip
I have been busy working and hadn't had a chance to upload it.
Don't mean to be persistent I am just trying to figure out why it is crashing. I changed the code in the above file to some ibm examples just to test if that worked and it too seems to crash. Am I starting the activity wrong?
there are some attributes missing in your test.xml (android:layout_width... and so on)
here is the correct test.xml
Code:
<?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">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/mobil"/>
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView
android:id="@+id/accelerometer_label"
android:layout_column="1"
android:text="Accelerometer"
android:textSize="9pt"
android:padding="3dip" />
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView
android:id="@+id/accel_x_label"
android:layout_column="1"
android:text="X:"
android:textSize="8pt"
android:padding="3dip" />
<TextView
android:id="@+id/accel_x_value"
android:gravity="right"
android:textSize="8pt"
android:padding="3dip" />
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView
android:id="@+id/accel_y_label"
android:layout_column="1"
android:text="Y:"
android:textSize="8pt"
android:padding="3dip" />
<TextView
android:id="@+id/accel_y_value"
android:gravity="right"
android:textSize="8pt"
android:padding="3dip" />
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView
android:id="@+id/accel_z_label"
android:layout_column="1"
android:text="Z:"
android:textSize="8pt"
android:padding="3dip" />
<TextView
android:id="@+id/accel_z_value"
android:gravity="right"
android:textSize="8pt"
android:padding="3dip" />
</TableRow>
<View
android:layout_height="2dip" android:layout_width="fill_parent"
android:background="#FF909090" />
</LinearLayout>
by replacing the test.xml with the correct one, the activity opens like it should (you can check that by adding a breakpoint in the onCreate method).
But there is another error too that will cause the activity to close immediately. At the moment i have no idea what's the problem, but i try to figure it out!
Okay thanks. I have been trying to solve the problem for about a week now. I am having a hard time figuring out the problem.
EDIT:
Okay after doing some research maybe it is getting information from the accelerometer to quickly?
no, the problem is the test.xml again... something is wrong with your table-rows.
while executing your application, logcat shows some exceptions like "null-child" in a table row...
Code:
09-05 07:45:40.114: ERROR/AndroidRuntime(223): java.lang.NullPointerException
09-05 07:45:40.114: ERROR/AndroidRuntime(223): at android.widget.TableRow.measureNullChild(TableRow.java:182)
I'm afraid I have too little experience with <TableRow> in Android. I suggest to use simple <TextView> elements to display the sensor data. You only need one TextView, and in your java class file you can put all the sensor values in this TextView.
for example:
accelValue.setText("Accel X: " + values[0]+"\n" + "Accel Y: " + values[] +.....)
The TestActivity is for testing purposes only... so don't care about the layout
Okay I will change over to using TextView and see then if I can get something up and running.
Thanks for the help! I have finally got it fully working!
OKay one last question. Is there any way to change android:layout_y outside of the xml? From the activity?
Have you set up your activities in manifest.xml file??
Sent from my ADR6300 using XDA App

New to android app development...switching between views or layouts or activities...

Hi,
i am new to developing apps on Android (Windows Phone 7 dev)...how ever i have created my first project (i am a Java developer for many years now, so i really have only problems with Android specific things).
And i already have my first problem and i have not enough knowledge to find the tutorials that i need for this or maybe i am just doing something wrong.
I have a main.xml file and a logon.xml file for the 2 different views (or how ever you call that) i need...i like to switch to logon.xml if i tap a button on main.xml. That works very well (i am using setContentView(R.layout.logon) so far but i am missing the animation between the two UIs...and so far none of the tutorials i have found are working with my scenario...can someone help me out?
I just know how easy it is in Windows Phone 7 to switch between panels with animations created in expression blend
main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<TextView android:layout_width="150dip" android:text="" android:id="@+id/space" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/InformationCreateNewAccount" android:layout_height="wrap_content" android:text="Don't have a Account?" android:layout_width="wrap_content"></TextView>
<Button android:id="@+id/btnCreateNewAccount" android:onClick="goToLoginView" android:layout_height="wrap_content" android:text="Create" android:layout_width="150dip"></Button>
<TextView android:text="" android:id="@+id/space" android:layout_width="150dip" android:layout_height="wrap_content"></TextView>
<TextView android:layout_width="wrap_content" android:id="@+id/InformationHaveAAccount" android:layout_height="wrap_content" android:text="Already have a Account?"></TextView>
<Button android:id="@+id/btnSignIn" android:layout_width="150dip" android:layout_height="wrap_content" android:text="Sign in"></Button>
</LinearLayout>
logon.xml
Code:
<?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">
</LinearLayout>
chatClient.java
Code:
package com.ayra.chat.com.ayra;
import com.ayra.chat.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class chat extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void goToLoginView(View view) {
setContentView(R.layout.logon);
}
}

[Q] Need help with discount calculator

I am a newbie in android dev.
I want to develop a very small and simple discount rate calculator.
in which there will be drop down menu with options like:
calculate 10%
calculate 20%
calculate 30%
Based on the selection of option from the drop down menu the discount rate should change.
After selection, there will be a textbox which should ask user to enter price.
and on the click even of "Calculate" button, the resultant amount (discounted rate) should be displayed.
I guess it is very simple for you guys. If anyone can help me with the code, i will deeply appreciate.
thank you
ok this is what I have done till now:
I created an Android Project names as :MyDC
MyDC.java
Code:
package com.super.mydc;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
public class MyDC extends Activity {
private EditText amount1;
private double x=0;
private double y=2.0;
private double z=0;
private TextView tt;
private Button calculate;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// We want to view some very simple text, so we need a TextView
TextView tv = new TextView(this);
// Put some text to the newly created TextVIew
tv.setText("Test");
// Tell our Application to display the textView
this.setContentView(tv);
super.onCreate(icicle);
setContentView(R.layout.main);
initControls();
}
private void initControls()
{
amount1=(EditText)findViewById(R.id.amount1);
tt=(TextView)findViewById(R.id.tt);
calculate=(Button)findViewById(R.id.calculate);
calculate.setOnClickListener(new Button.OnClickListener()
{public void onClick
(View v) { calculate();}});
}
private void calculate()
{
x=Double.parseDouble(amount1.getText().toString());
z=x-(x*y/100);
tt.setText(Double.toString(z));
}
}
This is main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/spinner1"></Spinner>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the price"
/>
<EditText android:layout_height="wrap_content" android:id="@+id/amount1" android:text="" android:layout_width="match_parent"></EditText>
<Button android:text="Calculate Result" android:id="@+id/calculate" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<EditText android:layout_height="wrap_content" android:id="@+id/tt" android:text="" android:layout_width="match_parent"></EditText>
</LinearLayout>
This is string.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hi, MyDC!</string>
<string name="app_name">My, DC</string>
<string name="spinner1">Choose discount type</string>
</resources>
This is what I am able to get:
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
I am able to calculate discount on the button click, BUT i have hardcoded the discount rate.
I want to select discount rate based on the dropdown menu (I don't know how to set dropdown values with different discount rate in this problem)
In the dropdown menu I would like to have values like this:
Type 1 discount
Type 2 discount
Type 3 discount
Here is the respective discount rate for the above drop-down values:
Type 1 discount - 10%
Type 2 discount - 15%
Type 3 discount - 18%
PLZ HELP ME
Ok somehow I was able to add spinner tool on my application.
Now the question is how to select discount rate based on selected value from spinner.
PLZ HELP ME WITH THE CODE
Here is my current code:
MyDC.java
Code:
package com.super.mydc;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
public class MyDC extends Activity {
private EditText amount1;
private double x=0;
private double y=2.0;
private double z=0;
private TextView tt;
private Button calculate;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// We want to view some very simple text, so we need a TextView
TextView tv = new TextView(this);
// Put some text to the newly created TextVIew
tv.setText("Test");
// Tell our Application to display the textView
this.setContentView(tv);
super.onCreate(icicle);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
initControls();
}
private void initControls()
{
amount1=(EditText)findViewById(R.id.amount1);
tt=(TextView)findViewById(R.id.tt);
calculate=(Button)findViewById(R.id.calculate);
calculate.setOnClickListener(new Button.OnClickListener()
{public void onClick
(View v) { calculate();}});
}
private void calculate()
{
x=Double.parseDouble(amount1.getText().toString());
z=x-(x*y/100);
tt.setText(Double.toString(z));
}
}
main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/spinner1"></Spinner>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="@string/planet_prompt"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/planet_prompt"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the price"
/>
<EditText android:layout_height="wrap_content" android:id="@+id/amount1" android:text="" android:layout_width="match_parent"></EditText>
<Button android:text="Calculate Result" android:id="@+id/calculate" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<EditText android:layout_height="wrap_content" android:id="@+id/tt" android:text="" android:layout_width="match_parent"></EditText>
</LinearLayout>
strings.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hi, MyDC!</string>
<string name="app_name">My, DC</string>
<string name="spinner1">Choose a planet</string>
<string name="planet_prompt">Choose a planet</string>
<string-array name="planets_array">
<item>10% discount A</item>
<item>15% discount B</item>
<item>18% discount C</item>
</string-array>
</resources>
Anyone ? Please Help ?
Im sorry to not be of very much help I studied java as a highschool unfortunately it was senior year and I am good at slacking. I would like to add suggestions though. first off i would try to find someway of implementing a way to get closer increments such as 5 or 1 %. such as a text box. let me sift through your code and see what i can see. are you having specific errors or what. we need some info.
Ok i looked at your code and im having a hard time reading it just because it isnt the norm that I look at. first off ya need to name your variables more recognizable things then x and y. that will make coding immensely more easy. also I am not seeing where you gathered the information from the drop down or spinner. at some point in the code you have to collect the information from the drop down or spinner and use it for the percent. also i didn't see any point where you gave the drop down any values. if you have any questions or anything feel free to pm I have xda app on my phone so even if not im not online ill get a notification on my phone.
{SBR}_L3GION said:
Ok i looked at your code and im having a hard time reading it just because it isnt the norm that I look at. first off ya need to name your variables more recognizable things then x and y. that will make coding immensely more easy. also I am not seeing where you gathered the information from the drop down or spinner. at some point in the code you have to collect the information from the drop down or spinner and use it for the percent. also i didn't see any point where you gave the drop down any values. if you have any questions or anything feel free to pm I have xda app on my phone so even if not im not online ill get a notification on my phone.
Click to expand...
Click to collapse
Hi,
Yes, I know I haven't defined values to drop down because that is the thing I need help with.
I also posted above this:
"Now the question is how to select discount rate based on selected value from spinner.
PLZ HELP ME WITH THE CODE"
I need help - How to define discount rate based on selection of drop down values.
Please help me with the code....
I also tried doing this quite awhile with an app I was writing and then discovered I had not enough experience to with the android system to write the program I should look it up and fix both of our programs but I've had a migraine for days and I'm just getting close to sleeping but when I get a chance I will look up giving values to the drop down and collecting them unless you want to google it first.
Sent from my Synergized xda Premium Evo
I have tried to find the solution on google but didn't get anything but somehow from google I was able to do (whatever I did above)
thanks, I think that would be great, if you can test my program on your side by that we both will be on the same page.
That's fine, you can test it whenever you get some time.
I appreciate your help!
I would setup radio buttons for the discount percentages
0 10% 0 20% etc.
Name each radio button and use a case statement to set a float to it's value:
switch(radioGroupName) {
case 10Percent:
int discountValue = .10
break;
...
Then do something like:
int discountedAmount = usersAmount - (usersAmount * discountValue);
Try this code.
myDC.java
Code:
package com.mydc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
import android.widget.Toast;
public class myDC extends Activity {
double initial_price;
double discount;
double final_price;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.discount, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos==0){
discount=10;
}
else if (pos==1){
discount=15;
}
else if (pos==2){
discount=18;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Button calculate = (Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
EditText amount1 = (EditText) findViewById(R.id.amount1);
if (amount1.length()>0 ) {
initial_price=Double.parseDouble(amount1.getText().toString());
TextView final_price_text = (TextView) findViewById(R.id.final_price);
final_price=initial_price-(initial_price*discount/100);
final_price_text.setText("Final Price : "+final_price);
}
else {
Toast.makeText(getBaseContext(), "Please enter price", Toast.LENGTH_SHORT).show();
}
}
});
}
}
main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner1"></Spinner>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the price" />
<EditText
android:layout_height="wrap_content"
android:id="@+id/amount1"
android:text=""
android:layout_width="match_parent"
android:numeric="decimal"></EditText>
<Button
android:text="Calculate Result"
android:id="@+id/calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:id="@+id/final_price"></TextView>
</LinearLayout>
strings.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, main!</string>
<string name="app_name">myDC</string>
<string-array name="discount">
<item>10% discount A</item>
<item>15% discount B</item>
<item>18% discount C</item>
</string-array>
</resources>
Re: Need Help
Hello nemoc 23 ,
Firstly thank you very much for replying back with a solution.
It did work and solved my problem upto some extend.
Now what I only want 2 things to be done:
1) Right now, if we enter price in decimal or anything, if the final result is in decimal, it is being displayed as EXACT something like 99.9999999
How to limit the RESULT upto 2 decimal points only ?
2) What I am trying to do is to have this calculator on 3 different pages (in each page I will just modify the discounted rate). So I want a homepage (which will work as MENU) with 3 or 4 buttons and each button should be connected with specific page that will have ITS OWN above discount calculator.
Could you please show me how to create a homepage screen (which will work as MENU) and 3 or 4 buttons on it which are linked with different pages (in each page you can copy the same above calculator and I will modify the rate by myself)
thank you
Why 3 pages? Just do this:
Enter discount amount: ___________
Enter discount rate:______
Discounted Amount: ___________
Easy peasy.
Rootstonian said:
Why 3 pages? Just do this:
Enter discount amount: ___________
Enter discount rate:______
Discounted Amount: ___________
Easy peasy.
Click to expand...
Click to collapse
I know it is easy but it is not what I am looking for. My requirement is what I posted above. Thanks
Help
Hello nemoc 23 ,
Just giving you an update. I did try the tutorial you asked me to try. I am having some problem in it which you can see here: http://forum.xda-developers.com/showthread.php?p=15612542
Coming back my question (this calculator topic) - I created a screen layout to give you an idea what I want to do and need your help with.
This is what I want to do:
In this each button click will open separate page (or I guess it is called activity). I have mentioned on button what each button will open.
Please help me. thanks
Intent i = new Intent(this, calculator1.class);
startActivity(i);
You are still better off creating just ONE new Activity and passing to it which calculator you want to run. It is poor programming to code 3 activities when one will suffice. You'll need "extras" to do this.
Need Help
Ok somehow I was able to code for this problem but I am not getting desired output.
I have 3 buttons on my welcome screen "calculator", "calculator2", "exit" - My buttons are not working, I don't know why.
**Below is my complete code / programs:**
**welcome.java**
Code:
package com.testcalculator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class welcome extends Activity implements OnClickListener
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome1);
//////// MENU //////////
Button playBtn = (Button) findViewById(R.id.playBtn);
playBtn.setOnClickListener(this);
Button playBtn2 = (Button) findViewById(R.id.playBtn);
playBtn2.setOnClickListener(this);
Button exitBtn = (Button) findViewById(R.id.exitBtn);
exitBtn.setOnClickListener(this);
}
/**
* Listener for game menu
*/
public void onClick(View v) {
Intent i;
switch (v.getId()){
case R.id.playBtn :
i = new Intent(this, testcalculator.class);
startActivity(i);
break;
case R.id.playBtn2 :
i = new Intent(this, testcalculator2.class);
startActivity(i);
break;
case R.id.exitBtn :
finish();
break;
}
}
}
**welcome1.xml**
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id ="@+id/playBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculator"
/>
<Button
android:id ="@+id/playBtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculator2"
/>
<Button
android:id ="@+id/exitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit"
/>
</LinearLayout>
**testcalculator.java**
Code:
package com.testcalculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
import android.widget.Toast;
public class testcalculator extends Activity {
double initial_price;
double discount;
double final_price;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.discount, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos==0){
discount=10;
}
else if (pos==1){
discount=15;
}
else if (pos==2){
discount=18;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Button calculate = (Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
EditText amount1 = (EditText) findViewById(R.id.amount1);
if (amount1.length()>0 ) {
initial_price=Double.parseDouble(amount1.getText().toString());
TextView final_price_text = (TextView) findViewById(R.id.final_price);
final_price=initial_price-(initial_price*discount/100);
final_price_text.setText("Final Price : "+final_price);
}
else {
Toast.makeText(getBaseContext(), "Please enter price", Toast.LENGTH_SHORT).show();
}
}
});
}
}
**testcalculator.xml**
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner1"></Spinner>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the price" />
<EditText
android:layout_height="wrap_content"
android:id="@+id/amount1"
android:text=""
android:layout_width="match_parent"
android:numeric="decimal"></EditText>
<Button
android:text="Calculate Result"
android:id="@+id/calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:id="@+id/final_price"></TextView>
</LinearLayout>
**testcalculator2.java**
Code:
package com.testcalculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
import android.widget.Toast;
public class testcalculator2 extends Activity {
double initial_price;
double discount2;
double final_price2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.discount2, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos==0){
discount2=20;
}
else if (pos==1){
discount2=25;
}
else if (pos==2){
discount2=28;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Button calculate2 = (Button) findViewById(R.id.calculate2);
calculate2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
EditText amount2 = (EditText) findViewById(R.id.amount2);
if (amount2.length()>0 ) {
initial_price=Double.parseDouble(amount2.getText().toString());
TextView final_price_text = (TextView) findViewById(R.id.final_price2);
final_price2=initial_price-(initial_price*discount2/100);
final_price_text.setText("Final Price : "+final_price2);
}
else {
Toast.makeText(getBaseContext(), "Please enter price", Toast.LENGTH_SHORT).show();
}
}
});
}
}
**testcalculator2.xml**
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner2"></Spinner>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the price" />
<EditText
android:layout_height="wrap_content"
android:id="@+id/amount2"
android:text=""
android:layout_width="match_parent"
android:numeric="decimal"></EditText>
<Button
android:text="Calculate Result"
android:id="@+id/calculate2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:id="@+id/final_price2"></TextView>
</LinearLayout>
**AndroidManifest.xml**
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testcalculator"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".welcome"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".testcalculator" />
<activity android:name=".testcalculator2" />
</application>
</manifest>
**strings.xml**
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, testcalculator!</string>
<string name="app_name">testcalculator</string>
<string-array name="discount">
<item>10% discount A</item>
<item>15% discount B</item>
<item>18% discount C</item>
</string-array>
<string-array name="discount2">
<item>20% discount A</item>
<item>25% discount B</item>
<item>28% discount C</item>
</string-array>
</resources>
**main.xml**
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
When I click Button1 "Calculator" - I get error saying ""The application testcalculator (process com.testcalculator) has stopped unexpectedly. Please try again.""
Button2 "Calculator2" doesn't do anything and same with 3rd button "exit:
**Please help me!!!**
You got a couple options. First instead of the startActivity() throw in a Toast so you can see if your onClick() is ok. You can also throw some inline clicklisteners (anonymous inner class) inside the setOnClickListener() instead of using 'this'. I find that way is a bit easier to organize.
Annd might I suggest you follow the Java guidelines for capitalisation of class names. Its confusing when reading your code and seeing testcalculator.class when usually instances of objects are lower case while the class name is capitalized. Like TestCalculator.class
From something awesome

[Q] View tabs at landscape next to each other

Hello,
is there a possibility to show the tabs from a TabHost next to each other, when the phone is in landscape-orientation?
Auroratic said:
Hello,
is there a possibility to show the tabs from a TabHost next to each other, when the phone is in landscape-orientation?
Click to expand...
Click to collapse
I don't think there is. I haven't seen anyone do this yet, and it would only be confusing... Three tabs, two displayed at the same time is weird and takes away half the displayed content of one tab.
Anyhow, why use a TabHost? That's been deprecated for a long while, with the introduction of the ActionBar.
bassie1995 said:
I don't think there is. I haven't seen anyone do this yet, and it would only be confusing... Three tabs, two displayed at the same time is weird and takes away half the displayed content of one tab.
Click to expand...
Click to collapse
see the image I attached, i hope you understand what I'm trying to do
bassie1995 said:
Anyhow, why use a TabHost? That's been deprecated for a long while, with the introduction of the ActionBar.
Click to expand...
Click to collapse
oh I don't know that..
Auroratic said:
see the image I attached, i hope you understand what I'm trying to do
oh I don't know that..
Click to expand...
Click to collapse
You can do that.
Have a look at Fragments. They are exactly what you want.
Just use a different layout for landscape.
pfuuu...that's too hard for me.
can you create a sample application which uses ActionBar-tabs in portrait and shows the content of the tabs in landscape-mode?
Auroratic said:
pfuuu...that's too hard for me.
can you create a sample application which uses ActionBar-tabs in portrait and shows the content of the tabs in landscape-mode?
Click to expand...
Click to collapse
AB tabs can't show the content of multiple tabs at once, I think... Unless you do some intricate custom layout work, as nikwen said.
Auroratic said:
pfuuu...that's too hard for me.
can you create a sample application which uses ActionBar-tabs in portrait and shows the content of the tabs in landscape-mode?
Click to expand...
Click to collapse
to expand on nikwen's answer what you need to do is have a layout file for listview and a layout file for portrait and landscape. To do the portrait and landscape you put the portrait file in the layout folder and create a layout-land folder. In the portrait file you would put one fragment and in the landscape file you would 5 (according to your image) fragments and you could put a textview above each if you are going to remove the tabs.
Action bar tabs are programmatically inserted so you would have to listen for orientation change and remove them when in landscape.
here are some resources that will help you:
Fragments:
Android guide to fragments
Android references for fragments
Vogella fragments tutorial
Action Bar Tabs:
Android Action Bar Guide
Android references for AB Tabs
Vogella Action Bar tutorial
and here are some resources for finding out orientation:
Android Configuration references
Android guide to handling configuration change
I have no rewritten my app and now its using action bar tabs and its creating a fragment 5 times with different data. How should i make the layout for landscape and how should i insert the fragments into the layout?
Thank you.
make a layout-land folder and create a custom layout for landscape
a horizontal linear layout with all the fragments would do
warlock9_0 said:
a horizontal linear layout with all the fragments would do
Click to expand...
Click to collapse
What should be in this file? In portrait I'm using a viewpager, which contains the fragments.
PORTRAIT FILES
act_viewpager.xml:
Code:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".KlassenAnzeige" />
site_fragment.xml:
Code:
<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="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="8dp"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="8dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:listSelector="@android:color/transparent" />
</RelativeLayout>
the site_fragment is used for all fragment in the viewpager
MainActivity
Code:
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onResume() {
super.onResume();
if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.odd, new OddNumbers()).commit();
manager.beginTransaction().replace(R.id.even, new EvenNumbers()).commit();
manager.beginTransaction().replace(R.id.alpha, new Alpha()).commit();
}
}
}
layout-land/activity_main.xml
Code:
<LinearLayout xmlns:android="...."
xmlns:tools="...."
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:baselineAligned="false"
android:orientation="horizontal" >
<FrameLayout android:id="@+id/odd"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
<FrameLayout android:id="@+id/even"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
<FrameLayout android:id="@+id/alpha"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
</LinearLayout>
one of the fragments
Code:
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
public class EvenNumbers extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
String[] values = new String[] { "2", "4", "6","8","10","12","14","16","18","20","22" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), android.R.layout.simple_list_item_1,values);
/** Setting the list adapter for the ListFragment */
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
}

listView Resize animation

I have this custom listView:
<?xml version="1.0" encoding="utf-8"?>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tableRow">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/ic_launcher"
android:contentDescription="@string/imdDesc" />
<TextView
android:id="@+id/titleList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="25sp"
android:autoText="false"
android:gravity="center_vertical|center|center_horizontal" />
</TableRow>
This adapter:
public class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final String[] title;
private final int[] colors;
private final Integer[] imageId;
public CustomList(Activity context,String[] title,int[] colors, Integer[] imageId) {
super(context, R.layout.list_single, title);
this.context = context;
this.title = title;
this.imageId = imageId;
this.colors=colors;
}
@override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.titleList);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(title[position]);
txtTitle.setTextColor(Color.parseColor("#FFffffff"));
txtTitle.setBackgroundColor(colors[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
}
and this is how I bind them:
final CustomList adapter = new
CustomList(MainActivity.this, title, colors, imageId);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
My list has 12 items. I need to resize the list when user click one item. If the user choses an Item for the first time, the list has to change its width to 1/3 of its original size and to double the height of the selected item. If an item is already selected in the list and the user selects another the old item has to go back to the original height and the new selected one has to double its height without any width changes.
I want to animate all the resizing for nice looking.
This is the animation for changing width:
public class ShrinkList extends Animation
{
int fromWidth;
View view;
int def;
public ShrinkList(View view) {
this.view = view;
this.fromWidth = view.getWidth();
def=fromWidth/3;
}
@override
public boolean willChangeBounds() {
return true;
}
@override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
@override
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newWidth;
newWidth = (int) ((def - fromWidth) * interpolatedTime + fromWidth);
if (newWidth > def/3) {
view.getLayoutParams().width = newWidth;
view.requestLayout();
}
else{
view.getLayoutParams().width = def/3;
view.requestLayout();
}
}
}
and this one is for changing height:
public class Grow extends Animation
{
int fromHeight;
View view;
int defaultHeight;
public Grow(View view,int height) {
this.view = view;
this.fromHeight = view.getHeight();
defaultHeight=height;
}
@override
public boolean willChangeBounds() {
return true;
}
@override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
@override
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newHeight;
newHeight = (int) (fromHeight / (1 - interpolatedTime));
if (newHeight < defaultHeight * 2) {
view.getLayoutParams().height = newHeight;
view.requestLayout();
}
else{
view.getLayoutParams().height = defaultHeight*2;
view.requestLayout();
}
}
}
and I play them like that:
if(h==0) { //first time the user selects an item =>change width and height
Animation a = new ShrinkList(list);
a.setInterpolator(new LinearInterpolator());
a.setDuration(1000);
list.setAnimation(a);
list.startAnimation(a);
}
Animation a1 = new Grow(view,height); //change only height
a1.setInterpolator(new LinearInterpolator());
a1.setDuration(300);
view.setAnimation(a1);
view.startAnimation(a1);
My problem is that when the list changes its width, the animation is not smooth at all and it only changes the width, not the height too.
How can I make the animation smoother and make the two animations play at once?
I have tried scaling the list, but that scales all the list. basically I need only to resize the textView within ListView nd changing layoutParams does that.
I have tried to insert the code from Grow.java into ShrinkList.Java in orefer to play both animations once and it only played the one from Grow.java.
Jaws1992 said:
I have this custom listView:
<?xml version="1.0" encoding="utf-8"?>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tableRow">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/ic_launcher"
android:contentDescription="@string/imdDesc" />
<TextView
android:id="@+id/titleList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="25sp"
android:autoText="false"
android:gravity="center_vertical|center|center_horizontal" />
</TableRow>
This adapter:
public class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final String[] title;
private final int[] colors;
private final Integer[] imageId;
public CustomList(Activity context,String[] title,int[] colors, Integer[] imageId) {
super(context, R.layout.list_single, title);
this.context = context;
this.title = title;
this.imageId = imageId;
this.colors=colors;
}
@override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.titleList);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(title[position]);
txtTitle.setTextColor(Color.parseColor("#FFffffff"));
txtTitle.setBackgroundColor(colors[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
}
and this is how I bind them:
final CustomList adapter = new
CustomList(MainActivity.this, title, colors, imageId);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
My list has 12 items. I need to resize the list when user click one item. If the user choses an Item for the first time, the list has to change its width to 1/3 of its original size and to double the height of the selected item. If an item is already selected in the list and the user selects another the old item has to go back to the original height and the new selected one has to double its height without any width changes.
I want to animate all the resizing for nice looking.
This is the animation for changing width:
public class ShrinkList extends Animation
{
int fromWidth;
View view;
int def;
public ShrinkList(View view) {
this.view = view;
this.fromWidth = view.getWidth();
def=fromWidth/3;
}
@override
public boolean willChangeBounds() {
return true;
}
@override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
@override
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newWidth;
newWidth = (int) ((def - fromWidth) * interpolatedTime + fromWidth);
if (newWidth > def/3) {
view.getLayoutParams().width = newWidth;
view.requestLayout();
}
else{
view.getLayoutParams().width = def/3;
view.requestLayout();
}
}
}
and this one is for changing height:
public class Grow extends Animation
{
int fromHeight;
View view;
int defaultHeight;
public Grow(View view,int height) {
this.view = view;
this.fromHeight = view.getHeight();
defaultHeight=height;
}
@override
public boolean willChangeBounds() {
return true;
}
@override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
@override
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newHeight;
newHeight = (int) (fromHeight / (1 - interpolatedTime));
if (newHeight < defaultHeight * 2) {
view.getLayoutParams().height = newHeight;
view.requestLayout();
}
else{
view.getLayoutParams().height = defaultHeight*2;
view.requestLayout();
}
}
}
and I play them like that:
if(h==0) { //first time the user selects an item =>change width and height
Animation a = new ShrinkList(list);
a.setInterpolator(new LinearInterpolator());
a.setDuration(1000);
list.setAnimation(a);
list.startAnimation(a);
}
Animation a1 = new Grow(view,height); //change only height
a1.setInterpolator(new LinearInterpolator());
a1.setDuration(300);
view.setAnimation(a1);
view.startAnimation(a1);
My problem is that when the list changes its width, the animation is not smooth at all and it only changes the width, not the height too.
How can I make the animation smoother and make the two animations play at once?
I have tried scaling the list, but that scales all the list. basically I need only to resize the textView within ListView nd changing layoutParams does that.
I have tried to insert the code from Grow.java into ShrinkList.Java in orefer to play both animations once and it only played the one from Grow.java.
Click to expand...
Click to collapse
Do it like this
Code:
<RelativeLayout
android:id="@+id/myRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
[B]android:animateLayoutChanges="true"[/B]
<ListView
android:id="@+id/myListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
[B]android:animateLayoutChanges="true"[/B]/>
</RelativeLayout>
Then you only need to change the width and height. And you don't have to worry about the animation
Rick Clephas said:
Do it like this
Code:
<RelativeLayout
android:id="@+id/myRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
[B]android:animateLayoutChanges="true"[/B]
<ListView
android:id="@+id/myListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
[B]android:animateLayoutChanges="true"[/B]/>
</RelativeLayout>
Then you only need to change the width and height. And you don't have to worry about the animation
Click to expand...
Click to collapse
I have tried what you suggested and the relative layout works great, but the list doesn't animate when it changes its witdh
Jaws1992 said:
I have tried what you suggested and the relative layout works great, but the list doesn't animate when it changes its witdh
Click to expand...
Click to collapse
You should add the animate layout changes to the first parent with match_parent as width
Sent from my SM-N9005 using XDA Premium 4 mobile app
Rick Clephas said:
You should add the animate layout changes to the first parent with match_parent as width
Sent from my SM-N9005 using XDA Premium 4 mobile app
Click to expand...
Click to collapse
I created a relative layout as in your example and added animate layout changes to both the listview and that layout and changed the witth of the layout and it doesn't animate the list getting smaller. Only the next relative layout getting bigger after the list is small
Jaws1992 said:
I created a relative layout as in your example and added animate layout changes to both the listview and that layout and changed the witth of the layout and it doesn't animate the list getting smaller. Only the next relative layout getting bigger after the list is small
Click to expand...
Click to collapse
Okey. AnimateLayoutChanges applies to the chields. So if you want to animate the relative layout the listview is in. You need to add animateLayoutChanges to his parent
Sent from my SM-N9005 using XDA Premium 4 mobile app
Rick Clephas said:
Okey. AnimateLayoutChanges applies to the chields. So if you want to animate the relative layout the listview is in. You need to add animateLayoutChanges to his parent
Sent from my SM-N9005 using XDA Premium 4 mobile app
Click to expand...
Click to collapse
I added animate layout changes to all the listView ancestors and it still doesn't animate the listView.
This is the java code for changing width. Maybe I doing something wrong here:
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) linLay.getLayoutParams();
if(sdkVersion>=Build.VERSION_CODES.HONEYCOMB) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
lp.width =size.x/3;
}
else{
Display display = getWindowManager().getDefaultDisplay();
lp.width = display.getWidth()/3;
}
linLay.setLayoutParams(lp); //linLay is the listView parrent and is a relativeLayout
Jaws1992 said:
I added animate layout changes to all the listView ancestors and it still doesn't animate the listView.
This is the java code for changing width. Maybe I doing something wrong here:
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) linLay.getLayoutParams();
if(sdkVersion>=Build.VERSION_CODES.HONEYCOMB) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
lp.width =size.x/3;
}
else{
Display display = getWindowManager().getDefaultDisplay();
lp.width = display.getWidth()/3;
}
linLay.setLayoutParams(lp); //linLay is the listView parrent and is a relativeLayout
Click to expand...
Click to collapse
You have to set the animateLayoutChanges to the paretn of linLay
Sent from my SM-N9005 using XDA Premium 4 mobile app
Rick Clephas said:
You have to set the animateLayoutChanges to the paretn of linLay
Sent from my SM-N9005 using XDA Premium 4 mobile app
Click to expand...
Click to collapse
Yes, I have already done that.As I said, I added animate layout changes to all of the listview ancestosr meaning linLay, parent of linLay, parent of parent of linLay and so on until the root of layouts, but the listView still doesn't animate
Okey could you post your xml
Sent from my SM-N9005 using XDA Premium 4 mobile app
Rick Clephas said:
Okey could you post your xml
Sent from my SM-N9005 using XDA Premium 4 mobile app
Click to expand...
Click to collapse
<LinearLayout
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:animateLayoutChanges="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="70dp"
android:id="@+id/titleLayout"
android:visibility="visible"
android:animateLayoutChanges="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/imagew"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true" />
<LinearLayout
androidrientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_alignRight="@+id/share"
android:layout_alignEnd="@+id/share"
android:layout_toRightOf="@+id/imagew"
android:layout_toEndOf="@+id/imagew"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/title"
android:textSize="40sp"
android:gravity="center|right"
android:layout_gravity="left"
android:textColor="#ffffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/date"
android:textSize="30sp"
android:gravity="clip_horizontal|center|center_horizontal"
android:layout_gravity="right"
android:layout_marginLeft="20dp"
android:textColor="#ffffffff"
android:textStyle="italic" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/share"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="@drawable/share"
android:adjustViewBounds="false"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentEnd="false"
android:layout_centerVertical="true"
android:contentDescription="@string/share" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linLay"
android:animateLayoutChanges="true">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"
android:headerDividersEnabled="false"
android:dividerHeight="0dp"
android:animateLayoutChanges="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:layout_toRightOf="@id/linLay"
android:id="@+id/contentLayout"
android:animateLayoutChanges="true">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="30sp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:visibility="visible"
android:textColor="#ffffffff"
android:typeface="normal"
android:gravity="left|fill" />
<LinearLayout
androidrientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:layout_alignParentEnd="false"
android:layout_alignParentBottom="true"
android:background="#ff647ab1"
android:visibility="gone">
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/imageView"
android:background="@drawable/fb" />
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/FB"
android:id="@+id/fb"
android:textColor="#ffffffff"
android:textStyle="bold"
android:textSize="50sp" />
</LinearLayout>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
Jaws1992 said:
<LinearLayout
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:animateLayoutChanges="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="70dp"
android:id="@+id/titleLayout"
android:visibility="visible"
android:animateLayoutChanges="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/imagew"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true" />
<LinearLayout
androidrientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_alignRight="@+id/share"
android:layout_alignEnd="@+id/share"
android:layout_toRightOf="@+id/imagew"
android:layout_toEndOf="@+id/imagew"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/title"
android:textSize="40sp"
android:gravity="center|right"
android:layout_gravity="left"
android:textColor="#ffffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/date"
android:textSize="30sp"
android:gravity="clip_horizontal|center|center_horizontal"
android:layout_gravity="right"
android:layout_marginLeft="20dp"
android:textColor="#ffffffff"
android:textStyle="italic" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/share"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="@drawable/share"
android:adjustViewBounds="false"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentEnd="false"
android:layout_centerVertical="true"
android:contentDescription="@string/share" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linLay"
android:animateLayoutChanges="true">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"
android:headerDividersEnabled="false"
android:dividerHeight="0dp"
android:animateLayoutChanges="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:layout_toRightOf="@id/linLay"
android:id="@+id/contentLayout"
android:animateLayoutChanges="true">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="30sp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:visibility="visible"
android:textColor="#ffffffff"
android:typeface="normal"
android:gravity="left|fill" />
<LinearLayout
androidrientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:layout_alignParentEnd="false"
android:layout_alignParentBottom="true"
android:background="#ff647ab1"
android:visibility="gone">
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/imageView"
android:background="@drawable/fb" />
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/FB"
android:id="@+id/fb"
android:textColor="#ffffffff"
android:textStyle="bold"
android:textSize="50sp" />
</LinearLayout>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
Click to expand...
Click to collapse
Does the width change?
Sent from my SM-N9005 using XDA Premium 4 mobile app
Rick Clephas said:
Does the width change?
Sent from my SM-N9005 using XDA Premium 4 mobile app
Click to expand...
Click to collapse
Yes. The width of changes, but it doesn't animate
Jaws1992 said:
Yes. The width of changes, but it doesn't animate
Click to expand...
Click to collapse
Okey I don't get it. It should work...
Sent from my SM-N9005 using XDA Premium 4 mobile app

Categories

Resources