Layering images in Android Studio? - java

Currently I have a UI designed with imageviews, the problem is they cannot overlap or be layered. (Or if they can, I haven't figure out how)
I'd like to do 3 layers, the bottom layer is the board,
The next layer is pieces,
The next layer is a selected piece with valid moves.
My idea is that I will have listeners on the top layer only.
I am just looking for the best data structure to use for this, the app is a chesslike game I am working on developing.
If there is another approach that might be better I am all ears as well.
Thanks!
As requested code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Game Activity"
android:id="#+id/textView5"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView22"
android:src="#drawable/ni_whitesquare"
android:layout_below="#+id/textView5"
android:layout_alignRight="#+id/textView5"
android:layout_alignEnd="#+id/textView5"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:layout_marginTop="76dp"
android:contentDescription="whitesquare"
android:clickable="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView32"
android:src="#drawable/ni_blacksquare"
android:layout_alignTop="#+id/imageView22"
android:layout_alignLeft="#+id/imageView22"
android:layout_alignStart="#+id/imageView22"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp"
android:contentDescription="blacksquare"
android:clickable="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView12"
android:src="#drawable/ni_blacksquare"
android:contentDescription="blacksquare"
android:clickable="true"
android:layout_alignTop="#+id/imageView22"
android:layout_toLeftOf="#+id/imageView22"
android:layout_toStartOf="#+id/imageView22" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView21"
android:src="#drawable/ni_blacksquare"
android:contentDescription="blacksquare"
android:clickable="true"
android:layout_below="#+id/imageView22"
android:layout_toRightOf="#+id/imageView12"
android:layout_toEndOf="#+id/imageView12" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView23"
android:src="#drawable/ni_blacksquare"
android:contentDescription="blacksquare"
android:clickable="true"
android:layout_above="#+id/imageView12"
android:layout_toLeftOf="#+id/imageView33"
android:layout_toStartOf="#+id/imageView33" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView31"
android:src="#drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignBottom="#+id/imageView21"
android:layout_alignLeft="#+id/imageView32"
android:layout_alignStart="#+id/imageView32" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView11"
android:src="#drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignTop="#+id/imageView21"
android:layout_toLeftOf="#+id/imageView21"
android:layout_toStartOf="#+id/imageView21" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView13"
android:src="#drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignTop="#+id/imageView23"
android:layout_alignLeft="#+id/imageView12"
android:layout_alignStart="#+id/imageView12" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView33"
android:src="#drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignTop="#+id/imageView23"
android:layout_alignLeft="#+id/imageView32"
android:layout_alignStart="#+id/imageView32" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/directions"
android:id="#+id/directionsstring"
android:layout_marginTop="30dp"
android:layout_below="#+id/imageView21"
android:layout_alignLeft="#+id/textView5"
android:layout_alignStart="#+id/textView5" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Startingpoint"
android:src="#drawable/ni_pawn"
android:layout_above="#+id/imageView11"
android:layout_toLeftOf="#+id/imageView12"
android:layout_toStartOf="#+id/imageView12" />
</RelativeLayout>
Activity :
public class GameActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
final ImageView mStarting = (ImageView) findViewById(R.id.Startingpoint);
final ImageView mCenterSq = (ImageView) findViewById(R.id.imageView22);
final ImageView m12 = (ImageView)findViewById(R.id.imageView12);
mCenterSq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
m12.setImageResource(R.drawable.ni_blacksquare);
mCenterSq.setImageResource(R.drawable.ni_portal);
//center square turns into a portal when clicked.
if (mStarting.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_whitesquare).getConstantState()) &&
mCenterSq.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_portal).getConstantState()) )
{
mCenterSq.setOnClickListener(null);
return;
}
mStarting.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
//Pawn selected
mStarting.setImageResource(R.drawable.ni_whitesquare);
m12.setImageResource(R.drawable.ni_greensquare);
mCenterSq.setImageResource(R.drawable.ni_greensquare);
if (mStarting.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_whitesquare).getConstantState()))
{
mStarting.setOnClickListener(null);
}
m12.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
if (m12.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_greensquare).getConstantState()))
{
m12.setImageResource(R.drawable.ni_pawn);
mCenterSq.setImageResource(R.drawable.ni_portal);
}
}
});
}
});
};
});
}
}

