how to prevent edit text from shrinking when keyboard opens? - java

I am a beginner in Android, I am trying to make a simple app where text in the edit text gets added to recycler view, but as soon as the Keyboard opens, edit text shrinks and content inside it is not visible
This is activty_main.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" >
<EditText
android:hint="ADD ITEMS"
android:layout_margin="10dp"
android:id="#+id/etItems"
android:textSize="24sp"
android:textColor="#android:color/black"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
<Button
android:layout_margin="10dp"
android:id="#+id/btnAdd"
android:text="ADD"
android:layout_weight="1"
android:autoSizeTextType="uniform"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvItems"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6" />
</LinearLayout>
This is MainActivity.Java
package com.example.todolist_09082020;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<String> items;
Button btnAdd;
EditText etItems;
RecyclerView rvItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = findViewById(R.id.btnAdd);
etItems = findViewById(R.id.etItems);
rvItems = findViewById(R.id.rvItems);
items = new ArrayList<>();
final ItemAdapter adapter = new ItemAdapter(items);
rvItems.setLayoutManager(new LinearLayoutManager(this));
rvItems.setAdapter(adapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = etItems.getText().toString();
if(!text.equals("")){
items.add(text);
adapter.notifyDataSetChanged();
}
etItems.setText("");
}
});
}
}
I tried solutions from other posts related to this, but this could not be solved.
edit text shrinked

Change layout_height of EditText and its parent LinearLayout to wrap_content. It should solve your issue

Your problem is the linear layout holding the text and button height. There's several things you can do, but a minimal way to keep it from collapsing is to change
android:layout_height="0dp"
to
android:layout_height="wrap_content"
You'll probably also see nicer results if you additionally change the text and button heights to wrap_content and choose a fixed font size (24sp looked nice) for your button text.

Related

Creating subText with visible/invisible method across TextViews

I have created two textViews that the first one is the main title and the second one is the subtext of the title. So I want to make the subtexts visibility to depend on clicking the main title.
It looks so easy as saying butI tried a lot of methods and MainActivity.java crashes all the time.
On the second step I want a different main title under the first one and the subtext above can toggle between them.
Maybe there is an easier widget instead of textView but I couldn't find any.
Please help.
Thanks.
Ps: The strings work well, there is no problem with them.
Activity_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"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="100dp"
android:clickable="true"
android:focusable="true"
android:text="#string/text1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.062" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/text11"
android:textAppearance="#android:style/TextAppearance.Medium"
android:textSize="15sp"
android:layout_alignTop="#id/textView"
android:layout_marginTop="35dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
app:layout_constraintVertical_bias="0.083"
android:visibility="invisible"
tools:visibility="visible" />
</RelativeLayout>
MainActivity.java
package com.example.Notes;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
#SuppressLint("ClickableViewAccessibility")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_MainActivity);
}
}
Edit: adding to java this code is the solution:
TextView title = findViewById(R.id.textView);
TextView subText = findViewById(R.id.textView2);
title.setOnClickListener(v -> {
if (subText.getVisibility() == View.VISIBLE)
subText.setVisibility(View.INVISIBLE);
else
subText.setVisibility(View.VISIBLE);
});
To change the visibility of the subText by clicking the title textview one could do:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_MainActivity);
TextView title = findViewById(R.id.textView);
TextView subText = findViewById(R.id.textView2);
title.setOnClickListener(v -> {
subText.setVisibility(View.VISIBLE); //OR View.GONE OR View.INVISIBLE
});
}
}
Thanks, I just modified this part and it became what I exatly want:
//subText.setVisibility(View.VISIBLE); //OR View.GONE OR View.INVISIBLE
if (subText.getVisibility() == View.VISIBLE)
subText.setVisibility(View.INVISIBLE);
else
subText.setVisibility(View.VISIBLE);

Text not visible when used setAlpha()

I am simply trying to make night mode activity but when the background turns black the text is not visible on screen. Text Disappears when switched to Dark Mode. Any solutions?
I tried tv.setTextColor(Color.WHITE); is also not working when the screen is black.
Any help would be appreciated. Thanks in advance.
Here are the images:
Image 1
Image 2
Here is the XML Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.saipriyank.daynight.MainActivity">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:layout_margin="50px"
android:gravity="center"/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/background_view"
android:background="#android:color/background_dark"
android:alpha="0"/>
<com.mahfa.dnswitch.DayNightSwitch
android:id="#+id/dayNight"
android:layout_width="80dp"
android:layout_height="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Here is the Java Code:
package com.saipriyank.daynight;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.mahfa.dnswitch.DayNightSwitch;
import com.mahfa.dnswitch.DayNightSwitchListener;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DayNightSwitch dayNight = (DayNightSwitch)findViewById(R.id.dayNight);
final View background_view = (View)findViewById(R.id.background_view);
final TextView tv = (TextView)findViewById(R.id.tv);
dayNight.setDuration(450);
dayNight.setListener(new DayNightSwitchListener() {
#Override
public void onSwitch(boolean isNight) {
if(isNight){
Toast.makeText(MainActivity.this, "Night Mode Enabled",Toast.LENGTH_SHORT).show();
tv.setAlpha(0f);
background_view.setAlpha(1f);
}
else {
background_view.setAlpha(0f);
}
}
});
}
}
You are making the TextView invisible by calling tv.setAlpha(0f).
If you want to set the text color white, you can use tv.setTextColor(Color.White);

Android Studio add EditText input to a ListView with button onClick

