Progressbar is not changing progress in loop - android-studio

I have problem with changing status in my progress bar on my app.
I have while loop which is changing status from 1-100 every 100ms.
Loop works cause when I'm making Log checker which shows how much is actually equals "I" it's from 1-100.
When i'm putting manualy progress to for example 50 it shows but nothing change.
class StartActivity : AppCompatActivity() {
private var progressBar: ProgressBar? = null
private val handler = Handler()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_start)
progressBar = findViewById<ProgressBar>(R.id.progressBar_activityStart)
startProgressBar()
}
private fun startProgressBar() {
progressBar!!.visibility = View.VISIBLE
var i: Int = progressBar!!.progress
progressBar!!.progress = 0
Thread {
while (i < 100) {
i += 1
handler.post {
progressBar!!.progress = i
Log.v("progress", i.toString())
}
try {
progressBar!!.progress = i
Thread.sleep(100)
} catch (e: InterruptedException) {
e.printStackTrace()
}
}
progressBar!!.visibility = View.INVISIBLE
}.start()
}
<?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="#color/white"
tools:context=".StartActivity">
<ImageView
android:id="#+id/imageView_activityStart_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:layout_centerInParent="true"
android:src="#drawable/logohorizontal" />
<ProgressBar
android:id="#+id/progressBar_activityStart"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView_activityStart_logo"
android:layout_alignBottom="#+id/imageView_activityStart_logo"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="30dp"
android:indeterminate="false"
android:visibility="visible" />
</RelativeLayout>

i had the same problem, then i changed
<ProgressBar
android:id="#+id/progressBar_activityStart"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible" />
to this:
<android.widget.ProgressBar
android:id="#+id/progressBar_activityStart"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible" />
then its worked. dont know what the problem is.

You can try to use androidx.core.widget.ContentLoadingProgressBar.

Related

android studio: SnackBar is beneath visable screen

i am having trouble with the Snackbar. I recently moved to Kotlin, Electric Eel and Material design 3. So it is a problem to pinpoint the source of the problem.
So in my main activity i have this XML
<?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:id="#+id/rootContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/barLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="#+id/barLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout="#layout/fragment_dummy" />
</androidx.constraintlayout.widget.ConstraintLayout>
Then in my Kotlin MainActivity.kt i do this:
fun deleteItem(position: Int) {
val lDeletedItem = mAdapter.removeItem(position) ?: return
val message = Snackbar.make(binding.rootContainer, R.string.DialogDeleteTripMessage, Snackbar.LENGTH_LONG)
message.setAction(R.string.DialogDeleteTripButton) { view ->
mAdapter.addItem(position,
lDeletedItem
)
}
message.setActionTextColor(Convertor.ResourceToColorInt(this, R.attr.colorSnackBarAction))
message.addCallback(object : Snackbar.Callback() {
override fun onDismissed(transientBottomBar: Snackbar, event: Int) {
if (event == DISMISS_EVENT_TIMEOUT) {
mViewModel.deleteItem(lDeletedItem.id)
}
}
})
message.show()
}
Everything works just fine but the Snackbar is display outside the visible screen:
Anybody an idea? did the same thing before and then is was working but changed so many things that i am lost

Kotlin Cannot Inflate Layout

I am building an application that has a GridView that contains a dynamic amount of CardViews. I am having issues accessing the TextView elements in the individual items layout from the custom adapter that I am creating.
tvCastleName and tvShieldTime are unresolved references, and the application will not compile.
main.xml
<?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">
<TextView
android:id="#+id/tvNickname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:text="Nickname"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textSize="24sp"
app:layout_constraintTop_toTopOf="parent" />
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/tvNickname"
android:horizontalSpacing="6dp"
android:verticalSpacing="6dp"
android:numColumns="3"
android:id="#+id/gvCastles" />
items.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
android:layout_margin="5dp"
app:cardElevation="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvCastleName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:fontFamily="sans-serif-medium"
android:text="Castle Name"
android:gravity="center"
android:textAppearance="#style/TextAppearance.AppCompat.Large" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/tvCastleName"
android:text="15:00"
android:id="#+id/tvShieldTime"
android:gravity="center" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
GridAdapter.kt
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
class GridAdapter(var nameList: ArrayList<Castle>, var context: Context?) : BaseAdapter() {
override fun getCount(): Int = nameList.size
override fun getItem(position: Int): Castle = nameList[position]
override fun getItemId(position: Int): Long {
TODO("Not yet implemented")
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val nameList = this.nameList[position]
var inflater = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val castleView = inflater.inflate(R.layout.items, null)
castleView.tvCastleName.text = nameList.name!!
castleView.tvShieldTime.text = "15:00"
return castleView
}
}
You must be getting Unresolved Reference as the method (Kotlin Synthetics) you are using to access view is now deprecated. You can use ViewBinding to access your views.
You've to make a few changes to your getItem method, to make it work with ViewBinding
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val nameList = this.nameList[position]
val itemsBinding = ItemsBinding.inflate(LayoutInflater.from(parent.context), parent, false)
itemsBinding.tvCastleName.text = nameList.name!!
itemsBinding.tvShieldTime.text = "15:00"
return itemsBinding.root
}

