Audio in Android Studio stop button issue (Kotlin) - android-studio

currently studying Kotlin and created an app that has 7 buttons (6 are playing certain audio and 7th is the stop button).When i play the first audio the audio starts and when i press stop button the audio stops, but the issue comes if i play the first audio and then the second and the third at same time , the stop button works only for the first audio and the rest keep playing at same time.Thanks for the help in advance.
This is the main activity & xml:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var layout = findViewById<RelativeLayout>(R.id.parent_layout)
val animation = layout.background as AnimationDrawable?
animation?.setEnterFadeDuration(3000)
animation?.setExitFadeDuration(1000)
animation?.start()
}
fun PlaySoundOnButtons(view: View){
val sound = view as Button
val mediaPlayer = MediaPlayer.create(this#MainActivity,resources.getIdentifier(sound.tag as String,"raw",packageName))
mediaPlayer.start()
if (mediaPlayer.isPlaying) {
sound.setBackgroundColor(Color.RED)
}
mediaPlayer.setOnCompletionListener { sound.setBackgroundColor(Color.WHITE)
}
val stopSound = findViewById(R.id.stop_btn) as Button
stopSound.setOnClickListener {
mediaPlayer.stop()
if (mediaPlayer.isPlaying){
mediaPlayer.stop()
}
}
}
}
**XML File**:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/parent_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/mix_colours"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="30dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:onClick="PlaySoundOnButtons"
android:tag="one"
android:text="Hip Hop"
android:textColor="#color/black"
android:textSize="20sp"
app:backgroundTint="#color/white" />
<Button
android:id="#+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:onClick="PlaySoundOnButtons"
android:tag="two"
android:text="Rap"
android:textColor="#color/black"
android:textSize="20sp"
app:backgroundTint="#color/white" />
<Button
android:id="#+id/button6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:onClick="PlaySoundOnButtons"
android:tag="three"
android:text="Jazz"
android:textColor="#color/black"
android:textSize="20sp"
app:backgroundTint="#color/white" />
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:onClick="PlaySoundOnButtons"
android:tag="four"
android:text="Agressive rap"
android:textColor="#color/black"
android:textSize="20sp"
app:backgroundTint="#color/white" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:onClick="PlaySoundOnButtons"
android:tag="five"
android:text="Old school"
android:textColor="#color/black"
android:textSize="20sp"
app:backgroundTint="#color/white" />
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:onClick="PlaySoundOnButtons"
android:tag="six"
android:text="Underground rap"
android:textColor="#color/black"
android:textSize="20sp"
app:backgroundTint="#color/white" />
<Button
android:id="#+id/stop_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop"
android:soundEffectsEnabled="false"
app:backgroundTint="#color/design_default_color_error" />
</LinearLayout>
</ScrollView>
</RelativeLayout>```

if you want playing single audio you need declare the mediaPlayer outside of your click function and the function of stop button to onCreate
lateinit var mediaPlayer:MediaPlayer
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var layout = findViewById<RelativeLayout>(R.id.parent_layout)
val animation = layout.background as AnimationDrawable?
animation?.setEnterFadeDuration(3000)
animation?.setExitFadeDuration(1000)
animation?.start()
val stopSound = findViewById(R.id.stop_btn) as Button
stopSound.setOnClickListener {
if (::mediaPlayer.isInitialized && mediaPlayer.isPlaying){
mediaPlayer.stop()
}
}
}
fun PlaySoundOnButtons(view: View){
val sound = view as Button
if (::mediaPlayer.isInitialized && mediaPlayer.isPlaying){
mediaPlayer.stop()
}
mediaPlayer = MediaPlayer.create(this#MainActivity,resources.getIdentifier(sound.tag as String,"raw",packageName))
mediaPlayer.start()
if (mediaPlayer.isPlaying) {
sound.setBackgroundColor(Color.RED)
}
mediaPlayer.setOnCompletionListener {
sound.setBackgroundColor(Color.WHITE)
}
}

All your buttons call the same click listener, which creates a new MediaPlayer, starts it playing, and then sets the stop button to stop that MediaPlayer instance.
So the problem is you have one stop button, and it can only stop the last media player that was assigned to it. Every time a new click happens a new player is created, and the stop button forgets whatever player it was handling before. Even if you click the same button twice.
You need to keep track of all your current MediaPlayer instances, so your stop button can call stop() on all of them. And really, you need to keep track of them anyway, so you can call release() and free up resources
When done with the MediaPlayer, you should call release(), to free the resources. If not released, too many MediaPlayer instances will result in an exception.
You might want to consider keeping a map of them, and when your click listener is called, try to get the media player associated with the view that was clicked, creating a new one for that view if one doesn't exist. Then you can iterate over them all to stop (and maybe call release on them while you're at it)

Related

xml layout won't display on emulator

I've been working on an app, all the code seems to be fine, xml and java. When i run the app in an emulator, my splash screen is no longer running, when i click on a button, it takes me to the right page but does not show me the layout of the file.
When i edit the xml file layout, the changes are not being shown on the app when it runs in the emulator.
i'll post the code below:
This code is for the xml layout of the page:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bgapp"
tools:context=".complain">
<ImageView
android:layout_width="310dp"
android:layout_height="86dp"
android:layout_marginLeft="45dp"
android:src="#drawable/bfcfulllogotransparent" />
<ImageView
android:layout_width="79dp"
android:layout_height="76dp"
android:layout_marginLeft="160dp"
android:layout_marginTop="500dp"
android:src="#drawable/bfchandtransparent"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Making a Complaint"
android:textAllCaps="false"
android:layout_marginTop="75dp"
android:textAlignment="center"
android:textColor="#ffff"
android:textStyle="bold"
android:textSize="18dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginHorizontal="10dp"
android:textAlignment="center"
android:textColor="#ffff"
android:textAllCaps="false"
android:text="There may be times when you feel that you are not happy with the services you have been provided, it is your right to be able to make a complaint about these issues.
"
android:fontFamily="sans-serif"
android:textSize="16dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="190dp"
android:layout_marginHorizontal="10dp"
android:textAlignment="center"
android:textColor="#ffff"
android:textAllCaps="false"
android:text="There are a number of ways that you can make a complaint…"
android:fontFamily="sans-serif"
android:textSize="16dp"/>
<Button
android:id="#+id/butthow"
android:layout_width="273dp"
android:layout_height="66dp"
android:layout_marginLeft="55dp"
android:layout_marginTop="280dp"
android:background="#drawable/custom_button"
android:text="How to make a complaint"
android:textAllCaps="false"
android:textColor="#ffff"
android:textSize="18dp"
android:textStyle="bold"/>
<Button
android:id="#+id/buttwhat"
android:layout_width="273dp"
android:layout_height="66dp"
android:layout_marginLeft="55dp"
android:layout_marginTop="380dp"
android:background="#drawable/custom_button"
android:text="What happens to your complaint"
android:textAllCaps="false"
android:textColor="#ffff"
android:textSize="18dp"
android:textStyle="bold"/>
</RelativeLayout>
This code is for the xml for the splash screen:
<?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"
android:background="#drawable/bgapp"
tools:context=".splashscreen">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:text="Childrens Guide"
android:textColor="#ffff"
android:textSize="35dp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView4" />
<TextView
android:id="#+id/textView4"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="214dp"
android:text="A guide for children living in foster care"
android:textAlignment="center"
android:textColor="#ffff"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/imageView5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="350dp"
android:layout_height="100dp"
android:layout_marginStart="15dp"
android:layout_marginTop="27dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="133dp"
android:src="#drawable/bfcfulllogotransparent"
app:layout_constraintBottom_toTopOf="#+id/imageView6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="101dp"
android:layout_height="0dp"
android:layout_marginBottom="41dp"
android:src="#drawable/bfchandtransparent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="1000dp"
android:layout_height="200dp"
android:layout_marginStart="18dp"
android:layout_marginEnd="18dp"
android:layout_marginBottom="173dp"
android:src="#drawable/coverkids"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView4" />
</androidx.constraintlayout.widget.ConstraintLayout>
this is the java for the splashscreen:
package com.example.bfcchildrensguidenew;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class splashscreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
Thread myThread = new Thread() {
#Override
public void run() {
try {
sleep(3000 );
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
myThread.start();
}
}
You can not call startActivity method outside of main thread(UI thread), it will crash.
User handler instead a Thread.
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
Intent intent = new Intent (getApplicationContext(),MainActivity.this);
startActivity(intent);
}
});

Why my Android setOnItemClickListener doesn't work?

I have problem in setting setOnItemClickListener. The following is my code. I've tested that setAdapter worked and the list and items were shown on the UI. When it came to setting setOnItemClickListener, it didn't work.
cool_simpleAdapter = new SimpleAdapter(this, items,
R.layout.mylistitem, new String[] { "title", "link" }, new int[] {
R.id.textView_title, R.id.textView_link });
cool_listView.setAdapter(cool_simpleAdapter);
Log.d("tag_1", "before setOnItemClickListener");
cool_listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.d("tag_setonItemClick", "in onItemClick");
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
Log.d("tag_2", "after setOnItemClickListener");
I put the log to trace what happened:
Log.d("tag_1","before setOnItemClickListener");
and
Log.d("tag_2","after setOnItemClickListener");
were displayed but
Log.d("tag_setonItemClick","in onItemClick");
were not displayed. And I cannot click on the item, neither open the URL. I don't know how should I fix the problem.
Edit: add mylistitem.xml layout
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView_link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
in this condition your Button is focused so listiten setOnItemClickListener not working..so make them focusable false..
add this line in xml of Button on
android:focusable="false"
I had this problem before, that was because each item of listview had a Button.
to solve this just need to set android:focusable of the button to false.
I hope this will help you

ListView row height and another interesting issue of selective focus (when the parent is scrolled)

First of all, excuse me if it sounds duplicate. I have visited so many threads but couldn't find a suitable answer of my problem. I have tried ScrollingMovementMethod, android:scrollbar, wrap_content in the parent and many other things suggested in those threads but nothing worked for me. Feel free to edit the title as I couldn't find a better one.
Problem Description
I have a list view and each row of the listview has three controls 1. Image View (to Show the contact image) 2. TextView (to show the Contact name) 3. TextView (to show the status message of the contact (if available)).
What I have tried:-
1. XML layout ,
<?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" >
<ImageView
android:id="#+id/icon"
android:layout_width="22dp"
android:layout_height="50dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="15dp"
android:contentDescription="#string/strBuddyImage" >
</ImageView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="56dp" >
<TextView
android:id="#+id/uNameTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/customMsg"
android:minHeight="?android:attr/listPreferredItemHeight"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="#dimen/fontSizeListRowHeader" />
<TextView
android:id="#+id/customMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:maxLines="4"
android:textSize="#dimen/fontSizeListRowText"
android:textStyle="italic" />
</RelativeLayout>
</LinearLayout>
Adapter Which extends ArrayAdapter (which only overrides the getView method)
static class ViewHolder {
public ImageView imageView;
public TextView uNameTxtView;
public TextView custMsgTxtView;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// Log.d("BuddyListAdapter", "Inside getView() " + position);
if ( rowView == null ) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.buddy_listview_row_layout, parent, false);
viewHolder = new ViewHolder();
viewHolder.uNameTxtView = (TextView) rowView.findViewById(R.id.uNameTxt);
viewHolder.imageView = (ImageView) rowView.findViewById(R.id.icon);
viewHolder.custMsgTxtView = (TextView) rowView.findViewById(R.id.customMsg);
rowView.setTag(viewHolder);
}
viewHolder = (ViewHolder) rowView.getTag();
synchronized (buddyList) {
buddy = buddyList.get(position);
}
viewHolder.uNameTxtView.setText(buddy.getDisplayName());
viewHolder.custMsgTxtView.setText(buddy.getCustomMessage());
// Change the icon for users who are offline
if ( buddy.getState() == 0 ) {
viewHolder.imageView.setImageResource(R.drawable.offline);
} else {
viewHolder.imageView.setImageResource(R.drawable.online);
}
// rowView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
return rowView;
}
What I get
Desired
I want to show all the rows of list view of same size and if someone has a bigger Status message (Which needs multiple lines) I would like to show the name of the person and first few lines (say 4) of the status message. No matter how big the status message is name should be always visible to the user.. How can I do that ?
Please point if any other mistake you find in the code.
One thing i noticed is you use a lot of hardcoded dimensions,is that really necesary? using wrap_content would be better practice on most layouts,that way you let android handle it.
As far as your question is concerned you can do two things:
1)Your two EditTexts are inside a relative layout,and if customMsg is 4 lines long,it will get scrolled ,and you'll loose your uNameTxt so you should limit customMsg more,to something like two lines,or less. OR if you are ok with loosing some parts of the code you could adapt your layout this way
<TextView
android:id="#+id/uNameTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout_alignParentTop="true"
android:minHeight="?android:attr/listPreferredItemHeight"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="#dimen/fontSizeListRowHeader" />
<TextView
android:id="#+id/customMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/uNameTxt"
android:maxLines="4"
android:textSize="#dimen/fontSizeListRowText"
android:textStyle="italic" />
it should scroll down no more and you should see Name and a part of customMsg
2)Edit your layout to something more efficient like:
<?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:minHeight="56dp">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="15dp"
android:contentDescription="sasd" >
</ImageView>
<TextView
android:id="#+id/uNameTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:singleLine="true"
android:layout_toRightOf="#+id/icon"
android:textSize="#dimen/fontSizeListRowHeader"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="#+id/customMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/uNameTxt"
android:maxLines="2"
android:layout_toRightOf="#+id/icon"
android:textSize="#dimen/fontSizeListRowText"
android:textStyle="italic" />
</RelativeLayout>
I agree with sokie on the use of wrap_content. I tend to use LinearLayout a lot since it provides a consistent layout on various display sizes. One way of implementation is as follows. By replacing your inner RelativeLayout to LinearLayout, your text view containing the name will always be displayed and the message text view will display the maximum possibles lines (or can be limited by specifying the maxLines) with the available remaining/space. If you would like to display the whole status message, then you can achieve this by changing the parent LinearLayout height parameter to wrap_content.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="56dp"
android:minHeight="?android:attr/listPreferredItemHeight">
<ImageView
android:id="#+id/icon"
android:layout_width="22dp"
android:layout_height="50dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="15dp" >
</ImageView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/uNameTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="#+id/customMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="4"
android:textStyle="italic"
/>
</LinearLayout>
</LinearLayout>

Android input boxes on the next line

Okay, so I decided to take the Google classes for learning droid devving. I decided instead of the simple input box and button I would make multiple ones, and have each display a set color. The issue is it is jamming all input boxes on one line, rather than their own separate lines. This is what I have in the activity_color.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal" >
<EditText android:id="#+id/edit_message_blue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message_blue" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send_blue"
android:onClick="sendMessageBlue" />
<EditText android:id="#+id/edit_message_orange"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message_orange" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send_orange"
android:onClick="sendMessageOrange" />
</LinearLayout>
This is the main java class
package com.example.color.texts;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class ColorTexts extends Activity {
public final static String EXTRA_MESSAGE = "com.example.colortexts.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_texts);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_color_texts, menu);
return true;
}
/** Called when the user clicks the Send button for Blue */
public void sendMessageBlue(View view) {
Intent intent = new Intent (this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message_blue);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
/** Called when the user clicks the Send button for Red */
public void sendMessageRed(View view) {
Intent intent = new Intent (this, DisplayMessageActivityRed.class);
EditText editText = (EditText) findViewById(R.id.edit_message_red);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
/** Called when the user clicks the Send button for Orange */
public void sendMessageOrange(View view) {
Intent intent = new Intent (this, DisplayMessageActivityOrange.class);
EditText editText = (EditText) findViewById(R.id.edit_message_orange);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
Now I guess the question is how to either tell it to start a new line, like android:layout_next="1" or something (I KNOW THAT DOESN'T EXIST), or would I have to add to the public class? Such as make another layout file and reference to that? I highly doubt the latter would work.
EDIT: Per the instructions laid out, I think this is how I was supposed to do it, but it only know shows the first line :(
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText android:id="#+id/edit_message_blue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message_blue" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send_blue"
android:onClick="sendMessageBlue" />
</LinearLayout>
<LinearLayout 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:orientation="horizontal" >
<EditText android:id="#+id/edit_message_red"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message_red" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send_red"
android:onClick="sendMessageRed" />
</LinearLayout>
<LinearLayout 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:orientation="horizontal" >
<EditText android:id="#+id/edit_message_orange"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message_orange" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send_orange"
android:onClick="sendMessageOrange" />
</LinearLayout>
</LinearLayout>
Your parent layout is set to be android:orientation="horizontal". This needs to be set to android:orientation="vertical".
EDIT: Here is the reference in the Android docs:
http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:orientation

Android - UI Disappear after startActivity

I'm starting an simple ACTION.VIEW activity to show up the web browser.
But when the user press the "back" key, it returns to my initial application. The application is working perfectly except that all the main UI elements have disappeared.
Anyone knows why ?
Here's how I start the web browser :
//Go to web page
ImageButton web = (ImageButton) _gears.findViewById(R.id.Web);
web.setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick(View v)
{
try
{
String url = "http://apps.toonboom.com/flip-boom-lite-ipad";
Intent i = new Intent(Intent.ACTION_VIEW);
Uri u = Uri.parse(url);
i.setData(u);
_mainActivity.startActivity(i);
}
catch (ActivityNotFoundException e)
{
// Raise on activity not found
Toast.makeText(_mainActivity.getApplicationContext(), "Browser not found.", Toast.LENGTH_SHORT);
}
}
});
When coming back from that browser page, onStart() and onResume() are called normally. What I don't understand is that the back and home button lifecycle works perfectly. The user can manually go away of the app and come back without any UI problems. The problem only occurs when coming back from that startActivity() call ?
Also, I don't need to retain any specific UI values....I just want them to be present in the layout ;)
EDIT
I have a gles view that I use to draw and I think it display itself in front of the other UI elements....But I don't understand why if that is the case....
Here's a piece of the xml and the onCreate method :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/MainLayout">
<!-- This is a dummy layout so we can add our custom GlView dynamically to this relative position -->
<LinearLayout
android:id="#+id/SurfaceViewLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"/>
<!-- This is a dummy layout so we can add the timeline dynamically to this relative position -->
<HorizontalScrollView
android:id="#+id/TlScroller"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="0dip"
android:padding="0dip"
android:scrollbars="none"
android:fillViewport="false"
android:scrollbarFadeDuration="0"
android:scrollbarDefaultDelayBeforeFade="0"
android:fadingEdgeLength="0dip"
android:scaleType="centerInside">
<!-- HorizontalScrollView can only host one direct child -->
<LinearLayout
android:id="#+id/TimelineContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_margin="0dip"
android:padding="0dip"
android:scaleType="centerInside"/>
</HorizontalScrollView >
<RelativeLayout
android:id="#+id/BottomTools"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/TlScroller" >
<ImageButton
android:id="#+id/PaletteBtn"
android:layout_width="30dip"
android:layout_height="30dip"
android:background="#android:color/transparent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dip"
android:layout_marginBottom="60dip"
android:src="#drawable/palette44x44"
android:scaleType="centerInside"/>
<RelativeLayout
android:id="#+id/PadLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:src="#drawable/pad_btn"
android:scaleType="centerInside">
<ImageButton
android:id="#+id/PadBtn"
android:layout_width="75dip"
android:layout_height="75dip"
android:background="#android:color/transparent"
android:layout_centerInParent="true"
android:src="#drawable/pad_btn"
android:scaleType="centerInside"/>
<TextView
android:id="#+id/FrameCounter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0dip"
android:padding="0dip"
android:layout_centerInParent="true"
android:textColor="#000000"
android:text="#/x"/>
</RelativeLayout>
<ImageButton
android:id="#+id/PreviousBtn"
android:layout_width="40dip"
android:layout_height="40dip"
android:background="#android:color/transparent"
android:layout_toLeftOf="#+id/PadLayout"
android:layout_alignParentBottom="true"
android:layout_marginBottom="15dip"
android:src="#drawable/previous_btn"
android:scaleType="centerInside"
android:scaleHeight="30%"
android:scaleWidth="30%"/>
// Goes like that with other ImageButton till the end bracket
= = =
#Override protected void onCreate(Bundle icicle)
{
super.onCreate(icicle);
//Set the main layout element
setContentView(R.layout.ui);
// Then most buttons are created like this . . .
//Create the tools pop-up menu
ImageButton toolBtn = (ImageButton) this.findViewById(R.id.CurrentToolBtn);
_tools = new ToolsPopup(toolBtn);
}
When you launching a web browser it uses a lot of memory so android killing your app, when it is going back to activity it recreates it. So you should save your dynamically added views using onsaveinstancestate and onrestoreinstancestate.
onSaveInstanceState () and onRestoreInstanceState ()

Resources