For layering views, you might want to use a FrameLayout see this answer: Layout Layers? Z-Axis?
Also, if you're trying to set up a layout in which you have several consecutive views that need to be lined up one next to the other, the most appropriate element is usually a LinearLayout. LinearLayout will automatically set up your views in consecutive order in the order you declared them, according to the android:orientation attribute you set for it.
Overall, I'd suggest looking at the docs for each of these. RelativeLayout is probably not the way to go since it involves a lot of managing of view positioning which just seems frivolous for what I think you are trying to do. I hope this helps

Related

Avoid clicking of full layout

I only want my textview with id clickable_txtView to be clickable in relative layout. Here is the code -
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#android:id/empty"
android:clickable="false">
<ImageView
android:layout_width="120sp"
android:layout_height="130sp"
android:layout_centerHorizontal="true"
android:src="#drawable/flower"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="120sp"
android:text="Dont click me />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/clickable_txtView"
android:gravity="center"
android:paddingTop="140sp"
android:clickable="true"
android:text="Click me" />
</RelativeLayout>
#OnClick(R.id.clickable_txtView)
public void txtViewClick() {
//Some event
}
Thr problem is I am able to click whole relative layout and some event happens after onclicking relative layout.
How do I avoid this and make it limited to text view only.
Your textview has an attribute android:paddingTop="140sp" which basically wraps over the entire relative layout, because of which you are having the problem with onClick behaviour.
Use this RelativeLayout structure (provide ID to the "don't click me" textview, and place the "clickable textview" below it.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:layout_width="120sp"
android:layout_height="130sp"
android:layout_centerHorizontal="true"
android:src="#mipmap/ic_launcher"
android:layout_alignParentTop="true"
/>
<TextView
android:id="#+id/do_not_click"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="120sp"
android:enabled="false"
android:text="Dont click me" />
<TextView
android:id="#+id/clickable_txtView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center"
android:layout_below="#id/do_not_click"
android:text="Click me" />
</RelativeLayout>
In your activity where you are inflating this view try this:
first initialize your textview
TextView textView = (TextView) findViewById(R.id.clickable_txtView);
then apply clickListner on it
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do your work here
}
});
I'm not sure but you can try adding below line to your parent layout
android:touchscreenBlocksFocus="true"
like
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#android:id/empty"
android:touchscreenBlocksFocus="true">

How can I make better custom dialogs?

