I'm trying to set an overline to a TextView using Compound Drawables.
I've created a line shape in an XML drawable resource which looks like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke android:width="1dp" android:color="#000000" />
<solid android:color="#00000000" />
</shape>
Now I'm trying to put it as an overline to my TextView like this:
TextView root=(TextView)findViewById(R.id.root);
Drawable background=getResources().getDrawable(R.drawable.overline);
root.setCompoundDrawables(null,background,null,null);
But it completely ignores the instruction and the overline doesn't appear.
I've also considered using CompoundDrawablesWithIntrinsicBounds, but that doesn't work either.
Is the XML for the shape wrong or am I using the method in the wrong way?
Thank you very much in advance!
EDIT: I tried with the XML android:drawableTop property, but still no luck.
At this point I think that there's clearly a problem with the shape itself.
Complete code here:
activity_my.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:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MyActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/hello"
android:textSize="30sp"
android:drawableTop="#drawable/overline"
android:text="Ciao" />
</RelativeLayout>
overline.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke android:width="1dp" android:color="#000000" />
<solid android:color="#00000000" />
</shape>
My java is the exact same one as the "Hello World" default example given in Android Studio:
package tesi.bordengsberiment;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewTreeObserver;
import android.widget.TextView;
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I had experienced the same problem before. You want to use
root.setCompoundDrawablesWithIntrinsicBounds();
Related
i am making a project in which i want to make a moving gradient background, like the background colour will change after a specific seconds. but when i run my code, my app crashes although there are no errors. here's my code:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:id="#+id/linear_layout"
android:background="#drawable/gradient_list"
tools:context=".MainActivity">
</androidx.coordinatorlayout.widget.CoordinatorLayout>
mainActivity.java:
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.Time;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
AnimationDrawable animationDrawable = (AnimationDrawable) linearLayout.getBackground();
animationDrawable.setEnterFadeDuration(2500);
animationDrawable.setExitFadeDuration(5000);
animationDrawable.start();
}
}
all my gradient files:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/play"
android:duration="4000"/>
<item android:drawable="#drawable/play1"
android:duration="4000"/>
<item android:drawable="#drawable/play2"
android:duration="4000"/>
</animation-list>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#66ff66"
android:endColor="#ff99cc"
android:angle="225" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#003366"
android:endColor="#66ffcc"
android:angle="135" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#ffcc00"
android:endColor="#ff99cc"
android:angle="45" />
</shape>
thnx!! in advance.
You have to change this line
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
to
CoordinatorLayout linearLayout = (CoordinatorLayout) findViewById(R.id.linear_layout);
your layout was wrong.
When i run the app overflow menu seen in emulator(i mean that three dots or lines on right top) , but while i was working, it doesn't seen on xml layout why ?? These are codes ;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
public class OverflowApp extends AppCompatActivity {
protected void onCreate(Bundle bambam){
super.onCreate(bambam);
setContentView(R.layout.overflow_xml);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main , menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
return true;
}
}
and xml codes ;
<?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="match_parent">
Firstly, there is no such thing as a layout for action overflow menu. The content that is to be displayed is stored in the following form.
<menu ...>
<item android:id="..." android:title="..."/>
<item android:id="..." android:title="..."/>
<item android:id="..." android:title="..."/>
...
</menu>
This file can be accessed from the menu directory in the res folder.
You can add, delete or change the menu items but not how they look from here.
As far as the appearance is concerned, you can change the appearance only in terms of the colors using styles. You can start with the answer given here.
I have created very simple demo project to demonstrate the bug:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class MenuBugActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.options_bug_demo, menu);
return true;
}
}
Main layout - 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"/>
Menu items options_bug_demo.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_bug"
android:title="Bug"
android:icon="#drawable/ic_action_settings"
android:showAsAction="always">
<menu>
<item
android:id="#+id/menu_bug_demo"
android:title="Bug demo">
<menu>
<item android:title="Settings"/>
</menu>
</item>
</menu>
</item>
</menu>
1.Clicking on "Preferences" icon:
2.Clicking on "Bug demo":
3.Clicking on "Settings":
4.Changing screen orientation to the horizontal:
Submenu "Settings" (closed at step 3) appears again after screen rotation! The only way to prevent the submenu from appearing after rotation - to remove string
android:id="#+id/menu_bug_demo"
from resource file options_bug_demo.xml
The question is how to avoid this unwanted behaviour of the submenu?
P.S. The bug was observed on 4.0.4 and 4.1.1 and was not observed on 4.4.2
I try to design action bar. When i create new android project, Appcompat imported directly. I found this question link,
I think this is due to library. it is showing on menu bar but I want to use action bar which has writing actionBarDemo(near actionBarDemo writing). How can I solve this problem ?
How can I change it ? What is problem ?
this is my main.class
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionmenu, menu);
return true;
}
}
this Xml of menu bar:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bbb.MainActivity"
tools:ignore="MergeRootFrame" />
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search / will display always -->
<item android:id="#+id/derya"
android:orderInCategory="100"
android:icon="#drawable/indoor"
android:title="#string/action_search"
android:showAsAction="always"/>
<!-- Location Found -->
<item android:id="#+id/duygu"
android:orderInCategory="100"
android:icon="#drawable/refresh"
android:title="#string/action_location_found"
android:showAsAction="always"/>
</menu>
I'm using the setBackgroundDrawable method to change the background of the button on my Activity. The onClickListener also has an Intent to open up a new Activity. However, when I return to the past Activity by hitting the physical back button, the button with the onClickListener assigned to it still has the onClick background set. If I return to the previous Activity by using the back button in the Action Bar, it works correctly. I tried to use a selector XML, but Android Studio gives me render errors, and it doesn't load when I compile.
Here is the MainActivity.java:
package com.jordandebarth.supercalculator;
import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends Activity {
ImageButton pythag;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#33b5e5")));
final ImageButton pythag = (ImageButton) findViewById(R.id.pythag_button);
pythag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pythag.setBackgroundResource(R.drawable.pythag_button_selector);
Intent pythagIntent = new Intent(MainActivity.this, PythagoreanActivity.class);
startActivity(pythagIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
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"
android:background="#e5e5e5"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageButton
android:layout_width="fill_parent"
android:background="#drawable/pythag_button"
android:layout_height="wrap_content"
android:id="#+id/pythag_button"
android:focusable="true"/>
</RelativeLayout>
user selector in xml file :
button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_hover" android:state_pressed="true"></item>
<item android:drawable="#drawable/btn_hover" android:state_focused="true"></item>
<item android:drawable="#drawable/btn" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:drawable="#drawable/btn_hover" android:state_enabled="false"></item>
</selector>
layout.xml
<Button
android:id="#+id/btnOk"
android:background="#drawable/button_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_margin="5dp"
android:text="#string/yes" />