How can I get a multiline custom title on android? - android

I am working on a xamarin project (has no relevance here as far as I can tell - the android layout properties are the same)
I am using Window.SetFeatureInt (WindowFeatures.CustomTitle, ...) to set the title (2 "columns". I wish to make two rows of text though for my title. Is this possible? I have not had much success. I have an upper left, upper right and lower left and lower right TextViews to set.
My previous attempts have ended up with the TextViews overwriting each other.

Here I am designing a XML file with an image, text for title & button. This is the XML file. U can use it however you want, for a complete application or perticular activity.
In case of activity add this activity's theme tag. For multi line just use text view in side the below linear layout, set it as multline true.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="35dip"
android:gravity="center_vertical"
android:background="#android:color/darker_gray">
<ImageView
android:id="#+id/header"
android:background="#drawable/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Window Text"
android:textColor="#android:color/black"
android:textStyle="bold"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
Create a theme in Custom_theme.xml,
<resources>
<style name="CustomWindowTitleBarBG">
<item name="android:background">#323331</item>
</style>
<style name="TitleBarTheme" parent="android:Theme">
<item name="android:windowTitleSize">35dip</item>
<item name="android:windowTitleBackgroundStyle"> #style/CustomWindowTitleBarBG</item>
</style>
Modify the AndroidManifest.xml file as follows for applying custom theme
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/TitleBarTheme" >
<activity
android:name=".CustomWindowTitle"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Now in main java file add “requestWindowFeature(Window.FEATURE_CUSTOM_TITLE)” &
“getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title)”
This is the code snippet for CustomWindowTitle.java
package com.example.android;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class CustomWindowTitle extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
}
}

Related

How can I view "Hello World!" TextView in Android Studio?

I have just started learning development on Android, and I am not able to view "Hellow World!" TextView in the Design Tab.
MainActivity.java:
package com.dummy.demoapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml:
<?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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dummy.demoapp">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The TextView appears in the Component Tree, but the preview of the activity is totally blank:
I tried zooming in, but the TextView is just not there.
[EDIT]: The red exclamation (!) gives me the following message:
And refreshing doesn't solve the problem. Any help?
Go to style.xml and change this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
to
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
Android Studio sometimes does not appear upto you've success build,
Build->Build&Run, then select your virtual device if there is a helloworld it's okay, it's not your code problem it's related with IDE.
I think it's you default AppTheme with color white , try to add color to TextView
android:textColor="#android:color/black"
Click on the blue icon at the top left corner and then click on force layout. and You will see the Hello World!

How to fix EditText not being rendered?

I have modified the tabbed activity example to create my own set of 3 tabs. Each tab has its own layout file and uses this theme:
<style name="AppTheme" parent="android:Theme.Holo.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Everything was working perfectly until I added an EditText to one of the fragments layout files. Then next time i built the app and ran it on an emulator the EditText did not render (however space was left for it). I then tried to add different types of text fields. Most of them were to the same effect but the Person Name one had 'name' render next to it as it should but the input box did not show again. This is the fragment layout file:
<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="com.*********.************.MainActivity$FragmentTransmit">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Transmit"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_below="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:clickable="true"/>
To conclude, I'm using the Holo theme with no action bar in a fragment and everything works except when the app is built and ran edit texts don't show. So my question to you is Why it is not shown? and How it can be made to be shown?
Edit: Using a Nexus 5X emulator
Android Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
The Fragment class:
public static class FragmentTransmit extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public FragmentTransmit() {
}
public static FragmentTransmit newInstance() {
FragmentTransmit fragment = new FragmentTransmit();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_transmit, container, false);
return rootView;
}
}

Android Splash Screen Center Image

