Android not animating the Relative Layout - java

I am creating a Relative layout which I want should come from above sliding into the layout so here's what I did
made the layout invisible
In oncreate animated the layout above the screen
and in onWindowsFocusChanged() I called animation , made layout visible and want layout to slide into the screen
BUT
when view is created the layout is at its right location with out showing any sliding effect from coming from top of screen
public class OverlayActivity extends Activity implements View.OnClickListener {
RelativeLayout question_box;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overlay);
// Slide Up the INVISIBLE layout so that I can call it by animation back to its original position
question_box = findViewById(R.id.question_box);
question_box.animate().translationY(-question_box.getHeight());
final Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}
public void animateInObjects() {
question_box.setVisibility(View.VISIBLE);
question_box.animate().setDuration(1000).translationY(0);
}
#Override
public void onClick(View v) {
//Some Code
}
#Override
protected void onStop() {
super.onStop();
finish();
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
animateInObjects();
super.onWindowFocusChanged(hasFocus);
}
}
Layout
<RelativeLayout
android:id="#+id/question_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:layout_marginTop="5dp"
android:layout_below="#+id/ad_view_container"
android:visibility="invisible">
<TextView
android:id="#+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/questiontext"
android:paddingStart="20dp"
android:paddingTop="7dp"
android:paddingEnd="20dp"
android:paddingBottom="20dp"
android:text="#string/sample_question"
android:textAlignment="center"
android:textColor="#color/text_quest"
android:textSize="23sp" />
<View
android:id="#+id/center_vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_centerVertical="true" />
<LinearLayout
android:id="#+id/cover_opt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/question"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:background="#drawable/main_layout">
<Button
android:id="#+id/opt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="1dp"
android:layout_marginHorizontal="2dp"
android:background="#android:color/transparent"
android:text="#string/sample_number"
android:textAlignment="center"
android:textColor="#color/text_quest"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/cover_opt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/question"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#drawable/main_layout">
<Button
android:id="#+id/opt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="1dp"
android:layout_marginHorizontal="2dp"
android:background="#android:color/transparent"
android:text="#string/sample_number"
android:textAlignment="center"
android:textColor="#color/text_quest"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/cover_opt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/question"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:background="#drawable/main_layout">
<Button
android:id="#+id/opt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="1dp"
android:layout_marginHorizontal="2dp"
android:background="#android:color/transparent"
android:text="#string/sample_number"
android:textAlignment="center"
android:textColor="#color/text_quest"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
this is the theme of the activity
<style name="Theme.Lockscreen" parent="Theme.AppCompat.NoActionBar">
<item name="android:background">#33000000</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation</item>
</style>

well you try to animate the view in onCreate method. at first the view is not drawn yet and you get a getHeight = 0 .so you must wait when the view is drawn by using view.getViewTreeObserver().addOnGlobalLayoutListener to be able to animate it
you need to add this on your onCreate() :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
// Slide Up the INVISIBLE layout so that I can call it by animation back to its original position
question_box = findViewById(R.id.question_box);
question_box.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (question_box.getHeight() != 0)
question_box.animate().translationY(-question_box.getHeight());
}
});
final Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}

Try this as your xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromYScale="0"
android:toYScale="50%"
android:duration="1000"></scale>
</set>
To have it till half of your screen and arrange layout respectively

Related

How to change color of navigation bar elements when they are selected ANDROID STUDIO

I want to change the color of the navigation element when its selected
menu i have
for example when clicking on the Calculations element I want the background to be a light color
menu i want
Also i want to change font of the text in menu bar, but i dont know how
You can do by changing background colour of layout on setOnClickListener event of that button like below.
xml File of navigation menu
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:layout_alignParentBottom="true"
android:animateLayoutChanges="true"
android:background="#color/nonitemclicked"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/ly_calculator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/img_calculator"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginTop="2dp"
android:padding="3dp"
android:src="#drawable/ic_calculator"
app:tint="#color/white" />
<TextView
android:id="#+id/txt_calculator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/robotoslabregular"
android:gravity="center_horizontal"
android:padding="3dp"
android:text="Calculation"
android:textColor="#color/white"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/ly_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/img_list"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginTop="2dp"
android:padding="4dp"
android:src="#drawable/ic_list"
app:tint="#color/white" />
<TextView
android:id="#+id/txt_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/robotoslabregular"
android:gravity="center_horizontal"
android:padding="3dp"
android:text="Calculation list"
android:textColor="#color/white"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/ly_setting"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/img_setting"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginTop="2dp"
android:padding="2dp"
android:src="#drawable/ic_setting"
app:tint="#color/white" />
<TextView
android:id="#+id/txt_setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/robotoslabregular"
android:gravity="center_horizontal"
android:padding="3dp"
android:text="Setting"
android:textColor="#color/white"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
Code
setOnClickListner of buttons
ly_calculator.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setcolorOnItemClick(ly_calculator, ly_list, ly_setting);
}
});
ly_list.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setcolorOnItemClick(ly_list, ly_calculator, ly_setting);
}
});
ly_setting.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setcolorOnItemClick(ly_setting, ly_calculator, ly_list);
}
});
Method for changing background colour
public void setcolorOnItemClick(LinearLayout ly1, LinearLayout ly2, LinearLayout ly3) {
ly1.setBackgroundColor(getResources().getColor(R.color.itemclicked));
ly2.setBackgroundColor(getResources().getColor(R.color.nonitemclicked));
ly3.setBackgroundColor(getResources().getColor(R.color.nonitemclicked));
}
And for change font of the text
You can do using fontFamily in xml side and i have some font in font folder
android:fontFamily="#font/robotoslabregular"
You can also do by programmatically using typeface:
txt_calculator.setTypeface(getResources().getFont(R.font.robotoslabmedium));
Create a drawable file of this code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="your_active_color" android:state_checked="true"/>
<item android:drawable="your_inactive_color"/>
</selector>
Then put it in this attribute of bottom nav bar app:itemBackground="#color/your_file"
The full answer is here. May it help you