Adding view dynamically in fragment not persist after moving back from another fragment through navigation

I'm adding view dynamically in fragment which works fine but when I'm navigating to another fragment and return back to first fragment all layout is set as initial one (dynamically added view is not present). So here I have to save UI state untill my fragment's parent activity is not closed.
Initial Fragment Class Code
package com.hp.billingapp.ui.billfragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.textfield.TextInputEditText;
import com.hp.billingapp.R;
import com.hp.billingapp.ui.BillingActivity;
public class OrderFragment extends Fragment {
Button pay, buttonAdd;
LinearLayout itemLayout;
Integer item = 1, emptyChk;
Float total;
ConstraintLayout constraintLayout;
Integer layoutId;
public OrderFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_order, container, false);
pay = root.findViewById(R.id.pay);
pay.setOnClickListener(v -> {
datafetch(root);
});
itemLayout = root.findViewById(R.id.itemLayout);
constraintLayout = root.findViewById(R.id.constraintLayout);
buttonAdd = root.findViewById(R.id.button);
buttonAdd.setOnClickListener(v -> addView());
return root;
}
private void datafetch(View root) {
Log.i("heetcount", String.valueOf(itemLayout.getChildCount()));
emptyChk = 0;
total = (float) 0;
for (int i = 0; i < itemLayout.getChildCount(); i++) {
View itemView = itemLayout.getChildAt(i);
TextView number = itemView.findViewById(R.id.number);
AutoCompleteTextView name = itemView.findViewById(R.id.namet);
TextInputEditText price = itemView.findViewById(R.id.pricet);
AutoCompleteTextView type = itemView.findViewById(R.id.typet);
if (name.getText().toString().isEmpty() || price.getText().toString().isEmpty() ||
type.getText().toString().isEmpty()) {
Snackbar.make(constraintLayout, "Please fill the required fields",
Snackbar.LENGTH_SHORT).show();
emptyChk += 1;
} else {
total = total + Float.parseFloat(price.getText().toString().trim());
}
}
if (emptyChk == 0) {
Log.i("heettotal", total.toString());
OrderFragmentDirections.ActionOrderFragmentToBillingFragment action =
OrderFragmentDirections.actionOrderFragmentToBillingFragment(total);
Navigation.findNavController(root).navigate(action);
((BillingActivity) getActivity()).heading("b");
}
}
private void addView() {
item = item + 1;
String text = "Item " + item;
View itemView = getLayoutInflater().inflate(R.layout.item_layout, null, false);
TextView number = itemView.findViewById(R.id.number);
ImageButton remove = itemView.findViewById(R.id.remove);
AutoCompleteTextView nametiet = itemView.findViewById(R.id.namet);
AutoCompleteTextView typetiet = itemView.findViewById(R.id.typet);
TextInputEditText pricetiet = itemView.findViewById(R.id.pricet);
number.setText(text);
remove.setOnClickListener(v -> removeView(itemView));
itemLayout.addView(itemView);
}
private void removeView(View itemView) {
Log.i("heetview", String.valueOf(itemView));
itemLayout.removeView(itemView);
}
}
Initial Fragment Layout Code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backdropcenter"
android:theme="#style/Theme.MaterialComponents.NoActionBar"
tools:context=".ui.billfragment.OrderFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="80dp"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/itemLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/item_layout" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:background="#drawable/allcornerround"
android:fontFamily="#font/montserrat"
android:text="Add Item"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/pay"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#drawable/allcornerround"
android:fontFamily="#font/montserrat"
android:text="Pay"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Dynamically Added Layout Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:theme="#style/Theme.MaterialComponents.NoActionBar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="horizontal">
<TextView
android:id="#+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:text="Item 1"
android:textColor="#191b20"
android:textSize="18sp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/remove"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#00000000"
android:src="#drawable/ic_close"
app:tint="#191b20" />
</LinearLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/nametil"
style="#style/InputDropDown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:elevation="5dp"
android:hint="Item Name"
app:boxBackgroundMode="filled"
app:endIconMode="dropdown_menu"
app:endIconTint="#AA191b20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/nametil">
<AutoCompleteTextView
android:id="#+id/namet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/montserrat"
android:gravity="center_vertical"
android:inputType="none"
android:textColor="#191b20"
android:textCursorDrawable="#drawable/cursor"
android:textSize="18sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/typetil"
style="#style/InputDropDown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:elevation="5dp"
android:hint="Type"
app:boxBackgroundMode="filled"
app:endIconMode="dropdown_menu"
app:endIconTint="#AA191b20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/nametil">
<AutoCompleteTextView
android:id="#+id/typet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/montserrat"
android:gravity="center_vertical"
android:inputType="none"
android:textColor="#191b20"
android:textCursorDrawable="#drawable/cursor"
android:textSize="18sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/pricetil"
style="#style/InputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:elevation="5dp"
android:hint="Price"
app:boxBackgroundMode="filled"
app:endIconMode="clear_text"
app:endIconTint="#AA191b20">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/pricet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/montserrat"
android:gravity="center_vertical"
android:inputType="numberDecimal"
android:textColor="#191b20"
android:textCursorDrawable="#drawable/cursor"
android:textSize="18sp" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
Snaps of issue
Initial state when first fragment starts
Dynamically view added and information filled
First fragment after returning from another fragment

