How can I make better custom dialogs? - java

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.

Related

Android Java Set OnClick to a ImageView within multiple nested tags

Hi everyone I'm trying to create a menu that comes down when clicking on the profile picture:
but as you can see that ImageView is inside a cardView which is in turn inside a tool bar and as the initial parent I have an App Bar layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primaryPopHome"
android:minHeight="?attr/actionBarSize"
android:theme="#style/AppTheme.bar"
>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="centerInside"
android:src="#drawable/poplogo"
/>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="300dp"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="1.9"
app:cardCornerRadius="80dp">
<ImageView
android:id="#+id/img_profilomain"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:backgroundTint="#color/white"
android:scaleType="centerCrop"
/>
</androidx.cardview.widget.CardView>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.appcompat.widget.ActionMenuView
android:id="#+id/mnuItem"
android:layout_width="match_parent"
android:layout_height="250dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appBarLayout"
android:background="#color/def2"
android:visibility="invisible"
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:src="#drawable/poplike"
android:backgroundTint="#color/redButton2"
app:borderWidth="0dp"
app:fabSize="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<!--Floating action button for add alarm-->
<!--Make sure that you are constraining this
button to the parent button-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add_alarm_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
app:fabSize="normal"
app:layout_constraintBottom_toTopOf="#+id/add_fab"
app:layout_constraintEnd_toEndOf="#+id/add_fab"
app:layout_constraintStart_toStartOf="#+id/add_fab"
app:srcCompat="#drawable/ic_baseline_add_24"
app:borderWidth="0dp"
android:backgroundTint="#color/redButton2"
/>
<!--Action name text for the add alarm button-->
<!--Make sure that you are constraining this Text to
the add Alarm FAB button-->
<TextView
android:id="#+id/add_alarm_action_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Crea Post"
app:layout_constraintBottom_toBottomOf="#+id/add_alarm_fab"
app:layout_constraintEnd_toStartOf="#+id/add_alarm_fab"
app:layout_constraintTop_toTopOf="#+id/add_alarm_fab" />
<!--Floating action button for add person-->
<!--Make sure that you are constraining this
button to the add Alarm FAB button-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add_person_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
app:fabSize="normal"
app:layout_constraintBottom_toTopOf="#+id/add_alarm_fab"
app:layout_constraintEnd_toEndOf="#+id/add_alarm_fab"
app:layout_constraintStart_toStartOf="#+id/add_alarm_fab"
app:srcCompat="#drawable/ic_baseline_search_24"
app:borderWidth="0dp"
android:backgroundTint="#color/redButton2"
/>
<!--Action name text for the add person button-->
<!--Make sure that you are constraining this Text
to the add Person FAB button-->
<TextView
android:id="#+id/add_person_action_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Cerca utenti"
app:layout_constraintBottom_toBottomOf="#+id/add_person_fab"
app:layout_constraintEnd_toStartOf="#+id/add_person_fab"
app:layout_constraintTop_toTopOf="#+id/add_person_fab" />
<Button
android:id="#+id/btn_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="152dp"
android:text="Logout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/appBarLayout"
app:layout_constraintVertical_bias="0.111" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rcViewPostMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="56dp"
app:layout_constraintTop_toBottomOf="#+id/appBarLayout"
tools:layout_editor_absoluteX="-16dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
I tried to set an OnClick listener from the java code but when I go to run the app the onclick doesn't work when I click on the image:
ImageView img_profilo = findViewById(R.id.img_profilomain);
img_profilo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "d", Toast.LENGTH_SHORT).show();
mnuItem.setVisibility(View.VISIBLE);
}
});
I tried to do it with the toolbar too but it doesn't work anyway:
tlb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "sad", Toast.LENGTH_SHORT).show();
}
});
Edit:
It doesn't work with this either
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if(item.getItemId() == R.id.img_profilomain)
Toast.makeText(MainActivity.this, "wds", Toast.LENGTH_SHORT).show();
return true;
}
});
Would anyone know how to solve this problem?

How to Display a variable from Mainactivity in a second Activity's Texview