Getting rid of unmodifiable blank area under actionBar?

I read a half-dozen of threads(particularly this one, who share the issue) about actionBar height, but it seems to no one already get this issue :
My actionBar has an unmodifiable blank(bg color) area under itself :
The custom action bar is defined like this :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityValveassemblyBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
mContentView = binding.fullscreenContent;
mContentView.getWindowInsetsController().setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
mContentView.getWindowInsetsController().hide(WindowInsets.Type.navigationBars()); // HIDE ANDROID UI
// ACTIONBAR
ActionBar actionBar = getSupportActionBar(); // define actionBar
assert actionBar != null;
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); // allow custom
actionBar.setCustomView(R.layout.actionbar_layout); // define custom
As screen said,
<item name="actionBarSize">48dp</item>
<item name="android:windowActionBarOverlay">true</item>
is defined. windowActionBarOverlay = false is the same result.
I can't find what is this area's name, or how to delete it. I can't use these 8 pixels, and until now I only had white bg activites, but I have to use grey background and this line is bothering me so hard.
Edit : and, according to the Layout inspector, this area is... nothing.
Edit2 : with default actionBar and Empty activity :
Edit3 : Add customAction bar layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="38dp"
android:orientation="horizontal"
android:id="#+id/actionbarlinearlayout">
<ImageButton
android:id="#+id/TopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="#string/settings"
android:src="#drawable/ic_back"
android:background="#00000000"
android:onClick="topLeftButton"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="50dp"
android:contentDescription="#string/shift"
android:src="#drawable/ic_shift" />
<TextView
android:id="#+id/Shift"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginStart="70dp"
android:layout_marginTop="-17dp"
android:fontFamily="#font/fontnats"
android:text="#string/Morningshift"
android:textColor="#color/black"
android:textSize="30sp"
/>
<ImageButton
android:id="#+id/ShiftReload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="172dp"
android:background="#00FFFFFF"
android:onClick="shiftReload"
android:padding="5.5dp"
android:src="#drawable/ic_reload" />
</RelativeLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_gravity="center"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_logo_bic" />

space between icon and title item in menu when call it in toolbar

I have the following menu:
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_main_help"
android:title="#string/menu_help"
android:icon="#drawable/ic_information" />
<item
android:id="#+id/menu_main_about"
android:icon="#drawable/ic_information"
android:title="#string/menuabout_us" />
</menu>
I need to space between icon and border in menu to be clear show image please
enter image description here
I want show icon in overflow menu of Toolbar like that enter image description here
but i face a problem when I click to show options menu in tool bar the icon of item menu overlapping or hidden part from icon in border , I have the following toolbar:
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/layoutbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/_6sdp">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/_24sdp"
android:background="#color/colorPrimaryDark" />
</com.google.android.material.appbar.AppBarLayout>
This is toolbar
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/teal_200"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<RelativeLayout
android:id="#+id/real_back"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerInParent="true"
android:layout_alignParentStart="true"
android:layout_margin="10dp"
android:background="#drawable/ic_launcher_background">
<TextView
android:layout_width="18dp"
android:layout_height="14dp"
android:text="ABC"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<TextView
android:id="#+id/activity_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Birthday Card"
android:gravity="center"
android:textSize="14dp"
android:textStyle="bold"
android:layout_centerInParent="true"/>
<TextView
android:id="#+id/end"
android:layout_width="25dp"
android:layout_height="25dp"
android:background="#drawable/ic_launcher_background"
android:layout_alignParentEnd="true"
android:layout_marginEnd="10dp"/>
<TextView
android:id="#+id/word"
android:layout_width="25dp"
android:layout_height="25dp"
android:background="#drawable/ic_launcher_background"
android:layout_toLeftOf="#+id/end"
android:layout_marginEnd="10dp"/>
</RelativeLayout>
Add This is the layout where in use it
<?xml version="1.0" encoding="utf-8"?>
<include
android:id="#+id/topbar"
layout="#layout/toolbar"/>
<ListView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_below="#+id/topbar"
android:layout_alignParentEnd="true"
android:visibility="gone"
android:entries="#array/listarray"/>
Create this array in value/string.xml
<string-array name="listarray">
<item>ABCC</item>
<item>ABCD</item>
<item>EFDD</item>
</string-array>
Now what will do in activity is,,,
TextView textView = findViewById(R.id.listshow);
ListView listView= findViewById(R.id.listview);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(listView.getVisibility() == View.VISIBLE){
listView.setVisibility(View.GONE);
}
else {
listView.setVisibility(View.VISIBLE);
}
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long
id) {
if(position ==0 ){
Toast.makeText(MainActivity.this, "P1!", Toast.LENGTH_SHORT).show();
}
if(position ==1){
Toast.makeText(MainActivity.this, "p2", Toast.LENGTH_SHORT).show();
}
}
});
See Styling toolbar menu item title
Specifically, you can specify a custom layout for menu items using the actionLayout parameter as demonstrated:
android:actionLayout="#layout/custom_layout"