I'm creating a game and in it I use a lot of dialogs. The dialogs make up the main menu in a more simple setup then making up an entire activity for it. These dialogs though, they have a grey outline which is really annoying, and in addition when going from one dialog to another, it shrinks down and blows up as one dissapears and another one reappears.
How can I remove the outline and make the transition more smooth? If it is not possible with dialogs, what can an alternative be? I am using custom layouts connected to Java classes that extend Dialog
EDIT
Java:
public class MenuDialog extends Dialog implements View.OnClickListener {
Context con;
Clicker c;
public MenuDialog(Context c, Clicker game) {
super(c);
this.con = c;
this.c = game;
Window window = this.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
show();
}
Button stat, gem, cash, shop, powerup, settings;
#Override
protected void onCreate(Bundle sis){
super.onCreate(sis);
setContentView(R.layout.menu);
setButtons();
}
private void setButtons(){
stat = (Button) findViewById(R.id.bStats);
gem = (Button) findViewById(R.id.bGems);
gem.setOnClickListener(this);
stat.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bStats:
StatDialog sd = new StatDialog(con,c);
sd.show();
break;
case R.id.bGems:
IAPDialog iapd = new IAPDialog(con, c);
iapd.show();
break;
//other buttons
}
dismiss();
}
}
XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:id="#+id/relativeLayout"
android:gravity="center_horizontal"
android:background="#drawable/phone_like_bc"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/linearLayout5"
android:layout_marginTop="51dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bStats"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/bGems"
android:background="#drawable/stat_button"
android:layout_toStartOf="#+id/bGems" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/stats"
android:id="#+id/textView17"
android:layout_below="#+id/bStats"
android:layout_alignLeft="#+id/bStats"
android:layout_alignStart="#+id/bStats" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/bShop"
android:background="#drawable/shop"
android:layout_below="#+id/textView17" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/shop"
android:textAlignment="center"
android:id="#+id/textView18"
android:layout_alignTop="#+id/textView16"
android:layout_alignLeft="#+id/bShop"
android:layout_alignStart="#+id/bShop" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/linearLayout"
android:layout_marginTop="51dp"
>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/gem"
android:id="#+id/bGems"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/linearLayout5"
android:layout_toEndOf="#+id/linearLayout5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/gems"
android:id="#+id/textView15"
android:layout_alignBaseline="#+id/textView20"
android:layout_alignBottom="#+id/textView20"
android:layout_toLeftOf="#+id/bPowerUp"
android:layout_toStartOf="#+id/bPowerUp"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/bPowerUp"
android:background="#drawable/lightning"
android:layout_alignTop="#+id/linearLayout"
android:layout_toLeftOf="#+id/bSettings"
android:layout_toStartOf="#+id/bSettings" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/powerups"
android:id="#+id/textView16"
android:layout_alignBaseline="#+id/bSettings"
android:layout_alignBottom="#+id/bSettings"
android:layout_toLeftOf="#+id/bSettings"
android:layout_toStartOf="#+id/bSettings" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="51dp"
android:gravity="center">
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/bCash"
android:background="#drawable/cash"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text=" " />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/cash"
android:id="#+id/textView19"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/bSettings"
android:background="#drawable/settings"
android:layout_below="#+id/bCash"
android:layout_alignLeft="#+id/bCash"
android:layout_alignStart="#+id/bCash" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/settings"
android:id="#+id/textView20"
android:layout_alignBottom="#+id/bCash"
android:layout_alignLeft="#+id/textView19"
android:layout_alignStart="#+id/textView19" />
</LinearLayout>
</LinearLayout>
There are two more dialog classes both XML and java. I added those two to show that I already know how to add the classes, but I need to know how to remove the outline of the dialog and I need to know how I can smoothen the transition between two dialogs. They have the same background too.
You can use DialogFragments instead of Dialogs, they works as Fragments and you can customize them. Read more about the use of Dialog fragments at https://developer.android.com/reference/android/app/DialogFragment.html
You have a lot of Tutorials to create your own DialogFragments in Google if you don't like the Android Documentation.

Android custom fonts Lollipop

