I am basic. in android studio, I want to start a basic project.
I want to type "any word" in a plain text view.
Only whenever I click on a button ,Just we can see that word as a TOAST.
this case I need : a plan text view, a button , and a toast.
How should i do this?
Thanks for the replies.
In your xml file you need to set an id for both textviews and button
In your activity:
//get the button
button = (Button) findViewById(R.id.but);
txt1= (text1) findViewById(R.id.text1);
txt2 = (text2) findViewById(R.id.text2);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
txt2.setText(txt1);
}
Your XML code should look like so. Note the id's of the EditText and the TextView which are crucial for extracting your strings and setting them respectively.
Also note the 'onClick' function on your button which is necessary to call upon the method in your MainActivity (Java Class) with the same name.
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/enterText"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="changeText"
android:text="Show Now"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/newText"/>
</LinearLayout>
Now for your MainActivity (Java) Class method:
public void changeText(View view){
EditText editText = findViewById(R.id.enterText);
String res = editText.getText().toString();
TextView textView = findViewById(R.id.newText);
textView.setText(res);
}
Again take note of the declaration of the EditText object using your 'id', "enterText" and "newText" respectively.
Hope this helps.
Related
So, I have made a button which on clicking opens a popup window with two buttons inside it, but now I am not able to assign setOnClickListner to those two buttons inside the OnCreateMethod as I did for all the other buttons in the main window (If I do then the app Stops responding) as till that time the buttons are not actually there(I think, that's the reason), So could you please help me assign on click listner to those buttons.
Here is the code for the popupWindow excution:
public void onButtonShowPopupWindowClick(View view) {
// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.confirm_number_popup, null);
hideSoftKeyboard(Register.this);
// create the popup window
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, false);
// show the popup window
// which view you pass in doesn't matter, it is only used for the window token
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
// dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});
}
Here is the layout file for the popup view:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:background="#color/white">
<Button
android:id="#+id/btn_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/green_shade_1"
android:text="#string/edit"
android:textSize="16sp"
android:layout_below="#id/txt_edit"
android:layout_alignParentStart="true"
android:layout_marginStart="20dp"
android:background="#color/white"
style="?android:attr/borderlessButtonStyle"/>
<Button
android:id="#+id/btn_Ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/green_shade_1"
android:text="#string/ok"
android:textSize="16sp"
android:layout_below="#id/txt_edit"
android:layout_alignParentEnd="true"
android:layout_marginEnd="20dp"
android:background="#color/white"
style="?android:attr/borderlessButtonStyle"/>
</RelativeLayout>
Please tell me if you are not able to understand the question or if any else code is needed.
Thank You!
Edit: I have already tried adding this in my OnCreate but it doesen't work:
Button btnEdit = findViewById(R.id.btn_edit);
btnEdit.setOnClickListener(v -> {
//The function
});
use
Button btn1 = popupView.findViewById(R.id.btn_edit);
btn1.setOnClickListener{}
I have the following simple recyclerview layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SurvivorPicksheetActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
This recycler view links to the detailed layout of the list items as such:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout 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/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_centerHorizontal="true"
app:srcCompat="#drawable/gamefield_background" />
<ImageView
android:id="#+id/away"
android:layout_width="160dp"
android:layout_height="150dp"
android:layout_gravity="top|left"/>
<ImageView
android:id="#+id/home"
android:layout_width="160dp"
android:layout_height="150dp"
android:layout_gravity="top|right"
android:scaleX="-1"/>
</androidx.cardview.widget.CardView>
</FrameLayout>
</LinearLayout>
Inside my Adapter java class I have an setOnClickListener for the two ImageView items.
If a user clicks on them the item is selected while the old item is unselected.
The problem I am encountering if that lets say the list of items has 25 items. If I click on the lets say 3rd item, everything works as expected.
Then I scroll to the end of the list, and scroll back up to the top, the selection is not longer valid even thought I clicked/selected prior to scrolling to the end of the list.
I can reclick and item at the top, scroll down and scroll back up and my selection is gone!?
Does anyone know why this is happening and more importantly how i can resolve it!?
UPDATE:
The following is my Adapter class:
public class GameAdapter extends
RecyclerView.Adapter<GameAdapter.GameViewHolder> {
// variable that holds the selected team
private String selectedTeam = "";
#Override
public void onBindViewHolder(GameViewHolder holder, int position) {
final Games game = gameList.get(position);
holder.awayTeamImageView.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
if (selectedTeam.equals(String.valueOf(game.getHomeId()))) {
// RESET PLAYER SELECTION
selectedTeam = "";
selectedGame = "";
} else {
// SET PLAYER SELECTION
selectedTeam = String.valueOf(game.getHomeId());
selectedGame = String.valueOf(game.getKey());
}
}
}
}
class GameViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public GameViewHolder(#NonNull View itemView) {
super(itemView);
itemView.setOnClickListener(this);
awayImageView = itemView.findViewById(R.id.away);
homeImageView = itemView.findViewById(R.id.home);
homeImageView.setOnClickListener(this);
awayImageView.setOnClickListener(this);
}
}
Based on my code above, I 'think' I am setting and unsetting the selected team so that when I scroll to the end and back to the top the selected team should remain vs being recycled and not displaying anymore?!
That's because the old Views are being recycled, back to the initial state. And this is a natutral behavior of RecyclerView.
In order to make each item View "memorize" its check/select state, you need to create a field in your model class to record the state, like isChecked or isSelected.
You can use own domain ViewHolder and ViewModel for recycleview.
Android developer documents gives basic understanding of how to create dynamic RecycleView in below link.
https://developer.android.com/guide/topics/ui/layout/recyclerview
This is expected as the views are recycled and lose their values accordingly whenever you scroll up/down the list; further for configuration changes (like screen rotation) the selected views will be lost.
To fix this, you should go for ViewModel to have persistent data even for configuration changes.
But, as a rescuer, assuming the adapter provided list of games gameList is in a persistent storage like mentioned above; then you can:
Create a selection boolean in the Games model class with getter/setter which has a false initially.
When a row is selected from the RecyclerView, then set the boolean to true like gameList.get(selectedPosition).setIsSelected(true)
Then notifyItemChanged() of that position.
I have a RecyclerView in MainActivity that shows a list of CardViews and that is working properly. A click on the CardView finishes the RecyclerView Activity and launches a Detail Activity that shows the clicked on CardView in a new RecyclerView list. The Detail Activity is used only to show that single CardView in a RecyclerView (I do this so I can use RecyclerView's ItemTouchHelper.SimpleCallback code on the CardView for easy left swipe for the user).
Here is the problem: I hit the left caret on the Detail Activity's Toolbar to return to the MainActivity. Then a click on the exact same CardView brings the user back to the Detail Activity. But this time only the background View (a border) is showing. The view of the CardView and its database data is completely missing.
The error appears to happen randomly. I can click to go from the MainActivity to the Detail Activity back and forth 5 times successfully and then on the sixth try, no CardView will show in the Detail Activity. Or I'll click two times successfully and then the third time, the CardView in the Detail Activity will not show. Note the left caret click in Detail Activity uses onBackPressed() so the Detail Activity finishes. So I don't think there should be any backstack issues. I also tried to adjust the xml height for the CardView to match_parent rather than wrap_content but no luck. The Detail Activity's ViewModel to Repository to Dao returns a List wrapped in LiveData. Perhaps there is an observer problem with the ViewModel, but I thought the observer gets removed/destroyed when the Detail Activity is destroyed? What am I missing here?
Adapter
...
itemHolder.cardView.setOnClickListener(view -> {
Card adapterItem= TodosAdapter.this.getItem(itemHolder.getAdapterPosition());
int adapPos = itemHolder.getAdapterPosition();
if (adapPos !=RecyclerView.NO_POSITION) {
onItemClick(adapPos, adapterItem);
}
});
MainActivity
...
public void onItemClick(int clickPos, Card cardFromClick) {
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
intent.putExtra("TAG","fromMain");
intent.putExtra("itemFromMain", cardFromClick);
startActivity(intent);
finish();
DetailActivity
...
public class DetailActivity extends AppCompatActivity {
private int cardId = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
// Get a new or existing ViewModel from the ViewModelProvider.
detsViewModel = new ViewModelProvider(this).get(CardViewModel.class);
Toolbar toolbar = findViewById(R.id.toolbar);
// The left caret is for Up navigation to the previous activity
// for OS versions 4.0 and earlier.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationIcon(R.drawable.ic_action_previous_item);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
String classname = extras.getString("TAG");
// The user clicked on a Card in the MainActivity
if (classname != null && classname.equals("fromMain")) {
card = extras.getParcelable("itemFromMain");
if (card != null) {
cardId = card.getId(); // card data is stored in Room database.
}
}
}
detsViewModel.getSingleCard(cardId).observe(this, singleAdapterList -> {
adapter2.setCardList(singleAdapterList);
});
}
activity_details.xml
...
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".DetailsActivity" >
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" >
</include>
<RelativeLayout
android:id="#+id/todoListLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar" >
<TextView
android:id="#+id/Card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:text="Card"
android:textStyle="bold"
android:textColor="#color/text_primary"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_centerHorizontal="true" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/details_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/Card"
android:scrollbars="vertical" />
<TextView
android:id="#+id/skytext5"
android:text="Cards"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/details_recyclerview"
android:background="#color/colorPrimary"
android:textAppearance="?android:attr/textAppearanceMedium"
android:clickable="true"
android:focusable="true" />
</RelativeLayout>
DetailsAdapter
...
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(mContext).inflate(R.layout.details_list_item, parent, false);
}
private List<Card> oneCardList
public void setCardList(List<Card> singleCardList) {
if (oneCardList != null) {
oneCardList.clear();
this.oneCardList = singleCardList;
} else {
// First initialization
this.oneCardList = singleCardList;
}
}
details_list_item.xml
...
<FrameLayout
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/detsinglecard_view"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:foreground="?android:attr/selectableItemBackground"
android:background="#FFFFFF"
tools:context=".DetailActivity">
<RelativeLayout
android:id="#+id/view_background2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
...
</RelativeLayout>
<RelativeLayout
android:id="#+id/view_foreground2"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#color/colorFlLabelFinal" >
<androidx.cardview.widget.CardView
android:id="#+id/cardview_dets"
android:layout_height="match_parent"
android:layout_width="match_parent"
...
}
ViewModel
...
LiveData<List<Card>> getSingleCard(int cardId) {
return repository.getSingleCard(cardId);
}
Repository
...
public LiveData<List<Card>> getSingleCard(int cardId) {
return quickcardDao.getSingleCard(cardId);
}
Dao
...
#Query("SELECT * FROM cards WHERE cardId = :cardId LIMIT 1")
LiveData<List<Card>> getSingleCard(int cardId);
So if the data does not change then going back to the same DetailActivity will not refresh the View. The answer was to re-use the LiveData (rather than re-loading the LiveData again from the database) if the data has not changed. See the Android Developers Architecture Components guide for ViewModel, "Implement a ViewModel" section for the "loadUsers()" example that solved my problem: https://developer.android.com/topic/libraries/architecture/viewmodel.
I've done professional development for over 14 years, but I am learning Android development. I came across a scenario that boggles my mind. I designed a simple layout, then decided to move some of the buttons from one LinearLayout to another LinearLayout within the same layout file.
Now, since moving the buttons, the click events are wired to the wrong buttons! It's as if the resource ids of the buttons were order-dependent.
BEFORE (works correctly):
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/previous_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/previous_button" />
<Button
android:id="#+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/true_button"/>
<Button
android:id="#+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/false_button" />
<Button
android:id="#+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/next_button"/>
</LinearLayout>
AFTER (works incorrectly):
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/true_button"/>
<Button
android:id="#+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/false_button" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/previous_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/previous_button" />
<Button
android:id="#+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/next_button"/>
</LinearLayout>
As you can see in the "AFTER" code, I want the previous/next buttons to be below the True/False buttons. And, if you look the designer, it's flawless -- perfect! But, the controller hiccups:
True button now becomes the Previous button
False button now becomes the True button
Previous button now becomes the False button
Next button works as it should.
Here is the code that is wiring up the click events (sorry if the code looks lame, I am going through an Android tutorial book, so please don't critique it, it's not mine!):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
mQuestionTextView.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
}
});
mTrueButton = (Button)findViewById(R.id.true_button);
mTrueButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
checkAnswer(true);
}
});
mFalseButton = (Button)findViewById(R.id.false_button);
mFalseButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
checkAnswer(false);
}
});
mNextButton = (Button)findViewById(R.id.next_button);
mNextButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
}
});
mPreviousButton = (Button)findViewById(R.id.previous_button);
mPreviousButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
int length = mQuestionBank.length;
mCurrentIndex = (mCurrentIndex + (length-1)) % length;
updateQuestion();
}
});
updateQuestion();
}
So these resource ids must be order specific; I can't explain it any other way. The problem is that I don't know how to make my buttons do what they want in the layout that I want.
OK, I think I managed to fix it. In Eclipse/ADT I went to Project -> Clean... and that seemed to do the trick. And yes, it was an issue with the resource ids! I tried posting a nice screen shot of the git repository before/after so you can see what lines from the R.java file changed but I don't have enough reputation points. Sorry.
I'm painfully new to android and I've run into a wall.
I'm trying get a linear layout to function more like a button, with different actions for press and long press - The reason being so I can have 2 differently formatted text labels on each "button". Something along the lines of:
-------------------------
| 2nd | <- Label for long press (regular/smaller type)
| = | <- Label for regular press (bold/larger type)
-------------------------
The posts I've found explain how to receive a regular click on a linear layout (I use the onClick attribute in the layout XML). But I've had no luck with long press. I've tried to define a new onLongClick attribute for xml as described in Aleksander Gralak's answer here: Long press definition at XML layout, like android:onClick does . But had no such luck - it looks like it was intended for a text view, I tried changing it to linear layout but failed miserably.
Here is the object in question: Main.xml
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:clickable="true"
android:focusable="true"
android:background="#drawable/darkgrey_button"
android:onClick="equals" android:longClickable="true" android:id="equalsbutton"
android:focusableInTouchMode="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2nd"
android:id="#+id/textView"
android:duplicateParentState="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" = "
android:id="#+id/textView1"
android:duplicateParentState="true"/>
</LinearLayout>
And Main.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void equals(View view) {
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
Add an id to your layout.
<LinearLayout
android:id="#+id/my_button_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
… >
In your Main.java
Get a reference to your LinearLayout and set a OnLongClickListener.
LinearLayout buttonLayout = (LinearLayout) findViewById(R.id.my_button_layout);
…
buttonLayout.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast.makeText(Main.this, "Long click!", Toast.LENGTH_SHORT).show();
return true;
}
});