how to get a layout on click of imageview?

Hello.. I have a layout in which I have ImageView and EditText view. I want to add a different layout on click of ImageView of arrow.
It should look like this after click event of ImageView. How can I get this??
Pleas help...
For perfect answer please add your xml file and java file source code.
Example
MainActivity.java
public class MainActivity extends Activity {
LinearLayout linear1,linear2;
ImageView imgArrow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linear1 = (LinearLayout) findViewById(R.id.linear1);
linear2 = (LinearLayout) findViewById(R.id.linear2);
imgArrow = (ImageView) findViewById(R.id.imgArrow);
linear1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(linear2.getVisibility() == View.GONE){
linear2.setVisibility(View.VISIBLE);
imgArrow.setImageResource(R.drawable.up);
}else{
linear2.setVisibility(View.GONE);
imgArrow.setImageResource(R.drawable.down);
}
}
});
}
}
activity_main.xml
<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
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/btnClick"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/tv1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:text="Additional Contact" />
<ImageView
android:id="#+id/imgArrow"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.2"
android:src="#drawable/down" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:visibility="gone"
android:orientation="vertical" >
<TextView
android:id="#+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User Name here" />
<TextView
android:id="#+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Phone number here" />
<Button
android:id="#+id/btnone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Click Here" />
</LinearLayout>
</LinearLayout>
Here down and up is image file for arrow.
so put that both arrow file in drawable folder.
You could just set an OnClickListener to the ImageView, which contains the arrow. Then assign a method, which handles the click and produces your different layout

Android Sliding Drawer Open On Create

I want to have a slider that is open when the app starts. It will be open with buttons and such and when the user closes it, there will be more buttons to access. Is this possible with a sliding drawer? What would I add to the onCreate() method?
Thanks
XML Layout - In a basic LinearLayout:
<SlidingDrawer
android:id="#+id/slide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:content="#+id/content"
android:handle="#+id/handle"
android:orientation="vertical"
android:scrollbars="vertical" >
<LinearLayout
android:id="#id/handle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/btn"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/handleImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_tray_expand" />
<Button
android:id="#+id/handleButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/btn"
android:text="Up me" />
</LinearLayout>
<LinearLayout
android:id="#+id/content"
android:paddingTop="2dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#013E53"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_commentDisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="10dp"
android:textSize="20dp" />
</LinearLayout>
</SlidingDrawer>
And your Activity will looks like this:
public class Home extends Activity implements OnDrawerScrollListener
{
private ImageView handleImage;
private Button handleButton;
private SlidingDrawer slide;
private TextView tv_commentDisplay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
tv_commentDisplay = (TextView)this.findViewById(R.id.tv_commentDisplay);
handleImage = (ImageView)this.findViewById(R.id.handleImage);
handleButton = (Button)this.findViewById(R.id.handleButton);
slide = (SlidingDrawer)this.findViewById(R.id.slide);
slide.open(); // not sure
slide.setOnDrawerScrollListener(this);
handleButton = ((Button)this.findViewById(R.id.handleButton));
tv_commentDisplay.setText("Hello World");
}
#Override
public void onScrollEnded() {
}
#Override
public void onScrollStarted() {
if (slide.isOpened())
handleImage.setImageResource(R.drawable.ic_tray_collapse);
else {
handleImage.setImageResource(R.drawable.ic_tray_expand);
}
}
Use open() in your onCreate(), it will open the drawer immediately.
You can take a look at the full API here

Categories

Resources