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);
Related
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);
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.
I am new to android and currently learning right now.
I faced a problem related to android-fragments, I write code and it did nothing! Just show the fragment color.
when I tried to remove the color of the fragment, it worked! I am unable to rectify this problem. Please help!
XML -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">
<Button
android:id="#+id/button"
android:layout_width="113dp"
android:layout_height="50dp"
android:text="Switch1"
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.945" />
<fragment
android:id="#+id/fragment"
android:name="com.example.fragments.Color_Purple"
android:layout_width="390dp"
android:layout_height="385dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.68"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.137" />
</androidx.constraintlayout.widget.ConstraintLayout>
XML - Fragment code
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".Color_Purple">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C403FA" />
</FrameLayout>
JAVA-mainActivity.java
package com.example.fragments;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button b1;
Fragment f1;
Boolean flag=true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
f1=new Color_Purple();
// Toast.makeText(getApplicationContext(),"Button pressed",Toast.LENGTH_SHORT).show();
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
if(flag)
{
findViewById(R.id.fragment).setBackgroundColor(Color.GREEN);
// Toast.makeText(getApplicationContext(),"Button pressed Color GREEN",Toast.LENGTH_SHORT).show();
flag=false;
}
else
{
findViewById(R.id.fragment).setBackgroundColor(Color.RED);
//Toast.makeText(getApplicationContext(),"Button pressed Color RED",Toast.LENGTH_SHORT).show();
flag=true;
}
}
});
}
}
Java - fragment java code
package com.example.fragments;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Color_Purple extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_color__purple, container, false);
}
}
Note:
If i remove android:background from xml - fragment, this code works but not with color.
just change :
Boolean flag = true;
to
boolean flag = true;
and remove android:background="#C403FA" from your TextView :
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C403FA" //remove this line
/>
I've found out the ImageViewTouch since I wanted to create a zooming image preview on my app though I need to display that image on one of my DialogFragment. But it works just fine on a normal FragmentActivity which is the MainActivity but not in my DialogImage and I'm not sure why.
I imported the library using the gradle and here's my code so far:
The MainActivity
import it.sephiroth.android.library.imagezoom.ImageViewTouch;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
public class MainActivity extends FragmentActivity {
ImageViewTouch mImage;
#Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView( R.layout.fragment_main );
mImage = (ImageViewTouch) findViewById( R.id.image );
ImageDownloader2 imgDownload = new ImageDownloader2();
imgDownload.download("MY_URL", mImage);
Button click = (Button)findViewById(R.id.button);
click.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
showPromoImage();
}
});
}
private void showPromoImage(){
DialogImage promo_details = new DialogImage();
promo_details.show(getFragmentManager(), "promo_details");
}
}
The DialogImage
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import it.sephiroth.android.library.imagezoom.ImageViewTouch;
/**
* Created by Bahay on 2/19/14.
*/
public class DialogImage extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_image, container);
getDialog().setTitle("Test");
getDialog().setCanceledOnTouchOutside(true);
ImageViewTouch promo = (ImageViewTouch) view.findViewById(R.id.image);
ImageDownloader2 imgDownload = new ImageDownloader2();
imgDownload.download("MY_URL", promo);
return view;
}
}
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<it.sephiroth.android.library.imagezoom.ImageViewTouch
android:id="#+id/image"
android:layout_width="158dp"
android:layout_height="322dp"
android:scaleType="fitCenter" />
<LinearLayout
android:layout_weight="0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:gravity="center"
android:weightSum="2" >
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_weight="1"
android:padding="20dp"
android:layout_height="wrap_content"
android:text="Load Image" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_weight="1"
android:padding="20dp"
android:layout_height="wrap_content"
android:text="Change Display" />
</LinearLayout>
</LinearLayout>
dialog_image.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<it.sephiroth.android.library.imagezoom.ImageViewTouch
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter" />
</LinearLayout>
The ImageDownloader2 is just a library which downloads the image from the URL then convert it into bitmap. I just made some customizations for this to work with ImageViewTouch.
As for the ImageViewTouch you can see it here:
https://github.com/sephiroth74/ImageViewZoom
For the ImageDownloader2 class you can see it here:
http://theandroidcoder.com/utilities/android-image-download-and-caching/
I just replaced the ImageView with ImageViewTouch for the download method.
UPDATE:
Tried using a normal old-school dialog wherein I use Activity and Intent where I just themed the DialogImage as android:theme="#android:style/Theme.DeviceDefault.Dialog". Output is same where image is not loaded.
Hope someone can help. Thanks!
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);