Checking empty string in kotlin doesn't work

i am begginer into kotlin development and i am doing a simple TicTacToe game with kotlin and i want to check if the first three button texts are equal and at the same time one of them is not empty but when i use !button1.text.toString().isEmpty it doesn't work. don't know why. help me. here are my kotlin and xml codes
package com.example.tictac
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_tic_tac.*
class TicTacActivity : AppCompatActivity() {
var isFirstPlayer = true;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_tic_tac)
init()
}
fun init() {
buttonClick(button1)
buttonClick(button2)
buttonClick(button3)
buttonClick(button4)
buttonClick(button5)
buttonClick(button6)
buttonClick(button7)
buttonClick(button8)
buttonClick(button9)
}
fun buttonClick(button: Button) {
button.setOnClickListener() {
if (isFirstPlayer) {
button.text = "X"
isFirstPlayer = false
} else {
button.text = "O"
isFirstPlayer = true
}
button.isClickable = false
}
if (!button1.text.toString().isEmpty() &&
button1.text.toString().equals(button2.text.toString()) &&
button2.text.toString().equals(button3.text.toString())) {
winner(button1)
}
}
fun winner(button: Button) {
Toast.makeText(this, button.text.toString(), Toast.LENGTH_LONG).show()
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TicTacActivity"
android:orientation="vertical"
>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="49sp"
/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="49sp"
/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="49sp"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="49sp"/>
<Button android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="49sp"/>
<Button android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="49sp"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="49sp"/>
<Button android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="49sp"/>
<Button android:id="#+id/button9"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="49sp"/>
</LinearLayout>
</LinearLayout>
You must include the code that you think it does not work inside button.setOnClickListener so it is executed every time you click on a button.
Of course you have to add more code to check other cases.
fun buttonClick(button: Button) {
button.setOnClickListener() {
if (isFirstPlayer) {
button.text = "X"
isFirstPlayer = false
} else {
button.text = "O"
isFirstPlayer = true
}
button.isClickable = false
if (!button1.text.toString().isEmpty() &&
button1.text.toString().equals(button2.text.toString()) &&
button2.text.toString().equals(button3.text.toString())) {
winner(button1)
}
}
}

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

Resources