I have two TextView which I want to put a custom font on.
In the OnCreate of my MainActivity, after the setContentView call, I wrote the following code:
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/BernerBasisschrift1.ttf");
TextView welcomeTextView = (TextView)findViewById(R.id.welcome_message);
TextView introTextView = (TextView)findViewById(R.id.introduction_message);
welcomeTextView.setTypeface(myTypeface);
introTextView.setTypeface(myTypeface);
But for some reason, the font stays default.
The app runs on Galaxy S6 Edge device (Lollipop 5.0.2).
I read here that there's a problem with custom fonts on Lollipop, and in order to fix it I converted my font with the tool provided in the thread to ttx and vice versa, but even this didn't help.
Any ideas what can I do?
Edit:
Tested on Jellybean (4.3), not working either.
Another edit:
It works on a clean new project! Cant figure out the difference.
Putting the MainActivity and its' XML for help:
public class MainActivity extends FragmentActivity {
private LoginFragment loginFragment;
private TextView welcomeTextView;
private TextView introTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
hideActionBar();
setContentView(R.layout.activity_main);
welcomeTextView = (TextView)findViewById(R.id.welcome_message);
introTextView = (TextView)findViewById(R.id.introduction_message);
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/BernerBasisschrift1.ttf");
welcomeTextView.setTypeface(myTypeface);
introTextView.setTypeface(myTypeface);
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
loginFragment = new LoginFragment();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, loginFragment, "facebookFragment").commit();
}
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus){
// get all views on screen
ArrayList<View> views = new ArrayList<View>()
{{
add(findViewById(R.id.welcome_message));
add(findViewById(R.id.introduction_message));
add(findViewById(R.id.below_login));
add(findViewById(R.id.above_login));
add(findViewById(R.id.authButton));
add(findViewById(R.id.logo));
}};
// set animations
for(View view : views)
YoYo.with(Techniques.Tada).duration(700).playOn(view);
}
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void hideActionBar(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
getActionBar().hide();
}
And the activity's XML:
<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/main_background">
<ImageView
android:id="#+id/logo"
android:layout_width="256dp"
android:layout_height="101dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:background="#drawable/logo" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp">
<TextView
android:id="#+id/welcome_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/welcome_message"
android:textColor="#color/White"
android:textSize="25sp"
style="#style/ShadowStyleText"
android:gravity="center" />
<TextView
android:id="#+id/introduction_message"
android:layout_below="#+id/welcome_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/introduction_message"
android:textColor="#color/White"
android:textSize="18sp"
android:gravity="center"
style="#style/ShadowStyleText"
android:layout_marginTop="5dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/bottom_login_chunk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:layout_marginStart="50dp"
android:layout_marginLeft="50dp"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp">
<TextView
android:id="#+id/above_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/above_login_button"
android:textColor="#color/White"
android:textSize="18sp"
style="#style/ShadowStyleText"
android:gravity="center" />
<com.facebook.widget.LoginButton
android:id="#+id/authButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/above_login"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp" />
<TextView
android:id="#+id/below_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/authButton"
android:layout_centerHorizontal="true"
android:text="#string/below_login_button"
android:textColor="#color/White"
style="#style/ShadowStyleText"
android:textSize="14sp" />
</RelativeLayout>
</RelativeLayout>
Ok so I found out recently that the facebook login button caused the problem, since everything worked when it was commented out.
So basically to solve this problem use another method to show the button instead using support fragment manager; sdk docs has lots of examples.

How to use scrollTo() with Spinner?