Im a little stuck, how can i get a variable from main activity to be displayed on a second activity?
a code example would be great.
Also a second Problem:
How can i create a function in mainactivty when a button is pressed in a second activity?
This is what i have so far, but when i press the button in the second activity, the app crashes.
the button's function needs to be able to change a variable's value in MainActivity and Run a toast saying it was selected.
Main Activity
//SETTING THE DRINK SIZE BASED ON POPUP BUTTONS
public int DrinkSize;
public void SetDrinkSize_Small(View view) {
DrinkSize = 1;
Toast Small = Toast.makeText(getApplicationContext(),
"Drink Size Set To Small",
Toast.LENGTH_SHORT);
Small.show();
}
public void SetDrinkSize_Medium(View view) {
DrinkSize = 2;
Toast Medium = Toast.makeText(getApplicationContext(),
"Drink Size Set To Medium",
Toast.LENGTH_SHORT);
Medium.show();
}
public void SetDrinkSize_Large(View view) {
DrinkSize = 3;
Toast Large = Toast.makeText(getApplicationContext(),
"Drink Size Set To Large",
Toast.LENGTH_SHORT);
Large.show();
}
CustomPopUp.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:background="#color/Orange"
android:gravity="center_horizontal"
android:orientation="vertical"
android:onClick="SetDrinkSize_Small">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small"
android:textColor="#color/White"
android:textSize="18dp"
android:textStyle="bold" />
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="15dp"
android:src="#drawable/drop" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:background="#color/Green"
android:gravity="center_horizontal"
android:orientation="vertical"
android:onClick="SetDrinkSize_Medium">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium"
android:textColor="#color/White"
android:textSize="18dp"
android:textStyle="bold" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="15dp"
android:src="#drawable/drop" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:background="#color/Orange"
android:gravity="center_horizontal"
android:orientation="vertical"
android:onClick="SetDrinkSize_Large">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large"
android:textColor="#color/White"
android:textSize="18dp"
android:textStyle="bold" />
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="15dp"
android:src="#drawable/drop" />
</LinearLayout>
You can easily send and receive data using intents, as you can see in this Android tutorial: Intent tutorial.
And in case you want to send some data back to the first one, you can use this post: Sending info back.

Force ScrollView at Bottom on Start of Activity

I have created a chatroom where user can discuss on several Topic. I'm using a Scrollview inside RelativeLayout which contains chatConversation
The Porblem is when user opens app then he has to scroll to the Bottom HImself to chat or view the new chat discussion.
How can I force scrollview to start at Bottom when activity is started..
Below is my layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="#style/AppTheme.NoActionBar"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
android:id="#+id/main_chat"
app:titleTextAppearance="#style/Toolbar.TitleText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#F0791F"
android:theme="#style/ThemeOverlay.AppCompat.Light"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="#drawable/ic_people_outline_black_24dp"/>
<Button
android:id="#+id/btn_send"
android:layout_width="42dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/ic_send_black_24dp" />
<EditText
android:id="#+id/msg_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/btn_send"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/btn_send"
android:layout_toStartOf="#+id/btn_send"
android:background="#null"
android:hint="Write a message" />
<ScrollView
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView"
android:transcriptMode="alwaysScroll"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/btn_send"
android:layout_below="#+id/main_chat"
android:layout_alignRight="#+id/btn_send"
android:layout_alignEnd="#+id/btn_send">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="2dp"
android:layout_weight="2"
android:autoLink="all" />
</ScrollView>
<View
android:layout_above="#+id/msg_input"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#android:color/darker_gray"/>
</RelativeLayout>
I already tried using
final ScrollView scrollLayout = ((ScrollView) findViewById(R.id.scrollView));
scrollLayout.post(new Runnable() {
#Override
public void run() {
scrollLayout.fullScroll(ScrollView.FOCUS_DOWN);
}
});
But it doesn't work either.
Please Help
How are you displaying the messages? With a While Loop?
Last line of the while loop should be...
mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
mScrollView or whatever name you gave to the ScrollView
I think you're appending the messages in a single TextView (correct me if I'm wrong).
Check if you are invoking fullScroll() after you are finished appending all the text in your TextView.
for (String oneMessage : messageList){
myTextView.append(oneMessage);
myTextView.append("\n");
....
....
}
//Scroll to bottom
scrollLayout.post(new Runnable() {
#Override
public void run() {
boolean success = scrollLayout.fullScroll(ScrollView.FOCUS_DOWN);
Log.d("Auto Scroll", success ? "Done" : "Failed");
}
});
Let me know if this fixed your issue!

Layering images in Android Studio?

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

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" />

Categories

Resources