I have an app with a splash screen view that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/black">
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/splash2"
android:scaleType="centerInside"
android:adjustViewBounds="true"
/>
<TextView
android:id="#+id/txtVersion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:textSize="20dip"
android:visibility="invisible"
android:text="Version "/>
<LinearLayout
android:id="#+id/lvSplashInfo"
android:layout_width="fill_parent"
android:layout_height="120dp"
android:layout_marginRight="40dp"
android:layout_marginLeft="40dp"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:visibility="invisible"
android:background="#android:color/black">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bleh"
android:layout_margin="5dp"
android:layout_gravity="center_horizontal"
android:textColor="#android:color/white"/>
<TextView
android:id="#+id/txtPercent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="0% of the total"
android:layout_margin="5dp"
android:textSize="18sp"
android:gravity="center_horizontal"
android:textColor="#9DBA32"/>
<TextView
android:id="#+id/txtFilename"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="File name: xxxxx"
android:layout_margin="5dp"
android:textSize="18sp"
android:visibility="invisible"
android:gravity="center_horizontal"
android:textColor="#android:color/white"/>
</LinearLayout>
</RelativeLayout>
It renders an image scaled and centered into the screen. Unfortunately, this isn't working well for us. It causes a slight flash of white before it loads the view. My solution was to use an activity style. That works great, except I can't seem to center AND scale the image. I can center it easily, but it is wider than the screen. Here is my xml drawable:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/black"/>
<item>
<bitmap
android:gravity="center|bottom|clip_vertical"
android:src="#drawable/splash2"
/>
</item>
</layer-list>
How do I scale and center that splash2 image?
You can make the image in Photoshop, put it in the xml then center the whole thing, but I would recommend having a Splash screen while loading the app, because if you have it with a timer it wastes user's time...
So that's what I did to have a splash screen like this one:
First, you have to add a new Java class, let's call it SplashActivity.java
You don't have to add much to it:
package YOUR-PACKAGE-NAME;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Intent i = new Intent(SplashActivity.this, MainActivity.class);
startActivity(i);
finish();
}
}
After that, you have to have a drawable for the splash screen, because if you have a layout it will appear when the app is already loaded, so there won't be any sense in the Splash...
Go to the drawable folder and create a file named background_splash.xml.
There you add
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center">
<item>
<bitmap
android:src="#drawable/ic_splash"
/>
</item>
</layer-list>
where ic_splash is the image I created with Photoshop with resolution 1280x720 and then created a 9-patch image... (You have to have the 9-patch image, not to have normal, small and cropped image on different devices..) So go here and generate yours... Create it, download it and copy all the folders (mdpi, hdpi etc) to your res folder...
Now, go to your values/styles.xml and add the NoActionBar style
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
THEN open your Manifest.xml and edit the activity with intent-filter.
Yours should be
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:icon="#drawable/ic_launcher"
android:theme="#style/AppTheme"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
so replace the .MainActivity with .SplashActivity and the .MainActivity as another activity, so in the end you should have at least two activities declared at the Manifest.xml:
<activity
android:name=".SplashActivity"
android:theme="#style/SplashTheme"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:icon="#drawable/ic_launcher" or "#mipmap/ic_launcher"
android:theme="#style/AppTheme">
</activity>
Good luck! :)
If there are any troubles, ask :)

What is causing my app to crash on button press?

My manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rufflez.pagertabstriplistview.pagertabstriplistview" >
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.rufflez.pagertabstriplistview.pagertabstriplistview.FirstScreen"
android:label="#string/app_name"
android:theme="#style/AppTheme.Base">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
On the Launch screen (FirstScreen):
<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="#drawable/bg">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/img_logo"
android:src="#drawable/logo2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/img_title"
android:layout_centerVertical="false"
android:layout_centerHorizontal="true"
android:layout_marginTop="230dp"
/>
<TextView
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/desc"
android:id="#+id/description"
android:textColor="#ffffffff"
android:textSize="24dp"
android:typeface="sans"
android:gravity="center"
android:layout_marginTop="375dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textStyle="italic" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btn_start"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/start"
android:background="#null"
android:layout_marginBottom="16dp" />
</RelativeLayout>
My FirstScreen.java:
package com.rufflez.pagertabstriplistview.pagertabstriplistview;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
public class FirstScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_screen);
setupMessageButton();
}
private void setupMessageButton() {
//1. Get reference to button
ImageButton messageButton = (ImageButton) findViewById(R.id.btn_start);
//2. Set the click listener to run code.
messageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("DemoButtonApp", "Clicked Begin");
startActivity(new Intent(FirstScreen.this, MainActivity.class));
finish();
}
});
}
}
When I tap the ImageButton (btn_start) It crashes and says "Unfortunately, Video Game Historium has stopped" It doesn't seem to want to load the next screen. I looked in Logcat (which keeps going and going and never stopping) it's spitting out a few red errors:
EnterpriseContainerManager﹕ ContainerPolicy Service is not yet ready!!!
E/SMD﹕ DCD ON
CameraController﹕ handleMessage signal: 0
CameraController﹕ handleMessage(0) before let go!{ when=-506ms what=0 target=com.sec.android.smartface.CameraController$EventHandler }
just to name a few. Any ideas? It was working until I took the screen from one project and imported it into this one. I imported the xml, java and drawables.
Edit: Added Crash Report:
11-18 10:04:47.683 9690-9690/com.rufflez.pagertabstriplistview.pagertabstriplistview
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.rufflez.pagertabstriplistview.pagertabstriplistview, PID: 9690
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.rufflez.pagertabstriplistview.pagertabstriplistview/com.rufflez.pagertabstriplistview.pagertabstriplistview.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
This Activity already has an action bar supplied by the window decor.
Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme
to use a Toolbar instead
This is the important part here. So, check your themes.xml and set `
<item name="windowActionBar">false</item>
Alternatively, you should be able to use as parent
parent="Theme.AppCompat.Light.NoActionBar"
and it should work.
Solved my issue. I had to change the order of my themes.xml around a little and add
<item name="windowActionBar">false</item>

Custom title bar issue

I set a custom title bar as example http://coderzheaven.com/2011/06/custom-title-bar-in-android/ , every thing works fine but still blue frame appear from original title . The only thing I added is to set the background color to custom title as (android:background="#EECBAD"). How can I remove this blue frame?
###custom_title.xml:
<RelativeLayout
android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#EECBAD"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="40dip"
android:id="#+id/ImageView01"
android:background="#drawable/ic_launcher"
android:layout_height="40dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv"
android:text="ELNABIH"
android:layout_toRightOf="#+id/ImageView01"
android:textColor="#drawable/red"
android:textStyle="bold"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip" />
</RelativeLayout>
java.class
package com.test.list;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
public class Tttt extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Boolean customTitleSupported = requestWindowFeature
(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if (customTitleSupported) {
getWindow().setFeatureInt
(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText("name");
}
}
}
Brother, I think this link would help you for sure. I think you'll have to define styles and themes in xml too.
Please create styles.xml & themes.xml in values folder folder under res. Write the following code :
Code for styles.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources> <style name="WindowTitleBackground" parent="android:Theme">
<item name="android:background">#android:color/background_dark</item>
</style>
</resources>
Code for themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme">
<item name="android:windowTitleSize">54px</item>
<item name="android:windowTitleBackgroundStyle">#style/WindowTitleBackground</item>
</style>
</resources>
and insert this in your manifest - android:theme="#style/MyTheme"
Try to override "android:windowTitleBackgroundStyle".
If you set the background color for tv TextView, it's because it also has some right and left padding, beside being set to wrap_content:
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
You can remove those lines, or try to set the background color for the parent, RelativeLayout01
put on style.xml:
<style name="CustomWindowTitleBackground">
<item name="android:background">#color/LightFacebook</item>
</style>
on #color/LightFacebook use u custom color.
You should hide the default title first. You should declare that in AndroidManifest.xml for the activity or set the window params in the java code.
<activity
android:name=".MainActivity"
android:theme="#android:style/Theme.Black.NoTitleBar" />

Categories

Resources