I am trying to make a simple Planner app so I have an XML file with a ListView on it, and another XML file with an EditText and a sumbit button on it, and of course my Java files. I have done a ton of research but I can't find a working method to make it when I press the button, it adds the EditText stuff to the ListView as an item. Here is my code:
activity_main.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"
tools:context=".MainActivity" >
<ListView
android:layout_width="300dp"
android:layout_height="fill_parent"
android:id="#+id/event_list"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/datePicker" />
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/datePicker"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<CalendarView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/calendarView"
android:layout_alignBottom="#+id/datePicker"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Event"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignEnd="#+id/datePicker"
android:layout_toEndOf="#+id/event_list"
android:background="#android:color/holo_blue_light"
android:onClick="newEvent"
android:textSize="20dp"
android:textColor="#android:color/white" />
</RelativeLayout>
new_plan.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/event_name"
android:hint="Event Name"
android:inputType="textCapWords"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/event_date"
android:hint="Event Date" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="#+id/button2"
android:layout_gravity="center_horizontal"
android:background="#android:color/holo_blue_light"
android:textSize="20dp"
android:textColor="#android:color/white" />
</LinearLayout>
MakePlan.java
package com.kass.planner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MakePlan extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_plan);
}
}
MainActivity.java
package com.kass.planner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void newEvent(View v){
Intent i = new Intent(this, MakePlan.class);
startActivity(i);}}
Please help, thank you.
Take a look on this google developers project. It's for animations but i think it also have the solution to your question.
https://developer.android.com/training/animation/layout.html
You should append data to the collection of items linked to your ListView via your adapter and notify your adapter that your data has changed. This illustration assumes that you have a List of String objects.
EditText editText = ...
List<String> list = ...
YourAdapter adapter = ...
ListView listView = ...
You must have done something like this to setup your listView.
listView.setAdapter(new YourAdapter(list, this));
Now on button click you should get the String in EditText, add it to your List and finally notify the adapter that your data has changed in order to update your ListView for e.g.
void OnButtonClick(View view){
String strToAdd = editText.getText().toString();
list.add(strToAdd);
adapter.notifyDataSetChanged();
}

Creating multiple selection list Android

I would like to add a form of input in my app where a list of options appear and multiple choices can be selected. I would use a spinner, but that only allows one option to be selected. I would then need for the options selected to be sensed for me to perform further operations with this. I think I would need something involving a checkedlistview but I am not sure. What would be the best way to go about doing this? I hope my situation is understood.
Use checkedlistview it Help you
ListMain.Java
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
#SuppressLint("ShowToast")
public class ListMain extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView mylist = (ListView) findViewById(R.id.companionsearch_listView1);
String[] list={"one","two","three"};
ArrayAdapter adapter = new ArrayAdapter<String>(ListMain.this,android.R.layout.simple_list_item_multiple_choice,list);
mylist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mylist.setAdapter(adapter);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/bottombarbuttonlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:baselineAligned="false"
android:weightSum="3" >
<ListView
android:id="#+id/companionsearch_listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#ffffff" >
</ListView>
</LinearLayout>
</RelativeLayout>
Output:

Items in another layout do not respond to user events

Please have a look at the following code
game_activity.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Game" >
//Please note the GUI Has been removed
<include layout="#layout/common_status_bar"/>
</RelativeLayout>
Game.java
package game.games;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
public class Game extends Activity {
private ImageView goToLanguageSelection;
private ImageView goToInternet;
private Button giveUp;
private LayoutInflater layoutInflater;
private View commonView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
layoutInflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
commonView = layoutInflater.inflate(R.layout.common_status_bar, null);
//Intializing instance variables
goToLanguageSelection = (ImageView)commonView.findViewById(R.id.backToLanguageSelectionButton);
goToInternet = (ImageView)commonView.findViewById(R.id.internetButton);
giveUp = (Button)commonView.findViewById(R.id.giveUpButton);
flag = getIntent().getBooleanExtra("LANGUAGE", true);
//Add listeners
goToLanguageSelection.setOnClickListener(goToLanguageSelectionClicked);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game, menu);
return true;
}
//Will get activated when the ThunderBolt image is clicked
private OnClickListener goToLanguageSelectionClicked = new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent intent = new Intent(getBaseContext(),LanguageSelector.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
};
}
common_status_bar.xml
<?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="wrap_content"
android:layout_alignParentBottom="true"
android:background="#373734"
android:orientation="horizontal" >
<ImageView
android:id="#+id/backToLanguageSelectionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="15dp"
android:paddingTop="5dp"
android:src="#drawable/thunderbolt" />
<ImageView
android:id="#+id/internetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:src="#drawable/globe_small_2" />
<ImageView
android:id="#+id/justForFun"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="5dp"
android:paddingTop="5dp" />
<Button
android:id="#+id/giveUpButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Button" />
</LinearLayout>
Here, the common_status_bar.xml is a common layout. I have added this layout to game_activity.xml by <include layout="#layout/common_status_bar"/>.
It displays everything fine, no issue. But the case is I can't make any of it's elements to work. I have attached a OnClickListener to the first element of it, the goToLanguageSelection but it is not responding to the Click. I tested this by adding the click event to all other buttons, images as well (one at a time) and I got the same result. It is not responding to user click, or whatever.
Why this button and images in separate layout do not respond to the user events?
The commonView should not be created again.
If you want keep a reference of the common_status_bar.xml, you can give name it by an id, like the code below:
<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:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<include
android:id="#+id/common_status_bar"
layout="#layout/common_status_bar" />
</RelativeLayout>
Then, change this code:
commonView = layoutInflater.inflate(R.layout.common_status_bar, null);
to:
commonView = findViewById(R.id.common_status_bar);

Categories

Resources