I'm working on my first Android project, which means that I'm not that familiar with Android and Java, and I want to move scroll of ScrollView after a user presses the button for next action (IME_ACTION_SEND). My codes are as follows.
activity_add.xml
It basically consists of TextView, EditText, NumberPicker, Spinner, Button. I want to move the scroll after IME_ACTION_SEND on EditText, so that the NumberPicker is centered on the screen.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/addActivity_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<LinearLayout
android:id="#+id/layout_activity_add"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_addItem"
android:id="#+id/textView"
android:layout_gravity="center_horizontal"
android:textSize="40sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/formLayout"
android:layout_marginTop="50dp">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/title_editText"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="#string/hint_title"
android:singleLine = "true"
android:maxLength="20" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/formLayout3"
android:layout_marginTop="50dp">
<TextView
android:id="#+id/period_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_period"
android:layout_centerHorizontal="true" />
<NumberPicker
android:id="#+id/period_number_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/period_textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</NumberPicker>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/formLayout4"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_category"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_toStartOf="#+id/background_spinner"
android:layout_toLeftOf="#+id/background_spinner" />
<Spinner
android:id="#+id/background_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_gravity="center_vertical">
</Spinner>
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_submit"
android:layout_gravity="center_horizontal"
android:text="#string/label_submit" />
</LinearLayout>
</ScrollView>
AddItemActivity.java
I've just copied the part which I think is relevant.
public class AddSinceItemActivity extends ActionBarActivity {
EditText title;
Spinner spinner;
NumberPicker numberPicker;
String title_string;
ViewGroup linearLayout;
ScrollView addActivity_scrollview;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
addActivity_scrollview = (ScrollView) findViewById(R.id.addActivity_scrollview);
linearLayout = (ViewGroup) findViewById(R.id.layout_activity_add);
title = (EditText) findViewById(R.id.title_editText);
numberPicker = (NumberPicker) findViewById(R.id.period_number_picker);
spinner = (Spinner) findViewById(R.id.background_spinner);
/* For title */
title.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
// I think here is the most important part.
addActivity_scrollview.smoothScrollTo(0, numberPicker.getBottom());
}
return false;
}
});
}
}
For addActivity_scrollview.smoothScrollTo(0, numberPicker.getBottom());,
I've tried many possible codes, such as
addActivity_scrollview.smoothScrollTo(0, spinner.getTop());
addActivity_scrollview.smoothScrollTo(0, 300); (300 is just a random number)
addActivity_scrollview.smoothScrollBy(0, 300);
but the scroll is always stuck (it moves a little but it's always the same position with above codes) and the screen barely shows the selected number of NumberPicker. How can I achieve the goal to set scroll so that the screen shows the entire NumberPicker?
just add this line to your edittext code in xml :android:imeOptions="actionSend" and then just try this into your activityaddActivity_scrollview.smoothScrollTo(0, 480);
I should've added android:imeOptions="actionSend" to <EditText> in order to properly listen on onEditorAction(). I thought it'd be okay with android:singLine="true" because that option enabled "next" action on keyboard.
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/title_editText"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="#string/hint_title"
android:singleLine="true"
android:maxLength="20"
android:imeOptions="actionSend" />

Moving buttons with animation and changing their position afterwards

I am learning Java and Android SDK. I want to create a simple application that is swapping 2 buttons so the left one is moving to the place of right one and v.v. with simple animation. I have tried the ObjectAnimator because I have read that it is moving the views objects permanently. But it's not :-( The objects stays there I mean the left one on the right and v.v. but their getLeft(), getTop() values are the same, and after next animations starts the objects are returning to the start position immediately. I have read that the ObjectAnimator needs some additional function to work properly but in the documentation there is no example :-( I have tried to add the setTranslationX function but it hasn't worked. Could somebody provide me with some simple example of how to do this?
http://developer.android.com/guide/topics/graphics/prop-animation.html#object-animator
"The object property that you are animating must have a setter function (in camel case) in the form of set(). Because the ObjectAnimator automatically updates the property during animation, it must be able to access the property with this setter method. For example, if the property name is foo, you need to have a setFoo() method. If this setter method does not exist, you have three options:
Add the setter method to the class if you have the rights to do so.
Use a wrapper class that you have rights to change and have that wrapper receive the value with a valid setter method and forward it to the original object.
Use ValueAnimator instead."
Thanks in advance.
try this code:
public class ActivityStartup extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("hello");
setContentView(R.layout.main);
SlidingMenu menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.SLIDING_WINDOW);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setBehindOffset(100);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
View view = G.layoutInflater.inflate(R.layout.menu, null);
view.findViewById(R.id.layout_crop).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(G.context, "Crop Clicked", Toast.LENGTH_SHORT).show();
}
});
view.findViewById(R.id.img_logo).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://example.com"));
startActivity(browserIntent);
}
});
menu.setMenu(view);
}
}
public class G extends Application {
public static LayoutInflater layoutInflater;
public static Context context;
#Override
public void onCreate() {
super.onCreate();
layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
context = getApplicationContext();
}
}
main.xml is:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
menu.xml is:
<ImageView
android:id="#+id/img_logo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="8dip"
android:scaleType="centerInside"
android:src="#drawable/hlogo" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#00ff00"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:id="#+id/layout_crop"
android:layout_width="match_parent"
android:layout_height="48dip"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dip"
android:scaleType="centerInside"
android:src="#android:drawable/ic_menu_crop" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Crop"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_day"
android:layout_width="match_parent"
android:layout_height="48dip"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/ImageView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dip"
android:scaleType="centerInside"
android:src="#android:drawable/ic_menu_day" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_delete"
android:layout_width="match_parent"
android:layout_height="48dip"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/ImageView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dip"
android:scaleType="centerInside"
android:src="#android:drawable/ic_menu_delete" />
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
android:textColor="#000000" />
</LinearLayout>

Categories

Resources